var LOC_CNF_AJAX_TIMEOUT = 'Server is overloaded. Please try again latter.';

var CCnfDialog = Class.create();

CCnfDialog.prototype = {

	initialize: function( id )
	{
		this.cnfName				= id;
		this.cnfElement				= null;
		this.cancelButtonTitle      = '';
		this.okButtonTitle          = '';
		this.okAlertButtonTitle     = '';
		this.cnfContainer			= '<div id="'+id+'" style="position:absolute;z-index:51"><div style="width: 300px;" class="popup"><div class="borderLT"></div><div class="borderRT"></div><div class="borderLB"></div><div class="borderRB"></div><div class="borderTop"></div><div class="borderL width300"><div class="borderR"><div class="popUpContent"><div class="text"></div><div class="Btns"></div></div></div></div><div class="borderBot"></div></div></div>';
		this.cnfConfirmBtns         = '';
		this.cnfAlertBtns           = '';
		this.cnfInitialized         = false;
	},

    initCnfButtons: function()
	{
		this.cnfConfirmBtns         = '<span class="InputButton InputButtonSmModelA"><input type="button" class="isHoverisHover" value="'+this.okButtonTitle+'" name="ok" alt="isHover" /></span><span class="InputButton InputButtonSmModelA"><input type="button" class="isHoverisHover" value="'+this.cancelButtonTitle+'" name="cancel" alt="isHover" /></span>';
        this.cnfAlertBtns           = '<span class="InputButton InputButtonSmModelA"><input type="submit" class="isHoverisHover" value="'+this.okAlertButtonTitle+'" name="ok" alt="isHover" /></span>';
	},
	
	displayDialog: function ( el, X, Y, options )
	{
		
		if ( this.cnfInitialized ) return;
		
		this.initCnfButtons();
		
		this.cnfInitialized         = true;
		
		this.cnfElement = el;

		this.stylePosObj = {
			display:'block',
			top:Y+'px',
			left:X+'px'
		}

		if ( !$(this.cnfName) )
		{
			var parentContainer = $(this.cnfElement).up('body');
			new Insertion.Bottom ( parentContainer, this.cnfContainer );
			$(this.cnfName).setStyle( this.stylePosObj );
		}

		if ( options && options.title )
			$(this.cnfName).down('.popUpContent div.text').update(options.title);
		else
			$(this.cnfName).down('.popUpContent div.text').update(el.title);

		var rel = '';
		if ( options && options.rel )
			rel = options.rel;
		else
			rel = el.rel

        
		var Btns = $(this.cnfName).down('.popUpContent div.Btns');
		
		if ( Btns )
		{
			Btns.removeClassName('alertBtns');
            Btns.removeClassName('cnfBtns');
    
            switch ( rel )
            {
                case 'confirm':
				{
					Btns.update(this.cnfConfirmBtns);
                    Btns.addClassName('cnfBtns');
					Btns.removeClassName('signinBtns');
                    break;
				}
    
                case 'alert':
				{
					Btns.update(this.cnfAlertBtns);
                    Btns.addClassName('alertBtns');
					Btns.removeClassName('signinBtns');
					break;
				}
                
				case 'signin':
                {
                    Btns.update(this.cnfConfirmBtns);
                    Btns.addClassName('signinBtns');
                    break;
                }
				
                case 'dialog':
				{
					Btns.update(this.cnfConfirmBtns);
                    Btns.addClassName('cnfBtns');
					Btns.removeClassName('signinBtns');
					break;
				}
    
                default:
                    this.hideCnf();
            }
			
			var inputs = Btns.getElementsBySelector('input');
			
			inputs.invoke('observe', 'mouseover', function(evt) { 
			 var tmp = this.className;
			 this.className = this.alt;
			 this.alt = tmp; 
			});
			
			inputs.invoke('observe', 'mouseout', function(evt) { 
             var tmp = this.className;
             this.className = this.alt;
             this.alt = tmp; 
            });
		}
			
		$(this.cnfName).setStyle ( this.stylePosObj );
		this.initCnfActions(options);

		return false;
	},

	initCnfActions: function ( options )
	{
		var ok 		= ( options && options.ok ) ? options.ok:this.OkCnf.bind(this);
		var cancel 	= ( options && options.cancel ) ? options.cancel:this.CancelCnf.bind(this);

		var okButton = ($(this.cnfName).down('input[name="ok"]'));
		if ( okButton )
		{
			okButton.onclick = function() { return false; }
			okButton.observe ( 'click', ok );
		}

		var cancelButton = ($(this.cnfName).down('input[name="cancel"]'));
		if ( cancelButton )
		{
			cancelButton.onclick = function() { return false }
			cancelButton.observe ( 'click', cancel );
		}
		
		$(this.cnfName).getElementsBySelector('form input[type="text"]').each(function(el) {
            el.focus();
			return; 
        });
	},

	OkCnf: function()
	{
		this.hideCnf();
		return false;
	},

	CancelCnf: function()
	{
		this.hideCnf();
		return false;
	},

	hideCnf: function ()
	{
		if ( $(this.cnfName) && ($(this.cnfName).getStyle('display') == 'block') )
		{
			this.cnfInitialized = false;
			$(this.cnfName).setStyle( { display:'none' } );
		}
	},

	hideCnfTimerSet: function()
	{
		this.timer = setTimeout(this.hideCnf.bind(this), this.hideTimeOut);
	},

	hideCnfTimerReset: function()
	{
		if ( this.timer ) clearTimeout(this.timer);
	}
}
