/*
	Global File By: Javi Alcala © 2011
*/
/* IE Clear type fix */
(function ($) {
    $.fn.customFadeIn = function (speed, callback) {
        $(this).fadeIn(speed, function () {
            if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined) callback();
        });
    };
    $.fn.customFadeOut = function (speed, callback) {
        $(this).fadeOut(speed, function () {
            if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined) callback();
        });
    };
    $.fn.customFadeTo = function (speed, callback) {
        $(this).fadeTo(speed, function () {
            if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined) callback();
        });
    };
    $.fn.customToggle = function (speed, callback) {
        $(this).toggle(speed, function () {
            if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined) callback();
        });
    };

})(jQuery);
$(document).ready(function() {

	// Scroll to top animation						   
	$('.scroll-top').click(function(){ 
	$('html, body').animate({scrollTop:0}, 'slow'); return false; 
	});
	
/*****[BEGIN]*******************************************
 [ Custom Function Validator             			   ]
 ******************************************************/
$.fn.validarForm = function(form,position,var1,var2,formClass) {

$(this).validator({
	lang: 'es',
	position: position, 
	offset: [var1, var2],
	messageClass: formClass,
	message: '<div><em/></div>' // em element is the arrow
});

$.tools.validator.localize("es", {
	':email'  		: 'Correo no Valido',
	':number' 		: 'Solo usa Numeros',
	'[max]'	 		: 'Valor maximo, de $1',
	'[min]'	 		: 'Valor Minimo, de $1',
	'[required]' 	: 'Campo Requerido'
});
    // Regular Expression to test whether the value is valid
    $.tools.validator.fn("[type=time]", "Fecha no Valida", function(input, value) { 
        return (/^\d\d:\d\d$/).test(value);
    });

    $.tools.validator.fn("[data-equals]", "Value not equal with the $1 field", function(input) {
        var name = input.attr("data-equals"),
        field = this.getInputs().filter("[name=" + name + "]"); 
        return input.val() === field.val() ? true : [name]; 
    });
     
    $.tools.validator.fn("[minlength]", function(input, value) {
        var min = input.attr("minlength");
        
        return value.length >= min ? true : {     
            en: "Please provide at least " +min+ " caracteres" + (min > 1 ? "s" : ""),
			es: "Por lo menos " +min+ " caracteres" + (min > 1 ? "s" : "")
        };
    });
        
    $.tools.validator.localizeFn("[type=time]", {
        en: 'Please supply a valid time',
		es: 'Tiempo no Valido'
    });
	
    return this;
}

/*****[END]*********************************************
 [ Custom Function Validator             			   ]
 ******************************************************/
 /*****[BEGIN]*******************************************
 [ Spam Image              			   				   ]
 ******************************************************/ 
    $("#img_spam").click(function(){
        $('#img_spam').attr('src','../process/spam_validate.php?r=' + Math.round(Math.random() * 10000));
    });

    $("body").bind('keyup',function(event) {
        if(event.keyCode==13){
            document.form.submit();
        }
    });

    if($ ("#hoverbody").length>0){
        hoverTagBgFromember_id("hoverbody","tr","#F1F1F1","#FFF","#FAFAFA");
    }

    if($ ("#hoverbody2").length>0){
        hoverTagBgFromember_id("hoverbody2","tr","#F1F1F1","#FFF","#FAFAFA");
    }

function hoverTagBgFromember_id(domember_id,hoverTag,color){
    var hoverElemt = document.getElementById(domember_id).getElementsByTagName(hoverTag);
    var formerlyColor = new Array();
    var len = hoverElemt.length;
    for(var i=1;i<len;i++){
        if(typeof(arguments[3]) != 'undefined' && i % 2 == 1) {
            hoverElemt[i].style.backgroundColor = arguments[3];
        }
        if(typeof(arguments[4]) != 'undefined' && i % 2 == 0) {
            hoverElemt[i].style.backgroundColor = arguments[4];
        }
        if(color != '') {
            hoverElemt[i].onmouseover = function(){
                formerlyColor[i] = this.style.backgroundColor;
                this.style.backgroundColor = color;
            }
            hoverElemt[i].onmouseout = function(){
                this.style.backgroundColor = formerlyColor[i];
            }
        }
    }
}
/*****[END]********************************************
 [ Spam Image              			   				   ]
 ******************************************************/ 
/*****[BEGIN]*******************************************
 [ Tooltip              			   				   ]
 ******************************************************/ 
	$("[title]").tooltip({
		effect: 'slide',
		predelay: 100,
		delay: 10,
		position: 'center right', // position it to the right
		offset:[5, 5]
		})
		.dynamic({bottom: { direction: 'down', bounce: true}   //made it dynamic so it will show on bottom if there isnt space on the top
	});
    // this set's up the sidebar tooltip for the recent contacts
    $('.contact').tooltip({
        position: 'center right', // position it to the right
        effect: 'slide', // add a slide effect
        offset: [-55,-69] // adjust the position 30 pixels to the top and 19 pixels to the left
    });	
/*****[END]*********************************************
 [ Tooltip              			   				   ]
 ******************************************************/
/*****[BEGIN]*******************************************
 [ popup balloons              			   			   ]
 ******************************************************/  
    $('.has-popupballoon').click(function(){
										  
        $('.popupballoon').fadeOut();// close last open popup balloon
        $(this).next().slideDown(300);
        return false;
    });
	
	$(".popupballoon").mouseleave(function(){
    	$(this).slideUp(300).hide(); 
  	});
	
/*****[END]*********************************************
 [ popup balloons              			   			   ]
 ******************************************************/

	/* Elements closing system */	
	$(".canhide").append("<div class='message-close'></div>").css("position", "relative").slideDown('slow');
	$(".message-close").click(function() {
		$(this).hide();
		$(this).parent().slideUp(300);
	});

/*****[BEGIN]*******************************************
 [ Pretty Tables             			               ]
 ******************************************************/
	$(".userTable, #stats tr:even").addClass("cell-even");
	$(".userTable, #stats tr:odd").addClass("cell");
		
	$(".userTable, #stats td:not(:first-child)").css("border-left", "1px solid #FFF");
	$(".userTable, #stats tr:not(:last-child) td").css("border-bottom", "1px solid #FFF");
	$(".userTable, #stats tr:last-child td").css("border-bottom", "1px solid #FFF");
/*****[END]*********************************************
 [ Pretty Tables            			               ]
 ******************************************************/ 
//cache nav
var nav = $("#topNav");

//add indicator and hovers to submenu parents
nav.find("li").each(function() {
	if ($(this).find("ul").length > 0) {
		$("<span>").text("^").appendTo($(this).children(":first"));

		//show subnav on hover
		$(this).mouseenter(function() {
			$(this).find("ul").stop(true, true).slideDown();
		});
		
		//hide submenus on exit
		$(this).mouseleave(function() {
			$(this).find("ul").stop(true, true).slideUp();
		});
	}
});
				
}); // End: $(document).ready(function()
/*****[BEGIN]*******************************************
 [ Global Messaging              			   		   ]
 ******************************************************/ 
