/* 
	All Source Code is Copyright (2008) ShopDev: http://www.shopdev.co.uk
	All Rights Reserved.
	You are prohibited from copying or making derivitive works from any code herein without prior written permission of ShopDev.
	This file is intened for use with the Neutrino CubeCart Skin (for CubeCart V4).
	
	File Version: 1.0a
	Released: 6th August 2008
	
*/

// START: Shake Plugin
	jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
		this.each(function() {
			$(this).css({position:'relative'});
			for (var x=1; x<=intShakes; x++) {
				$(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
				.animate({left:intDistance}, ((intDuration/intShakes)/2))
				.animate({left:0}, (((intDuration/intShakes)/4)));
			}
		});
		return this;
	};
// END: Shake Plugin

// START: Global Variables - change these if you wish
	var noAJAX = false;			// Disable the AJAX Add To Cart feature.
	var disableSB = false;		// Disable the ability for users to expand/collapse the side-boxes.
	var sbAnmSpd = "fast";		// Sidebox animation speed (options: "fast", "normal", "slow").
	var cookieExp = 365;		// Days until cookies expire - from time of creation.
	var cookiePrefix = "sd_";	// Prefix to cookie name.
	var cookiePath = "/";		// Cookie path - you probably won't want to change this.
	var hideStk = false;		// Always hide stock information from visitors.
	var DefGridView = false;	// Display products in grid view by default.
	var timeOutVal = 6000;		// Number of milliseconds to wait for an AJAX response before retrying
// END: Global Variables - don't change anything below this line

// START: Insert Stylesheet - Copyright ShopDev.co.uk 
	function insertStyleSheet(path) {
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = path;
		cssNode.media = 'screen';
		cssNode.title = 'dynamicLoadedSheet';
		document.getElementsByTagName("head")[0].appendChild(cssNode);
	}
// END: Insert Stylesheet - Copyright ShopDev.co.uk 

// START: Set Page Height - Copyright ShopDev.co.uk 
	function setPageHeight() {
		var getWindowHeight = $(window).height() + 'px';
		var getFooterHeight = $(".footer").height() + 'px';
		$("#pageSurround").css("min-height", getWindowHeight);
		$(".spacer").css("height", getFooterHeight);
	}
// END: Set Page Height - Copyright ShopDev.co.uk 

// START: Hide/Show Fields - Copyright ShopDev.co.uk 
	function hideDropDownBox() {
		$(".optField").css("visibility", "hidden");
	}
	function showDropDownBox(fade) {
		$(".optField").css("visibility", "visible");
	}
// END: Hide/Show Fields - Copyright ShopDev.co.uk 

// START: Collapsible Side Boxes - Copyright ShopDev.co.uk 
	// 'var q' takes the sidebox title image
	function slideBox(q) {
		if (q.attr("class")!= "noCollapse") {
			if(q.attr("class")=="collapsed") {
				q.parents(".sidebox").children(".boxContentLeft").slideDown(sbAnmSpd);
				q.removeClass("collapsed");
				q.parents(".boxTitleLeft").children(".expand").fadeOut(sbAnmSpd);
				var boxID = q.parents(".sidebox").attr("id");
				$.cookie(cookiePrefix + boxID, 1, { expires: cookieExp, path:cookiePath});
			} else {
				q.parents(".sidebox").children(".boxContentLeft").slideUp(sbAnmSpd);
				q.addClass("collapsed");
				q.parents(".boxTitleLeft").children(".expand").fadeIn(sbAnmSpd);
				var boxID = q.parents(".sidebox").attr("id");
				$.cookie(cookiePrefix + boxID, 0, { expires: cookieExp, path:cookiePath});
			}
		}
	}
// END: Collapsible Side Boxes - Copyright ShopDev.co.uk 

