function toolTipShowMosaico(obj){
    obj.children('.info-item').stop().show();
    obj.children('input[name=in-table]').show();
    obj.css('z-index', 1);
}
function toolTipShowTabla(obj){
    var e = obj.attr('rel');
    var x = obj.offset();
    $("#"+e).css('display', 'block').css({top: x.top+15, left: x.left+135});
}
                


var timeoutShowMosaico;//variable que almacena la referencia al timeout
var timeoutShowTabla;//variable que almacena la referencia al timeout
$(document).ready(function()
{
	
	/*--------[]<0>~~~~~<0>[]--------*/
	/*-  [Add this by :NESTOR:] {-   */
	jQuery.fn.scrollTo = function(time)
	{
		var t = $(this).offset().top;
		
		if(t > 10){t = t - 10}
		if(time == 'fast'){time = 400}
		if(time == 'medium'){time = 800}
		if(time == 'slow'){time = 1200}
		if(time == null){time = 1000}
		
		$('html,body').animate({scrollTop: t}, time);
	}
	/*-   } [Add this by :NESTOR:]  -*/
	/*--------[]<0>~~~~~<0>[]--------*/
	
	switch($('input[name=vista]').val())
	{
		case 'list':
			$('#my-table').css('display', 'none');
			$('#my-list').css('display', 'block');
			$('#my-mosaic').css('display', 'none');
			break;
		case 'mosaic':
			$('#my-table').css('display', 'none');
			$('#my-list').css('display', 'none');
			$('#my-mosaic').css('display', 'block');
			break;
		default:
			$('#my-table').css('display', 'block');
			$('#my-list').css('display', 'none');
			$('#my-mosaic').css('display', 'none');
	}
	
	controla = true;
	$('#nav .drop-down a').click(
		/*function () {
			if (controla == true) {
				$(this).parent('li').addClass('active');
				$('.drop-down-menu').show(200);
				controla = false;	
			}
			else {
				$(this).parent('li').removeClass('active');
				$('.drop-down-menu').hide(200);
				controla = true;
			}	
			
		}*/
			function () {
				if (controla == true) {
				$(this).parent('li').addClass('active');
				xCoor = this.offsetLeft;
				yCoor = this.offsetTop;
				$(this).parent('li').children('.drop-down-menu').show(200).css('top', yCoor+27).css('left', xCoor-13);
				controla = false;
				}
				else {
				$(this).parent('li').removeClass('active');
				$(this).parent('li').children('.drop-down-menu').hide(200).css('top', '0').css('left', '0');
				controla = true;
				}
			} 	
	);
	$('#nav .drop-down').mouseleave(
		function () {
			$('#nav .drop-down').removeClass('active');
			$('.drop-down-menu').hide();
			controla = true;	
		}
	);
	
	controla = true;
	$('#nav-lang .drop-downLang a').click(
		function () {
			if (controla == true) {
				$(this).parent('li').addClass('active');
				$('.drop-down-menuLang').show(200);
				controla = false;	
			}
			else {
				$(this).parent('li').removeClass('active');
				$('.drop-down-menuLang').hide(200);
				controla = true;
			}	
		}
	);
	$('#nav-lang .drop-downLang').mouseleave(
		function () {
			$('#nav-lang .drop-downLang').removeClass('active');
			$('.drop-down-menuLang').hide();
			controla = true;	
		}
	);
	/*Muestra caja de comentarios*/
	/*[]--------[~o0o~]--------[]*/
	
	$('.respond > a').live('click', function()
	{
		if ($(this).closest('.thread').find('.comment-box').hasClass('open')) 
		{
			$(this).closest('.thread').find('.respond a').text('Responder');
			$(this).closest('.thread').find('.comment-box').slideUp(300).removeClass('open');
			return(false);
		}
		else 
		{
			$(this).closest('.thread').find('.respond a').text('Cerrar');
			$(this).closest('.thread').find('.comment-box').slideDown(300).addClass('open');
			return(false);
		}
	});
        
        
	// Establece funcion tooltip
	function tooltip (event) {
                wTam = 0;
		hTam = 0;
		xCoor = 0;
		yCoor = 0;
		$('.design-grid').mousemove(
		//tamanos de la reticula y posicion del mouse en ella
		function(e){
			wTam = $(this).width();
			hTam = $(this).height();
			xCoor = e.pageX - this.offsetLeft;
			yCoor = e.pageY - this.offsetTop;
		});
		//mouseover sobre un elemento
		if (event.type == 'mouseover') {
                    //var obj=$(this);
                    //if(timeoutShowMosaico!=null && timeoutShowMosaico!=undefined){
                    //                   clearTimeout(timeoutShowMosaico);
                    //}
                    //timeoutShowMosaico=setTimeout(function(){toolTipShowMosaico(obj)},1000);
		}
		//mouseleave 
		else {
                        if(timeoutShowMosaico!=null && timeoutShowMosaico!=undefined){
                            clearTimeout(timeoutShowMosaico);
                        }
			$('.info-item').hide();
			$(this).children('input:checkbox').hide();
			$(this).children('input:checked').show();
			$(this).css('z-index', 0);
                }
                var i=0;
		//posicion del tooltip al mover sobre un elemento que depende de la posicion del mouse en la reticula y del tamano de esta
		$(this).mousemove(
			function(e){
                                var obj=$(this);
				xCoorThis = e.pageX;
				yCoorThis = e.pageY;
				obj.children('.info-item').css({'top': yCoorThis - this.offsetTop + 20});
				//Verticalmente se presenta siempre en la misma posicion porque se pierde foco del checkbox de otra form
				
				/*-[Add this by :NESTOR:] {-*/
                                if(obj.children('.info-item').css('display')=="none" && i==0){
                                    if(timeoutShowMosaico!=null && timeoutShowMosaico!=undefined){
                                       clearTimeout(timeoutShowMosaico);
                                    }
                                    timeoutShowMosaico=setTimeout(function(){toolTipShowMosaico(obj)},1000);
                                    i++;
                                }
				//$(this).children('.info-item').css('display', 'block');
				//$(this).children('input:checkbox').css('display', 'block');
				//$(this).css('z-index', 1);
				/*-} [Add this by :NESTOR:]-*/
				
				/*if ((hTam/2) < yCoor) {
					$(this).children('.info-item').css({'top': yCoorThis - this.offsetTop - 120});
				}*/
				if ((wTam/2) > xCoor) {
					obj.children('.info-item').css({'left': xCoorThis - this.offsetLeft + 20});
				}
				if (xCoor > 216 && xCoor < 432) {
					obj.children('.info-item').css({'left': xCoorThis - this.offsetLeft - 200});
				}
				if (xCoor > 432) {
					obj.children('.info-item').css({'left': xCoorThis - this.offsetLeft - 400});
				}
			}
		)	
	};
	
	
	/*--------[]<0>~~~~~<0>[]--------*/
	/*-  [Add this by :NESTOR:] {-   */
	$('#languages').click(function ()
	{
		$('#more-lang').toggle(400);
	});
	
	/*-   } [Add this by :NESTOR:]  -*/
	/*--------[]<0>~~~~~<0>[]--------*/
	
	
	
	//activa vista mosaico
	$('.view-serp .mosaic').click(function()
	{
		$('.serp li input:checkbox, .serp li .info-item').hide();
		$('.serp li input:checked').show();
		$('.serp li').live('mouseenter mouseleave', tooltip); //activa funcion tooltip
		
		$('#my-table').css('display', 'none');
		$('#my-list').hide();
		$('#my-list').css('display', 'none');
		$('#my-mosaic').css('display', 'block');

		if($('input[name=vista]').val() == 'table')
		{
			$('.paginator p a').each(function() {$(this).attr('href', $(this).attr('href').replace(/table/, "mosaic"));});
			$('#sidebar .item .categories li a').each(function() {$(this).attr('href', $(this).attr('href').replace(/table/, "mosaic"));});
		}
		else//($('input[name=vista').val() == 'list')
		{
			$('.paginator p a').each(function() {$(this).attr('href', $(this).attr('href').replace(/list/, "mosaic"));});
			$('#sidebar .item .categories li a').each(function() {$(this).attr('href', $(this).attr('href').replace(/list/, "mosaic"));});
		}
		$('input[name=vista]').val('mosaic');
	});
	
	//activa vista lista
	$('.view-serp .list').click(function()
	{
		$('.serp li input:checkbox, .serp li .info-item').show();
		$('.serp li').die('mouseenter mouseleave', tooltip); //desactiva funcion tooltip
		
		$('#my-table').css('display', 'none');
		$('#my-mosaic').hide();
		$('#my-mosaic').css('display', 'none');
		$('#my-list').css('display', 'block');
		
		if($('input[name=vista]').val() == 'mosaic')
		{
			$('.paginator p a').each(function() {$(this).attr('href', $(this).attr('href').replace(/mosaic/, "list"));});
			$('#sidebar .item .categories li a').each(function() {$(this).attr('href', $(this).attr('href').replace(/mosaic/, "list"));});
		}
		else//($('input[name=vista').val() == 'table')
		{
			$('.paginator p a').each(function() {$(this).attr('href', $(this).attr('href').replace(/table/, "list"));});
			$('#sidebar .item .categories li a').each(function() {$(this).attr('href', $(this).attr('href').replace(/table/, "list"));});
		}
		$('input[name=vista]').val('list');
	});
	
	$('.serp li:nth-child(6n)').addClass('last');
	$('.design-grid li input:checked').show();
	$('.design-grid li').live('mouseenter mouseleave', tooltip); //activa funcion tooltip
	
	//activa vista tabla (este es un html diferente)
	$('.view-serp .tabla').click(function ()
	{
		$('#my-table').css('display', 'block');
		$('#my-list').css('display', 'none');
		$('#my-mosaic').css('display', 'none');
		
		$('.serp').hide();
		$('.design-tabla').show();
		
		if($('input[name=vista]').val() == 'mosaic')
		{
			$('.paginator p a').each(function() {$(this).attr('href', $(this).attr('href').replace(/mosaic/, "table"));});
			$('#sidebar .item .categories li a').each(function() {$(this).attr('href', $(this).attr('href').replace(/mosaic/, "table"));});
		}
		else//($('input[name=vista').val() == 'list')
		{
			$('.paginator p a').each(function() {$(this).attr('href', $(this).attr('href').replace(/list/, "table"));});
			$('#sidebar .item .categories li a').each(function() {$(this).attr('href', $(this).attr('href').replace(/list/, "table"));});
		}
		$('input[name=vista]').val('table');
		
	});
	
	//abre y cierra popUp vista tabla:
	$('.design-tabla h2 a').mouseover(function ()
	{
            var obj=$(this);
            if(timeoutShowTabla!=null && timeoutShowTabla!=undefined){
                clearTimeout(timeoutShowTabla)
            }
            timeoutShowTabla=setTimeout(function(){toolTipShowTabla(obj)},1000);
		
	});
	$('.design-tabla h2 a').mouseout(function ()
	{
            if(timeoutShowTabla!=null && timeoutShowTabla!=undefined){
                clearTimeout(timeoutShowTabla)
            }
            var e = $(this).attr('rel');
            $("#"+e).css('display', 'none');
	});
	
	//sistema de votaciones rating:
	$(".rating li a").click(function(){
		$(this).closest("ul.rating").attr("class", "").addClass("rating");
		var votacion = $(this).attr('title');
		$(this).closest("ul.rating").addClass("rating"+votacion);
	});
	
	// textarea autoresize:
	$('textarea.redimensionable').autoResize({
	    onResize : function() {
	        $(this).css({opacity:0.8});
	    },
	    animateCallback : function() {
	        $(this).css({opacity:1});
	    },
	    animateDuration : 300,
	    extraSpace : 0
	});
	
	//acciones sbre la foto del perfil
	$('#profile-image').mouseover(function(){
		$('#change-picture-actions').css('display', 'block');
	});
	
	$('#profile-image').mouseleave(function(){
		$('#change-picture-actions').css('display', 'none');
	});
	
	
	// openThat y closeThis
	/*$("a.openThat").click(function(){
		var that = $(this).attr('rel');
		$("#"+that).show();
		return false;
	});
	$("a.closeThis").click(function(){
		var esto = $(this).attr('rel');
		$("#"+esto).hide();
		return false;
	});*/
	
	$("a.descrpFoto").click(function(){
		$(this).addClass('hidden');
	});
	$("a.descrpFotoC").click(function(){
		$('a.descrpFoto').removeClass('hidden');
	});
	
	//lista mensajes
	//$('#inbox-list .thread').css('height', '60px');
	$('#inbox-list .message').hide();
	//$('#inbox-list .options-message').hide();
	/*$('#inbox-list .subject a').toggle(
		function(){
			$(this).parent('.subject').siblings('.truncate').hide().siblings('.message').show();
			$(this).closest('.thread').removeClass('unread')//.css('height', '1%');
			$(this).closest('.user-comment').siblings('.options-message').show();
			return(false);
		},
		function(){
			//$(this).closest('.thread').css('height', '60px');
			$(this).parent('.subject').siblings('.message').hide().siblings('.truncate').show();
			$(this).closest('.user-comment').siblings('.options-message, .comment-box').hide().siblings('.close-box-comment').addClass('hidden');
			return(false);
		}
	);*/
	$('#inbox-list .options-message .show-box-comment').click(function(){
		$(this).closest('.options-message').css('display','none').siblings('.close-box-comment').removeClass('hidden').next('.comment-box').css('display','block');
		return(false);
	});
	$('#inbox-list .close-box-comment a').click(function(){
		$(this).closest('.close-box-comment').addClass('hidden').next('.comment-box').css('display','none').siblings('.options-message').css('display','block');
		return(false);
	})
	
	//editarArticulo
	/*$('.openPopUp--').live('click', 
		function()
		{
			var offset = $(this).offset();
			var e = $(this).attr('rel');
			
			var wscr = /*$(window).width();*/ /*window.innerWidth;
	        /*var hscr = /*$(window).height();*/ /*window.innerHeight;

		    var wcnt = $('#' + e + '.popUpModal').width();
		    var hcnt = $('#' + e + '.popUpModal').height();
		    
		    var mleft = ( wscr - wcnt ) / 2;
		    var mtop = ( hscr - hcnt ) / 2;
		        
		    $("#"+e).css("left", mleft+'px');
		    $("#"+e).css("top", mtop+'px');
		    
		    return false;
		}
	);*/
	
	//descripciones
	$('.condescr').live('focusin', 
		function()
		{
			if($(this).attr('descr'))
			{
				var d = $(this).attr('descr');
				var x = $(this).offset();
				if ($(this).attr('posx'))
				{
					var ajusteX = parseFloat($(this).attr('posx'));
				} else {
					var ajusteX = 0;
				}
				
				var w = $(this).outerWidth();
				var p = w + 20 + ajusteX;
				$('#footer').append('<div id=\"Descrp\"><p></p></div>');
				$('#Descrp').append('<span class=\"arrow\"></span>').css({top: x.top-13, left: x.left+p});
				$('#Descrp p').html(d);
			} else {
				var l = $(this).attr('rel');
				var d = $('#'+l).html();
				var x = $(this).offset();
				var w = $(this).outerWidth();
				$('#footer').append('<div id=\"Descrp\"></div>');
				$('#Descrp').html(d).append('<span class=\"arrow\"></span>').css({top: x.top-13, left: x.left+w+20});
			}
		}
	);
	$('.condescr').live('focusout',
		function()
		{
			$('#Descrp').remove();
		}
	);

	
})
