﻿jQuery.noConflict();


jQuery(document).ready(function(){

    jQuery("#contactForm").validate({
        errorLabelContainer: "#errorMessages",
        wrapper: "span",
        rules: {
            name: { required: true },
		    email: { required: true, email: true },
		    pref_contact: { required: true }
	    },
	    messages: {
	        name: { required: "Please enter your name in the space provided." },
		    email: "Please enter a valid email address in the space provided.",
		    pref_contact: "Please select your preferred contact method."
	    },
	    invalidHandler: function(form, validator)   {
	        var errors = validator.numberOfInvalids();
	        if (errors) 
	            jQuery("#errorMessages").show();
	        else
	            jQuery("#errorMessages").hide();
	    }
    });
    
    jQuery("#cmSignup").validate({
        errorLabelContainer: "#cmError",
        wrapper: "span",
        rules: {
            EmailAddress: { required: true, email: true }
        },
        messages: {
            EmailAddress: "Please enter a valid email address in the space provided."
        },
	    invalidHandler: function(form, validator)   {
	        var errors = validator.numberOfInvalids();
	        if (errors) 
	            jQuery("#cmSignup").show();
	        else
	            jQuery("#cmSignup").hide();
	    }
	});

	jQuery(document).ready(function () {
	    jQuery(".changeLng").click(function (e) {
	        e.preventDefault();
	        if (jQuery(".languageSel").is(":visible")) {
	            jQuery(".languageSel").slideUp(400);
	        } else {
	            jQuery(".languageSel").slideDown(400);
	        }
	    });

	});
    
//    jQuery("#topShopNav li").hover(
//        function () {
//            jQuery(this).find("ul").show();
//        },
//        function () {
//            jQuery(this).find("ul").hide();
//        }
//    );
    
    
    jQuery("#topShopNav li li").hover(
        function () {
            jQuery(this).addClass("hover");
        },
        function () {
            jQuery(this).removeClass("hover_parent");
            jQuery(this).removeClass("hover");
        }
    );
    
    jQuery("#topShopNav li").hover(
        function () {
            jQuery(this).addClass("hover_parent");
        },
        function () {
            jQuery(this).removeClass("hover_parent");
        }
    );
    
    jQuery("#home_Category_Links li").hover(
        function () {
            jQuery(this).addClass("hover");
        },
        function () {
            jQuery(this).removeClass("hover");
        }        
    );
    
//    jQuery("#refineSearch h3").click(function() {
//        jQuery(this).next().toggle();
//    });
    
   jQuery(".productsCarousel").jMyCarousel({
//        btnPrev: jQuery(".upArr"),
//        btnNext: jQuery(".downArr"),
//        vertical: true,
//        circular: false,
//        evtStart: "mouseover",
//        evtStop: "mouseout",
//        start: 0,
//        visible: '100%'
        btnPrev: jQuery(".upArr"),
        btnNext: jQuery(".downArr"),
        vertical: true,
        circular: false,
        evtStart: "mouseover",
        evtStop: "mouseout",
        start: 0,
        visible: "145px"
    });


    
    jQuery(".bestSellers").jMyCarousel({
        btnPrev: jQuery(".leftArr"),
        btnNext: jQuery(".rightArr"),
        vertical: false,
        circular: false,
        evtStart: "click",
        evtStop: "click",
        start: 0,
        visible: "750px",
        step: "230px",
        eltByElt: true
    });
    
    jQuery(".stockists").jMyCarousel({
        btnPrev: jQuery(".leftArr"),
        btnNext: jQuery(".rightArr"),
        vertical: false,
        circular: false,
        evtStart: "click",
        evtStop: "click",
        start: 0,
        visible: "667px",
        step: "170px",
        eltByElt: true
    });
    
    //jQuery("#topShopNav ul ul:first").show();
    
    jQuery(".fancylink_terms").fancybox({
        'hideOnContentClick': true
    });
   
    if (window.location.pathname.indexOf("map.aspx") > 0)
        initialize(); // Only run the initialise function if on the map page.
   
});


function initialize() {
    var myLatlng = new google.maps.LatLng(55.930885021117376, -4.648434519767761);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"White House Products."
    });   

  }

var popUpWin=0;
function popUpWindow(lsURL)
{
	var liLeftPos = 0;
	var liTopPos = 0;
	var liWidth = 500;
	var liHeight = 400;

	if(screen){
		liLeftPos = (screen.width/2) - (liWidth/2);
		liTopPos = (screen.height/2) - (liHeight/2);
	}

  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(lsURL, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+liWidth+',height='+liHeight+',left='+liLeftPos+', top='+liTopPos+',screenX='+liLeftPos+',screenY='+liTopPos+'');
}

/* registration script */

(function () {
    jQuery(document).ready(function(){
        /*
        jQuery("input.regDefault:text").live("each",function(){
            var $element = jQuery(this),
                defaultVal = $element.val();
            
            $element.focus(function(){
                var $current = jQuery(this),
                    currentVal = $current.val();

                if(currentVal === defaultVal){
                    $current.removeClass("regDefault").val("");
                }
            });

            $element.blur(function(){
                var $current = jQuery(this),
                    currentVal = $current.val();

                if(currentVal === ""){
                    $current.addClass("regDefault").val(defaultVal);
                }
            });

        });
        
        jQuery("select.regDefault").live("each",function(){
            var $element = jQuery(this),
                elementVal = $element.val();
            
            if(elementVal !== ""){
                $element.val("");
            }

            $element.focus(function(){
                var $current = jQuery(this),
                    currentVal = $current.val();

                if(currentVal === ""){
                    $current.removeClass("regDefault");
                }
            });

            $element.blur(function(){
                var $current = jQuery(this),
                    currentVal = $current.val();

                if(currentVal === ""){
                    $current.addClass("regDefault");
                }
                else {
                    $current.removeClass("regDefault");
                }
            });

        });
        */
        /* registration validation */

        if(jQuery("#registerForm").length > 0){
            
            jQuery(".regValidate").each(function(){
                var $this = jQuery(this);
                
                $this.data("defaultVal", $this.val());

            });

            jQuery("form").submit(function(e){
                //e.preventDefault();
                return reg.validate(".regValidate");
            });
            
            jQuery(".regSubmit input").live("click",function(e){
                //e.preventDefault();
                return reg.validate(".regValidate");
            });
            
            /*jQuery("select.country").live("change",function(){
                reg.regUpdateCountry(this);
            });*/

        }


        
    });
})();

