//the last scroll position of the window
var lastScrollPos = 0;

//the layer / pace of shards to move
var layers = new Array(1,2,2,2)

var totalMove = 0;

var isIpad = navigator.userAgent.indexOf("iPad");

var activeDiv = 0;
/*
 * var layer1move = 1;
		var layer2move = 2;
		var layer3move = 0.25;
		var layer4move = 0.1;
 */
 
 var SKY_TRACKING = { 
	site: "sky/portal/skyarts", 
	section: "ignition/home/", 
	headline: "", 
	contentType: "home", 
	contentId: "", 
	account: "bskybspskyarts,bskybnetworkglobal"//set dev account 
}


function trackLink(action,type){
	if(!type){
		type = action;
	}
	action = action.replace("#","");
	SKY_TRACKING.section = "ignition/" + action;
	SKY_TRACKING.contentType = type;
	sky.tracking.launch ( SKY_TRACKING );
}

//this is the min width of the design - change if the design changes
var minWidth = 1800;

//time in milliseconds to scroll between sections
var scrollTimer = 2000;

//timers for fading for lightboxes
var fadeInTimer = 100;
var fadeOutTimer = 100;

//whether faqs should autoclose or not
var autoClose = true;
var faqTimer = 300;

function resizeWindow(){
	var widthDiff = $(window).width() - minWidth;
	if(widthDiff < 0){
		$("#container").css({"margin-left" : (widthDiff / 2) + "px", "margin-right" : (widthDiff / 2) + "px"});
		if($.browser.msie && $.browser.version == "7.0"){
			//ie is doing something weird with resizing
			$("body").css({"background-position" : (widthDiff /2) + "px 0"});
		}
	} else {
		$("#container").css({"margin" : "0 auto"});
		if($.browser.msie && $.browser.version == "7.0"){
			//ie is doing something weird with resizing
			$("body").css({"background-position" : "center 0"});
		}
	}
	
}

function scrollShards(curScrollPos,scrollMove,initialScroll){
	
	for(i=0;i<layers.length;i++){
		var className = "span.layer" + (i+1); 
		//get all items in layer 1 and move them
		$('#shards').find(className).each(function(g){
			doScroll(curScrollPos,scrollMove,layers[i],$(this));
		});
	}
}

//make the item move
function doScroll(curScrollPos,scrollMove,layerMove,obj,initialScroll){
	if(scrollMove != 0){
		nextMove = layerMove * scrollMove;
//		if($(obj).attr("id") == "shard1"){
//			//totalMove += nextMove;
//			//console.log(totalMove);
//			console.log($(obj).offset().top);
//		}
		var elTop = $(obj).offset().top;
		var elBottom = elTop + $(obj).height();
		var top = curScrollPos - $(obj).height();
		var bottom = curScrollPos + $(window).height() + $(obj).height();
		
		//if(elBottom + nextMove < $('#container').height() && (elBottom + nextMove) < bottom && (elTop + nextMove) > top){
			$(obj).css({"top": parseInt($(obj).css("top")) + nextMove + "px"});
		//}
		elBottom = null;
		elTop = null;
		top = null;
		bottom = null;
	}
}

