﻿(function($) {
    // **** Date Extensions ****

    Date.prototype.compareDate = function(date) {
        var thisYear = this.getFullYear();
        var thisMonth = this.getMonth();
        var thisDate = this.getDate();

        var thatYear = date.getFullYear();
        var thatMonth = date.getMonth();
        var thatDate = date.getDate();

        if (thisYear != thatYear) {
            return thisYear - thatYear;
        }
        if (thisMonth != thatMonth) {
            return thisMonth - thatMonth;
        }
        if (thisDate != thatDate) {
            return thisDate - thatDate;
        }
        return 0;
    }
    Date.prototype.dateGreaterThan = function(date) {
        return this.compareDate(date) > 0;
    }
    Date.prototype.dateLessThan = function(date) {
        return this.compareDate(date) < 0;
    }

    // **** Splash ****

    // Configurations
    var startDate = new Date();
    var endDate = new Date();
    var today = new Date();
    var splashDuration = 5 * 1000;  // 5 seconds
    startDate.setFullYear(2010, 7, 30); // 2010-08-30
    endDate.setFullYear(2010, 9, 31);   // 2010-10-31
    var $splash = $("#splash");

    // Check if need to show splash
    if (today.dateLessThan(startDate) || today.dateGreaterThan(endDate)) {
        return;
    }
    // Check Cookie (Only show the splash one time)
    if (alreadyShown()) {
        return;
    } else {
        setCookie();
    }

    // Show Splash
    showSplash();

    // Add Event Hanlder for Close Button
    $splash.find("#splash-close-button").click(function() {
        closeSplash();
    });

    var timeoutId = setTimeout(function() {
        closeSplash();
    }, splashDuration);

    function showSplash() {
        var width = $(document.body).width() - 17;
        if ($.browser.msie) {
            width += 17;
        }
        $splash.width(width).show();    // Not set height, because we make the height fixed in css file
    }

    function closeSplash() {
        $splash.hide();
        clearTimeout(timeoutId);
    }

    function log(msg) {
        if (console && console.log) {
            console.log(msg);
        }
    }

    // Cookie Helpers
    function setCookie() {
        document.cookie = "splash=done;path=/";
    }
    function alreadyShown() {
        //alert(document.cookie + " -> " + document.cookie.indexOf("splash=done"));
        return document.cookie.indexOf("splash=done") >= 0;
    }

})(jQuery);
