var CCnfDialog = Class.create();

CCnfDialog.prototype = {

	initialize: function( id )
	{
		this.cnfName				= id;
		this.cnfElement				= null;
		this.cnfContainer			= '<div id="cnf-container" style="display:none"><div class="top"><div class="tips_tl">&nbsp;</div><div class="tips_tb">&nbsp;</div><div class="tips_tr">&nbsp;</div></div><div class="bd"><div class="inner"><div>&nbsp;</div><div class="Btns">&nbsp;</div></div></div><div style="clear:both"></div><div class="bottom"><div class="tips_bl">&nbsp;</div><div class="tips_bb">&nbsp;</div><div class="tips_br">&nbsp;</div></div></div>';
		this.cnfConfirmBtns			= '<a href="#" rel="ok" class="ButtonOk"><span>Ok</span></a><a href="#" rel="cancel" class="ButtonCancel"><span>Cancel</span></a>';
		this.cnfAlertBtns			= '<a href="#" rel="ok" class="ButtonOk"><span>Ok</span></a>';
	},

	displayDialog: function ( el, X, Y, options )
	{
		this.cnfElement = el;

		this.stylePosObj = {
			display:'block',
			top:Y+'px',
			left:X+'px'
		}

		if ( !$(this.cnfName) )
		{
			var parentContainer = $(this.cnfElement).up('#all');
			new Insertion.Bottom ( parentContainer, this.cnfContainer );
			$(this.cnfName).setStyle( this.stylePosObj );
		}

		if ( options && options.title )
			$(this.cnfName).down('.inner>div').update(options.title);
		else
			$(this.cnfName).down('.inner>div').update(el.title);

		var rel = '';
		if ( options && options.rel )
			rel = options.rel;
		else
			rel = el.rel

		var Btns = $(this.cnfName).down('.inner>div.Btns');
		Btns.removeClassName('alertBtns');
		Btns.removeClassName('cnfBtns');

		switch ( rel )
		{
			case 'confirm':
				Btns.update(this.cnfConfirmBtns);
				Btns.addClassName('cnfBtns');
			break;

			case 'alert':
				Btns.update(this.cnfAlertBtns);
				Btns.addClassName('alertBtns');
			break;

			default:
				this.hideCnf();
		}

		$(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('a[rel="ok"]'));
		if ( okButton )
		{
			okButton.onclick = function() { return false; }
			okButton.observe ( 'click', ok );
		}

		var cancelButton = ($(this.cnfName).down('a[rel="cancel"]'));
		if ( cancelButton )
		{
			cancelButton.onclick = function() { return false }
			cancelButton.observe ( 'click', cancel );
		}
	},

	OkCnf: function()
	{
		this.hideCnf();
		return false;
	},

	CancelCnf: function()
	{
		this.hideCnf();
		return false;
	},

	hideCnf: function ()
	{
		if ( $(this.cnfName) && ($(this.cnfName).getStyle('display') == 'block') )
		{
			$(this.cnfName).setStyle( { display:'none' } );
		}
	},

	hideCnfTimerSet: function()
	{
		this.timer = setTimeout(this.hideCnf.bind(this), this.hideTimeOut);
	},

	hideCnfTimerReset: function()
	{
		if ( this.timer ) clearTimeout(this.timer);
	}
}