function showSuccess(tipobj){
	$(tipobj).overlay({ top: 300, mask: { color: '#fff', loadSpeed: 200, opacity: 0.5 },
		closeOnEsc: false, closeOnClick: false, load: true
	}).append("<div style='text-align:center;'><p><img src='_images/ajaxLoaderBig.gif' /><br />Procesando...</p></div>");  
}

function showTipsCanHide( tipobj, status, data ){
    $(tipobj).removeClass();
    if(status =='success'){
        $(tipobj).addClass('message success').append("<div class='message-close'></div>").css("position", "relative").slideDown('slow');
			$(".message-close").click(function() {
				$(this).hide();
				$(this).parent().slideUp(300);
			});		
    }
    else if(status =='error'){
        $(tipobj).addClass('message error').append("<div class='message-close'></div>").css("position", "relative").slideDown('slow');
			$(".message-close").click(function() {
				$(this).hide();
				$(this).parent().slideUp(300);
			});		
    }
    else if(status =='warning'){
        $(tipobj).addClass('message warning').append("<div class='message-close'></div>").css("position", "relative").slideDown('slow');
			$(".message-close").click(function() {
				$(this).hide();
				$(this).parent().slideUp(300);
			});		
    }
    else{
        $(tipobj).addClass('message info').append("<div class='message-close'></div>").css("position", "relative").slideDown('slow');
			$(".message-close").click(function() {
				$(this).hide();
				$(this).parent().slideUp(300);
			});		
    }

    if(data != '')
        $(tipobj).html(data);
		return false;
}

function showTips( tipobj, status, data ){
    $(tipobj).removeClass();
    if(status =='success'){
        $(tipobj).addClass('message success').slideDown('slow');
    }
    else if(status =='error'){
        $(tipobj).addClass('message error').slideDown('slow');
    }
    else if(status =='warning'){
        $(tipobj).addClass('message warning').slideDown('slow');
    }
    else{
        $(tipobj).addClass('message info').slideDown('slow');
    }

    if(data != '')
        $(tipobj).html(data);

/*    setTimeout( function(){
        $(tipobj).slideUp('slow');
    }, ( 5000 ) );*/
	
	$("html, body").animate({
		scrollTop: 0
	}, 600);
				  
	return false;
}
