$(document).ready(function(){
	// Homepage main banner rotator
	if(jQuery().innerfade){
		$("#hero").innerfade({speed:1500,timeout:8000,containerheight:'236px'});
	}
	
	// Product Slider
    var conveyor = $(".content-conveyor", $("#sliderContent"));
	var item = $(".item", $("#sliderContent"));
	var itemWidth = 180;
	var contentWidth = (item.length * itemWidth) - 10;
	var conveyorMax = contentWidth - parseInt($(".viewer", $("#sliderContent")).css("width"));
	conveyor.css("width", contentWidth);
	$("#sliderContent .item:last").addClass("last");
	if($("#sliderContent .item").length>4)
	{
	    $("#slider").slider({
			max: conveyorMax,
			slide: function(e, ui){ 
				conveyor.css("left", "-" + ui.value + "px");
				$('#sliderContent').css('background','transparent url("/css/img/bg/slider-range.png") no-repeat bottom center');
			}
		});
	} else {
		$("#slider").hide();
		$("#sliderContent").css("background","none");
	}

	// Add Last to fourth item in a row in a grid
	$(".grid .row .item:nth-child(4n)").addClass("last");
	
	// Sort-by functionality to drop-down change
	$('#sort').change(function(){
		var sort = $(this).val();
		var page = gup('page');
		var query = gup('q');
		if (page==false)
			page = 1;
		var url = "?sort="+sort+"&page="+page;
		if (query!=false)
			url += "&q="+query;
		window.location = url;
	});
	
	// Add a lil form bot trap and then add the ajax snazziness
	if ($('#contact-form').length>0)
	{
		$('#contact-form').append('<input type="hidden" name="secretsquirrel" value="ihatebots" />');
		$('#contact-form').FormValidate({
			phpFile: "/ajax/contact.php",
			ajax: true,
			validCheck: true
		});
	}

	// Tabs
	$("#tabs").tabs();
	
	// Product Zoom
	if(jQuery().bezoom){
		$(".zoom").bezoom();
		// If there's no ModalImage view let's hide the link
		if($("#viewModalImage").attr("href")=="")
			$("#viewModalImage").hide();
		// Set LightBox properties
		$("a[rel='lightbox']").lightBox();
	}
	
	// Product thumbnail switcheroo
	$("#product-thumbs a").click(function(e){
		e.preventDefault();
		var imagesetid = $(this).attr('rel');
		var productid = $(this).parent().parent().attr('rel');
		var zoom = '/uploads/products/'+productid+'/'+imagesetid+'_600x370.jpg';
		var modal = '/uploads/products/'+productid+'/'+imagesetid+'_large.jpg';
		var mainsrc = '/uploads/products/'+productid+'/'+imagesetid+'_330x203.jpg';
		$('#product-viewer a img').attr('src',mainsrc);
		$('#bezoom_img').attr('src',zoom);
		jQuery().bezoom.bigImg = zoom;
		$('#product-viewer a').attr('href',zoom);
		$.ajax({
			url: modal,
			success: function(xhr,msg){
				$('#viewModalImage').attr('href',modal);
				$('#viewModalImage').show();
			},
			error: function(xhr,msg){
				$('#viewModalImage').hide();
				if(xhr.status==200){ //geez IE sucks
					$('#viewModalImage').attr('href',modal);
					$('#viewModalImage').show();
				}
			}
		 });
		return false;
	});
		
	// Add/Remove default value for the search box
	$("#q").labelify();
	$("#sidebar-enews input.text,#home-enews input.text").labelify({
		text: "label"
	});
	
	// Hack - add class to WordPress pagination
	$("#blog .pagination a img.prev").parent().addClass('prev');
	$("#blog .pagination a img.next").parent().addClass('next');
	
	// Auto-redirect for "View Products by Artist"
	$('#shop-by-artist select').change(function(){
		if($(this).val()!='')
			window.location = "/artist/"+$(this).val();
	});
});


// Get URL Parameter
function gup(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return false;
	else
		return results[1];
}




