
$( document ).ready ( function() {

    var height = $("body").height();
    
    //------------------------------------------------------------------
    // Styles
    //------------------------------------------------------------------
    var container_styles = "z-index: 100; position: absolute; border: 2px solid #b4bac7; padding: 5px; background-color: white; max-height: 600px; overflow: scroll;";
    var message_styles = "";
    var close_styles = "text-align: right; font-size: 11px;";
    var close_a_styles = "color: #333333;";
    
    //------------------------------------------------------------------
    // mouse out
    // message var is defined in the user's generated javascript file
    // which is included just before this file
    // Don't show again if they already closed it, based on cookie
    //------------------------------------------------------------------
    close_cookie = getCookie('no_popup');
    if (close_cookie=="")
    {
        $(document).mouseleave(function () {
            // Check if exit message has been shown yet
            if ($("#exit_message").length <= 0) {
                // Show Message
                $("body").append("<div id=\"exit_message\" style=\"" + container_styles + "\"><div style=\"" + close_styles + "\"><a id=\"close\" href=\"\" style=\"" + close_a_styles + "\">Close</a></div><div style=\"" + message_styles + "\">" + video_code + message + form_code + "</div></div>");
                
                // Center Message
                $("#exit_message").center();
                
                // Fade the background
                $("body").prepend("<div id=\"body\" style=\"left: 0; top: 0; position: absolute; height: " + height + "px; width: 100%\">&nbsp;</div>");
                $("#body").css({'background-color' : 'black', 'opacity' : '.5', 'filter' : 'alpha(opacity=50)'});
            }
            
            //------------------------------------------------------------------
            // Close everything
            //------------------------------------------------------------------
            $("#close").click(function () {
                $('#exit_message').remove();
                $('#body').remove();
                
                // Set cookie to not open again
                setCookie('no_popup', 1, 1);
                //return false;
            });
        });
    }
});

//**************************************************************
// Center Function
//**************************************************************
jQuery.fn.center = function ()
{
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

//**************************************************************
// Set Cookie
//**************************************************************
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());
}

//**************************************************************
// getCookie
//**************************************************************
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 "";
}
