

$(document).ready( jsInitialize );

// set global variables
var
galleryNav,
galleryNavA, 
galleryNavLi,
bg1, 
bg2, 
bgTop, 
bgBottom, 
objSelected, 
galleryTimer, 
bgContainer, 
iIEVersion, 
objTimer, 
iInterval,
bAnimate, 
wrapperTop;

function jsInitialize() {

	getInternetExplorerVersion(); 

	//selector caching
	bgContainer =  $(".wrapperTop");
	bg1 = $("#wTopBg1");
	bg2 = $("#wTopBg2");
	galleryNav = $(".gallery ul");
	galleryNavLi = $(".gallery ul li");
	galleryNavA = $(".gallery ul li a");
	
	
	//setup
	bAnimate = false;
	objSelected = {id:-1};
	bgTop = bg1; 
	bgBottom = bg2;
	iInterval = 7000;

	galleryNavA.click( jsInitNext );

	galleryNavA.each( function( index, object ) 
	{
		jsPreloader( object, "img" )
		jsPreloader( object, "bg_repeat" )
	});

	objTimer = setInterval( jsAnimate, iInterval );
	
	jsMoveNext( 0, 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 );
	}
	
	iIEVersion = rv;
}

function jsPreloader( object, sElement ) 
{
	//preload background images
	$('<img/>').attr("src",$(object).data( sElement ));
}


function jsAnimate() 
{
	var index = ( objSelected.id + 1 );
	
	// reset index if at end of slide show
	if ( index > galleryNavA.length-1 ) 
	{ 
		index = 0 
	};
	
	jsMoveNext( index, false );
	
}

function jsInitNext( e ) 
{ 
	e.preventDefault();
	if (!bAnimate) 
	{
		var index = $(this).data("id");
		jsMoveNext( index, true );
	}
}

function jsMoveNext( index, bReset ) 
{
	if (bReset) 
	{
		clearInterval(objTimer);
		objTimer = setInterval( jsAnimate, iInterval )
	}

	if (index != objSelected.id && bAnimate==false) 
	{
		bAnimate = true;
		// var w = $(window).width();
		var h = bgContainer.height();
		var w = bgContainer.width();

		h = 673

		galleryNavLi.removeClass("visible").addClass("invisible");
		// chnage class of selected button to visible
		galleryNavLi.eq(index).removeClass("invisible").addClass("visible");

		if(iIEVersion > 8 || iIEVersion == -1)
		{
			// All modern browsers use this:
			
			bgBottom.css({
				"background-image" : "url(\""+galleryNavA.eq(index).data("img")+"\"), url(\""+galleryNavA.eq(index).data("bg_repeat")+"\")",
				"height" : h
			});
			// opacity change on copy & transparent pngs:
			galleryNav.animate( {opacity:0}, 150 );
			
		}
		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");
		}
		
		h1 = -h;
		h2 = h;			
		if (objSelected.id < index) 
		{
			h1 = h;
			h2 = -h;
		} 

		iDuration= 550;
		sEasing = "easeInBack"
		//sEasing = "easeOutBounce"

		bgBottom.css("top", h2);
		bgTop.animate( {top: h1}, 
		{
			duration:iDuration, 
			easing: sEasing, 
			complete: gallerySwap, 
			useTranslate3d:true 
		});
		bgBottom.animate( {top:0}, {duration:iDuration, easing: sEasing, useTranslate3d:true});
		
		objSelected = { 
			id:galleryNavA.eq(index).data("id")
		};
		
	}
}

function gallerySwap() 
{
	
	if(iIEVersion > 8 || iIEVersion == -1){

		//galleryNav.animate( {opacity:1}, 300 );
		galleryNav.animate( {opacity:1}, {complete: bAnimate = false, duration: 150} );
	} 
	else 
	{
		// opacity change renders poorly - so not used here:
		galleryNav.css("visibility", "visible");
		bAnimate = false;
	}

	if (bgTop == bg1) {
		bgTop = bg2;
		bgBottom = bg1;
	} else {
		bgTop = bg1;
		bgBottom = bg2;
	}
	
	h= 673
	var h = bgContainer.height();
	bgBottom.css("top", h);

	//var w = bgContainer.width();
	//bgBottom.css("left", w);

}


function jsPause()
{
	clearInterval( objTimer );
	objTimer = setInterval( jsAnimate, 7000 );
}



