﻿window['oTimedoutPopup'] =
{
    events: [
                [window, 'scroll'],
                [window, 'resize'],
                [document, 'mousemove'],
                [document, 'keydown'],
                [document, 'click'],
                [document, 'dblclick'],
                [document, 'keypress'],
                [document, 'mousedown']
            ],
    initialized: false,
    seconds: 9999,
    options: {},

    initialize: function(oOptions) {
        oOptions = oOptions || {};
        oTimedoutPopup.options.url = oOptions.url || 'http://localhost/ravmilim/external/ContactUsPopup.aspx';
        oTimedoutPopup.options.title = oOptions.title || 'מלינגו';
        oTimedoutPopup.options.width = oOptions.width || 495;
        oTimedoutPopup.options.height = oOptions.height || 405;
        oTimedoutPopup.options.mode = oOptions.mode || 'window';
        oTimedoutPopup.options.timesToShowPerDay = oOptions.timesToShowPerDay || 0; //0 - show always

        oTimedoutPopup.initialized = true;
    },

    isToday: function(dateToCompare) {
        var today = new Date();
        var dateToCompare = new Date(dateToCompare);
        var retVal = false;

        if (typeof(dateToCompare) != 'undefined') {
            try {
                retVal = (today.getDate() == dateToCompare.getDate()) && (today.getFullYear() == dateToCompare.getFullYear()) && (today.getMonth() == dateToCompare.getMonth());
            }
            catch (ex) {
                //not valid date => not today
            }
        }

        return retVal;
    },

    shouldShow: function() {
        var retVal = true;

        if (oTimedoutPopup.options.timesToShowPerDay > 0) {
            var lastShown = $.cookie('lastShownPopup');
            var timesShown = $.cookie('timesShownPopup');
            var validExistingCookies = lastShown && timesShown;

            if (validExistingCookies && oTimedoutPopup.isToday(lastShown)) {
                if (parseInt(timesShown) >= oTimedoutPopup.options.timesToShowPerDay) {
                    retVal = false;
                }
            }
        }

        return retVal;
    },

    openWindow: function() {
        if (oTimedoutPopup.options.mode == 'modal') {
            $("#contactUsPopUp").slideToggle('slow', function() {
                var centerWidth = (window.screen.width - $("#contactUsPopUpContent").width()) / 2;
                var centerHeight = (window.screen.height - $("#contactUsPopUpContent").height()) / 2;

                $("#contactUsPopUpContent").css("top", centerHeight * 0.7);
                $("#contactUsPopUpContent").css("right", centerWidth);
                $("#contactUsPopUpContent").slideToggle('slow');

                if (oTimedoutPopup.options.timesToShowPerDay > 0) {
                    var lastShown = $.cookie('lastShownPopup');
                    var timesShown = $.cookie('timesShownPopup');
                    var validExistingCookies = lastShown && timesShown;
                    
                    if (!validExistingCookies || !oTimedoutPopup.isToday(lastShown)) {
                        $.cookie('lastShownPopup', new Date(), { expires: 1 });
                        $.cookie('timesShownPopup', 1, { expires: 1 });
                    }
                    else {
                        $.cookie('timesShownPopup', parseInt(timesShown) + 1, { expires: 1 });
                    }
                }
            });
        }
        else {
            var centerWidth = (window.screen.width - oTimedoutPopup.options.width) / 2;
            var centerHeight = (window.screen.height - oTimedoutPopup.options.height) / 2;

            newWindow = window.open(oTimedoutPopup.options.url, oTimedoutPopup.options.title, 'status=0,location=0,toolbar=0,scrollbars=0,menubar=0,resizable=0,' +
                'width=' + oTimedoutPopup.options.width +
                ',height=' + oTimedoutPopup.options.height +
                ',left=' + centerWidth +
                ',top=' + centerHeight);
            newWindow.focus();
        }
    },

    resetCounter: function(iSeconds) {
        oTimedoutPopup.seconds = iSeconds;
    },

    countDown: function() {
        if (oTimedoutPopup.seconds <= 0) {
            oTimedoutPopup.openWindow();
        }
        else {
            oTimedoutPopup.seconds--;
            window.setTimeout("oTimedoutPopup.countDown()", 1000);
        }
    },

    startIdlePopup: function(iSeconds) {
        if (oTimedoutPopup.initialized) {
            this.countDown();

            $(oTimedoutPopup.events).each(function() {
                $(this[0]).bind(this[1], function() {
                    oTimedoutPopup.resetCounter(iSeconds);
                });
            });
        }
    },

    startTimedoutPopup: function(milliseconds) {
        if (oTimedoutPopup.initialized) {
            setTimeout(
                "oTimedoutPopup.openWindow();",
                milliseconds);
        }
    },

    startTimedoutModalPopup: function(milliseconds) {
        if (oTimedoutPopup.initialized && oTimedoutPopup.shouldShow()) {
            setTimeout(function() {
                oTimedoutPopup.openWindow();
            }, milliseconds);
        }
    },

    startIdleModalPopup: function(iSeconds) {
        if (oTimedoutPopup.initialized && oTimedoutPopup.shouldShow()) {
            this.countDown();

            $(oTimedoutPopup.events).each(function() {
                $(this[0]).bind(this[1], function() {
                    oTimedoutPopup.resetCounter(iSeconds);
                });
            });
        }
    }

};

$(document).ready(function() {
    $("#contactUsPopUpCloseButton").click(function() {
        $("#contactUsPopUpContent").slideToggle('slow', function() {
            $("#contactUsPopUp").slideToggle('slow');
        });
    });
	
	$("#pageContainer").dblclick(function() {
        $("#contactUsPopUpContent").hide();
        $("#contactUsPopUp").hide();
    });
});

