/*
*************************************************************************
**  Home Slider
*************************************************************************
*/
var intPanelInterval = 0;
var numSeconds = 1000 * 5;
var transition = 3000;
var intTotalPanels = -1;
var isChanging = false;
var isRotate = true;
var blnFirst = true;

// Page init
jQuery(function($) {					
	
	// Init splash
	if(jQuery('#splash').length > 0){
		resetPanels();
	}
	
	var windowHeight = $(window).height() - 80;
	$("a[rel='lightbox']").colorbox(
		{	
			preloading:true,
			transition:"fade",
			initialWidth:800,
			slideshow:true,
			slideshowSpeed:5000,
			maxHeight:windowHeight,
			maxWidth:960,
			scalePhotos:true,
			title: function(){
	    		var url = $(this).attr('title');
		    	return '<a href="'+url+'" target="_blank">[Open New Window]</a>';
			}
	});

});

function getCurrentPanel(){
	return Number(jQuery('#splash-counter').val());
}
function clickPanel(num) {
	isRotate = false;
    showPanel(num);	
}
function clickNext() {
	isRotate = false;
    nextPanel();	
}
function clickPrev() {
	isRotate = false;
    prevPanel();	
}
function resetPanels() {

	// Apply left/right keystroke commands
	arrow = {left: 37, up: 38, right: 39, down: 40 };	
	jQuery(document).keydown(function (e) {
		switch (e.keyCode) {
			case arrow.left:
		  		clickPrev();
				break;			
			case arrow.right:
		  		clickNext();
				break;
		}
	});
	
	// Set Total Panels
	intTotalPanels = Number(jQuery('#splash-hidden img').length);
	
	// Build splash links	
	for (x=1; x <= intTotalPanels; x++){
	   jQuery('#splash-links').append('<a onclick="clickPanel('+x+')" href="javascript:;" id="splash-link-'+x+'" class=""><span>'+x+'</span></a>');
	}
	
	// Reset Counter
    jQuery('#splash-counter').val(-1);
	
	// Show First Panel
    showPanel(1);
}
function prevPanel(){
	var current = getCurrentPanel();	
    if (current == 1)
        showPanel(intTotalPanels, false);
    else {
        showPanel(current - 1, false);
    }	
}
function nextPanel(){
	var current = getCurrentPanel();	
    if (current == intTotalPanels)
        showPanel(1, true);
    else {
        showPanel(current + 1, true);
    }	
}
function showPanel(num, isLeft){	
	if (!isChanging){ // If Not Blocked	
		
		// Block UI changes
		isChanging = true;
		
		// If not current panel, show panel
		if (current != num) {
			
			// Stop Rotating
			clearInterval(intPanelInterval);
			
			// Get Current Panel
			var current = getCurrentPanel();
			
			// Set Counter
			jQuery("#splash-counter").val(num);		
			
			// Clear Selected Link	
			jQuery("#splash-links a").removeClass('selected');			
			jQuery("#splash-link-" + num).addClass('selected');
			
			// Generate URL
			var url = jQuery('#splash-hidden img:eq('+(num-1)+')').attr("src");
			
			// Set Splash Background to Current Image
			jQuery('#splash').css('background-image', jQuery('#splash-image').css('background-image'));
			
			// Hide Panel and Load New Image
			jQuery('#splash-image').hide().css('background-image', 'url("' + url + '")');
			
			// Preload New Image, and Fade-in Panel
			$('<img />').attr('src', url).load(function(){
				jQuery('#splash-image').fadeIn("slow", function(){
																
					// If Rotating, set timer																
					if(isRotate) intPanelInterval = setInterval(nextPanel, numSeconds);
					
					// If tools aren't visible, show them
					if (jQuery('#splash-overlay:visible').length == 0){jQuery('#splash-overlay').fadeIn();}
					
				});			
			});			
		}		
	
		// Un-block UI changes
		isChanging = false;

	} // end-if (!isChanging)
}


