/**************************************************
* Global container for jQuery actions
* For Cave Design
**************************************************/

$(function() {
	
	/***** Book selector *****/
	var selectorParent = "#content_selector"; // Where are the books?
	var selectorAnchor = "li"; // Clickable element
	var selectorElement = "img"; // Data element
	var selectorLeft = "#selector_left"; // Move list left when clicking this element
	var selectorRight = "#selector_right"; // Ditto for right
	var selectorPosition = 0; // Control pointer (slide counter)
	var selectorFadeTime = 250; // Milliseconds for fade transition when updating text
	
	var selectorImage = "#selector_display img"; // Which image am I updating?
	var selectorText = "#selector_display p"; // Where does the text go?
	
	$(selectorParent+" "+selectorAnchor).live("click", function() {
		var selectorData = $(this).find(selectorElement);
		var selectorID = $(selectorData).attr("class").substr(1);
		
		$(selectorImage).attr("src", $(selectorData).attr("src"));
		
		// Use PERCH container to fetch data
		$.ajaxSetup({cache : true});
		$.get('/load-selector.html?region='+selectorPage+'&filter='+selectorID+'&page='+selectorSelf, function(response) {
			$(selectorText).animate({"opacity" : "0"}, selectorFadeTime, function() {
				$(selectorText).html(response);
				$(this).animate({"opacity" : "1"}, selectorFadeTime, function() {
					if ($.browser.msie)
					this.style.removeAttribute('filter');
				});
			});
		});
	});
	
	$(selectorLeft).click(function() {
		if (selectorPosition>=1)
		{
			selectorPosition--;
		}
		return selectorSlide();
	});
	
	$(selectorRight).click(function() {
		if (selectorPosition<1)
		{
			selectorPosition++;
		}
		return selectorSlide();
	});
	
	function selectorSlide()
	{
		$(selectorParent).stop().animate({"scrollLeft" : $(selectorParent).width()*selectorPosition}, 1000);
		return false;
	}
	/*************************/
	
});