// START: document.ready
	$(function() {
		// START: Collapsible Side Boxes - Copyright ShopDev.co.uk 
			if (disableSB != true) {
				// change the cursor
				$("div.boxTitleLeft img").each(function(){
					if ($(this).attr("class") != "noCollapse") {
						$(this).css("cursor","pointer");
					}
				});
				// executed when DOM is ready
				$("div.sidebox").each(function(i) {
					// start - default status
					if ($(this).children(".boxTitleLeft").children("img").attr("class") == "collapsed") {
						$(this).children(".boxContentLeft").css("display","none");
						$(this).children(".boxTitleLeft").children(".expand").css("display","block");
					}
					// end - default status
					// start - user overwrite
					if(typeof(isCart) == "undefined") {
						// for store-front pages
						$(this).attr("id","box-" + i);
					} else {
						// for cart pages
						$(this).attr("id","cartbox-" + i);
					}
					var cookieName = cookiePrefix + $(this).attr("id");
					if ($.cookie(cookieName) == 0) {
						// collapse the sidebox
						$(this).children(".boxContentLeft").css("display","none");
						$(this).children("div.boxTitleLeft").children("img").addClass("collapsed");
						$(this).children("div.boxTitleLeft").children("div.expand").fadeIn(sbAnmSpd);
					} else if ($.cookie(cookieName) == 1){
						// expand the sidebox
						$(this).children(".boxContentLeft").css("display","block");
						$(this).children("div.boxTitleLeft").children("img").removeClass("collapsed");
						$(this).children("div.boxTitleLeft").children("div.expand").fadeOut(sbAnmSpd);
					}
					// end - user overwrite
				});
				// slide the sideboxes
				$(".boxTitleLeft").click(function() {
					slideBox($(this).children("img"));
				});
			}
		// END: Collapsible Side Boxes - Copyright ShopDev.co.uk 
		
		// START: Display Stock Info - Copyright ShopDev.co.uk 
		if ( $("div.stockLevel").length > 0 ) {
			if(($("div.stockLevel").html().toLowerCase() != '<span class="txtoutofstock"></span>') && (hideStk != true) && ($("div.stockLevel").html().toLowerCase() != '<span class=txtoutofstock></span>')) {
				$("div.stockLevel").show();
			}
		}
		// END: Display Stock Info - Copyright ShopDev.co.uk 
		
		// START: IE6 Add To Cart Buttons - Copyright ShopDev.co.uk 
			$(".addToCart").css("cursor","pointer");
			$(".addToCartWhiteBG").css("cursor","pointer");
		// END: IE6 Add To Cart Buttons - Copyright ShopDev.co.uk 
		
		// START: AJAX Add To Cart - Copyright ShopDev.co.uk 
			$("#ajaxCart").load(rootRel + 'ajaxCart.php' + '?nocache=' + Math.random());
		// END: AJAX Add To Cart - Copyright ShopDev.co.uk 
		
		// START: viewProd Tabs
			var tabContainers = $('div.tabs > div');
			$('div.tabs ul.tabMenu a').click(function () {
				tabContainers.hide().filter(this.hash).show();
				$('div.tabs ul.tabMenu a').removeClass('selected');
				$(this).addClass('selected');
				
				return false;
			}).filter(':first').click();
		// END: viewProd Tabs
		
		// START: Social Bookmark Widget - for IE6 - Copyright ShopDev.co.uk 
			$("a.snap_noshots").hover(
				function() {
					hideDropDownBox();
				}, function() {
					showDropDownBox();
				}
			);
			$("#addthis_dropdown").hover(
				function() {
					hideDropDownBox();
				}, function() {
					showDropDownBox();
				}
			);	
		// END: Social Bookmark Widget - for IE6 - Copyright ShopDev.co.uk 
		
		// START: Row/Grid View - Copyright ShopDev.co.uk 
			$('.changeView').click(function() {
				var getView = this.getAttribute("rel");
				$.cookie(cookiePrefix + "viewType", getView, {expires: cookieExp, path:cookiePath});
				location.reload(true);
			});
			// We don't want to be doing this in the cart pages
			if(typeof(isCart) == "undefined") {
				if ($.cookie(cookiePrefix + "viewType") == 'gridView') {
					insertStyleSheet(gridViewPath);
				} else {
					insertStyleSheet(rowViewPath);
				}
				if (!$.cookie(cookiePrefix + "viewType")) {
					if (DefGridView == true) {
						insertStyleSheet(gridViewPath);
					}
				}
			}
		// END: Row/Grid View - Copyright ShopDev.co.uk 
		
		// START: Set Page Height - Copyright ShopDev.co.uk 
			setPageHeight(Math.random());
		// END: Set Page Height - Copyright ShopDev.co.uk 
	});
