
// Funcion que llama a funciones al iniciar la primera carga de la web
$(document).ready(function()
	{
		addEvent(window,'load', doIframe);
		inicia();
	}
);

// funcion que se ejecuta al inicio de la carga de la pagina
function inicia(){
	$("#menu1").css("overflow","hidden");
	$("#mi_iframe").css("overflow","hidden");
	$(".fragmento").css("visibility","visible");
	

	var enlace, secc;
	var urll=window.location.href;
	
	if(urll.indexOf("_i.")!=-1)
		var pagina_inicio="presentacion_i.html";
	else
		var pagina_inicio="presentacion.html";
		
	if(urll.indexOf("ifema.es")!=-1)
		document.domain = "ifema.es";

	// si no existe cookie apartado incluimos la pagina de inicio en la variable enlace que mas tarde creará la cookie
	if($.cookie('apartado')==null){
		enlace=pagina_inicio;
		$.cookie('apartado',pagina_inicio);
		secc=1;
	}else{
		enlace=$.cookie('apartado');
		secc=$.cookie('seccion');
	}

	// Activamos las pestañas
	$('#pestanas').tabs(parseInt($.cookie('seccion')),{ fxSlide: true, fxFade: true, fxSpeed: 'fast'});
	
	// Activamos la pestaña que contenga la cookie seccion
	$('#container').triggerTab(parseInt($.cookie('seccion')));
	
	// borramos la clase activo del menu pestanas
	$(".cont_tabs .activo").removeClass();

	// anadimos la clase activo al enlace a cargar a traves de la cookie apartado (enlace)
	$("a[@href='"+enlace+"']").addClass("activo");
	
	// si el enlace no contiene http cargamos la pagina de inicio en el iframe
	if(enlace.indexOf("http")==-1 )
		$('#mi_iframe').attr("src",enlace);
	
	// si pulsamos un enlace con href="#" desactivamos el enlace
	$("a[@href='#']").click(function(){
		return false;
	});
	$(".with-tooltip").click(function () {
		return false;					   
	});
	
	$(".with-tooltip").simpletooltip();
	
	// funcion que imprime el iframe cuando pulsamos un enlace con el id='printt'
	$("#printt").click(function () {
		window.frames[0].focus();
		window.frames[0].print();
		return false;
	});
	
	// funcion que cambia el idioma de la pagina cuando pulsamos un enlace con el id='idiomas'
	$("#idioma").click(function(){
		var enlace=$.cookie('apartado');
		if(window.location.href.indexOf("_i.")==-1){
			if(enlace.indexOf("prensa.ifema.es")!=-1 || enlace.indexOf("utilidades.ifema.es")!=-1)
				$.cookie('apartado',enlace.replace("_01","_06"));
			else
				$.cookie('apartado',enlace.replace(".html","_i.html"));
			location.href="default_i.html";
		}else{
			if(enlace.indexOf("prensa.ifema.es")!=-1 || enlace.indexOf("utilidades.ifema.es")!=-1)
				$.cookie('apartado',enlace.replace("_06","_01"));
			else
				$.cookie('apartado',enlace.replace("_i.html",".html"));
			location.href="default.html";
		}
		return false;
	});
	
	// funcion que activa la pestaña correspondiente a traves de la cookie seccion
	$(".fragmento").click(function(){  
		// incluimos el id de la pestana clickada y lo anadimos a la cookie seccion
		$.cookie('seccion', $(this).attr("id"));
		
		// recorremos los enlaces y capturamos el id de la pestana
		for(i=0;i<($(".fragmento").length);i++){
			if($($(".fragmento")[i]).attr("id")==$(this).attr("id"))
				$.cookie('seccion', (i+1));
		}
	});	
	
	// Funcion que se ejecuta cuando pulsamos cualquier enlace del menu de pestanas
	$(".fragmento .cont_tabs a").click(function(){
												
		var clase=$(this).attr("class");
		
		if(clase.indexOf("with-tooltip")==-1){
			$(".carga").fadeIn("fast",function(){
					doIframe();					   
			 })
			// Borramos la clase activo
			$(".cont_tabs .activo").removeClass();
	
			// Activamos la clase activo correspondiente
			$(this).addClass("activo");	
	
			// guardamos una cookie con la pagina a cargar en el iframe
			var mi_url=$(this).attr("href");
			var mi_url_target=$(this).attr("target");
	
			if(mi_url_target.indexOf("blank")==-1)
				$.cookie('apartado', $(this).attr("href"));
	
			/*if(mi_url.indexOf("prensa.ifema.es")!=-1 || mi_url.indexOf("utilidades.ifema.es")!=-1){
				$("#mi_iframe").fadeOut("fast",function(){
					$("#col_der").prepend("<div class='cargador'>Cargando</div>");									
				});
			}*/
			
			
		}
	});
	
	$('.arriba a').click(function(){
         $('html,body').animate({scrollTop:'0px'}, 500); 
		 return false;
    });
    
    $('.top').click(function(){
         $('html,body').animate({scrollTop:'0px'}, 500);
		 return false; 
    });
    
    $('.ancla').click(function(){
		var donde=$(this).attr("href");
		var p = $(donde);
		var alto;
		if($.browser.msie){
			alto=window.document.body.clientHeight-820;
		}else{
			alto=0;
		}
		var posicion=document.getElementById(donde.substring(1,donde.length)).offsetTop;
		mueve((posicion+alto)+'px');
		return false;
    });

	var css = '@font-face {            '+
	'    font-family: Folio;      '+
	'    src: url(\'css/Folio.eot\'); '+   
	'    src: local(\'Folio\'), url(\'css/Folio.ttf\') format("truetype");'+
	'}                                  '+
	'.mi_fuente {                     '+
	'  font-family:Folio,Verdana, Arial, Helvetica, sans-serif;  '+
	'}';
	addDeclaration(css);
	
	$("#background").fullBg();
}

