$(document).ready(function(){   
	
	$(document).pngFix();
	
	$('#imagechanger').innerfade({
		speed: 4000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '339px'
	});
	
	$(".fancybox").fancybox({
		'titleShow': false
	});
	
	$(".fancyframe").fancybox({
		'width': 540,
		'height': 500
	});	
	
	$(".fancyframesmall").fancybox({
		'width': 540,
		'height': 220
	});
					
	$("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;
			}
		}
	});
	
	$(".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);
		}
	});
}
