﻿
/* This is used to open up a 'popup' type content item */
function openPopup(url) {
    var newPopup;

    if (navigator.appName.indexOf('Microsoft') != -1)
    { newPopup = window.open(url, '_blank', 'width=541,height=600,menubars=0,scrollbars=1,resizable=0,status=0,toolbar=0,left=100,top=70'); }
    else
    { newPopup = window.open(url, '_blank', 'width=541,height=600,menubars=0,scrollbars=1,resizable=0,status=0,toolbar=0,left=100,top=70'); }
}
function openParent(url)
{ window.opener.location = url; window.opener.focus(); window.close(); }
function extLink(url)
{ var extLinkWindow = window.open(url); }
function closeWindow()
{ window.close(); }
function activateFormItem(tID, selectedValue, ignoreValue)
{ tField = document.getElementById(tID); tField.disabled = 'false'; alert(selectedValue); }
function search(url) {
    var searchField = document.getElementById('searchInput'); if (searchField != null && searchField.value != 'Search' && searchField.value != '')
    { window.location.href = url + "?q=" + escape(searchField.value.replace(/"/g, "%22")); } else { alert('Please enter a valid search term'); }
}
function clearField(f, ignoreStr)
{ if (f.value == ignoreStr) { f.value = ''; } }
function submitOnEnter(tKey, tURL, tField) {
    var keynum; if (window.event)
    { keynum = tKey.keyCode; }
    else if (tKey.which)
    { keynum = tKey.which; }
    if (keynum == 13) { if (tField.id == 'searchInput') { search(tURL); } else { window.location = tURL + "?q=" + escape(tField.value) } }
}
function isIE6() {
    version = 0; if (navigator.appVersion.indexOf("MSIE") != -1) {
        temp = navigator.appVersion.split("MSIE")
        version = parseFloat(temp[1]); if (version < 7) { return true; }
    }
    return false;
}


//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {


    if ($.cookie('pop_up_cookie') == null) {
        if ($("#dvPopUpContent").html().length > 60) {
            centerPopup();
            loadPopup();
            var date = new Date();
            date.setTime(date.getTime() + (30 * 60 * 1000));
            $.cookie("pop_up_cookie", "true", { expires: date });
        }
    }

    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function() {
        disablePopup();
    });
    $("#Close").click(function() {
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").click(function() {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

});
