
// 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");
	
	DD_roundies.addRule('.caja', '6px', true);
	DD_roundies.addRule('.caja2', '6px', true);	
	
	/*$("h2").FontEffect({
		shadow:true, 
		shadowColor :"#1E4F3A", 
		shadowOffsetTop :2, // The top offset position (px) 
		shadowOffsetLeft:2, // The left offset position (px) 
		shadowBlur :1, // The shadow blur 1=none,2=low,3=high 
		shadowOpacity :0.05   
	});
	$(".tabs-nav a span").FontEffect({
		shadow:true, 
		shadowColor :"#000", 
		shadowOffsetTop :1, // The top offset position (px) 
		shadowOffsetLeft:1, // The left offset position (px) 
		shadowBlur :1, // The shadow blur 1=none,2=low,3=high 
		shadowOpacity :0.1   
	});
	$("h3 span").FontEffect({
		shadow:true, 
		shadowColor :"#000", 
		shadowOffsetTop :1, // The top offset position (px) 
		shadowOffsetLeft:1, // The left offset position (px) 
		shadowBlur :1, // The shadow blur 1=none,2=low,3=high 
		shadowOpacity :0.05   
	});
	$(".organiza").FontEffect({
		shadow:true, 
		shadowColor :"#1E4F3A", 
		shadowOffsetTop :1, // The top offset position (px) 
		shadowOffsetLeft:1, // The left offset position (px) 
		shadowBlur :1, // The shadow blur 1=none,2=low,3=high 
		shadowOpacity :0.05   
	});*/


	$(".with-tooltip").simpletooltip();
	
	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 pestaas
	$('#pestanas').tabs(parseInt($.cookie('seccion')),{ fxSlide: true, fxFade: true, fxSpeed: 'fast'});
	
	// Activamos la pestaa 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;					   
	});
	
	// 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 pestaa 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){
			// 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>");									
				});
			}*/
			doIframe();
		}
	});	
}

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;
		} else {
			e.height = e.contentWindow.document.body.scrollHeight + 25;
		}
	}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;
	}
}

/**
* 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;}};