function addDeclaration(css){
    if (document.createStyleSheet){
        var st = document.createStyleSheet();
        st.cssText = css;
		$("h2").addClass("mi_fuente");
		$("h2").css("display","block");
    } else {
        var st = document.createElement('style');
        st.textContent = css;
        document.getElementsByTagName('head')[0].appendChild(st);    
		$("h2").addClass("mi_fuente");
		$("h2").css("display","block");
    }
}


function doIframe(){
	o = document.getElementsByTagName('iframe');
	for(i=0;i<o.length;i++){
		if (/\bautoHeight\b/.test(o[i].className)){
		setHeight(o[i]);
		addEvent(o[i],'load', doIframe);
		}
	}
}

function setHeight(e){
	try {
		if(e.contentDocument){
			e.height = e.contentDocument.body.offsetHeight + 25;
			$(".carga").fadeOut("fast");
		} else {
			e.height = e.contentWindow.document.body.scrollHeight + 25;
			$(".carga").fadeOut("fast");
		}
	}catch(exception) {
		
	}
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener){
		obj.addEventListener(evType, fn,false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		
		return false;
	}
}

function mueve(donde){
	$('html,body').animate({scrollTop:donde}, 500);
}

/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
 */
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options=$.extend({},options);options.expires=-1;}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}expires='; expires='+date.toUTCString();}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}return cookieValue;}};

/*
 Color animation jQuery-plugin
 http://www.bitstorm.org/jquery/color-animation/
 Copyright 2011 Edwin Martin <edwin@bitstorm.org>
 Released under the MIT and GPL licenses.
*/
(function(d){function i(){var b=d("script:first"),a=b.css("color"),c=false;if(/^rgba/.test(a))c=true;else try{c=a!=b.css("color","rgba(0, 0, 0, 0.5)").css("color");b.css("color",a)}catch(e){}return c}function g(b,a,c){var e="rgb"+(d.support.rgba?"a":"")+"("+parseInt(b[0]+c*(a[0]-b[0]),10)+","+parseInt(b[1]+c*(a[1]-b[1]),10)+","+parseInt(b[2]+c*(a[2]-b[2]),10);if(d.support.rgba)e+=","+(b&&a?parseFloat(b[3]+c*(a[3]-b[3])):1);e+=")";return e}function f(b){var a,c;if(a=/#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(b))c=
[parseInt(a[1],16),parseInt(a[2],16),parseInt(a[3],16),1];else if(a=/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(b))c=[parseInt(a[1],16)*17,parseInt(a[2],16)*17,parseInt(a[3],16)*17,1];else if(a=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(b))c=[parseInt(a[1]),parseInt(a[2]),parseInt(a[3]),1];else if(a=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(b))c=[parseInt(a[1],10),parseInt(a[2],10),parseInt(a[3],10),parseFloat(a[4])];return c}
d.extend(true,d,{support:{rgba:i()}});var h=["color","backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","outlineColor"];d.each(h,function(b,a){d.fx.step[a]=function(c){if(!c.init){c.a=f(d(c.elem).css(a));c.end=f(c.end);c.init=true}c.elem.style[a]=g(c.a,c.end,c.pos)}});d.fx.step.borderColor=function(b){if(!b.init)b.end=f(b.end);var a=h.slice(2,6);d.each(a,function(c,e){b.init||(b[e]={a:f(d(b.elem).css(e))});b.elem.style[e]=g(b[e].a,b.end,b.pos)});b.init=true}})(jQuery);


