/*
Namespace: BNYS.homepage
Barneys Homepage Javascript

File: huge_homepage.js

About: Version
0.1

Description:
Homepage HUGE Javascript for barneys.com

Requires:
*/

var BNYS = (function ($, bnys, app) {
	
	bnys.views = bnys.views || {};

	var homepage = {
		scroller:function(){
		
			if (bnys.isiPad || bnys.isIe6 || bnys.isiPhone) return;
			
			var $window = $(window);
			
			
			// set the padding object
			var paddingObj  = {
				max:30,
				min:18,
				delta:12
			}
			
			var topObj = {
				max:34,
				min:20,
				delta:14
			}
			
			var logoObj ={
				maxHeight:51,
				minHeight:34,
				delta:17
			}
			
			var m1 = 0; // padding multiplier
			var m2 = 0 // opacity multiplier
			var maxScroll = 200;
			var midPoint = 170;
			var padding, top;
			var header =  $("#header").find(".content");
			var logo = header.find("h1.logo");
			var search = $("#header .sitesearch");
			
			var logoHeight  = 0;
			
			

			$(window).scroll(function (e) {
				
				if(($window.scrollTop() >= 0) && ($window.scrollTop() <= maxScroll)){
					
					// get the multiplier.
					m1 = $window.scrollTop()/maxScroll	
					
					//new padding
					padding = Math.ceil(paddingObj.max - m1*paddingObj.delta);
					
					// set the header padding
					header.css({
						paddingTop:padding,
						paddingBottom:padding
					});
					
					
					top = Math.ceil(topObj.max - m1*topObj.delta); 
					
					// move the search box
					search.css({
						top:top
					})
				
					
				}
				
				if(($window.scrollTop() >= 0) && ($window.scrollTop() <= midPoint)){
					
					// get the multiplier.
					m2 = $window.scrollTop()/midPoint;
					
					//logoHeight = logoObj.maxHeight - m2*logoObj.delta;
					
					// fade out the logo.
					logo.css({
						opacity:1-m2
					})
					
					
					if (1-m2 <= 0.05){
						if (!logo.hasClass("compressed")) logo.addClass("compressed");
					}else{
						if (logo.hasClass("compressed")) {
							logo.removeClass("compressed");
						}
					}
					
				} else if(($window.scrollTop() >= midPoint) && ($window.scrollTop() <= 300)){
					if (!logo.hasClass("compressed")) {
						logo.addClass("compressed");
						
					}
					// get the multiplier.
					m2 = ($window.scrollTop() - midPoint) / (300 - midPoint);
					
					
					// fade in the logo.
					logo.css({
						opacity:m2
					})
				

				} else {
					if (!logo.hasClass("compressed")) logo.addClass("compressed");	
					logo.css({
						opacity:1
					})
				}
				
				
				
				if (logo.hasClass("compressed")){
					$('#header-shipping, #leftCampaign, #rightHeaderBanner' ).animate({
					    top: '16px'
					  }, 0, 'swing');
					$('#header-shipping-tooltip' ).css(
						    'top', '65px');
				}else{
					$('#header-shipping, #leftCampaign, #rightHeaderBanner').animate({
					    top: '25px'
					  }, 0, 'swing');
					$('#header-shipping-tooltip' ).css(
						    'top', '76px');
				}
				
				
				
				
				
			});				
			

			
			
			
			
		},		
		videoPlayer:function(){
			
			
			$("a#play-button").click(function(e){
				e.preventDefault();
				$.colorbox({
					href:"http://player.vimeo.com/video/28959273?title=0&byline=0&portrait=0&color=ffffff&autoplay=1&loop=0",
					iframe:"true", 
					width:"828px", 
					height:"496px",
					opacity:.5,
					title:false,
					close:"close video",
					onOpen:function(){
						if (bnys.isiPad || bnys.isiPhone){
							$("#cboxOverlay").hide();
						}
					
					}
				});
			}); 
		},
				
		slideshow:function() {

			var homepage_data = {
				heightMap: []
			};
			
			var controlLinks = $('#controls').find('a');
			
			var slides = $('#slots').find('.slot');
			var prev = $('#controls').find('.prev');
			var next = $('#controls').find('.next');
			
			
			function findCurrentPanel() {
				
				var scrollTop = $(window).scrollTop();
				var hc = 0;
				var currentPanel;
				
				//we are adding to our homepage_data object the heights of the
				//objects, as well as a mapping to their div 
				
				for (var i=0;i<homepage_data.heightMap.length;i++) {
					var panelStats = homepage_data.heightMap[i];
					
					hc += panelStats[1];	
					if (hc >= scrollTop) {
						currentPanel = $('#'+panelStats[0]);
						break;
					}
				}
				
				//remove active from all other panels and assign to this one
				//slides.removeClass('active');
				$(currentPanel[0]).addClass('active');
				
								
				prev.attr('href', '#'+currentPanel.prev('.slot').attr('id'));
				next.attr('href', '#'+currentPanel.next('.slot').attr('id'));
		
				
				//find out if your current slide is first/last
				
				//if the first slide is active, make the prev button inactive
				if (slides[0] ==currentPanel[0]) {
					prev.addClass('inactive').attr('href', '');
					next.removeClass('inactive');
				} else if (slides[homepage_data.heightMap.length - 1] == currentPanel[0]) {
				//if the last slide is active, make the next button inactive
					next.addClass('inactive').attr('href', '');
					prev.removeClass('inactive');
				} else {
					next.removeClass('inactive');
					prev.removeClass('inactive');
				}
		
				var currentPanelId = $(currentPanel[0]).attr('id');
				var currentLink = $('#controls').find('a.'+currentPanelId);
				
				
				
				if (!currentLink.parent('li').hasClass('.active')) {
					currentLink.parent('li').siblings().removeClass('active');
					currentLink.parent('li').addClass('active');				
				}
				
				return currentPanel;
							
			};
			
			function setUpSlideshow() {
					
					var controlLi = $('#controls').find('ul');
					var controlLinks = $('#controls').find('a');
		
					// add a control for each number of slides present; set up prev/next
					$(slides).each(function(index) {
						homepage_data.heightMap.push([$(this).attr('id'), $(this).height()])
						var currentSlide = index + 1;
						// if index = 0 add class active (first slide) else if its last add class last
						if (index == 0) {
							controlLi.append('<li class="active first"><a  class="slot-'+ currentSlide +'" href="#slot-'+ currentSlide +'">'+ currentSlide +'</a></li>');
						} else if (index == (slides.size() - 1)) {
							controlLi.append('<li class="last"><a class="slot-'+ currentSlide +'" href="#slot-'+ currentSlide +'">'+ currentSlide +'</a></li>');
						} else {
							controlLi.append('<li><a class="slot-'+ currentSlide +'" href="#slot-'+ currentSlide +'">'+ currentSlide +'</a></li>');
						}
					});
					
					controlLinks.smoothScroll({
						offset: -100,
						easing:'swing',
						speed:800,
						afterScroll:function() {
							//If we are at the first slide, slide all the way to top of page
							var cp = findCurrentPanel();
							if ($(cp[0]).attr('id') == 'slot-1' ) {
								$('html, body').animate({scrollTop: '0px'}, 200);
								return false;
							}
						}
					});
					
					$('#controls').find('a[href=""]').click(function(e) {
						if( $(this).hasClass('inactive') ){
							return false;
						} else {
							e.preventDefault();
						}
					});
			};
			
			
			function setSliderFootScroll() {
				var controls = $('#controls-wrapper');
				
				var window_height = $(document).height();
				var pos = $(window).scrollTop();
				var scrollBottom = pos + scrollData.controls_height + scrollData.foot_height;

				if (scrollBottom >= window_height) {
					controls.hide();
				} else if (scrollBottom < window_height) {
					controls.show();
				}
		
			};
			
				
			if (bnys.isiPad || bnys.isIe6 || bnys.isiPhone) {
				$('#controls-wrapper').hide();
			} else {
				setUpSlideshow();
				// findCurrentPanel needs to be called both on load, and on scroll.
				findCurrentPanel();

				$(window).scroll(function (e) {
					findCurrentPanel();
					setSliderFootScroll();
				});
			}
			
			var scrollData = {
				controls_height: $('#controls').height() + 300 + 40, // 300 = offset of controller top, 40 = small padding so it doesn't touch footer
				foot_height: $('#footer').height()
			}

		}//end of slideshow
					
	};
	
	
	bnys.views.initHompage = function (){

			homepage.scroller();
			homepage.slideshow();
//			homepage.videoPlayer();
				
	};
	
	return bnys;
	
})(jQuery, BNYS || {}, app)
