// General site javascript functions

/*
* Page onload functions
*/
$(document).ready(function() {
	// Lightbox images
	$('a.lightbox').lightBox(); // Select all links that contains lightbox in the attribute rel

	
	// Slideshow hover state
	$('a.tarr, a.barr').hover(function(){$(this).siblings().fadeTo("fast", 0.33);},function(){$(this).siblings().fadeTo("fast", 1);});
	
	// Slideshow
	// option reference - http://malsup.com/jquery/cycle/options.html
	$('#slideshow')
		.cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 5000,  // milliseconds between slide transitions (0 to disable auto advance)
			delay: -1000, // additional delay (in ms) for first transition (hint: can be negative)
			speed: 2000, // speed of transistion
			pause: 1 // hover over pause
		});
	
	// Thumbnail Slideshow
	// option reference - http://malsup.com/jquery/cycle/options.html
	$('.tmbslide')
		.cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 2000,  // milliseconds between slide transitions (0 to disable auto advance)
			delay: -1000, // additional delay (in ms) for first transition (hint: can be negative)
			speed: 1000 // speed of transistion
		});
	
	// Search field delete
	$("#search_banner form input").focus(function(){$(this).attr("value","")});
	
	// Anything with an onclick attribute changes the cursor to a pointer on hover
	$("[onclick]").css("cursor", "pointer");
	
	// Validate product qty text inputs (int only)
	$("input[name=amount]").change(function(){
		// If the chaged value is anything other then a whole number change it to zero
		var pattern = "^[1-9]{1}[0-9]*$";
		var val = $(this).val();
		
		if(!val.match(pattern)){
			$(this).val(1);
		}
	});
});
	
/*
 * Variant option swaps
 */
function vSwap(prodID, color)
{
	// Get the correct prodID if not passed
	if(!prodID && color){ prodID = $('select.'+color).val(); }
	
	// Hides
	$('.vHide').attr('style','display:none;').attr('disabled','disabled'); // hide and disable anything .vHide
	$('option').removeAttr('selected'); // unselect options
	
	// Show correct options
	$('img.'+color).removeAttr('style'); // remove display:none
	$('.'+prodID).removeAttr('style'); // remove display:none
	$('.'+color).removeAttr('style').removeAttr('disabled'); // remove disabled from size selects
	$('option.s'+color).attr('selected','selected'); // select color option
	$('option.'+prodID).attr('selected','selected'); // select size option
	
	// Update the add to cart form values
	$('input#frmProdID').attr('value', prodID.substr(1));
}
	
/*
 * Show MMY model select menu
 */
function showModelSelect(make)
{
	// Hide/Disable all other elements
	$('.makeHide').css("display", "none");
	$('.makeHide').attr("disabled", "disabled");
	
	if(make == "all"){
		// Show all elements
		$('.modelHide').css("display","block");
	}else{
		// Show elements based on the make passed
		$('select.'+make).css("display","inline").removeAttr("disabled");
		$('input.makeHide').css("display","inline").removeAttr("disabled");
	}
}

/*
 * Submit MMY make and model form
 */
function getMmyProducts(loc, cat, smake, smodel)
{
	if(smake == "all" || smodel == "all")
	{
		window.location = loc+'home.php?cat='+cat;
	}else{
		//Redirect back to the same page but pass the mmy param
		window.location.href = loc+'home.php?cat='+cat+'&smake='+smake+'&smodel='+smodel;
	}
}