var reg = (function () {
    var r = {};

    var euCountry = [
        "169", //Austria
        "176", //Belgium
        "178", //Bulgaria
        "206", //Cyprus
        "203", //Czech Republic
        "209", //Denmark
        "214", //Estonia
        "219", //Finland
        "1", //France
        "207", //Germany
        "17", //Greece
        "26", //Hungary
        "29", //Ireland, Republic of
        "34", //Italy
        "58", //Latvia
        "56", //Lithuania
        "57", //Luxembourg
        "73", //Malta
        "85", //Netherlands
        "98", //Poland
        "100", //Portugal
        "104", //Romania
        "114", //Slovakia
        "113", //Slovenia
        "217", //Spain
        "111", //Sweden
        "5" //United Kingdom
    ];

    var usaCountry = [
        "136"//, //United States (Exc New York City)
		//"137" //United States (New York City)
    ];

    var businessLabel = {};

    businessLabel["manufacturer"] = "What do you manufacture?";
    businessLabel["distributor"] = "What do you distrubute?";
    businessLabel["farmer"] = "How many acres do you farm?";
    businessLabel["agricultural contractor"] = "Is agricultural contracting your full time occupation?";
    businessLabel["end user"] = "Nature of business?";
    businessLabel["plant hirer"] = "Do you sell mini excavators?";
    businessLabel["other"] = "Please describe the nature of your business";

    r.regUpdateCountry = (function (element) {
        var $select = jQuery(element),
            $regVat = jQuery("#regVat"),
            $regState = jQuery("#regState"),
            selectVal = $select.val(),
            euArrayPos = jQuery.inArray(selectVal, euCountry),
            usaArrayPos = jQuery.inArray(selectVal, usaCountry);
        //console.log(euArrayPos);
        if (euArrayPos > -1) {
            $regVat.removeClass("dn");
        }
        else {
            $regVat.addClass("dn");
        }

        if (usaArrayPos > -1) {
            $regState.removeClass("dn");
        }
        else {
            $regState.addClass("dn");
        }

    });

    r.regChangeBusiness = (function (element) {
        var $select = jQuery(element),
            $regBusinessLabel = jQuery("#regBusinessLabel"),
            selectVal = $select.val().toLowerCase(),
            newCopy = businessLabel[selectVal],
            otherCopy = businessLabel["other"];

        if (newCopy !== undefined) {
            $regBusinessLabel.text(newCopy);
        }
        else {
            $regBusinessLabel.text(otherCopy);
        }

    });

    r.validate = (function (fields) {
        var valid = false,
            $fields = jQuery(fields),
            fieldsCount = $fields.length,
            errorClass = "regError",
            errorMessage = "errorMessage",
            emailReg = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i,
            errorCount = 0,
            errorFields = [];

        jQuery("." + errorMessage).css({ "visibility": "hidden" });
        $fields.removeClass(errorClass);

        $fields.each(function () {
            var $this = jQuery(this),
                thisType = $this.attr("type"),
                thisVal = $this.val(),
                defaultVal = $this.data("defaultVal");

            if ($this.hasClass("state") === true) {
                if (jQuery(".country").val() === "136" && (thisVal === "" || thisVal === defaultVal)) {
                    errorCount += 1;
                    errorFields.push($this);
                }
            }
            else if ($this.hasClass("regInputVat") === true) {
                var euArrayPos = jQuery.inArray(jQuery(".country").val(), euCountry);

                if (euArrayPos > -1 && jQuery(".vatRadio input[type='radio']:checked").val() === "vatYes") {
                    if (thisVal === "" || thisVal === defaultVal) {
                        errorCount += 1;
                        errorFields.push($this);
                    }
                }
            }
            else if ($this.hasClass("regCheckbox") === true) {
                if ($this.find("input").is(":checked") === false) {
                    errorCount += 1;
                    errorFields.push($this.find("input"));
                }
            }
            else if ((thisVal === "" || thisVal === defaultVal) || ($this.hasClass("email") === true && emailReg.test(thisVal) === false)) {
                errorCount += 1;
                errorFields.push($this);
            }

        });

        if (errorCount === 0) {
            valid = true;

            //console.log("VALID");

        }
        else if (valid === false && errorCount > 0 && errorFields.length > 0) {
            var errorFieldsCount = errorFields.length,
                i = 0;

            for (i = 0; i < errorFieldsCount; i += 1) {
                var $element = errorFields[i],
                    $errorLabel = $element.next("span"),
                    $parentLabel = $element.parent().nextAll("span");

                if ($errorLabel.hasClass(errorMessage) === true) {
                    $errorLabel.css({ "visibility": "visible" });
                    $element.addClass(errorClass);
                }
                else if ($parentLabel.hasClass(errorMessage) === true) {
                    $parentLabel.css({ "visibility": "visible" });
                    $element.parent().addClass(errorClass);
                }


            }


        }


        return valid;

    });

    return r;
})();
