$(document).ready(function(){

	//###############   MENU: Expandable menu options   ###############
	$('#product-nav li.toggler ul').hide();
	$('#product-nav li.toggler a:first').click(function() {  
		var $parent = $(this).parent();
		$(this).next().slideToggle("fast");
		if( $parent.is('.closed') ) {
			$parent.removeClass('closed');
			$parent.addClass('open');
		} else {
			$parent.removeClass('open');
			$parent.addClass('closed');
		}
		return false;
	});
			
	//###############   PRETTYPHOTO: Zoom functionality throughout content   ###############
	$("a[rel^='prettyPhoto']").prettyPhoto({
		showTitle: false
	});

	//###############   Image SLIDESHOW   ###############
	$('#slideshow').cycle({
		timeout:10000,	// milliseconds between slide transitions (0 to disable auto advance) 
		speed:1000,		// speed of the transition
		pause:1			// true to enable "pause on hover" 
	});

	//###############   Expandable USER PANEL (login/register)   ###############

	//###   Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#login-toggle a").click(function () {
		$("#login-toggle a").toggle();
		$("#logout-panel").hide();
		if ($("#register-toggle a").css("display") == "none") {
			//###   Panel already open on Register form   ###
			$("#register-toggle a").toggle();
			$("#register-panel").hide("slow");
			$("#login-panel").show("slow");
		}
		if ($("#login-panel").css("display") == "none") {
			$("#login-panel").show();
			$("#register-panel").hide();
		}
		return false;
	});
	$("#register-toggle a").click(function () {
		$("#register-toggle a").toggle();
		$("#logout-panel").hide();
		if ($("#login-toggle a").css("display") == "none") {
			//###   Panel already open on Login form   ###
			$("#login-toggle a").toggle();
			$("#login-panel").hide("slow");
			$("#register-panel").show("slow");
		}
		if ($("#register-panel").css("display") == "none") {
			$("#register-panel").show();
			$("#login-panel").hide();
		}
		return false;
	});
	$("a.user-butn").click(function(){
		$("div#user-panel").slideDown("slow");
		$("#user-nav").addClass("show-panel");
		$("#user-nav .form-success").hide();
	});
	$("a.close-panel").click(function(){
		$("div#user-panel").slideUp("slow");
		$("#user-nav").removeClass("show-panel");
	});
	//###   LOGOUT   ###
	$("#user-nav #logout-link a").click(function(){
		$("#login-panel").hide();
		$("#register-panel").hide();
		$("#logout-panel").show("slow");
		$.ajax({
			url: "/index.php?ACT=10"
		});
		setTimeout( function(){
			//###   Show/hide user nav links   ###
			$("#user-nav #login-toggle a.close-panel").trigger("click");
			$("#user-nav #login-toggle").removeClass('hidden');
			$("#user-nav #register-toggle").removeClass('hidden');
			$("#user-nav #logout-link").addClass('hidden');
			$("#user-nav #account-link").addClass('hidden');
		}, 4500);

		if( $("#user-nav #login-toggle .user-butn:visible").length > 0 ) { $("#login-toggle a").toggle(); }
		if( $("#user-nav #register-toggle .user-butn:hidden").length > 0 ) { $("#register-toggle a").toggle(); }

		return false;
	});

	//###############   PRODUCT DETAILS - TABBED PANEL   ###############
	$("#madetomeasure-tab").click(function () {
		$(this).removeClass('inactive');
		$("#readymade-tab").addClass('inactive');

		$("#readymade-panel").hide();
		$("#madetomeasure-panel").show();

		return false;
	});
	$("#readymade-tab").click(function () {
		$(this).removeClass('inactive');
		$("#madetomeasure-tab").addClass('inactive');

		$("#madetomeasure-panel").hide();
		$("#readymade-panel").show();

		return false;
	});
	
	//###############   PRODUCT DETAILS - COLOUR SWATCH   ###############
	$RadioButns = $("form .colour-options input[type=radio]");
	if ($RadioButns.length > 0) {
		//$("form .colour-options input[type=radio]").prettyCheckboxes({
		$RadioButns.prettyCheckboxes({
			checkboxWidth: 32,
			checkboxHeight: 32
		});
	}

	$TipObjects = $("form .colour-options label");
	if ($TipObjects.length > 0) {
		//$("form .colour-options label").qtip({
		$TipObjects.qtip({
			position: {
				corner: {
					target: 'bottomMiddle',
					tooltip: 'topMiddle'
				},
				adjust: { y: 5 }
			},
			style: { 
				padding: 5,
				border: {
					width: 2,
					radius: 0,
					color: '#A42238'
         			}
         		},
         		show: { delay: 500,
         			effect: 'fade'
         		}
		});
	}



	//###############   MODULE: CONVERTER  ###############
	$("#converter #convert-inches").keydown(function (event) {
		if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 110 || event.keyCode == 190 || (event.keyCode >= 48 && event.keyCode <= 57)) { return event.which; } else { return false };
	});
	$("#converter #convert-centimetres"). keydown(function (event) {
		if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 110 || event.keyCode == 190 || (event.keyCode >= 48 && event.keyCode <= 57)) { return event.which; } else { return false };
	});

	$("#converter #convert-inches").keyup(function (event) {
		//###   cm x 0.39* = in OR in x 2.54 = cm   ###
		if ( $(this).val() != "") {
			var value = parseFloat( $(this).val() ) * 2.54;
			var result = Math.round(value*100)/100;
			$("#converter #convert-centimetres").val(result);
		} else {
			$("#converter #convert-centimetres").val('Enter Centimetres');
		}
	});
	$("#converter #convert-centimetres").keyup(function (event) {
		if ( $(this).val() != "") {
			var value = parseFloat( $(this).val() ) * 0.39;
			var result = Math.round(value*100)/100;
			$("#converter #convert-inches").val(result);
		} else {
			$("#converter #convert-inches").val('Enter Inches');
		}
	});


	//###############   Payment Process   ###############
	$("#order-process li:not(.active,.arrow)").hoverIntent(function() {
		$(this).find(".rollover").fadeIn("normal");
		$groupElement = $(this).parent();
		$groupElement.find(".active .rollover").hide();
	}, function() {
		$(this).find(".rollover").hide();
		$groupElement = $(this).parent();
		$groupElement.find(".active .rollover").show();
	});
	$("#order-process li:not(.active,.arrow,.disabled)").click(function() {
		top.location.href = $(this).find("a").attr("href");
	});

	//###############   Payment Step 1   ###############
	$("#checkout #content #checkout-login").click(function () {
		 $("#user-nav #login-toggle .user-butn").trigger('click');
		 return false;
	});
	$("#checkout #content #checkout-register").click(function () {
		 $("#user-nav #register-toggle .user-butn").trigger('click');
		 return false;
	});

	//###############   Payment Step 2 - Further Information Delivery Address Toggle   ###############
