﻿var visibleMenu = null;
(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

function postRemote(url) {
    $("body form:first").attr('action', url);
}

$(document).ready(function () {
    try {
        $('input[hint!=""]').hint();
        $('textarea[hint!=""]').hint();
    } catch (e) { }

    $('div.third > div.lborder').each(function (i) {
        $(this).height($(this).parent().height());
    });

    try {
        $.localScroll.defaults.axis = 'xy';

        // Scroll initially if there's a hash (#something) in the url 
        $.localScroll.hash({
            target: '#content', // Could be a selector or a jQuery object too.
            queue: true,
            duration: 1500
        });

        $.localScroll();
    } catch (e) { }

    var urlRegEx = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

    $('div.hpFeatures').each(function () {
        var img = $(this).css('background-image').replace('.gif', 'Over.gif').replace('url(', '').replace(')', '');
        $.preLoadImages(img);
    });

    $('div.ddMenu a.menu').each(function (i) {
        var m = $(this).parent().children('div:first');
        var i = $(this).children('img');
        var n = i.attr('src');
        var nh = n.replace('arrow.jpg', 'arrowb.jpg');
        var x = n.replace('arrow.jpg', 'arrowe.jpg');
        var xh = n.replace('arrow.jpg', 'arroweb.jpg');


        try {
            $.preLoadImages(nh);
            $.preLoadImages(x);
            $.preLoadImages(xh);
        } catch (e) { }

        $(this).hover(function () {
            $(this).css('color', '#29AFE5');
            if (m.css('display') == 'none') {
                i.attr('src', nh);
            } else {
                i.attr('src', xh);
            }
        }, function () {
            $(this).css('color', '#8FB33D');
            if (m.css('display') == 'none') {
                i.attr('src', n);
            } else {
                i.attr('src', x);
            }

        });

        $(this).click(function () {
            changeHover = false;
            if (m.css('display') == 'none') {
                if (visibleMenu != null && visibleMenu != m) {
                    visibleMenu.slideUp(400, function () {
                        if ($(this).css('color') == '#29AFE5') {
                            i.attr('src', nh);
                        } else {
                            i.attr('src', n);
                        }
                    });
                }
                visibleMenu = m;
                m.slideDown(400, function () {
                    if ($(this).css('color') == '#29AFE5') {
                        i.attr('src', xh);
                    } else {
                        i.attr('src', x);
                    }
                });
            } else {
                visibleMenu = null;
                m.slideUp(400, function () {
                    if ($(this).css('color') == '#29AFE5') {
                        i.attr('src', nh);
                    } else {
                        i.attr('src', n);
                    }
                });
            }
        });
    });
});

function validateNewsletter() {
    var valid = true;
    $('#cm-name-error').hide();
    $('#cm-dlhulr-dlhulr-error').hide();
    if ($('#cm-name').val() == "" || $('#cm-name').val() == $('#cm-name').attr('hint') || $('#cm-name').val().length <= 2) {
        valid = false;
        $('#cm-name-error').show();
    }
    if ($('#cm-dlhulr-dlhulr').val() == "" || !/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($('#cm-dlhulr-dlhulr').val())) {
        valid = false;
        $('#cm-dlhulr-dlhulr-error').show();
    }

    if (valid) {
        postRemote('http://contract1.createsend.com/t/y/s/dlhulr/');
    }

    return valid;
}

function pageLoad() {
    try {
        $('input[hint!=""]').hint();
        $('textarea[hint!=""]').hint();
    } catch (e) { }
}
