jQuery(document).ready(function() {
    if (jQuery.browser.msie && jQuery.browser.version < 6) {
        return false
    } else {
		/* offers */
		jQuery(".offer div.offerDesc").hide();
		jQuery(".offer .offerTitle").append('<br/><a href="#fulldetails">&raquo; Read More</a>');
		jQuery(".offer .offerTitle a").click(function(){
			var offer = jQuery(this);
			offer.parent().next(".offerDesc").slideToggle("fast");
			return false;
		});
		jQuery("#offers tr:odd").css("background-color", "#f4f4f4");
        Cufon.replace('h1');
        Cufon.replace('h3#caseStudies');
        jQuery('#datePickerInner').css('display', 'none');
        jQuery('#pSubmit').val('');
        jQuery('#productSubmit').val('');
        jQuery('#enquireSubmit').val('');
        jQuery('.quoteTable input').keyup(function() {
            this.value = this.value.replace(/[^0-9\.]/g, '')
        });
        jQuery("#categories").hide();
        jQuery(".categoryButton").hide();
        jQuery(".sCategoryList").change(function() {
            top.location = "/productcategory/" + jQuery(this).val()
        });
        jQuery("#thumbnails a").click(function() {
            var source = jQuery("img", this).attr("src");
            jQuery("#swapImg").attr("src", source);
            jQuery("#thumbnails a").removeClass("selected");
            jQuery(this).addClass("selected");
            return false
        });
        jQuery('#quoteForm').submit(function() {
            if (jQuery('#pQuantity1').val() == '') {
                jQuery('#pQuantity1').addClass('errorInput');
                jQuery('#productErrorBox').html('Please enter a quantity (A)').slideDown(300);
                return false
            } else {
                jQuery('#pQuantity1').removeClass('errorInput')
            }
            if (jQuery('#pName').val() == '') {
                jQuery('#pName').addClass('errorInput');
                jQuery('#productErrorBox').html('Please enter your name').slideDown(300);
                return false
            } else {
                jQuery('#pName').removeClass('errorInput')
            }
            if (jQuery('#pEmail').val() == '') {
                jQuery('#pEmail').addClass('errorInput');
                jQuery('#productErrorBox').html('Please enter your email address').slideDown(300);
                return false
            } else {
                jQuery('#pEmail').removeClass('errorInput')
            }
            var str = jQuery('#pEmail').val();
            var filter = /^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i;
            if (!filter.test(str)) {
                jQuery('#pEmail').addClass('errorInput');
                jQuery('#productErrorBox').html('Please enter a valid email address').slideDown(300);
                return false
            } else {
                jQuery('#pEmail').removeClass('errorInput')
            }
            jQuery("#productErrorBox").animate({
                backgroundColor: "#832124"
            },
            200).slideDown(300);
            jQuery("#productErrorBox").html("<h4>Validating <span>(this won't take long)</span></h4>").addClass("messageValidate");
            formAction = jQuery("#quoteForm").attr("action");
            var str = jQuery("#quoteForm").serialize();
            final = str + "&action=" + formAction;
            jQuery.ajax({
                url: "/wp-content/themes/rosslyn/request-a-quote.php",
                type: "POST",
                data: final,
                error: function() {
                    jQuery("#productErrorBox").css("background", "none").html("<h4>Oops! <span>There was an error, please try again later</span></h4>")
                },
                success: function(html) {
                    setTimeout(function() {
                        jQuery("#pSubmit").slideUp(300);
                        jQuery("#productErrorBox").animate({
                            backgroundColor: "#4e8f4d"
                        },
                        500,
                        function() {
                            setTimeout(function() {},
                            4000)
                        });
                        jQuery("#productErrorBox").removeClass("messageValidate").html("<h4>Thanks! <span>We'll be in touch.</span></h4>")
                    },
                    3000)
                }
            });
            return false
        });
        jQuery('input').focus(function() {
            jQuery(this).addClass('highlight')
        });
        jQuery('input').blur(function() {
            jQuery(this).removeClass('highlight')
        });
        jQuery('#login form').submit(function() {
            jQuery('#loginMessage').fadeIn('fast');
            return false
        });
        jQuery('#loginMessage').hover(function() {
            jQuery('#loginMessage').css('cursor', 'pointer')
        },
        function() {
            jQuery(this).css('cursor', 'auto')
        });
        jQuery('#loginMessage span').click(function() {
            jQuery('#loginMessage').fadeOut('fast')
        });
        if (jQuery('#dylkjh-dylkjh').val() == 'Receive our monthly updates') {
            jQuery('#dylkjh-dylkjh').addClass('cmGrey')
        }
        jQuery('#dylkjh-dylkjh').focus(function() {
            if (jQuery(this).val() == 'Receive our monthly updates') {
                jQuery(this).val('').removeClass()
            }
        });
        jQuery('#dylkjh-dylkjh').blur(function() {
            if (jQuery(this).val() == '') {
                jQuery(this).val('Receive our monthly updates').addClass('cmGrey')
            }
        });
        jQuery("#mailingButton").click(function() {
            jQuery("#message").animate({
                backgroundColor: "#832124"
            },
            200).slideDown(300);
            jQuery("#messageInner").html("<h4>Validating <span>(just making sure everything is good)</span></h4>").addClass("messageValidate");
            jQuery('#mailingForm').submit(function() {
                return false
            });
            formAction = jQuery("#mailingForm").attr("action");
            emailId = formAction.replace("http://rosslyn.createsend.com/t/y/s/", "");
            emailId = emailId.replace("/", "");
            emailId = emailId + "-" + emailId;
            jQuery("#" + emailId).removeClass();
            if (!checkEmail(emailId)) {
                setTimeout(function() {
                    jQuery("#messageInner").removeClass("messageValidate").html("<h4>Oops! <span>You need to enter a valid email address!</span></h4>");
                    jQuery("#messageInner h4").css("padding-right", "0");
                    jQuery("#dylkjh-dylkjh").addClass("errorInput")
                },
                2000);
                return
            }
            jQuery("#messages").fadeOut("fast");
            var str = jQuery("#mailingForm").serialize();
            final = str + "&action=" + formAction;
            jQuery.ajax({
                url: "/wp-content/themes/rosslyn/mailingList.php",
                type: "POST",
                data: final,
                error: function() {
                    jQuery("#messageInner").css("background", "none").html("<h4>Oops! <span>There was an error, please try again later</span></h4>")
                },
                success: function(html) {
                    setTimeout(function() {
                        jQuery("#message").animate({
                            backgroundColor: "#4e8f4d"
                        },
                        500,
                        function() {
                            setTimeout(function() {
                                jQuery("#message").slideToggle(500)
                            },
                            4000)
                        });
                        jQuery("#messageInner").removeClass("messageValidate").html("<h4>Thanks! <span>You've now been signed up to our mailing list!</span></h4>");
                        jQuery("#messageInner h4").css("padding-right", "0");
                        jQuery("#dylkjh-dylkjh").removeClass()
                    },
                    3000)
                }
            })
        });
        jQuery('#phone-number').keyup(function() {
            this.value = this.value.replace(/[^0-9\.]/g, '')
        });
        jQuery("#contactMe").submit(function() {
            jQuery("#enquiryState").animate({
                backgroundColor: "#832124"
            },
            200).slideDown(300).html("<h4>Validating <span>(just making sure everything is good)</span></h4>").addClass("messageValidate");
            var cancelForm = false;
            var errorType = false;
            jQuery(":input", this).each(function() {
                if (jQuery(this).is('.required')) {
                    if (this.value == "") {
                        jQuery(this).addClass('errorInput');
                        cancelForm = true;
                        errorType = "empty"
                    } else {
                        jQuery(this).removeClass('errorInput')
                    }
                }
            });
            if (jQuery("#country").val() == "") {
                errorType = "empty";
                jQuery("#country").addClass('errorInput')
            } else {
                jQuery("#country").removeClass('errorInput')
            }
            if (errorType != "empty") {
                var telephone = jQuery("#phone-number").val();
                if (telephone.length < 10) {
                    jQuery("#phone-number").addClass('errorInput');
                    errorType = "telephone"
                } else {
                    jQuery("#phone-number").removeClass('errorInput')
                }
                if (errorType != "telephone") {
                    var str = jQuery('input#email-address').val();
                    var filter = /^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i;
                    if (!filter.test(str)) {
                        jQuery('input#email-address').addClass('errorInput');
                        errorType = "email"
                    } else {
                        jQuery('input#email-address').removeClass('errorInput')
                    }
                }
            }
            setTimeout(function() {
                if (errorType != false) {
                    if (errorType == "empty") {
                        jQuery("#enquiryState").removeClass("messageValidate").html("<h4>Oops! <span>Please complete all fields in 'Your details'</span></h4>")
                    } else if (errorType == "email") {
                        jQuery("#enquiryState").removeClass("messageValidate").html("<h4>Oops! <span>Please enter a valid email address</span></h4>")
                    } else if (errorType == "telephone") {
                        jQuery("#enquiryState").removeClass("messageValidate").html("<h4>Oops! <span>Please enter your full telephone number</span></h4>")
                    }
                } else {
                    var formData = jQuery("#contactMe").serialize();
                    jQuery.ajax({
                        data: formData,
                        url: "/wp-content/themes/rosslyn/forms/phpmailer-fe.php",
                        type: 'post',
                        error: function() {
                            console.log("Failed to submit")
                        },
                        success: function(r) {
                            tempName = r.substr(0, 5);
                            if (tempName == "<!--S") {
                                jQuery("#enquiryState").animate({
                                    backgroundColor: "#4e8f4d"
                                },
                                500,
                                function() {
                                    setTimeout(function() {
                                        jQuery("#enquiryState").slideToggle(500)
                                    },
                                    4000)
                                });
                                jQuery("#enquiryState").removeClass("messageValidate").html("<h4>Thanks! <span>We've received your enquiry and will be in contact shortly</span></h4>")
                            } else {
                                jQuery("#enquiryState").html("<h4>Oops! <span>There was an error, please try again later</span></h4>")
                            }
                        }
                    })
                }
            },
            2000);
            return false
        })
    }
});
function showRequest(formData, jqForm, options) {
    jQuery("#contactResponse").fadeOut('fast');
    var cancelForm = "";
    jQuery(".formBox :input").each(function() {
        if (jQuery(this).is('.required')) {
            if (jQuery(this).val() == "") {
                jQuery(this).addClass('requiredShow');
                cancelForm = "empty"
            } else {
                jQuery(this).removeClass('requiredShow')
            }
        }
    });
    jQuery(".formBox select").each(function() {
        if (jQuery(this).is('.required')) {
            if (jQuery(this).val() == "") {
                jQuery(this).addClass('requiredShow');
                cancelForm = "empty"
            } else {
                jQuery(this).removeClass('requiredShow')
            }
        }
    });
    if (cancelForm != "empty") {
        var str = jQuery('input#email').val();
        var filter = /^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i;
        if (!filter.test(str)) {
            jQuery('input#email').addClass('requiredShow');
            cancelForm = "email"
        } else {
            jQuery('input#email').removeClass('requiredShow')
        }
    }
    if (cancelForm == "") {
        jQuery("#ajax").fadeIn('fast');
        return true
    } else {
        if (cancelForm == "empty") {
            jQuery("#contactResponse").html('<big class="red"><strong>Error!</strong></big><br/>Please complete all fields marked with an asterisk (*)').fadeIn('slow')
        } else {
            jQuery("#contactResponse").html('<big class="red"><strong>Error!</strong></big><br/>Please enter a valid email address').fadeIn('slow')
        }
        return false
    }
}
function showResponse(responseText, statusText) {
    var tempName = responseText.substr(0, 5);
    if (tempName == "<!--S") {
        jQuery("#contactResponse").html(responseText).fadeIn('slow');
        jQuery("#formHide").css('display', 'none');
        jQuery("#contactSubmit").fadeOut('slow');
        jQuery("#contactSubmit").attr({
            'disabled': 'true'
        });
        jQuery("#ajax").fadeOut('slow')
    } else {
        jQuery("#contactResponse").html(responseText).fadeIn('slow');
        jQuery("#contactSubmit").attr({
            'disabled': ''
        });
        jQuery("#ajax").fadeOut('slow')
    }
}
function checkEmail(email) {
    var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = jQuery("#" + email).val();
    return pattern.test(emailVal)
} (function(jQuery) {
    jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'],
    function(i, attr) {
        jQuery.fx.step[attr] = function(fx) {
            if (fx.state == 0) {
                fx.start = getColor(fx.elem, attr);
                fx.end = getRGB(fx.end)
            }
            fx.elem.style[attr] = "rgb(" + [Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0), Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0), Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)].join(",") + ")"
        }
    });
    function getRGB(color) {
        var result;
        if (color && color.constructor == Array && color.length == 3) return color;
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55];
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)];
        return colors[jQuery.trim(color).toLowerCase()]
    }
    function getColor(elem, attr) {
        var color;
        do {
            color = jQuery.curCSS(elem, attr);
            if (color != '' && color != 'transparent' || jQuery.nodeName(elem, "body")) break;
            attr = "backgroundColor"
        }
        while (elem = elem.parentNode);
        return getRGB(color)
    };
    var colors = {
        aqua: [0, 255, 255],
        azure: [240, 255, 255],
        beige: [245, 245, 220],
        black: [0, 0, 0],
        blue: [0, 0, 255],
        brown: [165, 42, 42],
        cyan: [0, 255, 255],
        darkblue: [0, 0, 139],
        darkcyan: [0, 139, 139],
        darkgrey: [169, 169, 169],
        darkgreen: [0, 100, 0],
        darkkhaki: [189, 183, 107],
        darkmagenta: [139, 0, 139],
        darkolivegreen: [85, 107, 47],
        darkorange: [255, 140, 0],
        darkorchid: [153, 50, 204],
        darkred: [139, 0, 0],
        darksalmon: [233, 150, 122],
        darkviolet: [148, 0, 211],
        fuchsia: [255, 0, 255],
        gold: [255, 215, 0],
        green: [0, 128, 0],
        indigo: [75, 0, 130],
        khaki: [240, 230, 140],
        lightblue: [173, 216, 230],
        lightcyan: [224, 255, 255],
        lightgreen: [144, 238, 144],
        lightgrey: [211, 211, 211],
        lightpink: [255, 182, 193],
        lightyellow: [255, 255, 224],
        lime: [0, 255, 0],
        magenta: [255, 0, 255],
        maroon: [128, 0, 0],
        navy: [0, 0, 128],
        olive: [128, 128, 0],
        orange: [255, 165, 0],
        pink: [255, 192, 203],
        purple: [128, 0, 128],
        violet: [128, 0, 128],
        red: [255, 0, 0],
        silver: [192, 192, 192],
        white: [255, 255, 255],
        yellow: [255, 255, 0]
    }
})(jQuery);