/*
 * All scripting for product pages
 * Written by Steve Ariss
 * Elevations Marketing
 *
 */
	
$(function() {

/*********** GLOBAL VARIABLES **************/
	var detailOpen = false;
	
	//console.log("Product Detail Showing?: " + detailOpen);

/************* PRODUCT SLIDER **************/
	//vars
	var conveyor = $(".content-conveyor", $("#sliderContent")),
	item = $(".item", $("#sliderContent"));
	
	var viewerW = parseInt($(".viewer", $("#sliderContent")).css("width"));
	var maxScroll = (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $("#sliderContent")).css("width"));
	
	var mouseState = false;
	
	//set length of conveyor
	conveyor.css("width", item.length * parseInt(item.css("width")));

	//config
	var sliderOpts = {
		max: maxScroll,
		slide: function(event, ui) {
			if (mouseState) {
				conveyor.css("left", "-" + ui.value + "px");
			} else {
				conveyor.filter(':not(:animated)').animate({
	 				"left": "-" + ui.value + "px" 
				}, 200, "easeOutSine");
			}
		},
		stop: function (event, ui) {
			mouseState = false;
		}			
	};
	
	//create slider
	$(".slider").slider(sliderOpts);

	//figure out what the mouse is doing
	$(".ui-slider-handle").mousedown(function () {
		//console.log("The Slider Handle is being pushed!");
		mouseState = true;
		conveyor.stop();
	});
	$(".ui-slider-handle").mouseup(function () {
		//console.log("The Slider Handle is NOT being pushed!");
		mouseState = false;
	});
	
	//home and end buttons
	$(".start").click(function(){
		value = 0;

		$(".slider").slider('option', 'value', value);
		conveyor.filter(':not(:animated)').animate({
	 		"left": "0px"
		}, 500, "easeOutSine");
	
	});
	
	$(".end").click(function(){
		value = maxScroll;

		$(".slider").slider('option', 'value', value);
		conveyor.filter(':not(:animated)').animate({
	 		"left": "-" + value + "px"
		}, 500, "easeOutSine");
	
	});
	
	//left and right buttons
	$(".btn-left").click(function(){
		var value = $(".slider").slider('option', 'value');
		value -= viewerW
		
		//don't scroll any further than the beginning
		if (value <= 0) {
			value = 0;
		};
		
		$(".slider").slider('option', 'value', value);
		conveyor.filter(':not(:animated)').animate({
	 		"left": "-" + value + "px"
		}, 500, "easeOutSine");
	
	});
	
	$(".btn-right").click(function(){
		var value = $(".slider").slider('option', 'value');
		value += viewerW
		
		//don't scroll any further than the end
		if (value >= maxScroll) {
			value = maxScroll;
		};
		
		$(".slider").slider('option', 'value', value);
		conveyor.filter(':not(:animated)').animate({
	 		"left": "-" + value + "px"
		}, 500, "easeOutSine");
	});
	

/********** PRODUCT DETAIL REVEAL ***********/	
	function showDetail (page) {
	
		if (!detailOpen) {
			$(".productDetail").load(page, function (){
				$(this).show('blind', { direction: "vertical" });
				detailOpen = true;
			});
		}
		else {
			$(".productDetail").fadeTo(300, 0, function () {
				$(this).load(page, function (){
					detailOpen = true;
					$(this).fadeTo(300, 1);
				});
			});
		};
	};
 /* Button Event */
	$.address.change(function(event) {
		// do something depending on the event.value property, e.g.
		if (event.path == "/") {
			detailOpen = false;
			$(".productDetail").hide();
		} else {
			//console.log("Load up the proper page!");
			showDetail ('products'+ event.value + '.php');
		}
		
	});
	
});
