$(document).ready(function () {

	//Site variables
	var creditUnitPrice = 2; //Each credit costs £2


	//Modal lightbox
	Shadowbox.init();


	//Flyout navigation
	jQuery("#product-nav").superfish();

	//Rollover images
	$(".rollover").hover(
			function () {
				if ($(this).attr("src").indexOf("_over") == -1) {
					var newSrc = $(this).attr("src").replace(".", "_over.");
					$(this).attr("src", newSrc);
				}
			},
			function () {
				if ($(this).attr("src").indexOf("_over.") != -1) {
					var oldSrc = $(this).attr("src").replace("_over.", ".");
					$(this).attr("src", oldSrc);
				}
			}
		);

	//Open designated links in new window (done in JS to maintain valid XHTML strict)
	$(".new").attr("target", "_blank");

	//Validate form input
	$(".validate").validate();

	$("#credits").keyup(function () {
		var credits = $("#credits").attr("value");
		var blnIsNumeric = IsNumeric(credits);
		if (blnIsNumeric == true) {
			var totalCreditCost = credits * creditUnitPrice;
			$("#message").html("<strong>" + credits + " credits = &pound;" + totalCreditCost + ".00</strong>");
		} else {
			$("#message").html("Please enter a valid number of credits");
		}
	});

	//Site search box
	$("#search").css("color", "#999");
	$("#search").attr("value", "Site search");
	$("#search").focus(function () {
		$(this).css("color", "#fff");
		$(this).attr("value", "");
	});
	$("#search").blur(function () {
		$(this).css("color", "#999");
		var searchValue = $(this).attr("value");
		if (searchValue == "") {
			$(this).attr("value", "Site search");
		}
	});



	//Promo code box
	$('#promocode').focus(function () {
		var promoCodeValue = $(this).attr('value');
		if (promoCodeValue = 'Input promo code') {
			$(this).attr('value','');
		}
	});
	$('#promocode').blur(function () {
		var promoCodeValue = $(this).attr('value');
		if (promoCodeValue == '') {
			$(this).attr('value','Input promo code');
		}
	});



	//Site search box
	$("#duijud-duijud").css("color", "#999");
	$("#duijud-duijud").attr("value", "Email sign-up");
	$("#duijud-duijud").focus(function () {
		$(this).css("color", "#fff");
		$(this).attr("value", "");
	});
	$("#duijud-duijud").blur(function () {
		$(this).css("color", "#999");
		var searchValue = $(this).attr("value");
		if (searchValue == "") {
			$(this).attr("value", "Email sign-up");
		}
	});


	//$("#search").blur("value", "").css("color", "#fff");


	//Tabs
	$(".tabs .editors-description a").attr("class", "selected");
	$("div.editors-size, div.editors-care").css("display", "none");

	$(".tabs .editors-description a").click(function () {
		$(".tabs .editors-description a").attr("class", "selected");
		$("div.editors-description").css("display", "block");

		$(".tabs .editors-size a").attr("class", "");
		$("div.editors-size").css("display", "none");

		$(".tabs .editors-care a").attr("class", "");
		$("div.editors-care").css("display", "none");

		return false;
	});

	$(".tabs .editors-size a").click(function () {
		$(".tabs .editors-size a").attr("class", "selected");
		$("div.editors-size").css("display", "block");

		$(".tabs .editors-description a").attr("class", "");
		$("div.editors-description").css("display", "none");

		$(".tabs .editors-care a").attr("class", "");
		$("div.editors-care").css("display", "none");

		return false;
	});

	$(".tabs .editors-care a").click(function () {
		$(".tabs .editors-care a").attr("class", "selected");
		$("div.editors-care").css("display", "block");

		$(".tabs .editors-size a").attr("class", "");
		$("div.editors-size").css("display", "none");

		$(".tabs .editors-description a").attr("class", "");
		$("div.editors-description").css("display", "none");

		return false;
	});





	/*

	$('.refine a').click(function() {
	//		$('div').hasClass('size3').css('border','2px solid #ff0');
	//		$(this).toggleClass('selected');

	$('.refine a').attr('class', '');
	$(this).attr('class', 'selected');

	var selectedClasses = '';
	$('.refine a').each(function() {
	var filterClass = $(this).attr('id');
	if ($(this).attr('class') == 'selected') {
	selectedClasses += "." + filterClass;
	};
	});

	//		var arrSelectedClasses = new Array();
	//		arrSelectedClasses = selectedClasses.split(' ');		

	//		for (i=1;i<arrSelectedClasses.length;i++){
	//selectedItems += '.' + arrSelectedClasses[i] + ' ';
	//alert(arrSelectedClasses[i]);
	//		}

	//selectedClasses.substring(0,((selectedClasses.length)-2));

	//		selectedClasses = ".style8.style1";

	//		alert(selectedClasses);
	$(selectedClasses).css('display', 'block');


	return false;
	});

	*/



	$('.refine-style a').click(function () {

		//Check if current item is selected.  If it is, deselect it
		var currentClass = $(this).attr('class');
		if (currentClass == 'selected') {

			$('.refine-style a').attr('class', '');

			var sizeId = '';
			var colourId = '';

			//Get ids of other selections
			$('.refine-size a').each(function () {
				if ($(this).attr('class') == 'selected') {
					sizeId = '.' + $(this).attr('id');
				}
			});

			$('.refine-colour a').each(function () {
				if ($(this).attr('class') == 'selected') {
					colourId = '.' + $(this).attr('id');
				}
			});

			if (sizeId == '' && colourId == '') {
				$('div.product').css('display', 'block');
			} else {
				$('div.product').css('display', 'none');
				$('div.' + sizeId + colourId).css('display', 'block');
			}

		} else {

			$('.refine-style a').attr('class', '');
			$(this).attr('class', 'selected');

			var filterClass = $(this).attr('id');
			var sizeId = '';
			var colourId = '';

			//Get ids of other selections
			$('.refine-size a').each(function () {
				if ($(this).attr('class') == 'selected') {
					sizeId = '.' + $(this).attr('id');
				}
			});

			$('.refine-colour a').each(function () {
				if ($(this).attr('class') == 'selected') {
					colourId = '.' + $(this).attr('id');
				}
			});

			$('div.product').css('display', 'none');
			$('div.' + filterClass + sizeId + colourId).css('display', 'block');

		}

		//Apply price selection (hide items outside of range)
		var priceRange = $('#slider-range').slider('option', 'values');
		var minSliderValue = priceRange[0];
		var maxSliderValue = priceRange[1];

		$('.product').each(function () {
			var itemPrice = $(this).attr('id');
			if (itemPrice < minSliderValue || itemPrice > maxSliderValue) {
				$(this).css('display', 'none');
			}
		});

		return false;
	});




	$('.refine-size a').click(function () {

		//Check if current item is selected.  If it is, deselect it
		var currentClass = $(this).attr('class');
		if (currentClass == 'selected') {

			$('.refine-size a').attr('class', '');

			var styleId = '';
			var colourId = '';

			//Get ids of other selections
			$('.refine-style a').each(function () {
				if ($(this).attr('class') == 'selected') {
					styleId = '.' + $(this).attr('id');
				}
			});

			$('.refine-colour a').each(function () {
				if ($(this).attr('class') == 'selected') {
					colourId = '.' + $(this).attr('id');
				}
			});

			if (styleId == '' && colourId == '') {
				$('div.product').css('display', 'block');
			} else {
				$('div.product').css('display', 'none');
				$('div.' + styleId + colourId).css('display', 'block');
			}

		} else {

			$('.refine-size a').attr('class', '');
			$(this).attr('class', 'selected');

			var filterClass = $(this).attr('id');
			var styleId = '';
			var colourId = '';

			//Get ids of other selections
			$('.refine-style a').each(function () {
				if ($(this).attr('class') == 'selected') {
					styleId = '.' + $(this).attr('id');
				}
			});

			$('.refine-colour a').each(function () {
				if ($(this).attr('class') == 'selected') {
					colourId = '.' + $(this).attr('id');
				}
			});

			$('div.product').css('display', 'none');
			$('div.' + filterClass + styleId + colourId).css('display', 'block');
		}

		//Apply price selection (hide items outside of range)
		var priceRange = $('#slider-range').slider('option', 'values');
		var minSliderValue = priceRange[0];
		var maxSliderValue = priceRange[1];

		$('.product').each(function () {
			var itemPrice = $(this).attr('id');
			if (itemPrice < minSliderValue || itemPrice > maxSliderValue) {
				$(this).css('display', 'none');
			}
		});

		return false;
	});



	$('.refine-colour a').click(function () {
		//Check if current item is selected.  If it is, deselect it
		var currentClass = $(this).attr('class');
		if (currentClass == 'selected') {

			$('.refine-colour a').attr('class', '');

			var sizeId = '';
			var styleId = '';

			//Get ids of other selections
			$('.refine-size a').each(function () {
				if ($(this).attr('class') == 'selected') {
					sizeId = '.' + $(this).attr('id');
				}
			});

			$('.refine-style a').each(function () {
				if ($(this).attr('class') == 'selected') {
					styleId = '.' + $(this).attr('id');
				}
			});

			if (sizeId == '' && styleId == '') {
				$('div.product').css('display', 'block');
			} else {
				$('div.product').css('display', 'none');
				$('div.' + sizeId + styleId).css('display', 'block');
			}

		} else {

			$('.refine-colour a').attr('class', '');
			$(this).attr('class', 'selected');

			var filterClass = $(this).attr('id');
			var sizeId = '';
			var styleId = '';

			//Get ids of other selections
			$('.refine-size a').each(function () {
				if ($(this).attr('class') == 'selected') {
					sizeId = '.' + $(this).attr('id');
				}
			});

			$('.refine-style a').each(function () {
				if ($(this).attr('class') == 'selected') {
					styleId = '.' + $(this).attr('id');
				}
			});

			$('div.product').css('display', 'none');
			$('div.' + filterClass + sizeId + styleId).css('display', 'block');
		}

		//Apply price selection (hide items outside of range)
		var priceRange = $('#slider-range').slider('option', 'values');
		var minSliderValue = priceRange[0];
		var maxSliderValue = priceRange[1];

		$('.product').each(function () {
			var itemPrice = $(this).attr('id');
			if (itemPrice < minSliderValue || itemPrice > maxSliderValue) {
				$(this).css('display', 'none');
			}
		});

		return false;
	});




	/*
	$('.refine-size a').click(function() {
	$(this).toggleClass('selected');

	$('.refine-size a').each(function() {
	var filterClass = $(this).attr('id');
	if ($(this).attr('class') == 'selected') {
	$('div.' + filterClass).css('display', 'block');
	} else {
	$('div.' + filterClass).css('display', 'none');
	}
	});
	return false;
	});

	$('.refine-colour a').click(function() {
	$(this).toggleClass('selected');

	$('.refine-colour a').each(function() {
	var filterClass = $(this).attr('id');
	if ($(this).attr('class') == 'selected') {
	$('div.' + filterClass).css('display', 'block');
	} else {
	$('div.' + filterClass).css('display', 'none');
	}
	});
	return false;
	});
	*/




	//Font replacement
	Cufon.replace("h2, #product-nav ul, .c", { fontFamily: "Futura Md BT", hover: true });
	//Cufon.replace('h2', { fontFamily: 'Myriad Pro' });


});


function IsNumeric(input) {
	return (input - 0) == input && input.length > 0;
}
