﻿function toggle(div_id) {
    var el = document.getElementById(div_id);
    if (el.style.display == 'none') { el.style.display = 'block'; }
    else { el.style.display = 'none'; }
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 200 + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height = blanket_height / 2 - 300; //150 is half popup's height
    popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width = window_width / 2 - 350; //150 is half popup's width
    popUpDiv.style.left = window_width + 'px';
}

function popup(windowname) {
    var popupShowMode = getCookie('PopupShow');
    //alert("Stored value : " + popupShowMode);
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);
}

function closePopup(windowname) {
    //alert("Closing...");
//    blanket_size(windowname);
//    window_pos(windowname);
//    toggle('blanket');
//    toggle(windowname);
    if (getShowPopupSelection()) {
        setCookie('PopupShow', 'true', '3650'); // Set it for 10 years
    } else {
        setCookie('PopupShow', 'true', '30'); // Set it for 30 days
    }
}

/**
function showPopup(windowname) {
    if (document.getElementById('blanket') != null && document.getElementById('popUpDiv') != null) {
        var popupShowMode = getCookie('PopupShow');
        if (popupShowMode == null && !html5Supported()) {
            blanket_size(windowname);
            window_pos(windowname);
            toggle('blanket');
            toggle(windowname);
        }
    } else {
        //alert("Empty code.");
    }
}
**/
function getShowPopupSelection() {
    var el = document.getElementById('showPopupSelection');
    //alert("Selected value [" + el.checked +"]"); 
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    //alert("Writing cookie...." + c_name + "=" + c_value);
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
    return null;
}

function html5Supported() {
    //alert("Checking Html5  support");
    if (!Modernizr.canvas) {
        //alert("canvas not supportted");
        return false;
    }
    if (!Modernizr.video) {
        //alert("video not supportted");
        return false;
    }
    //alert("canvas & video are supportted");
    return true;
}


function setCSSPopUp() {
    //Close Popups and Fade Layer
    $('a.close').live('click', function () { //When clicking on the close
        $('#fade , .popup_block').fadeOut(function () {
            $('#fade, a.close').remove();  //fade them both out
        });
        closePopup('popUpDiv');
        return false;
    });
}

function cssPopUp(width, rel) {
    if (document.getElementById('popUpDiv') != null) {
        //alert("Pop check");
        var popupShowMode = getCookie('PopupShow');
        if (popupShowMode == null && !html5Supported()) {
            var element = document.getElementById('HTML5PopupBlankLink');
            if (element != null) {
                element.click();
            }

            //var t = setTimeout("timer()", 5 * 1000);
            var popID = rel; // $(this).attr('rel'); //Get Popup Name
            var popWidth = width;

            //Fade in the Popup and add close button
            $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }); //.prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');


            if(@_jscript_version >= 5.8){
                //Define margin for center alignment (vertical   horizontal) - we add 20px to the height/width to accomodate for the padding  and border width defined in the css
                var popMargTop = ($('#' + popID).height()) / 2;
                var popMargLeft = ($('#' + popID).width()) / 2;

                //Apply Margin to Popup
                $('#' + popID).css({
                    'margin-top': -popMargTop,
                    'margin-left': -popMargLeft
                });
                //Fade in Background
                $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
                $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
            } else { // IE6 stuff
                var popMargTop = ($(window).height() - $('#' + popID).height()) / 2;
                var popMargLeft = ($(window).width() - $('#' + popID).width()) / 2;
                //Apply Margin to Popup
                $('#' + popID).css({
                    'top': popMargTop,
                    'left': popMargLeft,
                    'overflow': 'hidden',
                    'position': 'absolute',
                    'height':'525 px'
                });
                //Fade in Background
                $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
                $('#fade').css({
                    'height': $(document).height(),
                    'width': $(document).width(),
                    'filter': 'alpha(opacity=80)'
                }).fadeIn();
            }


            return false;
        }
    }

}

