/************************************************/
/*				Hover Me Script					*/
/* 			Shawn Souto	 - 6/23/10				*/
/************************************************/

/* updates highlighted box in current hover-me object */
function SetActive(el) {
	$($(el).parent().parent().find("li a")).each(function(i) {
		if (i != 0 && i <= $.hoverMe.pageSize) {
			$(this).removeClass("active");
			if (i==$(el).parent().parent().parent().parent().attr("index")) {
				$(this).addClass("active");	
			}
		}
	});
	$(el).addClass("active");	
}

$(document).ready(function() {
	//hover-me configuration
	$.hoverMe = {
		distance: 920,
		pageSize: 10,
		index: 1
	};
	//on-load add an index attribute to represent
	//the current index for each hover-me object
	$(".hover-me").each(function(i) {
		$(this).attr("index", $.hoverMe.index);
	});
	
	//moves the hover-me object one index position to the left
	$(".arrows-prev").click(function() {
		var obj = $(this).parent().parent().parent().parent();
		if (obj.attr("index") > 1) { 
			var ctr = $(this).parent().parent().parent().parent().find(".sliderGallery").children();
			//decrement index for selected hover-me object
			var idx = obj.attr("index");
			if (idx > 1) {
				obj.attr("index", --idx);
				//animate the div control
				ctr.animate({ left: '+=' + $.hoverMe.distance }, 500); 
			}
		}
		SetActive(this);	
		return false;
	});
	
	//moves the hover-me object one index position to the right
	$(".arrows-next").click(function() {		
		var obj = $(this).parent().parent().parent().parent();
		if (obj.attr("index") < $.hoverMe.pageSize) { 
			var ctr = $(this).parent().parent().parent().parent().find(".sliderGallery").children();
			//increment index for selected hover-me object
			var idx = obj.attr("index");
			if (idx < $.hoverMe.pageSize) {
				obj.attr("index", ++idx);
				//animate the div control
				ctr.animate({ left: '-=' + $.hoverMe.distance }, 500); 
			}
		}
		SetActive(this);
		return false;
	});
	
	//disable clicking on active box
	$(".pages li a.active").live("click", function() { return false; });
	
	//for every anchor[box] that is active(selected)
	//move the hover-me object to that item's index
	//the item index is stored in it's title attribute
	$(".pages li a:not(.active)").live("click", function() {
		var ctr = $(this).parent().parent().parent().parent().find(".sliderGallery").children();	
		thisIdx = $(this).attr("title");
		var obj = $(this).parent().parent().parent().parent();
		if (thisIdx < obj.attr("index")) { //$.hoverMe.index
			var diff = obj.attr("index") - thisIdx;
			obj.attr("index", thisIdx); 
			ctr.animate({ left: '+=' + (diff*$.hoverMe.distance) }, 500); 
		}
		else if (thisIdx > obj.attr("index")) { //$.hoverMe.index
			var diff = thisIdx - obj.attr("index");
			obj.attr("index", thisIdx); 
			ctr.animate({ left: '-=' + (diff*$.hoverMe.distance) }, 500);
		}
		SetActive(this);
		return false;
	});
	
	
});