// END: document.ready
	
// START: AJAX Add To Cart - Copyright ShopDev.co.uk 
	var options = { 
		beforeSubmit: showRequest,
		success: showResponse,
		timeout: timeOutVal,
		error: timeoutError
	}; 
	
	function doAddToCart(productID,manual) { 
		if (manual == true) {
			// used for debugging mostly - no ajax
			$('#myForm'+productID).submit();
		}
		if (noAJAX == true) {
			// just submit the form
			$('#myForm'+productID).submit();
		} else {
			// use ajax. woohoo!
			errorProdID = productID;
			$('#myForm'+productID).ajaxSubmit(options); 
			return false;
		}
	}; 
	
	function showRequest(formData, jqForm, options) { 
		var queryString = $.param(formData); 
		var formElement1 = jqForm[0]; 
		var prodID1 = formElement1[0].value;
		var vShift = formElement1[3].value;
		$('#AJAX-confirm-'+prodID1).hide()
		$('#AJAX-process-'+prodID1).show()
		// shift the background of the button div
		$('#buy-'+prodID1).css("background-position","0% " + ("-" + (vShift*2 + "px")));
		return true; 
	} 
					  
	function showResponse(formData, jqForm, $form, options) {
		var formElement2 = $form[0];
		// Get the product ID
		var prodID2 = formElement2[0].value;
		// This field will have a value of 1 for viewCat pages
		var opt = formElement2[2].value;
		// The height of the button
		var vShift = formElement2[3].value;
		$.ajax({
		   url: rootRel + 'chkOpt.php',   //This is the location of the script that will check if the product has options
		   data: 'prodID=' + prodID2,
		   success: function(checkedOpt) {  //This callback will handle the results of the check
			// Has prod options & added from viewCat page:
			  if(checkedOpt == 1 && opt == 1) {
				$("#AJAX-process-" + prodID2).remove();
				$("#AJAX-opt-" + prodID2).show();
				// shift the background of the button div
				$("#buy-" + prodID2).css("background-position", "0% 0px");
				$("#AJAX-options-" + prodID2).show();
				// path to ajaxCart.php (rootRel prefix for CC4 SEO URLs)
				var path = rootRel + "ajaxCart.php" + "?nocache=" + Math.random();
				$("#ajaxCart").load(path);
			  }
			// No prod options or added from viewProd page:
			  else {
				$("#AJAX-process-" + prodID2).hide();
				$("#AJAX-confirm-" + prodID2).show();
				// shift the background of the button div
				$("#buy-" + prodID2).css("background-position","0% " + ("-" + (vShift*3 + "px")));
				// path to ajaxCart.php (rootRel prefix for CC4 SEO URLs)
				var path = rootRel + "ajaxCart.php" + "?nocache=" + Math.random();
				$("#ajaxCart").load(path);
				// shake the cart icon
				$("#cartIcon").shake(3, 5, 600);
			  }
		   }
		});
	}
	
	function timeoutError(options) {
		// retry adding the item
		doAddToCart(errorProdID,false);
	}
// END: AJAX Add To Cart - Copyright ShopDev.co.uk

// START: Stars~Hover - Copyright ShopDev.co.uk
	function starsHover(rating, pathImg) {
		/* Positive Stars */
		for (var i = 0; i <= rating; i++) {
			if (i>0) findObj("star"+i).src = pathImg+'1.gif';
		}
		/* Negative Stars */
		for (var i = rating+1; i <= 5; i++) {
			findObj("star"+i).src = pathImg+'0.gif';
		}
	}
// END: Stars~Hover - Copyright ShopDev.co.uk

// START: Stars~Mouse Out - Copyright ShopDev.co.uk
	function starsOut(pathImg) {
		var val = $("#rating_val").val();
		if (val == "") { val = 0; }
		/* Show selected stars as per rating */
		for (var i = 0; i <= val; i++) {
			if (i>0) {
				$("#star"+i).attr({src: pathImg+"1.gif"});
			}
		}
		/* Hide stars higher than selected rating */
		for (var i = 0; (i <= 5); i++) {
			if (i>val) {
				$("#star"+i).attr({src: pathImg+"0.gif"});
			}
		}
	}
// END: Stars~Mouse Out - Copyright ShopDev.co.uk