/* Homepage Extension */
site.homepage = window.site.homepage = {
	data : {
		windowSizeTimeout : null,
		homepageTimer : null,
		homepageTimerInterval : 6000,
		promoBottom : 0,
		imageFactor : (2000/1400),
		focusFactor : (2000/1200), 
		focusWidth : 1400,
		focusHeight : 840,
		viewHeight : null,
		viewWidth : null
	},
	func : {
		setupHomepageNavigation : function() {
			/* Create the Nav List */
			var navHTML = "";
			$(".js-homepageFrameTitle").each(function(i) { 
				//var thisLink = '<a href="#' + $(this).text() + '">' + $(this).text() + '</a>';
				var thisLink = '<a href="#' + $(this).text() + '">' + (i+1) + '</a>';
				navHTML += '<li class="ie6png frameLink">' + thisLink + '</li>';
			});
			$(".js-homepageNavigation").prepend( navHTML );
			/* --------------------- */
			
			/* Make Nav Buttons */
			new lib.obj.button({
				hasClick: true,
		        hasHover: true,
		        activeId: "js-hpActive",
		        cssButton: true,
		        cssOff: "",
		        cssOn: "active",
		        cssHover: "active",
		        buttonSelector: ".js-homepageNavigation .frameLink a",
		        buttonCollectionSelector : ".js-homepageNavigation .frameLink a"
			});
			/* ---------------------- */
		},
		setupHomepageFrameCycling : function() {
			/* Start Cycling the Images */
			var numberOfFrames = $(".js-homepageFrame").size();
			var currentIndex = 0;
			site.homepage.func.startCycling(currentIndex, numberOfFrames);
			/* ----------------- */

			/* Setup Manual Cycling */
			$(".js-homepageNavigation .frameLink a").click(function(evt) {
				evt.preventDefault();
				currentIndex = $(".js-homepageNavigation .frameLink a").index(this);
				site.homepage.func.startCycling(currentIndex, numberOfFrames);
			});
			$(".homePromoWrap a").click(function(evt) {
				evt.preventDefault();
				currentIndex = $(".homePromoWrap a").index(this);
				site.homepage.func.startCycling(currentIndex, numberOfFrames);
			});
			/* --------------------- */
		},
		setupWindowResizing : function() {
			$(window).bind("resize.homepage", function() {
				clearTimeout(site.homepage.data.windowSizeTimeout);
				site.homepage.data.windowSizeTimeout = setTimeout(function() {
					site.homepage.func.sizeHomepage();
					setTimeout(function() { site.homepage.func.sizeHomepage(); }, 100);
				}, 100 );
			}).resize();
		},
		setupPromos : function() {
			/* Position the promo */
			//$(".js-homepagePromoContent").css("bottom", site.homepage.data.promoBottom+"px");
			/* --------------- */
		},
		setupPromoEvents : function() {
			$(".js-homepagePromoActivator").each(function() {
				new lib.obj.button({
					off: $(this).attr("src"),
					on: $(this).attr("src").split("_off.png")[0] + "_on.png",
					hasClick : true,
					hasHover : false,
					activeId : "js-homepagePromoActive",
					buttonSelector: this,
					buttonCollectionSelector : this
				});
				$(this).parent().toggle(
					function(evt) { 
						evt.preventDefault();
						$(".js-promoContent").slideDown(500);
					},
					function(evt) { 
						evt.preventDefault();
						$(".js-promoContent").slideUp(500);
						$(".js-homepagePromoActivator").attr("id","").mouseout();
					}
				);
			});
		},
		sizeHomepage : function() {
			/* Get current Window Size, and check min height / width */
			site.data.currentWindowSize = lib.screen.size();
			site.data.currentWindowSize[0] = (site.data.currentWindowSize[0] < site.data.minSiteWidth) ? site.data.minSiteWidth : site.data.currentWindowSize[0];  
			site.data.currentWindowSize[1] = (site.data.currentWindowSize[1] < site.data.minSiteHeight) ? site.data.minSiteHeight : site.data.currentWindowSize[1] ;
			$("#homepageContent").css({ width : site.data.currentWindowSize[0]+"px", height : site.data.currentWindowSize[1]+"px" });
			/* ----------------- */
			
			/* Resize the Homepage */
				var footerHeight = $("#siteFooter").height();
			
				/* Figure out what the area is we have to put the focus */
				site.homepage.data.viewWidth = site.data.currentWindowSize[0];
				site.homepage.data.viewHeight = site.data.currentWindowSize[1] - footerHeight;
				/* -------------------- */
				
				/* Since using widescreen ratio, size the height first */
				site.homepage.data.focusHeight = site.homepage.data.viewHeight;
				site.homepage.data.focusWidth = site.homepage.data.focusHeight * ( site.homepage.data.focusFactor);
				
				/* If the Image is too wide for the window, reset to fit width */
				if( site.homepage.data.viewWidth < site.homepage.data.focusWidth )
				{
					site.homepage.data.focusWidth = site.homepage.data.viewWidth;
					site.homepage.data.focusHeight = site.homepage.data.focusWidth / ( site.homepage.data.focusFactor);
				}
			
				/* Resize the Frame based on the having the focus same propotion as image */
				var newFrameW = site.homepage.data.focusWidth * site.homepage.data.imageFactor;
				var newFrameH = site.homepage.data.focusHeight * site.homepage.data.imageFactor;
				var newLeft = ((site.homepage.data.viewWidth - site.homepage.data.focusWidth) / 2) - ((newFrameW - site.homepage.data.focusWidth) / 2);
				var newTop = ((site.homepage.data.viewHeight - site.homepage.data.focusHeight) / 2) - ((newFrameH - site.homepage.data.focusHeight) / 2) ;
				$(".js-homepageFrame").css({ width : newFrameW+"px", height : newFrameH+"px", top : newTop+"px", left : newLeft+"px" });
				/* ----------------------- */
			/* ------------------------- */
			
			/* Update the Footer */
			var footerTop = $("#homepageContent").height() - footerHeight;
			$("#siteFooter").css({ top : footerTop+"px", width : site.data.currentWindowSize[0]+"px" });
			/* ------------------ */
			
			/* Update the Promo Placement */
			site.homepage.data.promoBottom = footerHeight;
			site.homepage.func.setupPromos();
			/* --------------------- */
		},
		startCycling : function(currentIndex, numberOfFrames) {
			clearInterval(site.homepage.data.homepageTimer);
			$(".js-homepageFrame").hide();
			$(".js-homepageFrame").eq(currentIndex).fadeIn(500);
			$(".js-homepageNavigation .frameLink a").eq(currentIndex).triggerHandler("click.button");
			if( $(".js-homepageFrame").size() > 1 )
			{
				site.homepage.data.homepageTimer = setInterval(function() {
					var previousIndex = currentIndex;
					currentIndex = ( currentIndex == (numberOfFrames-1) ) ? 0 : currentIndex + 1;
					$(".js-homepageFrame").eq(previousIndex).fadeOut(500);
					$(".js-homepageFrame").eq(currentIndex).fadeIn(500);
					$(".js-homepageNavigation .frameLink a").eq(currentIndex).triggerHandler("click.button");
				}, site.homepage.data.homepageTimerInterval);
			}
		}
	}
};

$(function() {
	
	/* Setup the Homepage Navigation */
	site.homepage.func.setupHomepageNavigation();
	/* ------------------------ */
	
	/* Set minHeight for Site into global site data */
	site.data.minSiteHeight = $("#siteNavigation").height() + $("#siteFooter").height() + 5;
	/* ------------------- */
	
	/* Initialize the Window Resizing */
	site.homepage.func.setupWindowResizing();
	/* ---------------- */

	/* Initialize cycling of Homepage Frames */
	site.homepage.func.setupHomepageFrameCycling();
	/* ------------------- */
	
	/* Setup the Promos Events */
	site.homepage.func.setupPromoEvents();
	/* --------------- */
	
});	
