$(document).ready(function() {

    function hide_popup() {
        if ($("#popup_overlay").length > 0) {
            $("#popup_overlay").fadeOut(150, function() {
                $(this).remove();
            });
        }
        $(".popup-windows").fadeOut(150);
    }

    function getDocHeight() {
        var D = document;
        return Math.max(
            Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
            Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
            Math.max(D.body.clientHeight, D.documentElement.clientHeight)
            );
    }

    function check_ie(check_version) {
        if ($.browser.msie) {
            if (check_version) {
                version = parseInt(jQuery.browser.version);
                if (check_version == version) {
                    return true;
                }
            } else {
                return true;
            }
        }
        return false;
    }

    $(".popup-close-link").click(function() {
        hide_popup();
    });
    
    $(".openpopup").click(function() {

        popup_id = $(this).attr("title");

        if (popup_id && $("#" + popup_id).length > 0) {

            popup = $("#" + popup_id);

            // create overlay
            overlay = $("<div id='popup_overlay'></div>");

            if (check_ie(6)) {
                screen_height = getDocHeight();
                screen_width = parseInt($(window).width());
                overlay.css({
                    position: 'absolute',
                    width: screen_width,
                    height: screen_height
                });
            }

            // append the overlay to the document body
            $("body").append(overlay.click(function() {
                hide_popup();
            }));

            // set the css and fade in our overlay
            overlay.css("opacity", 0.5);
            overlay.fadeIn(150);

            // keydown event
            $(document).keydown(function(e) {
                if (e.keyCode == 27) {
                    hide_popup();
                }
            });

            if (check_ie(6)) {
                popup.css({
                    position: 'absolute'
                });
            } else {
                popup.css({
                    position: 'fixed'
                });
            }

            screen_width = parseInt($(window).width());
            popup_left = ((screen_width - popup.width())) > 0 ? parseInt((screen_width - popup.width()) / 2) : 0;
            popup.css({
                top: 88,
                left: popup_left,
                zIndex: 1000
            });

            popup.show();
        }

        return false;
    });

    var input_original_values = [];
    $(".popup-windows").find("input,textarea").each(function() {
        input_original_values[$(this).attr("id")] = $(this).val();
    });
    $('.popup-windows').find("input,textarea").bind('focus', function() {
        if($(this).val() == input_original_values[$(this).attr('id')]) {
            $(this).val('');
        }
    });
    $('.popup-windows').find("input,textarea").bind('blur', function() {
        if($(this).val() == '') {
            $(this).val(input_original_values[$(this).attr('id')]);
        }
    });

    var form_check = true;
    var email_regexp = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
    var form_alert = false;

    $('.popup-windows').find('form').submit(function() {
        form_check = true;
        form_alert = false;

        $(this).find("input,textarea").each(function () {
            check_input(this);
        });
        return form_check;
    });

    function check_input(input) {
        if (!input_check_valid(input)) {
            form_check = false;
            $(input).css({
                color: "#F00"
            });
            if (!form_alert) {
                alert("Please enter " + input_original_values[$(input).attr('id')].replace("*", ""));
                $(input).focus();
                form_alert = true;
            }
        } else {
            $(input).css({
                color: "#363636"
            });
        }

        $(input).blur(function() {
            if (!input_check_valid(input)) {
                form_check = false;
                $(input).css({
                    color: "#F00"
                });
            } else {
                $(input).css({
                    color: "#363636"
                });
            }
        });
    }

    function input_check_valid(input) {
        if (($(input).attr('name') != 'email' && ($(input).val() == '' || $(input).val() == input_original_values[$(input).attr('id')]) && $(input).hasClass('input-required')) || ($(input).attr('name') == 'email' && !email_regexp.test($(input).val()))) {
            return false;
        } else {
            return true;
        }
    }
   
});
