﻿function ModalWindow(handler)
{
    this._modalHandler  = handler;
    this._openHandler   = null;
    this._closeHandler  = null;
    
    this._onShown         = null;
}

ModalWindow.prototype =
{
    get_modalHandler: function(){return this._modalHandler;}
    ,set_modalHandler: function(value){this._modalHandler = value;}
    
    ,get_openHandler: function(value){return this._openHandler;}
    ,set_openHandler: function(value){this._openHandler = value;}
    
    ,get_closeHandler: function(){return this._closeHandler;}
    ,set_closeHandler: function(value){this._closeHandler = value;}
    
    ,get_onShown: function(){return this._onShown;}
    ,set_onShown: function(value){this._onShown = value;}
    
    ,get_handlers: function()
    {
        return {open: this._openHandler, close: this._closeHandler}
    }
    
    ,set_handlers: function(value)
    {
        if(value.open != null)
            this._openHandler = value.open;
        
        if(value.close != null)
            this._closeHandler = value.close;
    }
    
    ,show: function()
    {
	    var p = $find(this.get_modalHandler());
	    if(p != null)
	    {
	        var handler = this.get_openHandler();
	        var close = this.get_closeHandler();
	        var name = this.get_modalHandler().replace('Behavior', '');
	        var shown = this.get_onShown();
	        
		    p.add_showing(function()
		    {
		        handler();
		        $('#popup_container_inner').empty();
            });
            
            p.add_shown(function()
            {
                if(shown != null)
                    shown();
                
                $('#popup_container_inner').remove();
                
                //if(close != null)
                    $('#' + p.get_PopupControlID()).find('div[id$=Loader]').show();

                $.unblockUI();
            });
            
		    p.show();
	    }
    }
    
    ,layout: function()
    {
        var p = $find(this.get_modalHandler());
        if(p != null)
            p._layout();
    }
    
    ,hide: function()
    {
        var p = $find(this.get_modalHandler());
        if(p != null)
	    {
	        var handler = this.get_closeHandler();
	        var name = this.get_modalHandler().replace('Behavior', '');
	        
		    p.add_hiding(function()
		    {
                $('#' + name + '_hidden').remove();
                
                // Close all open tooltips
                $('span[id$=\'SmartC\'][open=\'true\']').parent().find('img[tooltipclose=yes]').click();
                
                if(handler != null)
					handler();
            });
            
		    p.hide();
	    }
    }
    
    ,hideLoader: function()
    {
        var p = $find(this.get_modalHandler());
        $('#' + p.get_PopupControlID()).find('div[id$=Loader]').hide();
    }
}

function _hideReferAndWin()
{
    var checked = $('#dontshow').is('[checked=true]');
    var COOKIE_NAME = '.ReferAndWin';
    var shortexpoptions = { path: '/', expires: 1 };
    var longexpoptions = { path: '/', expires: 36500 };
    
    if(checked)
    {
        $.cookie(COOKIE_NAME, '1', longexpoptions);
    }
    else
    {
        $.cookie(COOKIE_NAME, '1', shortexpoptions);
    }
}

function _hideVivaMailbox()
{
    var checked = $('#vivabox_dontshow').is('[checked=true]');
    var COOKIE_NAME = '.UserIsAwareOfVivaMailbox';
    var shortexpoptions = { path: '/', expires: 1 };
    var longexpoptions = { path: '/', expires: 36500 };
    
    if(checked)
    {
        $.cookie(COOKIE_NAME, '1', longexpoptions);
    }
    else
    {
        $.cookie(COOKIE_NAME, '1', shortexpoptions);
    }
}