/* ******************** FADE BACKGROUND **************** */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
		
	});
})(jQuery);

$(document).ready(function() {
	

	
	$("a[href='#toppage']").each(function(i){
		$(this).attr('href','javascript:;'); 
		$(this).click(function(){
			$('html,body').animate({scrollTop: $("a[name='toppage']").offset().top}, 600);
		});
	});
	
	$("a[href='#botpage']").each(function(i){
		$("a[name='botpage']").attr('name','botpagebackport'); 
		$(this).parent().next().before("<a name='botpage' id='botpage' />");
		// $(this).attr('href','#botpage'); 	
		$(this).attr('href','javascript:;'); 	
		$(this).click(function(){
			$('html,body').animate({scrollTop: $("#botpage").offset().top}, 1000);
		});
		
	});

		// botonera miembro
		$("#bot1").hover(function(){ botoneramiembro("show",110); $("div#bot1html").show(); $("div#boxmiembro2").show();
		}, function() { botoneramiembro("hide",0); $("div#bot1html").hide(); $("div#boxmiembro2").hide(); });

		$("#bot2").hover(function(){ botoneramiembro("show",190); $("div#bot2html").show(); $("div#boxmiembro2").show();
		}, function() { botoneramiembro("hide",0); $("div#bot2html").hide(); $("div#boxmiembro2").hide(); });

		$("#bot3").hover(function(){ botoneramiembro("show",130); $("div#bot3html").show(); $("div#boxmiembro2").show();
		}, function() { botoneramiembro("hide",0); $("div#bot3html").hide(); $("div#boxmiembro2").hide(); });

		
	
	$("div.panel_button").click(function(){
		$("div#panel").animate({
			height: "250px"
		})
		.animate({
			height: "230px"
		}, "fast");
		
		$("div#panel_container").animate({
			top: "-4px"
		}, "slow");

		$("div#hide_button").animate({
			top: "-20px"
		}, "slow");

		$("div.panel_button").toggle();
		
		
		

	
	});	
	
   $("div#hide_button").click(function(){
		
		$("div#panel").animate({
			height: "0px"
		}, "fast");

		$("div#panel_container").animate({
			top: "-20px"
		}, "slow");

		$("div#hide_button").animate({
			top: "0px"
		}, "slow");

	
   });	

});

function botoneramiembro(tipo,alto) {
	if(tipo == "show") {
			$("div#botoneras_miembros_show").stop()
			$("div#botoneras_miembros_show").animate({
				height: alto+"px"
			}, "fast");
	} else {
			$("div#botoneras_miembros_show").stop()
			$("div#botoneras_miembros_show").animate({
				height: "0px"
			}, "slow");	
	}
}
$(window).load(function() {

		$('.nodisplay').show();
		$("#copy").select();

		
		var changeFontMAX = 3;
		
		$('.changeFont').click(function(){
			var $ourText = $("div#contentr4");
			var $ourTextOut = $("div#contentr4 .grid_8 p, div#contentr4 .grid_9 p, div#contentr4 .grid_12 p, div#contentr4 .grid_9 ul, div#contentr4 .grid_12 ul, .parrafos, div.noticiatexto");
			var currFontSize = $ourText.css('fontSize');
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);
			if(this.id == 'tam_c') {
				if (changeFontMAX < 7) {
					finalNum *= 1.1;
					changeFontMAX++;
				}
			}
			else if (this.id == 'tam_a'){
				if (changeFontMAX > 1) {
					finalNum /=1.1;
					changeFontMAX--;
				}
			}
			$ourTextOut.animate({fontSize: finalNum + stringEnding},100);
			$ourText.animate({fontSize: finalNum + stringEnding},100);
		});
	
	$(".slider_img").append("<em></em>");
	
	$(".slider_img").hover(function() {
		$(this).find("em").animate({opacity: "show", top: "-245"}, "slow");
		var hoverText = "Pinche aquí para más información o llámenos al <b>902 15 30 20</b>";
	    $(this).find("em").html(hoverText);
	}, function() {
		$(this).find("em").animate({opacity: "hide", top: "-260"}, "fast");
	});
	
	$(document).bind('click', function(e) {

                var $clicked=$(e.target);

                if( ! ( $clicked.is('input, select') || 
				$clicked.parents().is('input, select') ) )
				{
                        $(".formError").remove();
                }

        });
	
	$(document).click(function() {
		setTimeout("$('#suggestions').hide();", 200);
		$("#messageWindow").slideUp("slow");
	});


	
	$("ul#dropdown > li > a").hover(function(){
			$(this).next("ul").fadeIn();
		}, function(){
			$(this).next("ul").hide();
	});	

	$("ul#dropdown li ul").hover(function(){
		$(this).show();
	}, function(){
		$(this).hide();
	});

	
	$('a, input, img, area').tooltip({
		track: true,
		delay: 0,
		fade: false,
		showURL: false,
		showBody: " - ",
		top: 14,
		left: 14
	});

	$('ul#dropdown li a.nonsel')
		.css( {backgroundPosition: "50% 0%"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(50% 100%)"}, {duration:200});
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(50% 0%)"}, {duration:600});
		});
	
	$("#close_message").click(function(){
			$("#messageWindow").slideUp("slow");
	});

	$("#T_LOGIN_FORM_P input[name='USUARIO']").focus();

});


function mensajeLanzar(titulo,texto,posicion) {
var p = $("#sloganheader");
var position = p.offset();
			$('#messageWindow').width($('.container_12').width());
			$("#messageWindow").animate({ top: posicion}, 1000).animate({ left: position.left}, 0).slideDown('slow');
			$('#messageWindowTitle').html(titulo);
			$('#messageWindowText').html(texto);
}


var timerSearch = null;

function lookup(buscadortopinput) {
    if (timerSearch != null) {
		clearTimeout(timerSearch);
	}
	eval('timerSearch = setTimeout("lookup_gen(\''+buscadortopinput+'\')",600);') 
}

function lookup_gen(buscadortopinput) {
    if(buscadortopinput.length < 3) {
        $('#suggestions').hide();
    } else {
		$.get("/goto/busclink", {PATRON: ""+buscadortopinput+""}, function(data,textStatus){
			if($(data).text().length > 2) {
				$('#autoSuggestionsList').html('<ul id="autoSuggestionsListUL"></ul>');
                $('#suggestions').show();
				$(data).find('LINK').each(function(){
					var caption = $(this).find('CAPTION').text();
					var url = $(this).find('URL').text();
					var prior = $(this).find('PRIOR').text();
					$('<li onclick="document.location.href=\''+url+'\'"></li>').html('<a href="'+url+'" >'+caption+'</a>').appendTo('#autoSuggestionsListUL');
				});
				
			} else {
				$('#suggestions').hide();
			}
        });
    }
}

function fill(thisValue) {
   $('#suggestions').hide(); 
}

