function columnas(group){
	var tallest = 0;
	group.each(function(e){
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
};

/*function ComprobarCodigos() {
    var codigo1 = $('#txtCodigo1').val();
    var codigo2 = $('#txtCodigo2').val();
    var codigo3 = $('#txtCodigo3').val();

    if (codigo1 != "" & codigo2 != "" & codigo3 != "") {
        if ((codigo1 == codigo2) || (codigo2 == codigo3) || (codigo1 == codigo3)) {
            $('#pError').show();
            return false;
        }
        else
            return true;
    }
    else
        return false;    
}*/
 
function columnaDcha() {
		var altoDcha = $('.side_ri').height();
		var altoMain = $('.principal').height();
		if (altoDcha > altoMain) {
			$('.principal').height(altoDcha + 40);
		}
}
function columnaIzqda() {
    var altoIzqda = $('.side_le').height();
    var altoMain = $('.main').height();
    if (altoMain > altoIzqda) {
        $('.side_le').height(altoMain);
    } else {
        $('.main').height(altoIzqda);
    }
}

function validate_checkAccept(sender, e) {    
    var chb = document.getElementById("ctl00_content_checkAccept");
    e.IsValid = chb.checked == true;
}

function validate_checkAcceptNuevo(sender, e) {
    var chb = document.getElementById("checkAccept");
    e.IsValid = chb.checked == true;
}

function media() {
    $('.sin_boton .social ul li:first').css({ 'width':'33%' });
    $('a.social_media').each(function() {
        var enlace = $(this);
        var padre = $(this).parent();
        var hijo = $(this).siblings('ul');
        hijo.css({ 'visibility': 'hidden' });
        $(this).toggle(
			function() {
			    $(this).addClass('hover');
			    $(hijo).css({ 'visibility': 'visible' });
		    
			    return false;
			},
			function() {
			    $(this).removeClass('hover');
			    $(hijo).css({ 'visibility': 'hidden' });
			    return false;
			}
		);
        if ($('a.social_media').size() > 1) {
            $(hijo).mouseleave(function() {
                $(enlace).removeClass('hover');
                $(hijo).css({ 'visibility': 'hidden' });
            });
        }
    });
//    var imagenWidth = $('.destacado .social .imagen').width();
//    var paddingCont = (imagenWidth + 15);
//    $('.destacado .social .contenido').css({ 'padding-left': paddingCont });
};
function anchoPrincipal(){
	var ancho = $('.principal').width();
	var ajuste = ancho + 1;
	$('#nav li ul').css({'width':ajuste});
};
function alturaModal(){
	var h =  $(document).height();
	$('.modal_flotante').css({'height': h})
}
function alturaSocial() {
	$('.modulo .social').each(function() {
		$(this).height($(this).parent().height());
	});
}

function tooltipFavorito() {
	$('.favorito a').hover(function() {
		$(this).parent().siblings('.caja_tooltip').children('.tooltip').show(250);
	}, function() {
		$(this).parent().siblings('.caja_tooltip').children('.tooltip').hide(250);
	});
}

function ratings() {
	$(".ratingstars li").live("mouseover", function() {
		var objThis = $(this);
		var parent = objThis.parent();
		if (parent.attr("Votado") == null) {
			objThis.attr("class", "filledRatingStar").text("Voto");
			objThis.prevAll().attr("class", "filledRatingStar").text("Voto");
			objThis.nextAll().attr("class", "emptyRatingStar").text("Sin Voto");
		}
	});
	
	$(".ratingstars li").live("click", function(e) {
		if ($(this).parent().attr("Votado") == null) {
			$(this).parent().attr("Votado", true);
			var asociado = $(this).parent().attr("ih_asociado");
			document.getElementById(asociado).value = $(this).prevAll().length + 1;
		}
	});
	
	
	$(".ratingstars").live("mouseout", function(e) {
		if ($(this).attr("Votado") == null) {
			$(this).children().attr("class", "emptyRatingStar").text("Sin Voto");
		}
	});
}

function cerrarModal() {
	location.href = $('.cerrarModal').attr("href");
};

function numeroDeLista() {
	var $lista = $('.articulo ol');
	$lista.each(function() {
		var e = parseFloat($(this).children('li').size());
		for ( i=1; i<=e; i++ ) {
			var html = '<div class="numero">'+ i +'</div';
			$(this).children('li').eq(i-1).prepend(html);
		}
	});
}

function acordeon(){
	if ($('.acordeon li').hasClass('active'))
	{
	    $('.acordeon > ul > li > ul').hide();
	    $('.acordeon li.active').children().show();
		$('.acordeon li.active').parents('ul:has(li.active)').show();
		return false;
	}
	else
	{
		$('.acordeon > ul > li > ul').hide();
		//$('.acordeon > ul > li > a').bind('click',function(event){$(this).next('ul').toggle();return false;});		
	}
};

function acordeonplus() {
    if ($('.acordeonplus li').hasClass('active')) {
        $('.acordeonplus > ul > li > ul').hide();
        //$('.acordeonplus li.active').children().show();
        $('.acordeonplus li.active').parents('ul:has(li.active)').show();
        return false;
    }
    else {
        $('.acordeonplus > ul > li > ul').hide();
        $('.acordeonplus > ul > li > a').bind('click',function(event){$(this).next('ul').toggle();return false;});		
    }
};

function falsoFile() {
	$('.registro input[type=file]').each(function() {
		var $fileFalso = $(this);
		$fileFalso.css({'opacity': 0});
		var $inputFalso = $('<div class="f_falso"></div>');
		$fileFalso.before($inputFalso);
		$inputFalso.html('<input type="text" class="f_input f_medio" /><div class="acciones"><p class="boton medio"><span><a href="#">Examinar</a></span></p></div>');
		var $f_inputFalso  = $inputFalso.find(':text');
		var $reset = $inputFalso.find(':reset');
		$(this).width($inputFalso.width()).height($inputFalso.height());
		$(this).change(function(e){
		  $in=$(this);
		  $f_inputFalso.attr("value",$in.val()).attr("disabled", "disabled");
		});
	});
}
function hrLadoDcho() {
    var $hr = $('.side_ri > .hr_doble:last');
    if ($hr.next().is('.cuenta')) {
    } else {
        $hr.remove();
    };
}
function alaCola(self) {
    self.insertAfter($('div.main > :last')).before('<div class="hr"></div>').show();
}
function tabs() {
    var cargar = $('.envoltura > div');
    var lista = $('.tabs li');
    $('.tabs a').click(function() {
        if (($(this.hash)).is(':visible')) {
            $(this.hash).show();
        }
        else {
            cargar.hide().filter(this.hash).show();
            $('.tabs li').removeClass('tabactual');
            $(this).parent().addClass('tabactual')
        };
        return false;
    });
};

(function($){
 $.fn.style_my_tooltips = function(options) {
    var defaults = {
        tip_follows_cursor: "on",
        tip_delay_time: 1000
    };
    var options = $.extend(defaults, options);
    $("body").append("<div id='smt_tooltip'></div>"); //create the tooltip container
    smtTip = $("#smt_tooltip");
    smtTip.hide(); //hide it
    return this.each(function() {
        function smtMouseMove(e){
            smtMouseCoordsX=e.pageX;
            smtMouseCoordsY=e.pageY;
            smtTipPosition();
        }
        function smtTipPosition(){
            var cursor_tip_margin_x=0; //horizontal space between the cursor and tooltip
            var cursor_tip_margin_y=24; //vertical space between the cursor and tooltip
            var leftOffset=smtMouseCoordsX+cursor_tip_margin_x+$(smtTip).outerWidth();
            var topOffset=smtMouseCoordsY+cursor_tip_margin_y+$(smtTip).outerHeight();
            if(leftOffset<=$(window).width()){
                smtTip.css("left",smtMouseCoordsX+cursor_tip_margin_x);
            } else {
                var thePosX=smtMouseCoordsX-(cursor_tip_margin_x)-$(smtTip).width();
                smtTip.css("left",thePosX);
            }
            if(topOffset<=$(window).height()){
                smtTip.css("top",smtMouseCoordsY+cursor_tip_margin_y);
            } else {
                var thePosY=smtMouseCoordsY-(cursor_tip_margin_y)-$(smtTip).height();
                smtTip.css("top",thePosY);
            }
        }
        $(this).hover(function(e) {
            // mouseover
            var $this=$(this);
            $this.data("smtTitle",$this.attr("title")); //store title
            var theTitle=$this.data("smtTitle");
            $this.attr("title",""); //remove title to prevent native tooltip showing
            smtTip.empty().append(theTitle).hide(); //set tooltip text and hide it
            smtTip_delay = setInterval(smtTip_fadeIn, options.tip_delay_time); //set tooltip delay
            if(options.tip_follows_cursor=="off"){
                smtMouseMove(e);
            } else {
                $(document).bind("mousemove", function(event){
                    smtMouseMove(event);
                });
            }
        }, function() {
            // mouseout
            var $this=$(this);
            if(options.tip_follows_cursor!="off"){
                $(document).unbind("mousemove");
            }
            clearInterval(smtTip_delay);
            if(smtTip.is(":animated")){
                smtTip.hide();
            } else {
                smtTip.fadeTo("fast",0);
            }
            $this.attr("title",$this.data("smtTitle")); //add back title
        });
        $(this).click(function() {
            var $this=$(this);
            $this.attr("title",$this.data("smtTitle")); //add back title
        });
        function smtTip_fadeIn(){
            smtTip.fadeTo("fast",1,function(){clearInterval(smtTip_delay);});
        }
    });
 };
})(jQuery);

$(document).ready(function() {
    $("img[title]").style_my_tooltips({
        tip_follows_cursor: "on", //on/off
        tip_delay_time: 800 //milliseconds
    });
    media();
    anchoPrincipal();
    alturaModal();
    tooltipFavorito();
    alturaSocial();
    ratings();
    numeroDeLista();
    acordeon();
    acordeonplus();
    if ($(".home .categorias").length) { alaCola($('.home .categorias')); }
    columnas($('.tabla_comunidad > article').not('div.limpiar'));
    columnas($('.home #escuela > div'));
    tabs();
    //  columnaIzqda();
    falsoFile();
    hrLadoDcho();

    //Colocacion de fondo de login
    var alturaLogin = ($('#login .container').height()) + 16;
    var alturaTools = $('#tools').innerHeight();
    $('.fondo_login').css({ 'opacity': 0.3, 'height': alturaLogin });
    $('#login').css('top', alturaTools);
    //Colocacion de fondo de destacado en Recetas y Menus
    var alturaDestacado = ($('.destacado .info .inner').height());
    $('.destacado .fondo .inner').css({ 'opacity': .7, 'height': alturaDestacado });

    //Identificacion de elementos primeros, últimos, arreglo de flotaciones...
    $('.side_ri > :first').addClass('primero');
    $('.modulo .edicion li').addClass('over');
    $('.modulo .edicion li .f_input').addClass('over');
    $('ul.sub li:last').addClass('last');
    $('.productos div:odd').after('<div class="limpiar" style="float:none;"></div>');
    $('span[disabled]').addClass('disabled').removeAttr('disabled');

    //Pijamas
    $('.recetas > ul > li:odd').addClass('altern');
    $('.articulo > table > tbody > tr:nth-child(2n)').addClass('altern');
    $('.resultados tr:even').children(':even').addClass('altern');
    $('.resultados tr:odd').children(':odd').addClass('altern');
})

$(window).load(function() {
    columnaDcha();
    $('.tercio .f_ri_t .social').show();
});


//$(function() {
//    $("form input").keypress(function(e) {
//        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {            
//            eval($("#ctl00_lnkAcceder").attr("href"));
//            return true;
//        }
//    });
//});
