if (!hasEnclosure)
	$(".summary-enclosure").css('opacity',0);
if (!hasCover)
	$(".summary-cover").css('opacity',0);
if (!hasLadder)
	$(".summary-ladder").css('opacity',0);
/*if (!hasDiscount)
	$(".summary-discount").css('opacity',0);*/

updatePrice ();
updateAnimationStages();
changeAnimation();
animation = setInterval( "changeAnimation()", 3000 );
var animationOn = true;

function stopAnimation() {
	if (animationOn) {
		clearInterval(animation);
		animationOn = false;
	}
}

var isIE = jQuery.browser.msie;

function changeAnimation() {
	var $active = $('.goods-box img.active');

	if ( $active.length == 0 ) $active = $('.goods-box img:last');

	var $next =  $active.next().length ? $active.next()
		: $('.goods-box img:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});

	$active.animate({opacity: 0}, 950);
}

function updateAnimationStages(stage) {
	stopAnimation();
	var baseUrl = "/content/img/buy-stage";
	var stage1 = baseUrl + "1";
	if (hasEnclosure) {
		stage1 += "E";
	}
	if (hasLadder) {
		stage1 += "L";
	}
	stage1 += ".png";
	$(".goods-box .stage1").attr("src", stage1);
	
	var stage3 = baseUrl + "3";
	if (hasCover) {
		stage3 += "C";
	}
	stage3 += ".png";
	$(".goods-box .stage3").attr("src", stage3);
	
	switch(stage)
	{
		case 1:
			if (!$(".goods-box .stage1").hasClass("active")) {
				$(".goods-box .stage2").removeClass("active").css("opacity", 0);
				$(".goods-box .stage3").removeClass("active").css("opacity", 0);
				$(".goods-box .stage1").addClass("active").css("opacity", 1);
			}
			break;
		case 3:
			if (!$(".goods-box .stage3").hasClass("active")) {
				$(".goods-box .stage1").removeClass("active").css("opacity", 0);
				$(".goods-box .stage2").removeClass("active").css("opacity", 0);
				$(".goods-box .stage3").addClass("active").css("opacity", 1);
			}
			break;
	}
}

function toggleEnclosure () {
	if (hasEnclosure) {
		$(".summary-enclosure").fadeTo("0.8",0);
		$(".text-enclosure").removeClass("selected");
		hasEnclosure = false;
	} else {
	    if (!isIE) {
		    $(".summary-enclosure").fadeTo("0.8",1);
		} else {
		    $(".summary-enclosure").css("opacity","1"); 
		}
		$(".text-enclosure").addClass("selected");
		hasEnclosure = true;
	}
	updatePrice ();
	updateAnimationStages(1);
}

function toggleCover () {
	if (hasCover) {
		$(".summary-cover").fadeTo("0.8",0);
		$(".text-cover").removeClass("selected");
		hasCover = false;
	} else {
	    if (!isIE) {
    		$(".summary-cover").fadeTo("0.8",1);
    	} else {
	        $(".summary-cover").css("opacity","1"); 
    	}
		$(".text-cover").addClass("selected");
		hasCover = true;
	}
	updatePrice ();
	updateAnimationStages(3);
}

function toggleLadder () {
	if (hasLadder) {
		$(".summary-ladder").fadeTo("0.8",0);
		$(".text-ladder").removeClass("selected");
		hasLadder = false;
	} else {
	    if (!isIE) {
		    $(".summary-ladder").fadeTo("0.8",1);
	    } else {
	       $(".summary-ladder").css("opacity","0.8"); 
	    }
		$(".text-ladder").addClass("selected");
		hasLadder = true;
	}
	updatePrice ();
	updateAnimationStages(1);
}

function updatePrice () {
	var priceEnclosure = 43;
	var priceCover = 12;
	var priceLadder = 12;
	//var discount = -28;

	var total = 0;
	if (size == 8)
		total += 144;
	else
		total += 250;

	if (hasEnclosure)
		total += priceEnclosure;
	if (hasCover)
		total += priceCover;
	if (hasLadder)
		total += priceLadder;
	/*if (hasEnclosure && hasCover) {
		if(hasDiscount == false) {
			hasDiscount = true;
			if (!isIE) {
		    	$(".summary-discount").fadeTo("0.8",1);
	    	} else {
	       		$(".summary-discount").css("opacity","0.8"); 
	    	}
		}
		total += discount;
	}
	else {
		if(hasDiscount == true) {
			hasDiscount = false;
			$(".summary-discount").fadeTo("0.8",0);
		}
	}*/
	$(".total-price strong").html("Total &pound; " + total);
}

function submitOrder () { 
	$.post("getdeliveryprice.rails", { size: size, withLadder: hasLadder, withCover: hasCover, withEnclosure: hasEnclosure }, 
		function(orderid){
			window.location = "/order/details.rails?orderid=" + orderid
			}, "html");
}