// JavaScript Document
/******************************************************/
$(document).ready(init);

var nav,
galleryNav,
galleryNavA, 
galleryNavLi,
galleryText, 
galleryLinks, 
bg1, 
bg2, 
bgTop, 
bgBottom, 
galleryIndex, 
galleryTimer, 
bgContainer, 
ieVersion, 
projectLink,
slideInterval, 
animating,
wrapperTop;

function init() {

	//selector caching
	nav = $("div#mainNav ul li a");
	galleryNav = $(".gallery ul");
	galleryNavLi = $(".gallery ul li");
	galleryNavA = $(".gallery ul li a");
	bg1 = $("#wTopBg1");
	bg2 = $("#wTopBg2");
	galleryText = $(".gallery .viewport h1");
	galleryLinks = $(".gallery a.textLink");
	bgContainer =  $(".wrapperTop");
	projectLink = $("#projectLink");
	
	
	//setup
	animating = false;
	galleryIndex = {id:-1};
	bgTop = bg1; bgBottom = bg2;
	getInternetExplorerVersion(); 

	galleryNavA.hover(handleGallery);
	
	//preload backgrounds
	galleryNavA.each(function(index, item) {
		$('<img/>').attr("src",$(item).data("img"));
		$('<img/>').attr("src",$(item).data("imgT"));
	});
	
	//shut off blog if no entries
	if ($(".posts ul li").length == 0) {
		$(".postsContent").hide();
	}
	
	//shut off jobs if no entries
	if ($(".jobs ul li").length == 0) {
		$(".jobsContent").hide();
	}
	
	//run slideshow
	slideInterval = setInterval(slideShow, 10000);
	gallerySelect(0);
	
}


/******************************************************/ 
// methods:
function slideShow() {
	var g = (galleryIndex.id + 1);
	if (g > galleryNavA.length-1) { g = 0 };
	gallerySelect(g, false);
}

function handleGallery(e) {
	e.preventDefault();
	if (!animating) {
		var tId = $(this).data("id");
		gallerySelect(tId, true);
	}
}

function gallerySelect(index, cancel) {

	if (cancel) {
		clearInterval(slideInterval);
	}

	if (index != galleryIndex.id && animating===false) {
		animating = true;
		// var w = $(window).width();
		var h = bgContainer.height();
		var w = bgContainer.width();
		
		galleryNavLi.removeClass("active").addClass("inactive");
		galleryNavLi.eq(index).removeClass("inactive").addClass("active");
		
		if(ieVersion > 8 || ieVersion == -1){
			// All modern browsers use this:
			
			//debug
			log("css:" + "url(\""+galleryNavA.eq(index).data("img")+"\"), url(\""+galleryNavA.eq(index).data("imgT")+"\")");
			
			bgBottom.css({
							"background-image" : "url(\""+galleryNavA.eq(index).data("img")+"\"), url(\""+galleryNavA.eq(index).data("imgT")+"\")",
							"height" : h
						});
			// opacity change on copy & transparent pngs:
			galleryNav.animate({opacity:0},200);
			galleryLinks.animate({opacity:0},200);
			galleryText.animate({opacity:0}, 400);
		}else{
			// IE 7 & 8 use this:
			bgBottom.css({
							"background-image" : "url(\""+galleryNavA.eq(index).data("img")+"\")",
							"background-position" : "50% 0px",
							"height" : h
						});
			// opacity change renders poorly - so not used here:
			galleryNav.css("visibility", "hidden");
			galleryLinks.css("visibility", "hidden");
			galleryText.css("visibility", "hidden");
		}
		
		if (galleryIndex.id < index) {
			/*
			bgBottom.css("top", w);
			bgTop.animate({top: -w}, {duration:500, easing:"easeOutQuart", complete:gallerySwap});
			bgBottom.animate({top:0}, {duration:500, easing:"easeOutQuart"});
			*/
			bgBottom.css("left", w);
			bgTop.animate({left: -w}, {duration:700, easing:"easeOutQuart", complete:gallerySwap, useTranslate3d:true});
			bgBottom.animate({left:0}, {duration:700, easing:"easeOutQuart", useTranslate3d:true});
		} else {
			/*
			bgBottom.css("top", -w);
			bgTop.animate({top: w}, {duration:500, easing:"easeOutQuart", complete:gallerySwap});
			bgBottom.animate({top:0}, {duration:500, easing:"easeOutQuart"});
			*/
			bgBottom.css("left", -w);
			bgTop.animate({left: w}, {duration:700, easing:"easeOutQuart", complete:gallerySwap, useTranslate3d:true});
			bgBottom.animate({left:0}, {duration:700, easing:"easeOutQuart", useTranslate3d:true});
		}
		
		var data = galleryNavA.eq(index).data("id");
		galleryIndex = { 
						id:galleryNavA.eq(index).data("id"), 
						img:galleryNavA.eq(index).data("img"), 
						name:galleryNavA.eq(index).data("name"),
						link:galleryNavA.eq(index).data("link")
						};
		
	}
}

function gallerySwap() {
	log("gallerySwap");
	// log(galleryText.html())
	galleryText.html(galleryIndex.name);
	projectLink.attr("href", galleryIndex.link);
	
	if(ieVersion > 8 || ieVersion == -1){
		// All modern browsers use this:
		galleryText.animate({opacity:1},{complete:swapHelper, duration:400});
		galleryNav.animate({opacity:1},200);
		galleryLinks.animate({opacity:1},200);
	} else {
		// opacity change renders poorly - so not used here:
		galleryNav.css("visibility", "visible");
		galleryLinks.css("visibility", "visible");
		galleryText.css("visibility", "visible");
		swapHelper();
	}
	
	if (bgTop == bg1) {
		bgTop = bg2;
		bgBottom = bg1;
	} else {
		bgTop = bg1;
		bgBottom = bg2;
	}
	
	/*
	var h = bgContainer.height();
	bgBottom.css("top", h);
	*/
	var w = bgContainer.width();
	bgBottom.css("left", w);
}

function swapHelper() {
	log("swapHelper");
	animating = false;
}

/******************************************************/
// Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
function getInternetExplorerVersion(){
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  ieVersion = rv;
}
/******************************************************/
// generic trace - prevents breakage in other browsers:
function log(something) {
	if (typeof console === "undefined") {
		return;
	} else {
		console.log(something);
	}
}
