
$(window).load(function () {
		//cuando este TODO cargado ajustamos imagen
		$.colocarhome();
		 
		 
		

		
});

$(document).ready(function() {
	/**********************************
	**     SLIDER 2.0 DE LA HOME     **
	**********************************/
	
	$.colocarhome = function() {
			$("#random-image").show();
			

			if(parseInt($("#random-image").attr("ref"))==parseInt('3'))
				$("#random-image").css({'margin-left': 12});
				
			var contentheight = $('#randimg').height();
			if(contentheight<275)
				var topmargin = 275/2-contentheight/2;
			
			if(parseInt($("#random-image").attr("ref"))==parseInt('2'))
				topmargin = topmargin-10;
			$("#randimg").css({'margin-top': topmargin});
			
			var contentwidth = $('#randimg').width();
			if(contentwidth<441)
				var leftmargin = 440/2-contentwidth/2;
			$("#randimg").css({'margin-left': leftmargin});
			
			
			
			$("#randimg2").fadeIn("slow",function(){});
			 $("#randimg").fadeIn("slow",function(){
			$("#randnew_title").fadeIn("slow",function(){
			$("#randnew_content").fadeIn("slow");
			});});
		 
		}
		
	//Desordenamos array
	function fisher_yates(array){
		var i=array.length;
		while(i--){
			var j=Math.floor( Math.random() * (i+1) );
			var tmp=array[i];
			array[i]=array[j];
			array[j]=tmp;
		}
	}	
		
	
				//CONTROL POR IP PARA SWICHEAR HISTORIAS CON NOVEDADES
				//Inicializamos variables
				$.histories= 0;
				$.novetats = 0;
				$.iteracio = 0;
				$.histories_rand= new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
				fisher_yates($.histories_rand);
				$("#menu-text li").removeClass("liselected"); // #permabann
				//Vaciamos el contenido del random-image
				$('#random-image').html("")
				$.cambiarhome= function (){	
					//Leemos el idioma
					var url = document.URL;
					lang = url.split("/");
					
					
					$("#random-image").fadeOut();
					
					if($.iteracio%2==0){
						//Mostramos una historia
						$("#menu-text li#noticies_home").removeClass("liselected");
						$("#menu-text li#histories_home").addClass("liselected");
						$.ajax({
							  url: "http://www.serradetramuntana.net/"+lang[3]+"/inicio_slider/histories/"+$.histories_rand[$.histories%10]+"/",
							  success: function(data) {
								$('#random-image').html(data);
								$('#randimg').load(function() {
									$.colocarhome();
								});
							  }
							});
						
						$.histories++;	
					}else{
						//mostramos una novetat
						$("#menu-text li#histories_home").removeClass("liselected");
						$("#menu-text li#noticies_home").addClass("liselected");
						$.ajax({
							  url: "http://www.serradetramuntana.net/"+lang[3]+"/inicio_slider/novetats/"+$.novetats%10+"/",
							  success: function(data) {
								$('#random-image').html(data);
								$('#randimg').load(function() {
									$.colocarhome();
								});
							  }
							});
						$.novetats++;
					}
					$.iteracio++;
				}
				$.cambiarhome(); // #permabann
				timer = setInterval($.cambiarhome, 10000); // #permabann
				//timer = setInterval($.colocarhome, 1000);
			
		   
		
	/**********************************
	**  FIN SLIDER 2.0 DE LA HOME    **
	**********************************/
	
	$('#firma_enviar').click(function(){
		/*$(this).attr("disabled", "true");*/
	});
	
	//Memorizamos el valor de la imagen de corporatiu
	$.corporatiu = $('.corporatiu_img').attr("src");
	
	$("#randnew_title").hide();
	$("#randnew_content").hide();
	
	function start_capa(langclick) {
		
		if(langclick==null)
		{
			$.lang = $('#enterlang').val();
		} else {
			$.lang = langclick;
		}
		
		if($.lang == 'en' || $.lang == 'es' || $.lang == 'ca') $.lang = 'gl';
		
		$.enlace_ca = Array();
		$.enlace_es = Array();
		$.enlace_en = Array();
		
		$.enlace_ca['cn'] = '加泰';
		$.enlace_es['cn'] = '西班牙语';
		$.enlace_en['cn'] = '英语';
		
		$.enlace_ca['ru'] = 'каталанском';
		$.enlace_es['ru'] = 'испанском';
		$.enlace_en['ru'] = 'английском';
		
		$.enlace_ca['he'] = 'בקטאלנית';
		$.enlace_es['he'] = 'ספרדית';
		$.enlace_en['he'] = 'ואנגלית';
		
		$.enlace_ca['fr'] = 'catalan';
		$.enlace_es['fr'] = 'espagnol';
		$.enlace_en['fr'] = 'anglais';
		
		$.enlace_ca['de'] = 'Katalanisch';
		$.enlace_es['de'] = 'Spanisch';
		$.enlace_en['de'] = 'Englisch';
		
		$.enlace_ca['it'] = 'catalano';
		$.enlace_es['it'] = 'spagnolo';
		$.enlace_en['it'] = 'inglese';
		
		$.enlace_ca['jp'] = 'カタロニア語';
		$.enlace_es['jp'] = 'スペイン語';
		$.enlace_en['jp'] = '英語で';
		
		$.enlace_ca['ar'] = 'والإنجليزية';
		$.enlace_es['ar'] = 'الاسبانية';
		$.enlace_en['ar'] = 'الكاتالوينية';
		
		$.enlace_ca['eu'] = 'katalanez';
		$.enlace_es['eu'] = 'gaztelanian';
		$.enlace_en['eu'] = 'ingelesean';
		
		$.enlace_ca['gl'] = 'catalán';
		$.enlace_es['gl'] = 'castelán';
		$.enlace_en['gl'] = 'inglés';
	   
	   $('#opacitator').animate({height: "100%", opacity: 0.80 }, 1000, 
			function() {
				$('#floater').fadeIn('normal', function(){
					$('#' + $.lang).slideDown();
					$('#' + $.lang).addClass('lactive');
					$('#pdf_idioma').attr('href', 'http://www.serradetramuntana.net/pdf/welcome_' + $.lang + '.pdf');
					$('#enlace_ca').html($.enlace_ca[$.lang]);
					$('#enlace_es').html($.enlace_es[$.lang]);
					$('#enlace_en').html($.enlace_en[$.lang]);
				});
			}
		);
	   
	   $('a.langselector[rel='+$.lang+']').addClass('idiactive');
	   $('#opacitator').click(function()
	   {
			$('#floater').fadeOut('normal', function(){
				$('#opacitator').fadeOut();
				$('#sordos, .lactive').hide();
			});		   
	   });
	
		/*$('#floater, #mapa, #contenido p').click(function(){
			$(this).fadeOut('fast', function(){$('#opacitator').fadeOut();});
			//Llamamos al slider
			$('#slider').s3Slider({
				timeOut: 6000
			});
		})*/
		
		$('a.langselector').click(function() {
			
			$('a.idiactive').removeClass('idiactive');
			$(this).addClass('idiactive');
			
			$.target = $(this).attr('rel');
			
			//$('#sordos').fadeOut();
			
			$('.lactive').slideUp('normal', function()
			{
				$('.lactive').removeClass('lactive');
				$('#' + $.target).slideDown('normal', function()
				{
					$(this).addClass('lactive');
					$('#pdf_idioma').attr('href', 'http://www.serradetramuntana.net/pdf/welcome_' + $.target + '.pdf');
					$('#enlace_ca').html($.enlace_ca[$.target]);
					$('#enlace_es').html($.enlace_es[$.target]);
					$('#enlace_en').html($.enlace_en[$.target]);
				})
			});
		});
		
		$('#enlace_sordos').click(function(){
			$('.lactive').slideUp('normal', function() {
				$('.lactive').removeClass('lactive');
				$('#sordos').addClass('lactive');
				$('a.idiactive').removeClass('idiactive');
				$('#sordos').fadeIn();
				$('#pdf_idioma').attr('href', '#');
			});					   
		});
		
	}
	
	$('#enlace_mapa').click(function()
	{
		start_capa(null);
	});
	$('.enlace_mapa a').click(function()
	{
		start_capa($(this).attr('rel'));
	});
	
	if($('#firstenter').val() == '1')
	{
		start_capa(null);
	}
	else
	{
		$('#slider_static .sliderImage').fadeIn(1000, function(){
			//$('.sliderImage span').slideToggle(300);
			$('.sliderImage span').slideDown(1000, function() {
					//$('.sliderImage span').toggle(300);
			});
			
		});
		
		//$('.sliderImage span').css('display', 'block');
		//$('.sliderImage span').show();
		
	}
	
	//Galeria de fotos
	/*$('a.lightbox').lightBox({
	overlayBgColor: '#FFF',
	txtImage: '',
	txtOf: '/'
   });*/
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'serradetramuntana',
		allow_resize: false,
		slideshow: false, 
		autoplay_slideshow: false, 
		gallery_markup: '',
		show_title: false


		});
	//$.prettyPhoto.open('http://www.serradetramuntana.net/images/pdf.gif','pr','Description');



	
	//$('a.ajaxLink').attr('href', 'javascript:;');
	
	//Iniciamos menu
	$(" #nav ul ").css({display: "none"}); // Opera Fix
	$(" #nav li").hover(function(){
				$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
			},function(){
				$(this).find('ul:first').css({visibility: "hidden"});
	});
	
	//Navegacion por jquery (ajax?)	
	/*$('a.ajaxLink').click(function(){			
		
		$.new_id = $(this).attr('rel');
		$.animation_speed = 300;
		
		//desactivamos activos
		$("a[rel='" + $(".currentPost").attr('id') + "']").removeClass("active");
		
		//abrimos contenido
		$("a[rel='" + $.new_id + "']").addClass('active');
		
		//fade out del post anterior
		$(".currentPost").fadeOut($.animation_speed, function() {
			
			$(".currentPost").removeClass("currentPost");
			
			//fade in nuevo post
			$("#" + $.new_id).fadeIn($.animation_speed);
			
			//asignamos clase "currentPost" al nuevo post
			$("#" + $.new_id).addClass('currentPost');
		
		});

	});*/
	
	//Introducimos elcor
	$('.elcor_ca').flash({
			// test.swf is the flash document   
			swf: '../swf/FLVPlayer_Progressive.swf',height:253,width:450,
			// these arguments will be passed into the flash document
			flashvars: {
				MM_ComponentVersion: '1',
				skinName: '../swf/Clear_Skin_1',
				streamName: 'Spot_Serra_10_HD_OK_CA',
				autoPlay: 'true',
				autoRewind: 'false'
			}
	});
	
	$('.elcor_es').flash({
			// test.swf is the flash document   
			swf: '../swf/FLVPlayer_Progressive.swf',height:253,width:450,
			// these arguments will be passed into the flash document
			flashvars: {
				MM_ComponentVersion: '1',
				skinName: '../swf/Clear_Skin_1',
				streamName: 'Spot_Serra_10_HD_OK_ES',
				autoPlay: 'true',
				autoRewind: 'false'
			}
	});
	
	//Slider_control

	
	
	$('#controlslider').click(function(){
									if($.session("mostrarslider")=="false"){
										mostrarslider();
									}else{
										esconderslider();
									}
										
									});
	function mostrarslider()
	{
		/*$('#slidersubstitute').hide();
		$('#slidercontenedor').slideDown();	
		$.session("mostrarslider","true");
		actualiza_botones_cslider()
		$('.sliderImage').css("display","none");*/
		$.session("mostrarslider","true");
		location.reload();
		
	}
	function esconderslider()
	{
		$('#slidercontenedor').slideUp();
		$('#slidersubstitute').show();
		$.session("mostrarslider","false");
		actualiza_botones_cslider()
		$('#slider').s3Slider({
			parado: true
		});
		
	}
	if($.session("mostrarslider")=="false"){
		$('#slidercontenedor').hide();
		$('#slidersubstitute').show();
		$.session("mostrarslider",false);
		actualiza_botones_cslider()
	}else{
		$('#slidercontenedor').show();
		$('#slidersubstitute').hide();
		$.session("mostrarslider",true);
		actualiza_botones_cslider()
	}
	
	
	$('#controlslider').mouseover(function(){
								if($.session("mostrarslider")=="false"){
							    	$("#controlslider").attr("src", "/images/cabecera/up_over.png");
								} else {
									$("#controlslider").attr("src", "/images/cabecera/down_over.png");
								}
								
							   });
	$('#controlslider').mouseout(function(){
								actualiza_botones_cslider()
								
							   });
	
	function actualiza_botones_cslider(){
		if($.session("mostrarslider")=="false"){
			$("#controlslider").attr("src", "/images/cabecera/up_out.png");
		} else {
			$("#controlslider").attr("src", "/images/cabecera/down_out.png");
		}	
	}
	
	//Ampliación y reducción de letra!
	if($.session("letra")==undefined)
	{
		$.session("letra",100);	
	} else {
		$("#container *").css("font-size", $.session("letra")+"%");
	}
	
	$('.letramenos').click(function(){
									 if( $.session("letra")>90)
									 {
										 $.session("letra",parseInt($.session("letra"))-4);
										 $("#container *").css("font-size", $.session("letra")+"%"); 
									 }
									});
	$('.letramas').click(function(){
								   	if( $.session("letra")<110)
									{
										$.session("letra",parseInt($.session("letra"))+4);
										$("#container *").css("font-size", $.session("letra")+"%");
									}
								   });
	$('.letradefault').click(function(){
									  	$.session("letra",100);
										$("#container *").css("font-size", "");
									  });
	
	//Corporatiu
	$('.corporatiu_link').mouseover(function(){
									$.tempimg = $(this).attr("rel");
									$('.corporatiu_img').fadeOut('fast',function(){
											$('.corporatiu_img').attr("src",$.tempimg);
											$('.corporatiu_img').load(function(){
												$('.corporatiu_img').attr("heigh","300px");
												$('.corporatiu_img').css("heigh","300px");
												$('.corporatiu_img').fadeIn('slow');
											});
										});
									});
	/*$('.corporatiu_link').mouseout(function(){
									$('.corporatiu_img').fadeOut('fast',function(){
										$('.corporatiu_img').attr("src",$.corporatiu);
										$('.corporatiu_img').load(function(){
											$('.corporatiu_img').attr("heigh","300px");
											$('.corporatiu_img').css("heigh","300px");
											$('.corporatiu_img').fadeIn('fast');
											});
										});
									});*/

	$('.categoria_enllas').hide('fast');
	$('.titulo_categoria_enllas').click(function(){
												 	
			$('.categoria_enllas').slideUp('fast');
			$('.categoria_enllas[rel='+$(this).attr('rel')+']').slideDown('slow');
	});
	
});



//comprobación navegador
var browser_type=navigator.appName
var ieversion=/*@cc_on function(){ switch(@_jscript_version){ case 1.0:return 3;
      case 3.0:return 4;case 5.0:return 5; case 5.1:return 5; case 5.5:return 5.5;
      case 5.6:return 6; case 5.7:return 7; default: return 8; }}()||@*/0;


// Si es una versión anterior a internet explorer 7
if (browser_type=="Microsoft Internet Explorer" && ieversion < 7) {
window.location.replace("http://www.serradetramuntana.net/ca/navegadorinvalido/")
}
//si es ie7
if (browser_type=="Microsoft Internet Explorer"&& ieversion == 7) {
document.write("<link rel='stylesheet' type='text/css' href='css/ie7.css'>");
}
/*
if (browser_type=="Microsoft Internet Explorer"&& ieversion >= 8) {
document.write("<link rel='stylesheet' type='text/css' href='css/ie8.css'>");
}*/

