// JavaScript Document
var idioma = $("html").attr("lang");


$(document).ready ( function() {
	

	$(document).pngFix();
	$("body").addClass("js");
	$("h2.tit_webs_destacades").hide();

	// Pestanyes, galeria alcalde i webs destacades
	// ---------------------------------------------------------------------------------------------------------------------
	
	 galeria_alcalde();
	 pestanyes();
	 webs_destacades();

	/* treure label Cercador
************************************************************************/

$("#search input").attr("value","")


$("#search input").focus(function(){
			$("#search label").hide();
});

$("#search input").blur(function(){
	if ($(this).attr("value")==""){
		$("#search label").show();
	}else{
		$("#search label").hide();
	}
});

	







/** FancyBox **/

	$("a.iframe").fancybox({
		'padding': 0,
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	800, 
		'speedOut'		:	200,
		'width'         : 915,
		'height'        : 397,
		'overlayColor'  : '#000',
		'overlayOpacity': .85,
		'scrolling' : 'no',
		'href' : this.href,
		'titleShow'	:	false,
		'onStart' : function(){$("object").css("visibility","hidden");},
		'onClosed' : function(){$("object").css("visibility","visible");}
	});
	
		
	$(".video a").click(function() {
		$.fancybox({
				'padding': 0,
				'transitionIn'	:	'elastic',
			    'transitionOut'	:	'elastic',
				'width'		: 640,
				'height'		: 385,
				'overlayColor'  : '#000',
				'overlayOpacity': .85,
				'scrolling' : 'no',
				'type' : 'iframe',
				'titleShow'	:	true,
				'href' : this.href,
				'onStart' : function(){$("object").css("visibility","hidden");},
				'onClosed' : function(){$("object").css("visibility","visible");}
			});
		
		return false;
	});
	
   $("a#plenari").click ( function() {



		window.open(this.href, '', '0, 0, 0, 0, 0, 0, 0');

		

		return(false);



	});

	

/** Cantonades **/
	
	$("#la-ciutat-per .bloc, #tramits #ciutadans, #tramits #empreses").append('<span class="sd"></span><span class="se"></span><span class="id"></span><span class="ie"></span>');
	


 /**Imatge de fons aleatòria **/

	var totalImatges = 14;
	var valorRandom = (Math.floor(Math.random() * totalImatges) + 1);
	var nImatge = "";
	switch(valorRandom){
			case 1: case 2:	case 3:
				nImatge = "01";
			break;
			case 4: case 5: 
				nImatge = "02";
			break;
			case 6: case 7: case 8:
				nImatge = "03";
			break;
			case 9: case 10:
				nImatge = "04";
			break;
			case 11: case 12:
				nImatge = "05";
			break;
			case 13: case 14:
				nImatge = "06";
			break;
		}
		
		$("body:not(#tramits,#webs-bcn)").css("background", "url(../imatges_accessible/img10/bg-body-"+nImatge+".jpg) top center no-repeat #f2f2f2");
		$("ul#menu-principal li.separa a").css("background","url(../imatges_accessible/img10/separador-menu-"+nImatge+".gif) no-repeat  right center transparent");
	/* Alcalde */

retallaAlcalde();
	
});






/*-----------------------------------------------------------------------------------------*/
function montaObjectAlcalde(url,titol,x){
	x.parents("li").siblings("li").animate({opacity:1});
	x.parents("li").children("a").css("cursor","auto");
	x.parents("li").animate({opacity:.2},function(){
												  
	titol=titol.replace("[..]","");											  
											  
	$("#reproductor-alcalde").html('<div><object tabindex="0" title="'+titol+'" type="application/x-shockwave-flash" width="418" height="260" data="'+url+'&amp;autoPlay=false&amp;ubicacio=Home_Alcalde"><param name="movie" value="'+url+'&amp;autoPlay=false&amp;ubicacio=Home_Alcalde" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><p><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&amp;Lang=Spanish&amp;P5_Language=Spanish"><img src="http://www.bcn.cat/imatges_accessible/ca/descarrega_flash.gif" alt="Baixeu-vos el reproductor de Flash." /></a></p></object></div><p>'+titol+'</p>');});
	
}
function retallaAlcalde(){
	$("#galeria a").each(function(){
			titol =$(this).children("strong").text();
			if (titol.length > 67){
				retalla=titol.substr(0,67);
				posult=retalla.lastIndexOf(" ");			
				titol=titol.substr(0,posult)+ " [..] <span>"+titol.substr(posult)+"</span>" ;
				$(this).children("strong").html(titol);	
			}
	});
}

