/**
 * Copyright (C) 2009 Drumbit - www.drumbit.com
 * 
 * This file is part of ExperienceGifts
 *
 * ExperienceGifts is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ExperienceGifts is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ExperienceGifts.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
var MERCADO_URL;
$(document).ready(function(){
	$('#mercadonav').click(function (){
		MERCADO_URL = $(this).attr('href');
		loginCheck('Sección exclusiva', function(){
			window.location =  MERCADO_URL;
		});
		return false;
	});
})

if (LANG == 'en')
{
	var MESSAGES = { 
	  'required' : 'required',
	  'email' : 'e-mail address not valid',
	  'maxlength' :$.format('too long (max:{0})'),
	  'equalTo' : 'passwords don\'t match'
	}
}
else
{
	var MESSAGES = { 
	  'required' : 'Información requerida',
	  'email' : 'La dirección ingresada no es válida',
	  'maxlength' :$.format('El texto ingresado es muy largo (max:{0})'),
	  'equalTo' : 'Las contraseñas ingresadas no coinciden'
	};
}

function loginCheck(title, successHandler)
{
	$('.overlayBox .top .title').text(title);
	$.ajax({
		type: 'POST',
		url: BASE_URL + 'index/login-check',
		dataType: 'json',
		success: function(res){
			if(res.status == 1)
			{
				successHandler();
			}
			else
			{
				//Login
				$.blockUI({ 
			      	message: $('#loginbox'),
			      	css:  { 
					  'backgroundColor': 'transparent', 
			  	      'textAlign': 'left',
			  	      'border': 0,
			  	       'padding':0,
			  	       'width' : '626px',
			  	       'top': ($(window).height() - $('#loginbox').height()) /2 + 'px',
			  	       'cursor': 'default',
			  	       'left': ($(window).width() - 626) /2 + 'px'
			      	},
			      	fadeIn : 0,
			      	focusInput: true
			      });
				$('#login').validate({
					rules: {
					  'email': {
					    'required': true,
					    'maxlength': 50,
					    'email': true
					  },
					  'contrasena': {
						'required': true,
						'maxlength': 50
					  }
				  	},
				  	messages: {
				  	 'email': {
					    'required': MESSAGES.required,
					    'maxlength': MESSAGES.maxlength,
					    'email': MESSAGES.email
					  },
					  'contrasena': {
						'required': MESSAGES.required,
						'maxlength': MESSAGES.maxlength
					  }
				  	},
				  	errorElement: "li",
				    wrapper: "ul",
				  	submitHandler:function(){
				  		$.ajax({
				  			type: 'post',
				  			url: BASE_URL + 'index/login',
				  			data: $('#login').serialize(),
				  			dataType: 'json',
				  			success: function(res){
				  				if(res.status == 1)
				  				{
				  					successHandler()
				  				}
				  				else
				  				{
				  					alert('e-email o contraseña incorrectos')
				  				}
				  			}
				  		});
				  	}
				});
			}
		}
	});
}


function register()
{
	$.blockUI({ 
      	message: $('#registerbox'),
      	css:  { 
		  'backgroundColor': 'transparent', 
  	      'textAlign': 'left',
  	      'border': 0,
  	       'padding':0,
  	       'width' : '626px',
  	       'top': ($(window).height() - $('#registerbox').height()) /2 + 'px',
  	       'cursor': 'default',
  	       'left': ($(window).width() - 626) /2 + 'px'
      	},
      	fadeIn : 0,
      	focusInput: true
      });
	
	$('#register').validate({
		rules: {
		  'email': {
		    'required': true,
		    'maxlength': 50,
		    'email': true
		  },
		  'contrasena': {
			'required': true,
			'maxlength': 50
		  },
		  'contrasena_confirm': {
			'required': true,
			'equalTo': "#contrasena"
		  }
	  	},
	  	messages: {
	  	 'email': {
		    'required': MESSAGES.required,
		    'maxlength': MESSAGES.maxlength,
		    'email': MESSAGES.email
		  },
		  'contrasena': {
			'required': MESSAGES.required,
			'maxlength': MESSAGES.maxlength
		  },
		  'contrasena_confirm': {
			'required':  MESSAGES.required,
			'equalTo':  MESSAGES.equalTo
		  }
	  	},
	  	errorElement: "li",
	    wrapper: "ul",
	  	submitHandler:function(){
	  		$.ajax({
	  			type: 'POST',
	  			url: BASE_URL + 'index/register',
	  			data: $('#register').serialize(),
	  			dataType: 'json',
	  			success: function(res){
	  				if(res.status == 1)
	  				{
	  					$.blockUI({ 
	  				      	message: $('#registerokbox'),
	  				      	css:  { 
	  						  'backgroundColor': 'transparent', 
	  				  	      'textAlign': 'left',
	  				  	      'border': 0,
	  				  	       'padding':0,
	  				  	       'width' : '626px',
	  				  	       'top': ($(window).height() - $('#registerokbox').height()) /2 + 'px',
	  				  	       'cursor': 'default',
	  				  	       'left': ($(window).width() - 626) /2 + 'px'
	  				      	},
	  				      	fadeIn : 0,
	  				      	focusInput: true
	  				      });
	  				}
	  				else
	  				{
	  					$.blockUI({ 
	  				      	message: $('#registerfailbox'),
	  				      	css:  { 
	  						  'backgroundColor': 'transparent', 
	  				  	      'textAlign': 'left',
	  				  	      'border': 0,
	  				  	       'padding':0,
	  				  	       'width' : '626px',
	  				  	       'top': ($(window).height() - $('#registerfailbox').height()) /2 + 'px',
	  				  	       'cursor': 'default',
	  				  	       'left': ($(window).width() - 626) /2 + 'px'
	  				      	},
	  				      	fadeIn : 0,
	  				      	focusInput: true
	  				      });
	  					
	  				}
	  			}
	  		});
	  	}
	});
}

function dismiss()
{
	$('#login :input, #register :input, #retrieve :input').val('');
	$.unblockUI({fadeOut : 0}); 
}

function retrieve()
{
	$.blockUI({ 
      	message: $('#retrievebox'),
      	css:  { 
		  'backgroundColor': 'transparent', 
  	      'textAlign': 'left',
  	      'border': 0,
  	       'padding':0,
  	       'width' : '626px',
  	       'cursor': 'default',
  	       'top': ($(window).height() - $('#retrievebox').height()) /2 + 'px',
  	       'left': ($(window).width() - 626) /2 + 'px'
      	},
      	fadeIn : 0,
      	focusInput: true
      });
	
	$('#retrieve').validate({
		rules: {
		  'email': {
		    'required': true,
		    'maxlength': 50,
		    'email': true
		  }
	  	},
	  	messages: {
	  	 'email': {
		    'required': MESSAGES.required,
		    'maxlength': MESSAGES.maxlength,
		    'email': MESSAGES.email
		  }
	  	},
	  	errorElement: "li",
	    wrapper: "ul",
	  	submitHandler:function(){
	  		$.ajax({
	  			type: 'POST',
	  			url: BASE_URL + 'index/retrieve',
	  			data: $('#retrieve').serialize(),
	  			dataType: 'json',
	  			success: function(res){
	  				if(res.status == 1)
	  				{
	  					$.blockUI({ 
	  				      	message: $('#retrieveokbox'),
	  				      	css:  { 
	  						  'backgroundColor': 'transparent', 
	  				  	      'textAlign': 'left',
	  				  	      'border': 0,
	  				  	       'padding':0,
	  				  	       'width' : '626px',
	  				  	       'top': ($(window).height() - $('#retrieveokbox').height()) /2 + 'px',
	  				  	       'cursor': 'default',
	  				  	       'left': ($(window).width() - 626) /2 + 'px'
	  				      	},
	  				      	fadeIn : 0,
	  				      	focusInput: true
	  				      });
	  				}
	  				else
	  				{
	  					$.blockUI({ 
	  				      	message: $('#retrievefailbox'),
	  				      	css:  { 
	  						  'backgroundColor': 'transparent', 
	  				  	      'textAlign': 'left',
	  				  	      'border': 0,
	  				  	       'padding':0,
	  				  	       'width' : '626px',
	  				  	       'top': ($(window).height() - $('#retrievefailbox').height()) /2 + 'px',
	  				  	       'cursor': 'default',
	  				  	       'left': ($(window).width() - 626) /2 + 'px'
	  				      	},
	  				      	fadeIn : 0,
	  				      	focusInput: true
	  				      });
	  				}
	  			}
	  		});
	  	}
	});
}
