var LoginLayer = Class.create (
{
	moving:false,
	visible:false,
	layerId:'userLoginLayer',
    fbLoginLayerLoaded: false,
	initialize: function()
	{

	},
	show : function (returnBack)
	{
		
		if (!$(this.layerId)) return;
		
		if (registerLayer.visible)
		{
			registerLayer.callback = function(){loginLayer.show()};
			registerLayer.hide();
			return;
		} 
		
		
		if (this.visible) return;	

		this.visible = true;
		this.moving = true;

		if(returnBack)
		{
			$('returnBack').value = 1;
		}
	
	/*
	//schovanie active-x komponentov v IE6, aby neboli stale zobrazene
	isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
	if (isIE6)
	{
		$$('#product_list_filter select').each(function(e) {e.style.visibility='hidden';});	
	}
	*/
	if ($(this.layerId).hasClassName('error'))  $(this.layerId).removeClassName("error");
	$('emailLoginInput').value = '';
	$('passwordLoginInput').value = ''; 
	$('autoLoginCb2').checked = false;
	$('loginErrorMessage').hide();
	
	Effect.SlideDown(this.layerId,{ duration: 0.4, afterFinish: function() {
				  this.moving = false;				
				  }.bind(this)});
	
	
	},
	hide : function ()
	{
		if (!this.visible) return;
		this.moving = true;
		
		$('returnBack').value = 0;
		
		Effect.SlideUp(this.layerId,{ duration: 0.4, afterFinish: function() {
					  this.visible = false;	
					  this.moving = false;	
						 /* //zobrazenie active-x komponentov v IE6, aby neboli stale zobrazene
						  isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
						  if (isIE6)
						  {
								$$('#product_list_filter select').each(function(e) {e.style.visibility='visible';});
						  }*/
					 this.callback();	  
					  }.bind(this)});
	
	
	},
	toogle :function (returnBack, fbUserLoginAjax)
	{
		if (!this.moving && !this.visible)
		{
            if(fbUserLoginAjax && !this.fbLoginLayerLoaded)
            {
                ajax("/facebook/login-layer", null, "get",  this.onSuccessFbLoginLayer.bind(this));
            }
			this.show(returnBack);
		}
		if (!this.moving && this.visible)
		{
			this.hide();
		}
	},

    onSuccessFbLoginLayer: function(transport)
    {
        this.fbLoginLayerLoaded = true;
       $('fbUserLogin').update(transport.responseText);
    },


   	callback: function()
	{
		
	},
	submit:function(form)
	{
		var params = $(form).serialize(true); 		
		ajax("/prihlasenie", params, "post",  this.onSuccessSubmit.bind(this));
	},
	onSuccessSubmit:function(transport)
	{
		var data = transport.responseJSON; 
		if (data['isLogged'])
		{
			if($F('returnBack'))
			{
				window.location = window.location;
				return;
			}
			window.location = data['rurl'];
		}
		else
		{
			if (!$(this.layerId).hasClassName('error'))  $(this.layerId).addClassName("error");
			$('loginErrorMessage').show();
			
			//if (!$('productReviewLogin').hasClassName('error'))  $('productReviewLogin').addClassName("error");
			
		}
	
	} 
	
});


var RegisterLayer = Class.create (
{
	moving:false,
	visible:false,
	layerId:'userRegistrationLayer',
	initialize: function()
	{

	},
	show : function ()
	{
		
		if (loginLayer.visible)
		{
			loginLayer.callback = function(){registerLayer.show()};
			loginLayer.hide();
			return;
		} 
		
		
		if (this.visible) return;	

		this.visible = true;
		this.moving = true;

	
	/*
	//schovanie active-x komponentov v IE6, aby neboli stale zobrazene
	isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
	if (isIE6)
	{
		$$('#product_list_filter select').each(function(e) {e.style.visibility='hidden';});	
	}
	*/
	Effect.SlideDown(this.layerId,{ duration: 0.4, afterFinish: function() {
				  this.moving = false;				
				  }.bind(this)});
	
	
	},
	hide : function ()
	{
		if (!this.visible) return;
		this.moving = true;
		
		Effect.SlideUp(this.layerId,{ duration: 0.4, afterFinish: function() {
					  this.visible = false;	
					  this.moving = false;	
						 /* //zobrazenie active-x komponentov v IE6, aby neboli stale zobrazene
						  isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
						  if (isIE6)
						  {
								$$('#product_list_filter select').each(function(e) {e.style.visibility='visible';});
						  }*/
					 this.callback();	  
					  }.bind(this)});
	
	
	},
	toogle :function ()
	{
		if (!this.moving && !this.visible)
		{
			this.show();
		}
		if (!this.moving && this.visible)
		{
			this.hide();
		}
	},
   	callback: function()
	{
		
	},
	submit:function(form)
	{
		var params = $(form).serialize(true); 		
		ajax("/registracia", params, "post",  this.onSuccessSubmit.bind(this));
	},
	onSuccessSubmit:function(transport)
	{
		var data = transport.responseJSON; 
		if (data['registrationDone'])
		{
			window.location = data['rurl'];

		}
		else
		{
			$(this.layerId).update(data['registrationLayer'])
			
			
		}
	
	} 
	
});


var loginLayer;
var registerLayer;
Event.observe(window, 'load', function() 
	{
		registerLayer = new RegisterLayer();
		loginLayer = new LoginLayer();
	}
);