function galeria_alcalde(){



	
/*$("#navegacio-videos-alcalde").addClass("js");
$("#navegacio-videos-alcalde #galeria div").addClass("js");*/

	/*carga el primer video de l'alcalde*/
	  
	var url=$("#galeria a:first").attr("href");
	var titol=$("#galeria a:first").children("strong").text();
	montaObjectAlcalde(url,titol, $("#galeria a:first"));
	
	
	/**/
	
	$("#navegacio-videos-alcalde").append('<img id="sup" src="../imatges_accessible/img10/fletxa-video-amunt.gif" alt="Anterior"  /><img id="inf" src="../imatges_accessible/img10/fletxa-video-abaix.gif" alt="Següent"  /> ');
	
	//$("img#sup, img#inf").animate({opacity:.5});
	
	$("img#sup").css("display","none");
	
	 var contAlc = 0;
	 var totalAlc = $("#navegacio-videos-alcalde ul li").length / 3;
	 
	 
	 
	 if( (Math.round(totalAlc) - totalAlc) >= 0) {totalAlc=Math.round(totalAlc)}
	 else{totalAlc=Math.round(totalAlc)+1};
	 	
		 
	$("img#sup").mouseover(function() {
		$(this).css("cursor","pointer");	
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-amunt-sin.gif" )
		
	}).mouseout(function() {
		
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-amunt.gif" )
	});
	
	$("img#inf").mouseover(function() {
		$(this).css("cursor","pointer");	
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-abaix-sin.gif" );
		
	}).mouseout(function() {
	
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-abaix.gif" );
	});
	
	
	$("img#inf").click(function() {
		
		
		if(contAlc	== 0){$("img#sup:hidden").fadeIn('slow');}			 
		if(contAlc < totalAlc-1){						 
		   
		   $("#navegacio-videos-alcalde ul").animate({"top": "-=382px"}, 'normal');
		   
		   contAlc++;
		 
		   
		   if(contAlc == totalAlc-1){$("img#inf").fadeOut('slow');}
		}
	});
	
	$("img#sup").click(function() {
		
		if(contAlc	< totalAlc){$("img#inf").fadeIn('slow');}
	
		if(contAlc <= totalAlc && contAlc != 0){	
		   $("#navegacio-videos-alcalde ul").animate({"top": "+=382px "}, 'normal');
		  contAlc--;
		}
		if(contAlc == 0){$("img#sup").fadeOut('slow');}
	});


	/* obre video */
	
	$("#galeria a.actiu").unbind('click');

	$("#galeria a").click(function(){
						   
		var url=$(this).attr("href");
		var titol=$(this).children("strong").html();
		montaObjectAlcalde(url,titol, $(this));
		$("#galeria a").css("cursor","pointer");
		$(this).css("cursor","auto");
		
		return false;
		
	});
	
}

function pestanyes(){

/*$("#la-ciutat-per h3, #webs-bcn h3").addClass("js");*/
	 $("#la-ciutat-per .bloc:not(:first), #webs-bcn .bloc:not(:first)").hide();
	 $("#la-ciutat-per h3:first, #webs-bcn h3:first").addClass("activa");
		$("#la-ciutat-per h3, #webs-bcn h3").wrapInner("<span tabindex=\"0\"></span>")
	 
	 
	 $("#la-ciutat-per h3, #webs-bcn h3").each(function(){
		 $(this).click(function(){
				
				$(this).siblings("h3").removeClass("activa");
				$(this).addClass("activa");
				
				$("#la-ciutat-per h3,  #webs-bcn h3").next("div").hide();
				$(this).next("div").show();
											  
		 });
		 $(this).keypress ( function(event) {
				
					if (keyNum(event)==13)
						$(this).siblings("h3").removeClass("activa");
						$(this).addClass("activa");
						
						$("#la-ciutat-per h3,  #webs-bcn h3").next("div").hide();
						$(this).next("div").show();
					});
					function keyNum(e) {
						var keynum;
						if (window.event) keynum = e.keyCode; // IE
						else if (e.which) keynum = e.which; // Estandars
						return(keynum);
					}
			
	 });

}
// script de navegacio webs destacades
	// ---------------------------------------------------------------------------------------------------------------------
function webs_destacades(){

    /*$("#webs-destacadas ul").addClass("js");*/
	$("#webs-destacadas").append('<img id="esq" src="../imatges_accessible/img10/fletxa-video-esq-blau.gif" alt="Anterior"  /><img id="dret" src="../imatges_accessible/img10/fletxa-video-dret-blau.gif" alt="Següent"  /> ');
	
	//$("img#dret, img#esq").animate({opacity:.5});
	
	$("img#esq").css("display","none");
	
	 var cont = 0;
	 var total = $("#webs-destacadas ul li").length / 6;
	 
	 if( (Math.round(total) - total) >= 0) {total=Math.round(total)}
	 else{total=Math.round(total)+1};
	 	
		 
	$("img#esq").mouseover(function() {
		$(this).css("cursor","pointer");
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-esq-blau-sin.gif");
	
	}).mouseout(function() {
		
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-esq-blau.gif");
	});
	
	
	$("img#dret").mouseover(function() {
		$(this).css("cursor","pointer");
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-dret-blau-sin.gif");
		
	}).mouseout(function() {
		
		$(this).attr("src","../imatges_accessible/img10/fletxa-video-dret-blau.gif");
	});
	
	
	$("img#dret").click(function() {
		
		if(cont	== 0){$("img#esq:hidden").fadeIn('slow');}				 
		if(cont < total-1){						 
		   
		   $("#webs-destacadas ul").animate({"left": "-=864px"}, 'normal');
		   
		   cont++;
		   if(cont == total-1){$("img#dret").fadeOut('slow');}
		}
	});
	
	$("img#esq").click(function() {
		if(cont	< total){$("img#dret").fadeIn('slow');}
	
		if(cont <= total && cont != 0){	
		   $("#webs-destacadas ul").animate({"left": "+=864px "}, 'normal');
		  cont--;
		}
		if(cont == 0){$("img#esq").fadeOut('slow');}
	});


	}
