$(document).ready(function(){   

	$('#imagechanger').innerfade({
		speed: 3000,
		timeout: 6000,
		type: 'sequence',
		containerheight: '326px'
	});
	
	$(".fancybox").fancybox({
		'titleShow': false
	});
	
	$(".fancyframe").fancybox({
		'width': 540,
		'height': 500
	});	
	
	$(".fancyframesmall").fancybox({
		'width': 540,
		'height': 220
	});
	
				
	$('#keyword').liveSearch({url: '/ajax/search.php?iconography&q=', typeDelay: 500});
	
	var t;
	
	$('#filterform input').click(function(){  
		 var checkedCount = 0;
		 if (this.checked) {
			 checkedCount++;
		 }
		 $(this).parent().siblings().children().each(function() {
		   if (this.checked) {
			 checkedCount++;
		   }
		 }); 
		$(this).parents(".foldout").children("span").html('('+checkedCount+')');
	 	$.post('/ajax/presearch.php', $("#filterform").serialize(), function(data) {
			$('#presearch span').html(data);
			clearTimeout(t);
			if (data != "0 products") {
				t = setTimeout('document.forms["submitfilterform"].submit()',1500);
			}
		});
	});
		
	$('#quicksearchbox1, #quicksearchbox2').change(function(){  
	 	$.post('/ajax/presearch.php', $("#quicksearch form").serialize());
	});
	
	$("#quicksearchcategory").change(function(){
		$.getJSON('/ajax/quicksearch.php', 'cat='+$(this).attr("value"), function(data) {
			$("#quicksearchbox1").html(data.box1);
			$("#quicksearchbox2").html(data.box2);
		});
	});
	
	$('#filtersubmit').click(function(){   
		if ($('#presearch span').html() == '0 products') {
			alert("0 products matched, please modify your filters.");
			return false;
		}
	});
	
	$("form").submit(function () {
		error = false;
		i = 0;
		$('.required', this).each(function() {
			if ($(this).val() == '') {
				$(this).addClass("requirederror");
				error = true;
			} else {
				$(this).removeClass("requirederror");
			}
			i++;
		});
		if (i > 0) {
			if (error == true) {
				return false;
			} else {
				return true;
			}
		}
	});
	
	$("#brandjump").change(function(){
		if ($(this).val() != '') {						
			window.location.assign($(this).val()); 	
		}
	});
	
	// Logo parade event handlers
	$("#brandscroller").smoothDivScroll({ 
		autoScroll: "onstart" , 
		autoScrollDirection: "backandforth", 
		autoScrollStep: 1, 
		autoScrollInterval: 25,	
		visibleHotSpots: "always"
	});
	$("#brandscroller").bind("mouseover", function() {
		$(this).smoothDivScroll("stopAutoScroll");
	}).bind("mouseout", function() {
		$(this).smoothDivScroll("startAutoScroll");
	});
	
	$(".foldouttoggle").click(function(){
	   $(this).parent().children(".foldoutbox").slideToggle("fast");
	   $(this).toggleClass("foldoutopen");
	});

	/* header drop navigation */
	$(".tabs ul li").hoverIntent(
	  function () {
		$(this).children("ul").slideDown("slow");
	  },
	  function () {
		$(this).children("ul").slideUp("fast");
	  }
	);
	
	/* side bar navigation */
	$(".nav ul li").hoverIntent(
	  function () {
		$(this).children("ul").fadeIn("slow");
	  },
	  function () {
	  }
	);
	$(".nav").hover(
	  function () {
	  },
	  function () {
		$(".nav ul li ul").fadeOut("fast");
	  }
	);
		
	$("#twitter_update_list li:odd").addClass("odd");
	
	$(".addtobasket").submit(function(){
		
		var addtobasket = $(this);
		
	   if ($('#variant').attr('value') == ''){
		   alert('Please select a product option'); 
		   return false;
	   }
	   
		$.getJSON('/ajax/addtobasket.php', $(this).serialize(), function(data) {
			if (data.errmsg) {
				alert(data.errmsg);
			} else {
				$("#basketsummary").animate({opacity: '0'}, 200, function() {
					$("#basketsummary").html(data.basketsummary);
					$("#basketsummary").animate({opacity: '1'}, 200);
			    });
				addtobasket.find(".itemcount").html(data.itemcount);
				addtobasket.addClass("inbasket");
				
				$.prompt('<img src="/images/point.jpg" class="checkpoint" /> This product has been added to your shopping basket.<br/>You have '+data.itemcount+'.',{ 
					buttons:{"checkout now":true, "continue shopping":false},
					overlayspeed: "fast",
					top: "16%",
					callback: function(v){
						if (v) { location.href="/shopping-basket"; }
					}
				});

			}
		});

	});
	
	$(".qtyup").click(function(){
	   $("#"+$(this).attr("rel")).val(parseInt($("#"+$(this).attr("rel")).val()) + 1);
	});
	
	$(".qtydown").click(function(){
	   $("#"+$(this).attr("rel")).val(parseInt($("#"+$(this).attr("rel")).val()) - 1);
	   if (parseInt($("#"+$(this).attr("rel")).val()) < 0) {
		   $("#"+$(this).attr("rel")).val(0);
	   }
	});
	
    jQuery('#homebrandbox').jcarousel({
		auto: 0.001,
		scroll: 1,
		easing: 'linear',
		animation: 1400,
        wrap: 'circular',
		initCallback: mycarousel_initCallback
    });
								  
});

function mycarousel_initCallback(carousel){
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


$(function () {
	var tabContainers = $('div.prodtabs > div');
	$('div.prodtabs ul.tabNav a').click(function () {
		tabContainers.hide().filter(this.hash).show();
		$('div.prodtabs ul.tabNav a').removeClass('selected');
		$('div.prodtabs ul.tabNav li').removeClass('current');
		$(this).addClass('selected');
		$(this).parent().addClass('current');
		return false;
}).filter(':first').click();});	

// used by checkout
function deleteitem(code) {
	$('#delete').val(code);
	if (confirm('Are you sure? Click OK to confirm.')) {
		$('#basket').submit();
	} else {
		return false;
	}
}

// used by get variant
function getVariant() {
	var q = $('#quantity').val();
	var p = $('#prodid').val();
	var v1 = $("input:radio[name=variant1]:checked").val();
	if (!v1) { v1 = $("input:hidden[name=variant1]").val(); }
	var v2 = $("input:radio[name=variant2]:checked").val();
	if (!v2) { v2 = $("input:hidden[name=variant2]").val(); }
	$.getJSON('/ajax/getvariant.php', 'p='+p+'&v1='+encodeURIComponent(v1)+'&v2='+encodeURIComponent(v2)+'&q='+q, function(data) {
		if (data.variant1 != "") {
			$("#variant1").html(data.variant1);
		}
		if (data.variant2 != "") {
			$("#variant2").html(data.variant2);
		}
		$("#variant").attr('value', data.variant);
		$("#varianttext").html(data.varianttext);
	});	
}

// add to wishlist
function addToWishList(id) {
	var url="/ajax/addtowishlist.php"
	url=url+"?id="+escape(id)
	$.ajax({
		url: url,
		success: function(data) {
			alert("Item added to your wish list");
		}
	});
}
