// JavaScript Document
//BluSolutions Site
/* --------------- Functions that need to run after the document loads -------------------*/

$(document).ready(function(){

// google-talk new window
	$('a.new-window').click(function(event){
		var popup = window.open(this.href,"_blank","toolbar=no, location=yes, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=330, height=550");	 
		event.preventDefault();
	});


// quick nav dropdown functionality
	$(".quick-item").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});


//homepage footer text expand functionality
   $(".expand-btn").toggle(function(event){
     $("#footer-text p").show();
     $("#footer-text li").show();
	 $(".expand-btn").html("Show Less...");
   }, function(event){
	   $("#footer-text p").not("#footer-text p.short-desc").hide();
     $("#footer-text li").hide();
	 $(".expand-btn").html("Show More...");
	   }
   );

// Load Images for rotation
	if($("body").is("#main")) {
		$.get("/site-resources/home-page-image-rotation.html", function(data){
			setupRotation(data);
		});
	}
		
});

/* -------------------------------------------------------------------- */

/* ---------- START EXTRA FUNCTIONS --------- */

function setupRotation (data) {

	//default values
	itemClass = "nav-item";
	itemFirstClass = "first-item";
	itemLastClass = "last-item";
	timerLen = 16000; // equals 16 secs
	elementID = "banner-counter";
	
	$('#featured-content').append('<a id="mainbanner-link"></a><ul id="banner-startstop" class="banner-controls"><li class="first-item"><a><span>pause</span></a></li></ul><ul id="banner-counter" class="banner-controls"></ul>');	

	//parse data
	itemArray = data.split(",");
	itemArray.splice(itemArray.length - 1, 1); // remove the extra data
	itemArrayLen = itemArray.length;
	// data format: pagetitle,imageurl,linkurl

	//load images
	for (x = 1; x < itemArrayLen; x = x + 3) {		
		//image
		tmpValue = itemArray[x];
		itemArray[x] = new Image();
		itemArray[x].src = tmpValue;
	}
	
	//if(itemArrayLen > 0) $('#' + elementID).append('<li class="' + itemClass + ' control play"><a>Play</a></li>');	
	
	//create html for the gallery nav
	for (x = 0; x < (itemArrayLen/3); x++) {
		
		if (x == 0) {
			itemTmpClass = itemClass + ' ' + itemFirstClass;
		} else if (x == itemArrayLen - 1) {
			itemTmpClass = itemClass + ' ' + itemLastClass;
		} else {
			itemTmpClass = itemClass;
		}
			
		$('#' + elementID).append('<li class="' + itemTmpClass + '"><a>' + (x+1) + '</a></li>');	
	}
	
	// When the gallery nav is clicked switch banner
	$('#' + elementID + ' > ' + '.' + itemClass).click(function () {
		// Clear the old timer. Switch to the clicked item, and restart the timer.
		clearTimeout(bannerTimer);
		switchActive(this);
		bannerTimer = setTimeout("advanceActive()", timerLen);		
	});
	
	// play / pause button
	$('#banner-startstop li').toggle(function (e) {
		// pause
		$(this).children('a').css('background-position','11px -31px');
		bannerPause();
	}, function () {
		// play
		$(this).children('a').css('background-position','11px 7px');
		bannerPlay();
	});
	
	switchActive();
	bannerTimer = setTimeout("advanceActive()", timerLen);
}

function bannerPlay () {
	bannerTimer = setTimeout("advanceActive()", timerLen);
}

function bannerPause () {
	clearTimeout(bannerTimer);
}

function activateEl(elementRef) {
	$(elementRef).addClass("active");
	$(elementRef).siblings().removeClass("active");
}

function switchActive (elementRef) {
	
	// Find the next element or use the passed element reference
	if (elementRef == null) {
		object = $('#' + elementID + ' > ' + '.active').next('.' + itemClass);
	} else {
		object = elementRef;
	}
	
	// If at the end then start over
	if ($(object).length == 0) {
		object = $('#' + elementID + ' > ' + '.' + itemClass + ':first-child');
	}

	// Find the element index
	var itemIndex = $('#' + elementID + ' > ' + '.' + itemClass).index(object);
	
	// Set the image, title, and url live
	$("#mainbanner-link > span").text(itemArray[(itemIndex * 3)]);
	$("#container").css("background-image", 'url(' + itemArray[(itemIndex * 3) + 1].src + ')');
	$("#mainbanner-link").attr('href', itemArray[(itemIndex * 3) + 2]);

	activateEl(object);
}

function advanceActive () {
	switchActive();
	bannerTimer = setTimeout("advanceActive()", timerLen);		
}

/* ----- SEARCH FUNCTIONS ----- */

function get(param) {		
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
		
	if (param) {
		return qsParm[param]; // if param is specified then just pass one back.
	} else {	
		return qsParm; // if no param spec. then pass all back.
	}
}