//these just link between panels
function switchPanel(){
	$('ul.subnav').each(function(e){
		$(this).attr("data-height",$(this).height()).css({"height":"0"});
	});
	$('ul.nav, .subLink').find('a').click(function(e){
		trackLink($(this).closest("div.article").attr("id"),$(this).attr("href"));
		e.preventDefault();
		var refEl = $(this).attr("href");
		var id = refEl.replace("#",'');
		if($(this).hasClass("subLink") === false){
			$(this).closest('ul').find('a').removeClass('active');
			$(this).addClass('active');
		} else {
			$(this).closest("div.article").find("ul.nav a").each(function(f){
				if($(this).attr("href") == refEl){
					$(this).addClass("active");
				} else {
					$(this).removeClass("active");
				}
			});
		}
		var openNav = $(this).closest("ul.nav").find("li.subnav ul.subnav");
		if(openNav.length > 0){
			openNav.each(function(e){
				if($(this).closest("li").attr("id") != "subnav_" + id && $(this).height() > 0){
					$(this).animate({"height":"0px"},500);
				}
			});
		}
		if($('#subnav_'+ id).length > 0){
			$('#subnav_'+ id).find("ul.subnav").each(function(e){
				var toHeight = 0;
				if($(this).height() > 0){
					//$(this).closest("li.subnav").prev().find("a").removeClass("active");
				} else {
					toHeight = $(this).attr("data-height");
				}
				$(this).animate({"height":toHeight + "px"},500);
				if($("#" + id + "_intro").length == 0){
					$(this).find("li a").first().addClass("active");
				}
			});
		}
		var target = $(this).attr("href");
		var switchContent = $(this).closest('.article').find('.panelSwitchContent');
		var areaOffset = $(switchContent).offset().left;
		var targetOffset = $(target).offset().left;
		var newOffset = areaOffset - targetOffset;
		$(switchContent).animate({"left" : (newOffset) + "px"}, 300);
		switchContent = null;
	});
}

//figure out the current scroll position of the window
function getScrollPos() {
	
	var fromTop = 0;
	//most browsers
	
	if($.browser.webkit){
		fromTop = document.body.scrollTop;
	}
	//irritating browsers;
	else if($.browser.msie || $.browser.opera || $.browser.mozilla){
		fromTop = document.documentElement.scrollTop;
	}
	/*else if(){
		fromTop = document.body.scrollTop;
	}*/
	else {
		fromTop = document.pageYOffset;
	}
	
	//alert(fromTop);
	
	return fromTop;
}

//keep track of what area on the page we're on
function doNav (){
	
	//get all the nav items
	var navItems = $('#mainNav').find("a").each(function(e){
		
		//get the content area they point to
		var content = $($(this).attr("href"));
		
		//check its position from the top
		var thisTop = content.offset().top + parseInt(content.css("padding-top"));
		
		//check where it ends
		var thisBottom = thisTop + content.height() - parseInt(content.css("padding-bottom"));
		
		//get our current scroll position
		var winTop = getScrollPos();
		
		//get the window height
		var winBottom = winTop + $(window).height();
		
		//if the top of the element is above or just at the top of the window and the bottom of the element is below the top of the window
		//or if the top is below or just at the top of the window and the top is above the bottom of the window
		if((thisTop <= winTop && thisBottom > winTop) || (thisTop >= winTop && thisTop < winBottom)){
			
			//if the item takes up at least 51% of the window then set as active
			if((thisBottom - winTop) / (winBottom - winTop) >= 0.51 && (thisTop - winTop) / (winBottom - winTop)  <= 0.49){
				$(this).addClass("active");
			//otherwise it's not active
			} else {
				$(this).removeClass("active");
			}
		//otherwise it's not active
		} else {
			$(this).removeClass("active");
		}
		
		//memory cleanup
		content = null;
		thisTop = null;
		thisBottom = null;
		winTop = null;
		winBottom = null;
	});
}

function setupLightbox(){
	//add the lightbox elements (overlay and container)
	$('body').append("<div id='overlay'></div>");
	$('body').append("<div id='mediaPlayer'><div id='mediaContent'></div><a href='#' id='closer'><!-- --></a></div>");
	
	//ensure we can't see the overflow as the overlay is set to be really tall
	$('window').css({"overflow" : "hidden"});
}

function destroyLightbox(){
	
	//close the lightbox
	$('#mediaPlayer').remove();
	$('#overlay').animate({"opacity" : 0},fadeOutTimer,function(g){$(this).remove();});
}

function positionPopup(toHeight){
	
	if($.browser.msie && $.browser.version == "6.0"){
		//position absolute for ie6
		var top = (getScrollPos() + (($(window).height() - toHeight) / 2)) + "px";
		$('#mediaPlayer').css({
			"top" : top,
			"left" : parseInt(($(window).width() - $('#mediaPlayer').width()) / 2) + "px"
		});
	} else {
	
		//position the media player popup centrally in the window
		$('#mediaPlayer').css({
			"top" : parseInt(($(window).height() - toHeight) / 2) + "px",
			"left" : parseInt(($(window).width() - $('#mediaPlayer').width()) / 2) + "px"
		});
	}
}

