var CInterface = Class.create();

CInterface.prototype = Object.extend( (new CCnfDialog('cnf-container')), {

	initialize: function()
	{
		this.loginWindowName 	= 'login-container';
		this.LoginElement 		= null;
		this.stylePosObj		= {};
		this.timer				= null;
		this.hideTimeOut 		= 300;
		this.packageForm		= 'order_package_form';
		this.snWindowName		= 'sn-container';
		this.snElement			= null;
		this.stylePosSN			= {};
		this.snElementHref		= '';
		this.trackForms			= ['food_form','track_form','food_popup_form','choose_meal_form','refresh_meal_form'];
	},

	initTopMenu: function()
	{
		var Container = $('top-menu');
		var loginBtn = Container.getElementsBySelector('li[class="bg-none"] a[class="login"]');
		if ( loginBtn )
		{
			for ( var i = 0; i < loginBtn.length; i++ )
			{
				loginBtn[i].onclick = function() { return false }
				loginBtn[i].observe('click',this.loginWindowDisplay.bindAsEventListener(this));
			}
		}
		var logoutBtn = Container.getElementsBySelector('li[class="bg-none"] a[class="logout"]');
		if ( logoutBtn && logoutBtn[0] )
		{
			logoutBtn[0].onclick = function() { return false }
			logoutBtn[0].observe('click',this.logoutWindowDisplay.bindAsEventListener(this));
		}
		var form = $(this.packageForm);
		if (form)
		{
			var securityNumber = form.getElementsBySelector('a[rel="alert"]');
			if ( securityNumber )
			{
				securityNumber.each(function(el) { el.onclick = function() { return false } } )
	            securityNumber.invoke('observe', 'click', this.securityNumberDisplay.bindAsEventListener(this));
			}
		}
		for ( var i = 0; i < this.trackForms.length; i++ )
		{
			var track_form = $(this.trackForms[i]);
			if (track_form)
			{
				var nutFacts = track_form.getElementsBySelector('a[rel="alert"]');
				if (nutFacts)
				{
					nutFacts.each(function(el) { el.onclick = function() { return false } } )
		            nutFacts.invoke('observe', 'click', this.nutFactsDisplay.bindAsEventListener(this));
				}
			}
		}
	},
	
	nutFactsDisplay: function ( evt)
	{
		this.snElement = Event.findElement(evt,'a');
		this.hideSelects('hidden');
		if ( $(this.snWindowName) )
		{
        	if (this.snElement.href != this.snElementHref)
        	{
        		if ( $(this.snWindowName).getStyle('display') == 'block' )
				{
					$(this.snWindowName).setStyle( { display:'none' } );
				}
        		this.snElementHref = this.snElement.href;
        		new Ajax.Request ( this.snElement.href, { onSuccess: this.successNutFactsWindow.bind(this) } )
        	}
        	else
        		$(this.snWindowName).setStyle( this.stylePosSN );
        }
        else
        {
        	this.snElementHref = this.snElement.href;
        	new Ajax.Request ( this.snElement.href, { onSuccess: this.successNutFactsWindow.bind(this) } );
        }
	},
	
	hideSelects: function ( visibility )
	{
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	closeNutFactsWindow: function ( evt )
	{
		$(this.snWindowName).setStyle( { display:'none' } );
		this.hideSelects('visible');
		return false;
	},
	
	successNutFactsWindow: function ( responce )
	{
		var parentContainer = this.snElement.up('#all');
		
		new Insertion.After( parentContainer, responce.responseText );

		var X = Utils.getLeftPos(this.snElement)+13;
		var Y = Utils.getTopPos(this.snElement)-330;

		this.stylePosSN = {
			display:'block',
			top:Y+'px',
			left:X+'px'
		}
		var cnf_container = $(this.snWindowName);
		if (cnf_container)
		{	
			cnf_container.setStyle( this.stylePosSN );
			var okButton = (cnf_container.down('a[rel="ok"]'));
			if ( okButton )
			{
				okButton.onclick = function() { return false; }
				//okButton.observe ( 'click', function(){cnf_container.setStyle( { display:'none' } );return false;} );
				okButton.observe ( 'click', this.closeNutFactsWindow.bindAsEventListener(this) );
			}
		}
	},
	
	securityNumberDisplay: function ( evt )
	{
		this.snElement = Event.findElement(evt,'a');
        if ( $(this.snWindowName) )
        	$(this.snWindowName).setStyle( this.stylePosSN );
        else
        	new Ajax.Request ( this.snElement.href, { onSuccess: this.successSecurityNumberWindow.bind(this) } );
	},
	
	successSecurityNumberWindow: function ( responce )
	{
		var parentContainer = this.snElement.up('#all');
		new Insertion.After( parentContainer, responce.responseText );

		var X = Utils.getLeftPos(this.snElement)-100;
		var Y = Utils.getTopPos(this.snElement)-200;

		this.stylePosSN = {
			display:'block',
			top:Y+'px',
			left:X+'px'
		}
		var cnf_container = $(this.snWindowName);
		if (cnf_container)
		{	
			cnf_container.setStyle( this.stylePosSN );
			var okButton = (cnf_container.down('a[rel="ok"]'));
			if ( okButton )
			{
				okButton.onclick = function() { return false; }
				okButton.observe ( 'click', function(){cnf_container.setStyle( { display:'none' } );return false;} );
			}
		}
	},
	
	logoutWindowDisplay: function ( evt )
	{
		var el = Event.findElement(evt,'a');
		this.displayDialog ( el, (Utils.getLeftPos(el)-300), (Utils.getTopPos(el)+30) );
		return false;
	},

	OkCnf: function()
	{
		var logoutForm = this.cnfElement.up('form#unauth');
		if ( logoutForm )
		{
			logoutForm.submit();
		}
		return false;
	},

	loginWindowDisplay: function ( evt )
	{
		this.LoginElement = Event.findElement(evt,'a');
		var X = Utils.getLeftPos(this.LoginElement);
		var Y = Utils.getTopPos(this.LoginElement);

		this.stylePosObj = {
			display:'block',
			top:(Y+62)+'px',
			left:(X-200)+'px'
		}

		if ( $(this.loginWindowName) )
		{
			$(this.loginWindowName).setStyle( this.stylePosObj );
			this.hideTimerReset();
		}
		else
		{
			this.showLoadingProgress();
			new Ajax.Request ( this.LoginElement.href, { onSuccess: this.successLoginWindow.bind(this) } )
		}
	},

	removeLoadingProgress:function()
	{
		this.LoginElement.removeClassName('LoadingProcess');
	},

	showLoadingProgress: function()
	{
		this.LoginElement.addClassName('LoadingProcess');
	},

	hide: function ()
	{
		if ( $(this.loginWindowName) && ($(this.loginWindowName).getStyle('display') == 'block') )
		{
			$(this.loginWindowName).setStyle( { display:'none' } );
		}
	},

	hideTimerSet: function()
	{
		this.timer = setTimeout(this.hide.bind(this), this.hideTimeOut);
	},

	hideTimerReset: function()
	{
		if ( this.timer ) clearTimeout(this.timer);
	},

	successLoginWindow: function ( responce )
	{
		var parentContainer = this.LoginElement.up('#top-menu');
		new Insertion.After( parentContainer, responce.responseText );
		$(this.loginWindowName).setStyle( this.stylePosObj );
		$(this.loginWindowName).observe('mouseover',this.hideTimerReset.bindAsEventListener(this));
		$(this.loginWindowName).observe('mouseout',this.hideTimerSet.bindAsEventListener(this));

		/*if ( Prototype.Browser.IE )
		{
			var loginForm = $(this.loginWindowName).getElementsByTagName('form');
			if ( loginForm && loginForm[0] )
			{
				var submitButton 	= document.createElement('input');
				submitButton.type 	= 'submit';
				submitButton.name 	= 'signin';
				submitButton.value	= 'submit';
				submitButton.style.filter = "alpha(opacity=0)";
				loginForm[0].appendChild(submitButton);

			}
		}*/
		this.removeLoadingProgress();
	}
});

var interfaceObj = new CInterface();

Event.observe(window,'load', interfaceObj.initTopMenu.bindAsEventListener(interfaceObj) );

imgs = [
'win_bb',
'win_bl',
'win_br',
'win_login_main',
'win_login_pointer',
'win_tl',
'win_tr',
'cnf-main',
'cnf-br',
'cnf-bl',
'cnf-bb',
'cnf-tb',
'cnf-tr',
'cnf-tl'
];
ii = Array();
for ( var i = 0; i < imgs.length; i++ )
{
	ii[i] = new Image();
	ii[i].src = '/images/tips_'+imgs[i]+'.png';
}