﻿
//Everything that loads on page load should go in here
jq13(document).ready(function() {
    try {
        var debug = false;
        //Create a floater variable
        var floater = jq13('#FloatingDiv');

        var classList = floater.attr('class').split(' ');

        jq13.each(classList, function(index, item) {
            if (item.toString().toLowerCase().indexOf('debug') >= 0) {
                debug = true;
            }
        });
        var expdate1 = new Date(); // pre-set to the current time and date

            expdate1.setTime(expdate1.getTime() + 1000 * 60); // add one minute to it
        setCookie('test',"1",expdate1);
        if (getCookie('test') && (!getCookie('skipAd') || debug)) {

            var expdate = new Date(); // pre-set to the current time and date

            expdate.setTime(expdate.getTime() + 1000 * 60 * 60 * 24); // add one day to it

            //24 hours/day * 60 minutes/hour * 60 seconds/minute * 1000 milliseconds/second

            // = howevermany milliseconds/day.

            setCookie("skipAd", "true", expdate);

            //create container for close button
            var closediv = jq13('<div></div>').attr('id', 'FloatingDivClose').css('float', 'right');

            //create close button
            var closelink = jq13('<a></a>');
            closelink.click(function() { CloseFloatingDiv() });
            closelink.html('<img src="/images/closebutton.gif" width="75" height="23">');
            closelink.attr('href', '#');

            //create the separator div
            var sep = jq13('<div></div>').css('clear', 'both');

            //wrap the contents in a div before moving forward
            var contentwrapper = jq13('<div></div>');
            contentwrapper.attr('id', 'FloatingContent');
            floater.contents().filter(function() { return this.nodeType != 1; }).wrap(contentwrapper);



            //add close button to container
            closelink.prependTo(closediv);

            //add the separator first since we are prepending
            sep.prependTo(floater);
            //add close container to floating div
            closediv.prependTo(floater);

            //fadeout the background
            var fadespeed = 1500;
            GrayOverlay('black', .7, fadespeed);

            //after the background has faded animate centering the div
            setTimeout(
                "CenterFloatingDiv(true)", fadespeed);


            //center the div if the window is resized
            jq13(window).resize(function() {
                CenterFloatingDiv(false);
            });
            jq13(window).scroll(function() {
                if(floater.css('position') == 'absolute' && floater.css('display') != 'none')
                {
                var win_h = jq13(window).scrollTop();
                jq13('#overlay').css('top', win_h);
                CenterFloatingDiv(false);
                }
            });
        }
    }
    catch (err) { CloseFloatingDiv(); if(debug){alert("An error has occured: "+ err);} }

});

//center the floating div
//the firstrun parameter will decide whether or not to animate the centering of the div
function CenterFloatingDiv(firstrun) {
    //find the center of the window and the center of the div
    var floater = jq13('div#FloatingDiv');
    var win_w = jq13(window).width();
    var win_h = jq13(window).height();
    var floater_w = floater.width();
    var floater_h = floater.height();
    var Scrolltop = jq13(window).scrollTop();
    
    //figure out which effect to use and the starting position using the classes of the floating div
    var classList = floater.attr('class').split(' ');
    var effect = "easeOutElastic";
    var fromleft = false;
    var fromright = false;
    var fromtop = false;
    var frombottom = false;
    var speed = 3000;
    jq13.each(classList, function(index, item) {
        if (item.toString().indexOf('ease') >= 0) {
            effect = item;
        }
        if (item.toString().toLowerCase().indexOf('fromtop') >= 0) {
            fromtop = true;
        }
        if (item.toString().toLowerCase().indexOf('frombottom') >= 0) {
            frombottom = true;
        }
        if (item.toString().toLowerCase().indexOf('fromleft') >= 0) {
            fromleft = true;
        }
        if (item.toString().toLowerCase().indexOf('fromright') >= 0) {
            fromright = true;
        }
        if (item.toString().toLowerCase().indexOf('speed') >= 0) {
            speed = parseInt(item.toString().replace('speed_', ""));
        }
    });
    
    
    //calculate the left and top of the floating div given the width and height of the div and window
    //for centering the div in it's final resting place
    var floater_l = (win_w - floater_w) / 2;
    var floater_t = (win_h - floater_h) / 2;
    //animate the flyin only on the first run
    if (firstrun) {
        var leftstart = floater_l;
        var topstart = floater_t;
        if (fromtop) { topstart = -floater_h; }
        if (frombottom) { topstart = win_h; }
        if (fromleft) {leftstart = -floater_w;}
        if (fromright) {leftstart = win_w;}
        floater.css('left', leftstart);
        floater.css('top', topstart);
        floater.css('display', 'block');
        
        floater.animate({'left':floater_l,'top':floater_t}, speed, effect);
    }
    else {
        floater.css('left', floater_l);
        if (floater.css('position') == 'absolute') {
            floater_t = floater_t + Scrolltop;
        }
        floater.css('top', floater_t);
        floater.css('display', 'block');
    }
}

function CloseFloatingDiv() {
    jq13('div#FloatingDiv').css('display', 'none');
    jq13('div#overlay').css('display', 'none');
}

function getCookie(name) {

    var cookies = document.cookie;

    if (cookies.indexOf(name) != -1) {

        var startpos = cookies.indexOf(name) + name.length + 1;

        var endpos = cookies.indexOf(";", startpos) - 1;

        if (endpos == -2) endpos = cookies.length;

        return unescape(cookies.substring(startpos, endpos));

    }

    else {
        
        return false; // the cookie couldn't be found! it was never set before, or it expired.

    }

}

function setCookie(name, value, expires) {

    // no expiration date specified? use this date and it will just be deleted soon.

    if (!expires) expires = new Date();

    document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=/";

}

function GrayOverlay(backgroundcolor,finalopacity,speed) {
    //get width of window
    var win_w = jq13(window).width();
    var win_h = jq13(window).height();

    //create a div
    var over = jq13('<div></div>');
    over.attr('id', 'overlay');
    over.css('width', win_w);
    over.css('height', win_h);
    over.css('z-index', '500');
    over.css('background-color', backgroundcolor);
    over.css('top', 0);
    over.css('left', 0);
    over.css('opacity', 0);
    jq13('body').append(over);

    jq13('#overlay').animate({ 'opacity': finalopacity }, speed);
}

function GetEasingEffectFromClass(el) {
    var classList = el.attr('class').split(' ');
    var returneffect =""
    jq13.each(classList, function(index, item) {
        if (item === 'someClass') {
        }
    });

}

