/*
Author: RK
Created: 2009-Jul-22
Modified: 2009-Nov-26
Modified By: Omair Al Hamid

Code modified from sources:
http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/
http://www.w3schools.com/JS/js_cookies.asp
*/

$(document).ready(function() {

    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) +
        ";domain=canadianinteriors.com" + ";path=/;";

        //#5761 : cookie needs to be set to a centralized path to avoid showing popup again
        //in subdirectories
        //#5761 : manually set domain name for cookie, to avoid www vs non-www problem
    }

    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + (c_name.length + 1);
                c_end = document.cookie.indexOf(";", c_start);

                if (c_end == -1) {
                    c_end = document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }

    function visitorHasSeenPopup() {
        hasBeenSeen = getCookie('c5761 ');
        if (hasBeenSeen == 'true') {
            return true;
        } else {
            return false;
        }
    }

    function neverShowPopupAgain() {
        setCookie('c5761 ', 'true', 365);
    }

    function markPopupAsSeen() {
        setCookie('c5761 ', 'true', 30);
    }

    function toggle(div_id) {
        var el = document.getElementById(div_id);
        if (el.style.display == 'none' && !visitorHasSeenPopup()) { el.style.display = 'block'; }
        else { el.style.display = 'none'; }
    }

    function window_pos(popUpDivVar) {
        var popUpDiv = document.getElementById(popUpDivVar);
        var superContainer = document.getElementById("super_container");
        var blanket = document.getElementById("blanket");

        window_width = document.documentElement.clientWidth;
        window_height = document.documentElement.clientHeight;

        blanket.style.height = superContainer.offsetHeight + 200 + "px";

        var browser = navigator.appName;
        if (browser.indexOf("Opera") != -1) { blanket.style.height = superContainer.offsetHeight + 220 + "px"; }

        //Since fixed position wasn't working in IE, we won't use it for IE 6.0. Style is already defined as absolute in SS
        var b_version = navigator.appVersion;
        if (b_version.indexOf("MSIE 6") == -1) { popUpDiv.style.position = "fixed"; } else { blanket.style.width = window_width + "px"; }

        window_width = (window_width / 2) - (popUpDiv.offsetWidth / 2); //Centers the popup
        window_height = (window_height / 2) - (popUpDiv.offsetHeight / 2); //Centers the popup

        if (location.href.indexOf('/ads/') != -1) {
            popUpDiv.style.left = window_width - 75 + 'px';
            popUpDiv.style.top = window_height + 'px';
        }
        else {
            popUpDiv.style.left = window_width + 'px';
            popUpDiv.style.top = window_height + 'px'
        }
    }

    function popup(windowname) {
        toggle('blanket');
        toggle(windowname);
        window_pos(windowname);
    }

    //$("#cboxClose #cboxOverlay").click(function() { markPopupAsSeen(); });
    if (!visitorHasSeenPopup) { 
        $(".popup").colorbox({ open: true });
        //$(".popup").colorbox({ inline: true, href: "#inline_content", open: true });
    }
});