// Executa el codi quan la pàgina ja està carregada 
// (sense esperar les imatges com el onLoad)
$(document).ready(function() {
	// Enllaços ràpids
    $('#Opcions>li:last').addClass("ultim");
    $('#LinksPeu>li:last').addClass("ultim");
    // Elimina el border-rigth del últim element dels enllaços ràpids de capçalera
	$("#LinksPeu li:last-child").addClass("NoBarra");
	// Afegeix l'estil "sel" a l'ultim enllaç del Fil d'Ariadna
	$("#Fil .cont a:last").addClass("sel");
	
    // Calendari per formularis
	$("body").append("<div id='CalendariDiv'></div>");
	// Div per mostrar errors de formualri
	$("body").append("<div id='Error_msg'></div>");
	
	// Estableix l'acció d'imprimir per l'enllaç amb id="btn_imprimir"
	$("#btn_imprimir").click(
		function(e) { 
			self.print();
		}
	);
	
	// Estableix l'acció de canvi de mida de font per l'enllaç amb id="canviaFont"
	$("#canviaFont").bind("click keydown", function(e) {
			canviaFont(1);
		}
	);
	
	// Tots els tags <a> amb el target="_finestra*" 
	$("a[target^='_finestra']").each(
		function() {
			// Obté el href
			var link = $(this).attr("href");
			
			// Assigna href="#" per deshabilitar el link
			$(this).attr("href","#");
			
			// Li assigna l'event onClick per obrir una finestra apuntant al href original
			$(this).bind("click keydown", function(e){
				DoModal(link, 660, 495);
				return false;
			});
		}
	);
	
	// Estableix el rollover sobre imatges
	$(".swapImg").hover(
		function () {
			CanviSrcImg(this);
		}, 
		function () {
			CanviSrcImg(this);
		}
    );
	
	// Mostra el títol en els portlets de llistat d'imatges
    $("ul.Llista_Portada_imatges li").hover(function(){
        $('p',this).show();
    }, function(){
    	$('p',this).hide();
    });
    
    // Botons de formulari
	$(".Tbutton").after("<img src=\"/img/boto-d.gif\" alt=\"\" class=\"Tbuttonimg\" />");
	$(".Tbutton").before("<img src=\"/img/boto-e.gif\" alt=\"\" class=\"Tbuttonimg\" />");
	if ($.browser.mozilla) {
		$(".Tbutton").css("margin-bottom", "-1px");
	} else if ($.browser.msie) {
		$(".Tbutton").css("margin-bottom", "-2px");
	}
	$(".Tbutton_Login").after("<img src=\"/img/boto-login-d.gif\" alt=\"\" class=\"Tbuttonimg\" />");
	$(".Tbutton_Login").before("<img src=\"/img/boto-login-e.gif\" alt=\"\" class=\"Tbuttonimg\" />");
	if ($.browser.mozilla) {
		$(".Tbutton_Login").css("margin-bottom", "-1px");
	} else if ($.browser.msie) {
		$(".Tbutton_Login").css("margin-bottom", "-2px");
	}
	
	// Estableix un text d'exemple extret de les label als inputs de Login i la cerca
	$("#log_email").clearingInput();
	$("#log_password").clearingInput();
	$("#cer_cerca").clearingInput();
	$(".cerca_adreca").clearingInput(); 
});


