$(document).ready(function(){   
	
	$(document).pngFix();
	
	$('#imagechanger').innerfade({
		speed: 2600,
		timeout: 5000,
		type: 'sequence',
		containerheight: '351px'
	});
	
	$(".fancybox").fancybox({
		'titleShow': true
	});
	
	$(".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);
			}
		});
	});
	
	$('#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()); 	
		}
	});
	
	$(".foldouttoggle").click(function(){
	   $(this).parent().children(".foldoutbox").slideToggle("fast");
	   $(this).toggleClass("foldoutopen");
	});
			
	$("#twitter_update_list li:odd").addClass("odd");
	
	$(".addtobasket").submit(function(){
		
		var addtobasket = $(this);
		
	   if ($('#variant').attr("selectedIndex") == 0){
		   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('This product has been added to your shopping basket.<br/>You have '+data.itemcount+'.',{ 
					buttons:{"checkout now":true, "continue shopping":false},
					overlayspeed: "fast",
					top: "35%",
					callback: function(v){
						if (v) { location.href="/shopping-basket"; }
					}
				});

			}
		});

	});
									  
});

$(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(int,prodid) {
	var url="/includes/getvariant.php"
	url=url+"?n="+escape(int)
	url=url+"&p="+escape(prodid)
	url=url+"&sid="+Math.random()
	$.ajax({
		url: url,
		success: function(data) {
			$('#variant2').html(data);
		}
	});
}

// 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");
		}
	});
}