function faqs(){
	/*$('#faqAccordion').find("div.faqContent").each(function(e){
		$(this).attr("data-height",$(this).height());
		$(this).css({"height":"0","overflow":"hidden","position":"relative"});
	});
	$('#faqAccordion').find('h3 a').click(function(e){
		trackLink("about","faq");
		e.preventDefault();
		var actionDiv = $(this).closest("li").find("div.faqContent");
		activeDiv = $(this).text();
		if($(actionDiv).height() == 0){
			$(actionDiv).animate({"height":$(actionDiv).attr("data-height") + "px"},faqTimer);
			$(this).closest("li").animate({"margin-left":"0","margin-right":"0","padding-right":"10px","padding-left":"27px"},faqTimer).addClass("active");
		} else {
			$(actionDiv).animate({"height":"0px"},faqTimer);
			$(this).closest("li").animate({"margin-left":"5px","margin-right":"5px","padding-right":"5px","padding-left":"22px"},faqTimer).removeClass("active");
		}
		if(autoClose === true){
			$('#faqAccordion').find("div.faqContent").each(function(e){
				if($(this).closest("li").find("h3 a").text() != activeDiv && $(this).height() != 0){
					$(this).animate({"height":"0px"},faqTimer);
					$(this).closest("li").animate({"margin-left":"5px","margin-right":"5px","padding-right":"5px","padding-left":"22px"},faqTimer).removeClass("active");
				}
			});
		}
	});*/
}

function playVideo(vidUrl,type,width,height){
	$('#mediaContent').flash(
		{
			swf: "http://hayonsky.skyarts.co.uk/inc/videoplayer/flash/SkyvideoWrapper.swf",
			flashvars: { 
						 playerType : "embedded",
						 type : type,
						 videoSourceID : null,
						 flashVideoUrl : vidUrl
						},
			wmode : "transparent",
			allowScriptAccess : "always",
			allowFullScreen : "true",
			width: width,
			height: height
		}
	);
}



$(window).load(function(e){
	resizeWindow();
	
	doNav();
	
	$('#container').css({"overflow" : "hidden"});
	$('body').css({"overflow-x":"hidden"});
	
	//keep an eye on resizing
	$(window).resize(function(e){
		resizeWindow();
	});
	
	lastScrollPos = getScrollPos();
	if(lastScrollPos != 0){
		scrollShards(0,-1 * lastScrollPos);
	}
	
	//do scroll downs based on main nav or jump links in articles
	$('#mainNav, .article p.link, #logo span.link').find("a").click(function(f){
		trackLink($(this).attr("href"));
		f.preventDefault();
		
		//add active link (if you need to)
		//$("#mainNav").find("a").removeClass("active");
		//$('#mainNav').find("a[href='" + $(this).attr("href") + "']").addClass("active");
		var toEl = $(this).attr("href");
		if(isIpad > -1){
			$('html,body').animate({"scrollTop":$(toEl).offset().top},{duration: scrollTimer, step : function (curPos) { 
				var curScrollPos = getScrollPos();
				var scrollMove = lastScrollPos - curScrollPos;
				scrollShards(curScrollPos,scrollMove);
				lastScrollPos = curScrollPos;
				doNav();
			}});
		} else {
			$.scrollTo({top: $(toEl).offset().top, left: 0},scrollTimer);
		}
		
	});
	
	//observe scroll of window, move layers accordingly
	$(this).scroll(function(f){
		var curScrollPos = getScrollPos();
		var scrollMove = lastScrollPos - curScrollPos;
		
		scrollShards(curScrollPos,scrollMove);
		
		lastScrollPos = curScrollPos;
		
		doNav();
	});
	
	/* no longer used
	if($('#videoPlayer').length > 0){	
		
	}
	
	if($('#galleryPlayer').length > 0){
		
	}*/
	
	switchPanel();
	$("#container").css({"display":"block"});
	$("#loadOverlay").animate({"opacity":"0"},1500,function(){$('#loadOverlay').remove()});
});