// Executa el codi quan la pàgina i les imatges estan carregades 
$(window).load(function () {
	
	$(".personatge_llista").css("visibility", "visible");
	$("ul.Llista_descarregues li").css("visibility", "visible");
	 
	// build menu with ID="Menu" on page:
	jquerycssmenu.buildmenu("Menu", arrowimages);
	
	// Afegeix el botó de play ens les imatges dels vídeos
	$("a.video").each(
		function(){
			$(this).css('width', $('img', this).css('width'));
			$(this).css('height', $('img', this).height() + 'px');
			
			$(this).append("<img src='/img/fons_play.png' class='fons_video' alt='' />");
		}
	);
	
	// Passa les imatges d'un contenidor RoundCanvas al background per poder
	// aplicar l'arrodoniment
	$("li.RoundCanvas>a>img").each(function() {
		$(this).parent().parent().css('height', $(this).height() + 'px');
		$(this).css('visibility','hidden');
		$(this).parent().parent().css('background','url("' + $(this).attr('src') + '") left top no-repeat');
	});
	$("ul.Llista_descarregues>li.RoundCanvas>a>img").each(function() {
		$(this).parent().parent().css('height', '88px');
	});
		
	// Lateral personatges (web oficial, tendes)
	$("div.RelPersonatge>a>img").each(function() {
		//$(this).parent().parent().css('height', ($(this).height() + 26) + 'px');
		$(this).parent().css('display', 'block');
		$(this).parent().css('height', ($(this).height()) + 'px');
		
		$(this).css('visibility','hidden');
		$(this).parent().css('background','url("' + $(this).attr('src') + '") left top no-repeat');
	});
	
	$("a.RoundCanvas>img").each(function() {
		$(this).parent().css('height', $(this).height() + 'px');
		$(this).css('visibility','hidden');
		$(this).parent().css('background','url("' + $(this).attr('src') + '") left top no-repeat');
	});
	$("div.RoundCanvas>img").each(function() {
		$(this).parent().css('height', $(this).height() + 'px');
		$(this).css('visibility','hidden');
		$(this).parent().css('background','url("' + $(this).attr('src') + '") left top no-repeat');
	});
	$("div.RoundCanvas_Sup>img").each(function() {
		$(this).parent().css('height', $(this).height() + 'px');
		$(this).css('visibility','hidden');
		$(this).parent().css('background','url("' + $(this).attr('src') + '") left top no-repeat');
	});
	$("li.RoundCanvas>img").each(function() {
		$(this).parent().css('height', $(this).height() + 'px');
		$(this).css('visibility','hidden');
		$(this).parent().css('background','url("' + $(this).attr('src') + '") left top no-repeat');
	});
	
	
	// Round Corners
	var settings2 = {
			tl: { radius: 7 },
	        tr: { radius: 7 },
	        bl: { radius: 0 },
	        br: { radius: 0 },
	        antiAlias: true
	};
	$('.RoundCanvas_Sup').corner(settings2);
	
	var settings3 = {
			tl: { radius: 0 },
	        tr: { radius: 0 },
	        bl: { radius: 7 },
	        br: { radius: 7 },
	        antiAlias: true
	};
	$('.RoundCanvas_Inf').corner(settings3);
	
	var settings = {
		tl: { radius: 7 },
        tr: { radius: 7 },
        bl: { radius: 7 },
        br: { radius: 7 },
        antiAlias: true
	};
	$('.RoundCanvas').corner(settings);
});


function mostraVideoDescarrega(titol, video, width, height) {
	jwplayer('Video_Gran_Div').setup({
		flashplayer: '/comu/jwplayer/player.swf',
		file: video,
		image: '/media/thumbnails/' + video.replace('.flv', '.png').replace('/media/videos/', ''),
		height : height,
		width: width,
		skin:'/comu/jwplayer/glow.zip',
		events: {
			onPlay: function () {
				if (video_act!=this.config.file) {
				    video_act = this.config.file;
			    	incrementaStats(0, this.config.file, '', 'V');
				}
			}
		}
	});
	$('#Video_Gran_Div_wrapper').append("<p>" + titol + "</p>");
}


function CanviSrcImg(elem) {
	var classes = $(elem).attr("class").split(" ");
	if (classes.length>0) {
		var src_aux;
		if ($(elem).find("img").get(0)) {
			src_aux = $(elem).find("img").get(0).src;
			$(elem).find("img").get(0).src = classes[1];
			$(elem).removeClass(classes[1]);
			$(elem).addClass(src_aux);
		}
	}
}

var midafont = 0.8;
function canviaFont(accio) {
	aux = midafont;
	if (accio==1)
		aux = aux + 0.25;
	else if (accio==-1)
		aux = aux - 0.25;
	else if (accio==0)
		aux = 0.8;
	
	if (aux>1.4)
		aux = 0.8;
	
	if (aux>0.5 && aux<=1.6) {
		midafont = aux;
		document.body.style.fontSize = midafont + 'em';
	}
} 

function afegirFavorits(url, titol) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(titol, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, titol); 
	} else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}