/*	if ( $("#user-details-form #delivery-same-address").attr("checked") ) {
		$("#user-details-form #delivery-address").hide();
	}
	$("#user-details-form #delivery-same-address").change(function() {
		if ( $(this).attr("checked") ) {
			$("#user-details-form #delivery-address").fadeOut("normal");
		} else {
			$("#user-details-form #delivery-address").fadeIn("normal");
		}
	}); */

	//###############   LOGIN FORM   ###############

	//###   Password visibility toggle   ###
/*	$("#login-password").after('<input id="login-password-shown" name="password-disregard" rel="remove-on-submit" type="text" minlength="6" maxlength="20" title="" />');
	$("#login-password-shown").attr("title", $("#login-password").attr("title")).hide();

	//###   Update duplicate fields  ###
	$("#login-password").change(function (event) {
		$("#login-password-shown").val( $(this).val() );
	});
	$("#login-password-shown").change(function (event) {
		$("#login-password").val( $(this).val() );
	});
	//###   Password visibility toggle   ###
	$("#login-form .show-password").click(function () {
		if ( $(this).html() == "Show" ) {
			$(this).html("Hide");
			$("#login-password").hide();
			$("#login-password-shown").show();
		} else {
			$(this).html("Show");
			$("#login-password").show();
			$("#login-password-shown").hide();
		}
	});
*/

	//###############   REGISTRATION FORM   ###############

	//###   Password visibility toggle   ###
/*	$(".show-password").show();
	$("#reg-password").after('<input id="reg-password-shown" name="password-disregard" rel="remove-on-submit" type="text" minlength="6" maxlength="20" title="" />');
	$("#reg-password-shown").attr("title", $("#reg-password").attr("title")).hide();*/

	//###   Update duplicate fields  ###
	$("#reg-email").hide().prev().hide();
	$("#reg-username").change(function (event) {
		$("#reg-email").val( $(this).val() );
	});
	$("#reg-password-confirm").hide().prev().hide();
	$("#reg-password").change(function (event) {
		$("#reg-password-confirm").val( $(this).val() );
		//$("#reg-password-shown").val( $(this).val() );
	});
/*	$("#reg-password-shown").change(function (event) {
		$("#reg-password-confirm").val( $(this).val() );
		$("#reg-password").val( $(this).val() );
	});*/

	//###   Password visibility toggle   ###
/*	$("#register-form .show-password").click(function () {
		if ( $(this).html() == "Show" ) {
			$(this).html("Hide");
			$("#reg-password").hide();
			$("#reg-password-shown").show();
		} else {
			$(this).html("Show");
			$("#reg-password").show();
			$("#reg-password-shown").hide();
		}
	});*/
});
		
$(function() {
	//###############   PRICE CHANGE: Rollover code for price labelling   ###############
	$("#content .products li").hover(function() {
		//jQuery(".more", jQuery(this).parent()).show();
		jQuery(".more", jQuery(this)).show();
		jQuery(".price", jQuery(this)).hide();
	}, function() {
		jQuery(".more", jQuery(this)).hide();
		jQuery(".price", jQuery(this)).show();
	});
});

