/*
 * main.js v 1.0.0
 *
 */

//Hint input, using title attribute
//Pone en gris el title de un input dentro del control
jQuery.fn.hint = function () {
	  return this.each(function (){
	    var t = jQuery(this); 
	    var title = t.attr('title'); 
	    if (title) { 
	      // on blur, set value to title attr if text is blank
	      t.blur(function (){
	        if (t.val() == '') {
	          t.val(title);
	          t.addClass('inline_hint');
	        }
	      });
	      // on focus, set value to blank if current value matches title attr
	      t.focus(function (){
	        if (t.val() == title) {
	          t.val('');
	          t.removeClass('inline_hint');
	        }
	      });

	      // clear the pre-defined text when form is submitted
	      t.parents('form:first()').submit(function(){
	          if (t.val() == title) {
	              t.val('');
	              t.removeClass('inline_hint');
	          }
	      });

	      // now change all inputs to title
	      t.blur();
	    }
	  });
	};
	
//--- Resetear un form ---//	
/*jQuery.fn.reset = function () {
	$(this).each (function() { 
		//console.log(this);
		formName = this.name;
		document.formName.reset();
		//this.reset(); 
	});
};*/
$.fn.extend({  
	reset: function() {  
	return this.each(function() {  
		$(this).is('form') && this.reset();  
	});  
	}  
});	
	
//---- Despliegue de mensajes  (ini) ---- 
function display_msj(msjType, msj){
	//if(msjType == "error" || msjType == "info" || msjType == "warning" || msjType == "success"){
    $("#msgBox").removeClass()
    			.addClass(msjType)
    			.html(msj)
    			.append(' <span class="close">(x)</a>')
    			.fadeIn("slow");
	//}
	return false;
}

$("#msgBox").click(function(e){
	if($(e.target).is(".close")){
		$("#msgBox").fadeOut("slow").html();
	}
}); 

function checkEmail(value)
{	
	var Email = value;
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (Email.length == 0 )
	{
		//alert("You must enter a valid e-mail address.");
		return false;
	}
	
	if (filter.test(Email))
	{
		return true;
	}
	else
	{
		//alert("You must enter a valid e-mail address.");
		return false;
	}
}

function ppage_refresh ()
{	
	parent.location.href = parent.location.href;
}

$.fn.vAlign = function() {
	return this.each(function(i){
	$(this).css('position','absolute');
	wWidth = $(window).width();
	objWidth = $(this).width();
	$(this).css('left',wWidth / 2 - objWidth / 2);
	$(this).css('position','absolute');

	vScroll = $(window).scrollTop();
	wHeight = $(window).height();
	objHeight = $(this).height();
	$(this).css('top', wHeight / 2 + vScroll - objHeight / 2);
	});
};


$("#lightbox-chica-container").hide();
//$("#wrapper_box").hide();

function hideMessage(divid) {
	$("#"+divid).fadeOut("slow");
}

function displayAlert(message,coordY) {
	var alertMessage = $('#alert');
	if(alertMessage.attr("id")) {
		if(message) {
			alertMessage.html(message);
		}
		
		var alertContent = alertMessage.html();
		if(alertContent.length)
		{
			var alerttimer = window.setTimeout(function () {
				alertMessage.trigger('click');
			}, 3000);
			alertMessage.animate({height: alertMessage.css('line-height') || '50px'}, 200)
			.click(function () {
				window.clearTimeout(alerttimer);
				alertMessage.animate({height: '0'}, 200);
			});
		}
	}
}

function showAlert(message) {
	var alertMessage = $('#alert');
	if(message) {
		alertMessage.html(message);
	}
	
	alertMessage.animate({height: alertMessage.css('line-height') || '50px'}, 200);
}

function hideAlert() {
	var alertMessage = $('#alert');
	alertMessage.animate({height: '0'}, 200);
}

$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
};

$('.qTip').each(function()
{
	$(this).qtip({
		content: {
        text: false // Use each elements title attribute
     	},
		style: 'green'
	});
});

$('.hint').each(function()
{
	$(this).hint();
});

$(window).scroll(function() {
    $('#alert').css('top', $(this).scrollTop() + "px");
});

function replaceAll( text, busca, reemplaza ){  
	while (text.toString().indexOf(busca) != -1){  
		text = text.toString().replace(busca,reemplaza);  
	}  
	return text;  
}

function setActualChars(textid, limit, infofield) {
	if(typeof textid == 'string') {
		var textObj = $('#'+textid);
	} else {
		var textObj = textid;
	}
	if(textObj) {
		var text = textObj.val();
		if(text || text == "") {
			var textlength = text.length;
			$('#' + infofield).html(limit-textlength);
		}
	}
}

function limitChars(textid, limit, infofield) {
	if(typeof textid == 'string') {
		var textObj = $('#'+textid);
	} else {
		var textObj = textid;
	}
	if(textObj) {
		var text = textObj.val();
		if(typeof infofield == 'string') {
			var infoFieldObj = $('#' + infofield);
		} else {
			var infoFieldObj = infofield;
		}
		if(text) {
			var textlength = text.length;
			if(textlength >= limit) {
				textObj.val(text.substr(0,limit));
				infoFieldObj.html('0');
				return false;
			} else {
				infoFieldObj.html(limit-textlength);
				return true; 
			}
		}
	}
}

$().ready(function() {	
	displayAlert();
	
	$(function() {
	    $("#log-in-link").click(function() {
	        $("#log-in").toggle();
	        $("#log-in-link").toggleClass("active-login");

	    });
	    $("#cancel-log-in").click(function() {
	        $("#sign-up").toggle();
	        $("#log-in").toggle();

	    });

	});

	$(".r-over img").hover(
	 function()
	 {
	      this.src = this.src.replace("-up","-dn");
	     },
	     function()
	     {
	      this.src = this.src.replace("-dn","-up");
	     }
	);
});