﻿//function formKeyPress(form, event) {
//    if (event.keyCode == event.DOM_VK_RETURN) {
//        form.submit();
//    }
//}

var CC_PopupBackgroundDivSelector = ".popupBackgroundDiv";
var KC_Escape = 27;
var KC_Return = 13;

$("#homeicon").ready(function() {
    $("#homeicon").hide();
});

$(document).ready(function() {

    registerReturnSubmitForForms();
    registerPopups();
    $("#oa-logo a").hover(
        function() {
            $("#homeicon").show();
            },
        function() {
            $("#homeicon").hide();
        });
});

function disableDefaultEventForLinkButtons() {
    $(".linkButton").click(function(event) {
        event.preventDefault();
    });
}

function registerReturnSubmitForForms() {
    $("form").keypress(function(event) {

        if (event.keyCode == KC_Return) {
            this.submit();
        }
    });
}

function registerPopups() {

    $(".popupDiv").each(function() {
        var popupDivSelector = "#".concat(this.id);
        var showButtonSelector = ".".concat(this.id).concat("Show");

        $(showButtonSelector).click(function(event) { showPopup(popupDivSelector); event.preventDefault(); });

        // Click out event
        $(".popupBackgroundDiv").click(function(event) { hidePopup(popupDivSelector); event.preventDefault(); });

        // Escape keypress event!
        $(document).keypress(function(e) {

            if (e.keyCode == KC_Escape) {
                hidePopup(popupDivSelector);
            }
        });
    });
}

function showPopup(popupSelector) {

    var background = $(CC_PopupBackgroundDivSelector)
    var popup = $(popupSelector);

    background.css("opacity", "0.8");
    background.css("filter", "alpha(opacity=80)");

    background.fadeIn("slow");
    popup.fadeIn("slow");

    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = popup.height();
    var popupWidth = popup.width();

    popup.css("left", ((windowWidth / 2) - (popupWidth / 2)));
    popup.css("top", ((windowHeight / 2) - (popupHeight / 2)));

    //only need force for IE6
    background.css("height", windowHeight);
}

function hidePopup(popupSelector) {

    var background = $(CC_PopupBackgroundDivSelector)
    var popup = $(popupSelector);

    background.fadeOut();
    popup.fadeOut();
}
