function equalHeight(elements, next_element_selector) {
	var next_element_height = 0;
	var element_height = 0;
	var maxHeight = 0;
	var property = jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';

	$(elements).each(function() {
		element = $(this);
		element_height = element.height();

		if (next_element_selector === undefined) {
			//Normal calculation. Compares all elements of the same selector
			//Just calculate, don't set the new height since we have to do it after we know the tallest.
			maxHeight = (element_height > maxHeight) ? element_height : maxHeight;
		}
		else {
			//Compares all selected elements with ONLY the next element (2nd parameter)
			next_element = element.next(next_element_selector);
			next_element_height = next_element.height();
			maxHeight = (element_height > next_element_height) ? element_height : next_element_height;

			//Set the elements' height
			next_element.css(property, maxHeight + 'px');
			element.css(property, maxHeight + 'px');
		}
	});

	//Set all elements to the same height (maxHeight)
	if (next_element_selector === undefined) {
		$(elements).each(function() {
			$(this).css(property, maxHeight + 'px');
		});
	}
}

function groupElementsAndCall(parent, elements, group_by, func_to_call) {

	$(parent).each(function() {

		var childElements = $(this).find(elements);
		var groups = Math.ceil(childElements.length / group_by);

		//Weird math, but it's correct :)
		var sliceStart = -group_by, slideEnd = 0;

		for (var i = 0; i < groups; i++) {

			//"Calculate" where to slice
			sliceStart += group_by;
			slideEnd += group_by;

			//This slice contains the grouped elements!
			var slice = childElements.slice(sliceStart, slideEnd);

			//Do what you need
			func_to_call(slice);

		} //End for

	}); //End parent grouping

}

var kan = kan ? kan : new Object();
kan.watermark = kan.watermark ? kan.watermark : new Object();

kan.watermark = {

	init: function() {
		$("input[title], textarea").each(function (i) {
			var elmInput = $(this)[0];
			if(!$(elmInput).hasClass("watermark"))
			{
				if (elmInput.title.length > 0 && (elmInput.value.length == 0 || elmInput.value == elmInput.title))
				{		
					$(elmInput).addClass("watermark");
					elmInput.value = elmInput.title;
				}
				
				elmInput.autocomplete = "off";
				$(elmInput).change(kan.watermark.refresh)
				$(elmInput).focus(kan.watermark.focus)
				$(elmInput).blur(kan.watermark.refresh)
			}
		});
	},
	
	refresh: function() {
		elmInput = $(this)[0];
		if(elmInput.value.length == 0)
		{		
			$(elmInput).addClass("watermark");
			elmInput.value = elmInput.title;
		}
	},
	
	focus: function(elm) {
		if(elm.nodeName=="INPUT")
			elmInput = elm;
		else
			elmInput = $(this)[0];
		if(elmInput.value == elmInput.title)
		{
			elmInput.value = "";
			$(elmInput).removeClass("watermark");
		}
	}
};


$(document).ready(function() {
	$(".item > a").parent().mouseover(function() {
		$(this).addClass("hover");
	}).mouseout(function() {
		$(this).removeClass("hover");
	}).click(function() {
		window.location = $(this).find("a").attr("href");
	});

	groupElementsAndCall("div.columns", "div.column", 3, equalHeight);
	equalHeight("#contact-boxes div.box");
	groupElementsAndCall("div.catalogue-group", "div.catalogue h3", 4, equalHeight);

	kan.watermark.init();

	if ($("#style-finder-results").length > 0) {
		ResultsFadeInEffect();
	}

	//Product page thumbnail previewer
	if ($(".product-thumbnails").length > 0) {
		$(".product-thumbnails a").click(function() {
			$(".product-thumbnails li.selected").removeClass("selected");
			var highResSrc = $(this).attr("rel");
			$("#product-image").attr("src", highResSrc);
			$(this).parent().addClass("selected");
		});
	}

	//UK Online - add order row
	if ($("a#add_order_row").length > 0) {
		$("input#order_rows").val($("#order table tr > td.name").length);
		$("a#add_order_row").click(function(event) {
			event.stopPropagation();

			//Get number of rows, and set the new value
			var row_number = $("input#order_rows").val();
			var new_index = parseInt(row_number, 10) + 1;
			$("input#order_rows").val(new_index);

			//Clone the last row and replace indicies
			var lastTR = $("#order table tr:last").clone();
			var newTR = lastTR.html().replace(/_\d+/g, "_" + new_index);

			//Add new html
			$("#order table").append("<tr>" + newTR + "</tr>");

			return false;
		});
	}

});

function ResultsFadeInEffect() {
	//Highlight result
	$("#result-text").css({ backgroundColor: '#fff' }).animate({ backgroundColor: '#ffff99' }, 500, function() {
		//Success
		$(this).animate({ backgroundColor: '#fff' }, { duration: 500 });
	});
	
	//Hide all
	$("#style-finder-results div.result img").hide();

	//Bubble up all images
	var elementIndex = 0;
	$("#style-finder-results div.result img").each(function() {
		var e = $(this);
		e.animate({ opacity: 0 }, { duration: elementIndex * 25 });
		e.show().hide();
		e.animate({
			width: 'toggle',
			height: 'toggle',
			opacity: 1.0
		}, {
			duration: 500
		});

		elementIndex++;
	});

}

function ClientValidateCheckBox(source, arguments)
{
	arguments.IsValid =  $(".MustBeChecked input:not(:checked)").length == 0;	
}