/**
 * DonPiso Expansion
 * -----------------------------------------------------------------
 *
 * author:           Alcaraz & Estevez Consultores
 * projectId:        
 * version:          0.5
 * creation date:    26-08-2009
 * description:      
 * notes:            
 *
 */



$(document).ready(function()
{
	/**
	 * generic lib initialisation
	 */
	ALCARAZ.htmlUtils.initExternalLinks();
	ALCARAZ.forms.initDefaultValues();
	ALCARAZ.misc.initFullLinks();
	ALCARAZ.misc.initWidgets();

	if( $.browser.msie )
	{
		ALCARAZ.cssFixes.fixIESelectors();
		ALCARAZ.cssFixes.fixLegends();
	}

	if( $.browser.mozilla )
	{
		ALCARAZ.cssFixes.fixLegends();
	}
	
	
	// home banners link expansion
	$("body.home .banner").each(function(i)
	{
		var href = $(this).find("a").eq(0).attr("href");
		
		$(this).css("cursor", "pointer").bind("click", function()
		{
			window.location = href;
		});
	});
	
	
	$('.banner-magazine').css("cursor", "pointer").unbind("click").bind("click", function()
	{
		ALCARAZ.misc.launchMagazine( $(this).find("a").eq(0).attr("href") );
		return false;
	});
	
	
	
	
	/**
 	 *  Forms:
 	 * ------------------------------------
 	 *  
 	 *
 	 ***********************************************************************************************************************************/
	var registerForm = {
		
		returnVal    : true,
		validatorObj : new ALCARAZ.forms.Validator(),
		formId       : "#registerForm",
		
		init : function()
		{
			var _this = this;
			
			$(this.formId).submit(function()
			{
				if(!registerForm.validate())
				{
					alert("Algunos de los datos introducidos en el formulario no son válidos:\n" + _this.validatorObj.errors.join("\n"));
					return false;
				}
			});
		},
		
		validate : function()
		{
			this.validatorObj.init();
			
			var _this = this;
			
			$("input", $(this.formId)).each(function(i, n)
			{
				$(n).removeClass("error");
			});
			
			$.each($("#fNombre, #fApellidos, #fEmail, #fTelefono"), function(i, n)
			{
				_this.validatorObj.checkRequired($(n));
			});
			
			if( $("#fTelefono").val() != $("#fTelefono").data("val") )
			{
				this.validatorObj.checkNumeric($("#fTelefono"));
			}
			
			if( $("#fEmail").val() != $("#fEmail").data("val") )
			{
				this.validatorObj.checkValidMail($("#fEmail"));
			}
			
			this.validatorObj.checkMinLength([$("#fPassword")], 8);
			
			for(var i = 0; i<this.validatorObj.erroneousFields.length; i++)
			{
				this.validatorObj.erroneousFields[i].addClass("error");
			}
			
			return this.validatorObj.returnVal;
		}
	};
	
	if( $("body").hasClass("register") )
	{
		registerForm.init();
	}
	
	
	
	
	var loginForm = {
	
		returnVal    : true,
		validatorObj : new ALCARAZ.forms.Validator(),
		formId       : "#loginBox",
		
		init : function()
		{
			var _this = this;
			
			$(this.formId).submit(function()
			{
				if(!loginForm.validate())
				{
					alert("Algunos de los datos introducidos en el formulario no son válidos:\n" + _this.validatorObj.errors.join("\n"));
					return false;
				}
			});
		},

		validate : function()
		{
			this.validatorObj.init();

			var _this = this;
			
			$("input", $(this.formId)).each(function(i, n)
			{
				$(n).removeClass("error");
			});

			$.each($("#login, #password"), function(i, n)
			{
				_this.validatorObj.checkRequired($(n));
			});

			if($("#login").val() != "")
			{
				_this.validatorObj.checkValidMail( $("#login") );
			}

			for(var i = 0; i<this.validatorObj.erroneousFields.length; i++)
			{
				this.validatorObj.erroneousFields[i].addClass("error");
			}
	
			return this.validatorObj.returnVal;
		}
	};
	
	loginForm.init();
	
	
	
	
	
	
	
	/**
 	 *  Effects:
 	 * ------------------------------------
 	 *  rounded corners, dropshadows, ...
 	 *
 	 ***********************************************************************************************************************************/	
	
	/**
	 *  rounded corners
	 * ------------------------------------
	 */
	ALCARAZ.misc.setCssCorners("#siteNav", "top", "6px");
	ALCARAZ.misc.setCssCorners("#siteNav", "bottom", "5px");
	
	ALCARAZ.misc.setCssCorners(".banner", "top", "8px");
	ALCARAZ.misc.setCssCorners(".banner", "bottom", "8px");
	
	ALCARAZ.misc.setCssCorners("#contents-wrapper", "top", "8px");
	
	ALCARAZ.misc.setCssCorners("#banner-intro .btn", "top", "6px");
	ALCARAZ.misc.setCssCorners("#banner-intro .btn", "bottom", "6px");
	
	ALCARAZ.misc.setCssCorners("#loginBox", "bottom", "15px");
	
	
	/**
	 *  dropshadows
	 * ------------------------------------
	 *
	 */
	
	
	
	
	
	/**
	 *  other
	 * ------------------------------------
	 *
	 */
	
	/* pretty file inputs */
	$("input[type=file].pretty").each(function(i)
	{
		$(this)
			.wrap('<div class="fileWrapper"></div>')
			.parent(".fileWrapper")
			.append('<input type="text" class="fakeFile" value="' + $("label[for=" + $(this).attr("id") + "]").text() + '" />')
			.append('<div><em>examinar</em><span /></div>')
			.end()
			.css("opacity", "0")
			.bind("change", function()
			{
				$(this).next("input.fakeFile").val( $(this).val() );
			});
	});
	
	
	
	
	/**
	 * CSS fixes:
	 * ------------------------------------
	 * workarounds for some css related problems, especially on IE
	 * the most of this problems with Internet Exploder are fixed
	 * via additional css files linked with conditional comments,
	 * but some other problems (like some selectors) can not be fixed
	 * only with CSS or would be a PITA to fix
	 * (if you don't know what PITA means, look here... http://www.urbandictionary.com/define.php?term=PITA)
	 *
	 ***********************************************************************************************************************************/
	
	if($.browser.mozilla)
	{
		
	}
	
	if($.browser.safari)
	{
		$(".magazines-container li span").css("top", ".5em");
	}
	
	
	if($.browser.msie)
	{
		if($.browser.version < 8)
		{
		}
		
		if($.browser.version < 7)
		{
		}
	}	
	
});

var userIsLogged = false;
