$(document).ready(function(){
	var activePreview = "previewBtn0";
	$(".il_image_wrapper").each(function(){
		$(this).mouseover(function(){
			$(this).css("cursor","pointer");
			$(this).css("background-color","#F0F0F0");
		});
		$(this).mouseout(function(){
			$(this).css("background-color","transparent");
		});
		$(this).click(function(){
			window.location = $(this).find("a").attr("href");
		});
	});
	$(".input_amount").focus(function(){
		this.select();
	});
	$(".input_amount").keyup(function(){
		var html = $(this).parents("tr").find(".rule_piece").html();
		var pricePattern = /[0-9]+\.[0-9]{2}/;
		var val = parseInt(this.value);
		if(val != NaN && val > 0){
			var price = pricePattern.exec(html) * val * 100;
			
			var pricePattern = /([0-9]+)([0-9]{2})$/;
			var prices = pricePattern.exec(price);
		}
		if(prices == null) { 
			prices = Array('', '0', '00');
		}
		$(this).parents("tr").find(".rule_total").html(+prices[1]+'.'+prices[2]+'&nbsp;EUR');
	});
	$(".previewBtn").each(function(){
		$(this).mouseover(function(){
			$(this).css("cursor","pointer");
			$(this).css("background-color","#EDEDED");
			$(this).css("border-color","#999999");
		});
		$(this).mouseout(function(){
			if(activePreview != this.id){
				$(this).css("background-color","transparent");
				$(this).css("border-color","#CCCCCC");
			}
		});
		$(this).click(function(){
			if(activePreview != this.id){
				$("#"+activePreview).css("background-color","transparent");
				$("#"+activePreview).css("border-color","#CCCCCC");
				activePreview = this.id;
				$("#previewImage").attr("src",$(this).find("a").attr("href"));
			}
			return false;
		});
		if(this.id=="previewBtn0"){
			$(this).css("background-color","#EDEDED");
			$(this).css("border-color","#999999");
		}
	});
	$(".popup").click(function(){
		window.open(this.href,'popup','width=800,height=700,location=no,status=no,menubar=no,toolbar=no,scrollbars=no');
        return false;
	});
	$(".zoomImage").click(function(){
		window.open(this.href,'popup','width=1064,height=1064,location=no,status=no,menubar=no,toolbar=no,scrollbars=no');
        return false;
	});
	// MSIE Button workaround
	if(jQuery.browser.msie){
		$("#form-step3 button").click(function(){
			var value = this.value.match("Opslaan") != null ? "next" : "";
			$("#form-step3").append('<input type="hidden" name="update" value="'+value+'"/>');
		});
	}
	
	addChangeEvent("#il_category, #il_color, #il_location, #il_interval");
});


function updateCounter(){
	var total = $('#slides').children().size();
	var next = parseInt($("#slideshow #captionWrapper #counter span").html()) +1;
	
	next = next > total ? 1 : next;
	
	$("#slideshow #captionWrapper #counter span").html(next);
}

$('#slideshow IMG:last').ready(function(){
	$('#slides').cycle({fx:'fade',before:updateCounter});
});

function updateShippingCost(elm, cost) {
	if(elm.checked){
		cost = cost == '' ? 0 : cost; 
		var total = $("#totalCost").attr("value");
		total = parseFloat(total) + parseFloat(cost);
		$('#shippingCost').html(printPrice(cost));
		$("#totalCostEUR").html(printPrice(total));
	}
}

function printPrice(cost){
	var part1 = Math.floor(cost);
	var part2 = Math.floor((cost-part1) * 100);
	part2 = String(part2).length == 1 ? part2+'0' : part2;
	return part1+'.'+part2+'&nbsp;EUR';
}

function addChangeEvent(elements){
	$(elements).change(function(event){
		// Store original request URI
		var xajaxRequestUri_original = xajaxRequestUri;
		// Append request URI with params
		xajaxRequestUri += "?il_cat="+$("#il_category").val()
						+"&il_col="+$("#il_color").val()
						+"&il_loc="+$("#il_location").val()
						+"&il_interval="+$("#il_interval").val();
		
		xajax_updateCategorySelect();
		xajax_updateColorSelect();
		xajax_updateLocationSelect();
		xajax_updateIntervalSelect();
		
		// Reset request URI
		xajaxRequestUri = xajaxRequestUri_original;
	});
}