// JavaScript Document

(function(){
	
	var dialog = [];
	
	var swf = null;
	
	var init = function(){
						
		$('#sashes,#sashesXmas').each(function(){
		
			var flashvars = null;
			
			var params = { allowscriptaccess : "always", wmode : 'transparent' };
			
			if ($('#sashes-content').length > 0)
			
				swf = swfobject.embedSWF("/flash/bow-selector.swf", "sashes-content", "520", "885", "9.0.0", null, flashvars, params);

			else if ($('#sashes-content-large').length > 0)

				swf = swfobject.embedSWF("/flash/bow-selector-large.swf", "sashes-content-large", "956", "580", "9.0.0", null, flashvars, params);

			else if ($('#sashes-content-xmas').length > 0)

				swf = swfobject.embedSWF("/flash/bow-selector-xmas.swf", "sashes-content-xmas", "520", "680", "9.0.0", null, flashvars, params);
				
			$('#Sash').change(bowSelected);

		});
		
		$('.slideshow').each(initSlideshow);
		
		// $('#order-now').click(confirmOrder);
							
		$('#shippingSameAsBilling').each(checkBillingAddress);
		
		$('#shippingSameAsBilling').change(checkBillingAddress);

		$('#quoteform').each(initQuoteForm);

		window.selectBow = selectBow;

		window.getSelected = getSelected;
		
		$('.rangeBlackChairs').mouseover(function() {
  			$('.rangeFeaturePanel-black').show();
		});
		$('.rangeBlackChairs').mouseout(function() {
  			$('.rangeFeaturePanel-black').hide();
		});
		
		$('.rangePurchase').mouseover(function() {
  			$('.rangeFeaturePanel-purchase').show();
		});
		$('.rangePurchase').mouseout(function() {
  			$('.rangeFeaturePanel-purchase').hide();
		});
		

  }
	
	var initQuoteForm = function() {
		
		Date.format = 'dd/mm/yyyy';

		$('.date-select', this).datePicker({
				clickInput:true
		});
		
		initValidation();
		
		loadSizeDefault();

		initStyles();
					
		initCounties();
		
		if ($('#errorAnchor1').length > 0) {
			
			$('#Submit2').get()[0].focus();
			
		}
		
		// console.log($('#clist,#Date', this));
					
		// $('#clist,#Date,#Quantity,#Fitting', this).change(checkDelivery);		
		
		// checkDelivery();
		
	}

	var checkFitting = function() {
		
		var selected = $('#clist').find('option[selected=true]');
		
		
		if (selected) {
		
			if( $(selected).hasClass('fitting') ) {
				$('#fitting-service').css('display', 'block');
				$("#Fitting").removeAttr("disabled");
			} else {
				$('#fitting-service').css('display', 'none');
				$("#Fitting").val("");
				$("#Fitting").attr("disabled","disabled");
			}

		}

	}	

	var initCounties = function() {
		
		$("select#clist").each(checkFitting);
	
		$("select#clist").change(checkFitting);
		
	}
	
	var selectBow = function(selected) {
		
		$('#Sash').val(selected);
		
	}
	
	var getSelected = function() {
		
		return $('#Sash').val();
		
	}

	var bowSelected = function() {
		
		if ($('#Sash').val() != null) {
		
			var swf = $('#sashes object').get(0);
			
			swf.bowSelect($('#Sash').val());
			
		}
		
	}
	
	var initStyles = function() {

		var form = this;
		
//	console.log($('.selectChair .chair'));
		
		$('.selectChair .chair').click(function(){
			
			$('#Style').val($(this).attr('id'));
			
			//  option[value=' + $(this).attr('id') + ']').attr('selected', true);
			
			$('.selectChair .chair').removeClass('selected');
			
			$(this).addClass('selected');
			
		});
				
	}
	
	var checkBillingAddress = function() {
		
		if ($(this).val() == 1) {
			
			disableBillingAddress();
			
		} else {
			
			enableBillingAddress();
			
		}
		
	}
	
	var disableBillingAddress = function() {
		
		$('#billing-element').hide();
		
	}
	
	var enableBillingAddress = function() {
		
		$('#billing-element').show();
		
	}
	
	var conf = null;
	
	var confirmOrder = function(e) {
		
		e.preventDefault();
		
		var url = $(this).attr('href');
		
		if (!conf) {
		
			conf = document.createElement('div');
			
			$(conf).html('<p id="confirm-order"><span style="float: left; margin: 0pt 7px 10px 0pt;" class="ui-icon ui-icon-alert"></span>Do you really want to confirm this order?</p>');

			$(conf).dialog({
				title: 'Confirm order?',
				resizable: false,
				height:130,
				modal: true,
				buttons: {
					'Confirm': function() {
						location.href = url;
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			
		} else {
			
			$(conf).dialog('open');
		
		}
					
	}
	
	var initValidation = function() {
		
		jQuery.validator.addMethod("ukdate", function(value, element, params) { 
		 return this.optional(element) || /^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/.test(value); 
		}, jQuery.format("This is not a valid date. Please use the date select button to select a correct date."));
		
		var params = {
			
			ignoreTitle : true,
			
			rules : {
				
				clist : 'required',
				date : 'ukdate',
				chairSize : 'required',
				quanitity : {
					min : 40
				}
			
			},
			
			messages: {
				name: "This information is required.",
				email: {
					
					required : "This information is required.",
					email : "Please enter your email address."
				
				},
				venue : "This information is required.",
//			date : "This information is required.",
				style : "This information is required.",
				sash : "This information is required.",
				fitting : "This information is required.",
				chairSize : "This information is required.",
				quanitity : "You must order at least 40 items."

			}
		
		};

		$('#quoteform').validate(params);
									
	};
	
	var initSlideshow = function() {
		
		$(this).cycle({
			
			fx: 'fade'
			
		});
		
	};
	
	var loadSizeDefault = function() {
		
		var val = $('#quoteform #chairSize').val();
		
		if (val)
			
			var selectedVal = val + ' (click to change)';
			
		else
		
			var selectedVal = 'Click here to select size';
			
		var content = '<input type="text" name="chairSizeSelect" id="chairSizeSelect" value="' + selectedVal + '" class="required" /><input type="hidden" id="chairSize" name="chairSize" value="' + val + '"/>';
		
		$('#quoteform #chairSize').replaceWith(content);
		
		if (dialog['select-size'] == null) {
		
			loadDialog('select-size', '/size-guide.php?ajax=true', selectSize);
		
		}
			
		$('#quoteform #chairSizeSelect').focus(selectSizes);
		
	};

	var loadDialog = function(name, url, selectFunc) {
		
		var func = arguments[3] ? arguments[3] : null;
				
		$.post(url, null, function(content){
			
			if (dialog[name] == null) {
		
				dialog[name] = document.createElement('div');
				
				$(dialog[name]).attr('id', name);
				
				$(dialog[name]).hide();
				
				//console.log($(dialog));

				$('#container').append(dialog[name]);
				
			}
			
			$(dialog[name]).html(content);

			$(dialog[name]).find('ul.selectable').find('li').css('cursor', 'pointer');

			$(dialog[name]).find('ul.selectable').find('li').click(selectFunc);

			if (func != null)
			
				func();
		
		});
								
	}
	
	var selectSizes = function(e) {
				
		e.preventDefault();
			
		var params = {
			height : '565',
			width : '785px',
			modal : true
		};
					
		$(dialog['select-size']).dialog(params);
		
		$(dialog['select-size']).dialog('open');
			
	};
	
	var selectSize = function(e) {
		
		e.preventDefault();
		
		var val = $(this).find('.description').html();
		
		$('#quoteform #chairSize').val(val);

		$('#quoteform #chairSizeSelect').val(val  + ' (click to change)');
		
		$(dialog['select-size']).dialog('close');
		
	}

	$(document).ready(init);
	
})();
