var productCustomizations = {
	replaceWoodloreRailHeightWithInput : function(){
		$("#ColorLabel_5233").replaceWith("<input id='rail_height' name='rail_height' type='text' />");
	}
};


$(document).ready(function(){

	$("input[value='Pay Now By Credit Card']").click(function (){
		$("#checkout_form").submit();
		return false;
	});
	
	$("a.readmore").click(function(){
	  var j_this = $(this);
	  j_this.parents("div.expandable").find("div.expandable-more").show();
	  j_this.hide();
	  return false;
	});
	
	$("#process_error").slideToggle("slow");
	
	$("input[type='text']").addClass("text");

	$("#checkout_container #state").each(function (){
		$(this).find("option:contains('Canada')").remove();
	});

	$("#checkout_form #state_billing, #checkout_container #state").addClass("required").change(function(){
		var j_this = $(this);
		if (parseInt(j_this.val()) >= 65)	
		{
			alert("There is a freight surcharge to Alaska and Hawaii not priced on your order. We will call you with pricing before order is placed.");
		}
	}).find("option:first").val("");
  $("#checkout_form").validate({
    rules: {
    },
		messages: {
		 first_name: "Shipping first name is required.",
		 last_name: "Shipping last name is required.",
		 email: {
			 required: "Email address is required.",
			 email: "Your email address must be in the format of name@domain.com"
		 },
		 address: "Shipping address is required.",
		 city: "Shipping city is required.",
		 state: "Shipping state is required",
		 zip_code: "Shipping zip code is required.",
		 
		 firstName: "Billing first name is required.",
		 lastName: "Billing last name is required.",
		 creditCardNumber: {
		 	required: "Credit card number is required.",
		 	creditcard: "A valid credit card number is required."
		 },
		 address1: "Billing address is required.",
		 billing_city: "Billing city is required.",
		 state_billing: "Billing state is required,",
		 zip: "Billing zip code is required.",
		 cvv2Number: "Card verification number is required."
		 
		},
  	submitHandler: function(form) {
  		$("#submitButton, #showButton").toggle();
			form.submit();
 		},
 		invalidHandler: function(form, validator) {
 			var errors = validator.numberOfInvalids();
      if (errors) {
			$.scrollTo("#error_target", "medium", {
				onAfter: function(){
					$("#errors_container").slideDown();
				}
			});
			}
			else{
				$("#errors_container").slideUp();
			}
			return false;
    },
    focusInvalid: false,
    errorLabelContainer: "#errors",
    errorContainer: "#errors_container",
 		wrapper: "li"
 	});
  productCustomizations.replaceWoodloreRailHeightWithInput();

  if( $("form#order_form a[href='category-cellular-shades.html']").size() > 0 ){
    $("select").change(hide_cord_position_for_cordless);
  }
  
	$("td.order_title:contains('Cordless')").each(function(){
		$("tr[id*='_position_']").hide();
	});

  $("select").each(hide_cord_position_for_cordless);
  
  function hide_cord_position_for_cordless(){
    var has_cordless_option = false;
    $(this).find("option").each(function(){
      if($(this).html().match(/.*cordless.*/i)){
        has_cordless_option = true;
      }
    });
    if(has_cordless_option) {

        if($(this).find("option:selected").html().match(/.*cordless.*/i) ){
          $("tr[id*='option_label_']:contains('Position:')").hide();
        }
        else{
          $("tr[id*='option_label_']:contains('Position:')").show();
        }

    }
  }
	  
  var moveSlider = function(){
    var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first'); 
    
    var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
    
    current_image.removeClass("selected");
    next_image.addClass("selected");
    $("#gallery-mask").scrollTo(next_image, 800);
    return false;
  };
  
  $("a.pic-navigator").click(function(){
    var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first'); 
    
    var next_image = $("#" + $(this).attr("rel") );
    
    current_image.removeClass("selected");
    next_image.addClass("selected");
    $("#gallery-mask").scrollTo(next_image, 800);
    return false;
  });
  
  var run = setInterval(moveSlider, 4000);  
  $('#slider').hover(  

          function() {  
              clearInterval(run);  
          },   
          function() {  
              run = setInterval(moveSlider, 4000);    
          }  
  );
  
  var gallery_width = 0;
  $("#gallery img").each(function(){
    gallery_width += $(this).attr("width");
  });
  
  $("#gallery").css("width", gallery_width);

  $("a[href$='jpg']").fancybox();
  $("a.fancybox").prepend("<div class='image-enlarge-text'>Click to Enlarge</div>");
 
  $("#installer-form").validate();
  
  $("select.headrail").change();
  
  $("form#order_form").submit(function(){
  	var invalid = false;
  	var wrapped_this = $(this);
  	var headrail_select = wrapped_this.find("select.headrail");
  	if(headrail_select.attr("selectedIndex") > 0){
  		$("select[name^='width_']:visible").each(function(){
  			var wrapped_this = $(this);
  			if(!wrapped_this.attr("name").match(/frac/)){
					if(wrapped_this.val() < 1){
						invalid = true;
					}
  			}
  		});
  		if(invalid){
  			alert("Please enter a width for all panel sections");
  			return false;
  		}
  	}
  });
 
});

