//global vars
var i = 0;

$(function(){
		
	combo(); //cargo el select de area dinamicamente dependiendo del nivel

	programacio(); //mostrar/ocultar texto en programacio educativa
	
	confirmar(); //para confirmar enlaces con el texto "Estas segur?"

	if(document.getElementById("fitxa")) {
		playerControlers();
	}
	
	//inserto el flash
	/*$('#flash').flash({
	    src: 'banner2_.swf',
	    width: 460,
	    height: 46
	});*/

	if(document.getElementById("publisher")) {
		publisherCopy();
	}

	if($.browser.mozilla == true) {

		$("#template .resultat > div.marc > .left").css("margin-top","3px");
		}

	if($.browser.msie != true) {
		$("#template .resultat > div.marc > .left").css("margin","3px 0px 0px 0px !important");
	}
	
	//mostrar/ocultar recursos audiovisuales
	$('#toggle-recursos a').click(function(){
		if ($(this).parent().next().is(':hidden')){
			var tmp = $(this).attr('rel');
			
			$(this).attr('rel',$('span',this).html())
			$('span',this).html(tmp);
			
			$('img',this).attr('src','/img/obert.png');
			
			$(this).parent().prev().slideUp();
			$(this).parent().next().slideDown();
		}
		else{
			var tmp = $(this).attr('rel');
			$(this).attr('rel',$('span',this).html());
			$('span',this).html(tmp);
			
			$('img',this).attr('src','/img/tancat.png');
			
			$(this).parent().prev().slideDown();
			$(this).parent().next().slideUp();
		}
		return false;
	});
	
	$('#toggle-recursos.open a').each(function(){
		var tmp = $(this).attr('rel');
		
		$(this).attr('rel',$('span',this).html())
		$('span',this).html(tmp);
		
		$('img',this).attr('src','/img/obert.png');
		
		$(this).parent().prev().slideUp();
		$(this).parent().next().slideDown();
	})
	
	//acces professors
	if (document.getElementById('professors_acces')) document.getElementById('professors_acces').href = 'PORTAL.wwsec_app_priv.login?p_requested_url=' + escape(location.href);
	
	//left border
	$('td[@width="2%"]').css('border-right','1px solid #eee');
	
	//clear at .audiovisuals
	$('div.audiovisuals:nth(0), div.audiovisuals:nth(3), div.audiovisuals:nth(6), div.audiovisuals:nth(9)').css('clear','left');
	
	//incremento y decremento fuente
	font();
	
	//imprimir
	$("#fitxa-print").click(function(){
		window.print();
		return false;
	});
	
	//advanced search
	$('#cercador.cerca > a').toggle(function(){
		$('#cercadoravbt').show();
		$('#cercador.cerca > form').show();
		$('#cercadorav_on',$(this).parent().parent()).slideUp('slow');
	},function(){
		$('#cercador.cerca > form').hide();
		$('#cercadorav_on',$(this).parent().parent()).slideDown('slow');
		$('#cercadoravbt').hide();
	});
		
	$('#cercador').not('.cerca').find('a').toggle(function(){
		$('#cercadoravbt').hide();
		$('#cercador > form').hide();
		$('#cercadorav',$(this).parent().parent()).slideDown('slow',function(){
			$('div.selectArea').remove();
			$('div.optionsDivInvisible').remove();
		});
		$('#cercadorav',$(this).parent().parent()).show();
		$('#cercadorav input').show();
	},function(){
		$('#cercador > form').show();
		$('#cercadorav',$(this).parent().parent()).slideUp('slow',function(){ $('input,select').show(); });
		$('#cercadoravbt').show();	
	});
	
	
	$('#containerbg').corner();

	//if (document.getElementById('cercadorav')) $('#cercadorav #form-cercadorav > div > div').corner();


	if (navigator.appName != "Microsoft Internet Explorer"){
		if (document.getElementById('cercadorav_on')) $('#cercadorav_on #form-cercadorav > div > div').corner();
		$('div.programaciotv, #text').corner();
		$('#programacio_dies li:first').corner('tl,bl');
		$('#programacio_dies li:last').corner('tr,br');
		$('div.programaciotv > ul .canal:first').corner('tr');
	}
	
	$('#template .programaciotv li:last-child div.programa, #main div.resultat:last').css('border','none');
	
	//filtros
	$('#form-cercadorav > div :radio').click(function(){
		if (this.value == 2){
			//oculto nivell
			$('#nivell,#area').css('visibility','hidden');
			$('#ambit').css('visibility','visible');
		}
		else{
			//oculto ambit
			$('#ambit').css('visibility','hidden');
			$('#nivell,#area').css('visibility','visible');
		}
	});
	
	//cuando cambiamos algun select, ocultamos el otro
	$('#form-cercadorav > div select').change(function(){		
		if ($(this).parent().find(":radio").attr("value") == 2){
			//marco radio ambit
			$(this).parent().find(":radio").attr("checked","checked");
			//oculto nivell
			$('#nivell,#area').css('visibility','hidden');
			$('#ambit').css('visibility','visible');
		}
		else{
			//marco radio nivell
			$(this).parent().find(":radio").attr("checked","checked");
			//oculto ambit
			$('#ambit').css('visibility','hidden');
			$('#nivell,#area').css('visibility','visible');
		}
	});
	
	//miramos si hay algun radio checked, y ocultamos el otro
	$('#form-cercadorav > div :radio:checked').each(function(){	
		//ambit
		if (this.value == 2){
			//oculto nivell
			$('#nivell,#area').css('visibility','hidden');
		}
		else{
			//oculto ambit
			$('#ambit').css('visibility','hidden');
		}
	});

	//$('#cercador, #form-cercadorav').onkeydown = function(e) {

	if(document.getElementById("experiencia-form")) {
		$("#experiencia-form").append("<input type=\"hidden\" name=\"disableSubmit\" id=\"disableSubmit\" value=\"0\" />");

		$("#experiencia-form").bind("click",function() {
			$("#disableSubmit").attr("value","1");
		});
		$("#form-cercador").bind("click",function() {
			$("#disableSubmit").attr("value","0");
		});
	}

	//fer submit al clickar enter
	if (navigator.appName == "Microsoft Internet Explorer"){
		document.onkeydown = function(e) {	        
			if (e == null) { // ie
       	   		keycode = event.keyCode;
        		} 
			/*else { // mozilla
          			keycode = e.which;
        		}*/
			
			if (keycode == 27 ||  keycode == 13){
				if($("#disableSubmit").val() == "0") {
				//document.onkeydown = null;
				if ($('#cercadorav').is(':visible') || $('#cercadorav_on').is(':visible')){
					$('#form-cercadorav').submit();
				}
				else {
					$('#form-cercador').submit();
				}
				}
			}
		}
	}
	
	//hacer favorito
	$('#homepage').click(function(){
			if (document.all) window.external.AddFavorite('http://www.edu3.cat', 'Edu3.cat'); else window.sidebar.addPanel('Edu3.cat', 'http://www.edu3.cat','');
		});					 
				 
	//formulari intruduir experiencies
	$('#experiencia-form input[@type="image"]').click(function(){
		if (!$('input[@value="accepto-condicions"]').is(':checked')){
			alert("Cal llegir i acceptar les condicions d's");
			return false;		
		}
		else return true;
	})

	//areas de fitxa video
	areaVideo();
	
	//copiar codi
	$('.intro-code textarea').focus(function(){
		this.select();
	})
	
	//tornar de condicions
	$('.tornar').click(function(){
		history.back();
	})
});



function font(){
		
		//incremento de fuente
		$("#fitxa-ampliar").click(function(){
			i++;
			if (i>3) i=3;
			switch(i){
				case 0: j=100; break;
				case 1: j=110; break;
				case 2: j=120; break;
				case 3: j=130; break;
			}
			
			$(".caixa, #fitxa ul, #guia-didactica ul, #consulta-exp ul, #info p").css("fontSize",j + "%");
			
			/*document.cookie = "fwi_fontSize=" + j + "% 'Trebuchet MS'";
			document.cookie = "var_i=" + i;*/
			
			return false;
		});
		
		//decremento de fuente
		$("#fitxa-reduir").click(function(){
			i--;
			if (i<0) i=0;
			switch(i){
				case 0: j=100; break;
				case 1: j=110; break;
				case 2: j=120; break;
				case 3: j=130; break;
			}
	
			$(".caixa, #fitxa ul, #guia-didactica ul, #consulta-exp ul, #info p").css("fontSize",j + "%");			
						
			/*document.cookie = "fwi_fontSize=" + j + "% 'Trebuchet MS'";
			document.cookie = "var_i=" + i;*/
			
			return false;
		});
	}
	
function combo(){
	$('#nivell').change(function(){
		$.get("edu3tv.edu3_pkg_portlets_cerca.SelectsNivells",
		  { p_niv: $(this).val() },
		  function(data){
			$('#area').html(data);
		  }
		);
	});
}


function programacio(){
	
	var xheight = 62;
	//if (navigator.userAgent.indexOf('MSIE 7.0')+1 ) xheight = 70;
	
	//miro altura
	$('div',$('.programa')).each(function(){
		$(this).attr('rel',$(this).height()).attr('estado','cerrado');
		
		$(this).height(xheight);
		
		//si el texto tiene mas de 2 lineas añado enlace
		if (parseInt($(this).attr('rel')) >= 77) {
			//$('h4',this).html($('h4',this).html() + ' <a class="programa-detall" href="#"><img alt="text complet" src="/img/bullet.png"/></a>');
			$(this).parent().html( $(this).parent().html() + ' <a class="programa-detall" href="#"><img alt="text complet" src="/img/tancat.png"/></a>' );
		}
	});
	
	//div h4 a
	$('a.programa-detall',$('.programa')).click(function(){
		var div = $(this).prev();
		var divHeight = parseInt(div.attr('rel')) + 16; //16 de la altura del icono
		
		if (div.attr('estado') == 'cerrado'){
			if (navigator.userAgent.indexOf('MSIE 6.0')+1 && divHeight >= 77) divHeight = Math.round(divHeight*1.22);
			else if (navigator.userAgent.indexOf('MSIE 7.0')+1 && divHeight >= 77) divHeight = Math.round(divHeight*1.1);
			
			div.animate({height: divHeight},500);
			div.removeAttr('estado').attr('estado','abierto');
			$(this).html('<img alt="text complet" src="/img/obert.png"/>');
		}
		else{
			div.animate({height: 62},500);
			div.removeAttr('estado').attr('estado','cerrado');
			$(this).html('<img alt="text complet" src="/img/tancat.png"/>');
		}
		
		
		//var div = $(this).parent().parent();
		
		
		
		/*if (div.attr('estado') == 'escondido'){ 
			div.animate({height: divHeight},500);
			div.removeAttr('estado');
		}
		else{ 
			div.animate({height: xheight},500);
			div.attr('estado','escondido');
		}*/
		
		return false;
	})
}

function areaVideo(){
	if (document.getElementById('fitxa')){
		var li = $('#fitxa li.area');
		h_default = 15;
		
		if (navigator.userAgent.indexOf('MSIE 6.0')+1) h_default = 30;
		
		if (li.height() > h_default){ //escondo
			li.attr('altura',li.height()).attr('estado','cerrado');
			li.height(h_default).addClass('escondido'); 
			li.html( li.html() + ' <a class="area-detall" href="#"><img alt="text complet" src="/img/tancat.png" /></a>' );
		}
	}
	
	//click
	$('.area-detall').click(function(){
		var li = $(this).parent();
		var altura = li.attr('altura');
		
		if (li.attr('estado') == 'cerrado'){
			li.animate({height: altura},500);
			li.removeAttr('estado').attr('estado','abierto');
			$(this).html('<img alt="text complet" src="/img/obert.png" />');
		}
		else{
			li.animate({height: h_default},500);
			li.removeAttr('estado').attr('estado','cerrado');
			$(this).html('<img alt="text complet" src="/img/tancat.png" />');
		}
		
		return false;
	})
	
}

function confirmar(){
	$('.confirmar-despublicar').click(function(){
		return confirm("Segur que voleu deixar de publicar les dades personals?");	
	})	

	$('.confirmar-eliminar').click(function(){
		return confirm("Segur que voleu eliminar l'experincia?");	
	})	
}

function publisherCopy() {




$("#copyCode1").hover(function() {
var clip = new ZeroClipboard.Client();
clip.glue("copyCode1");
clip.setText($("#widgetCode").val());
},function() { return true; });
$("#copyCode1").click(function() {
clip.setText($("#widgetCode").val());
});

$("#wordpress").hover(function() {
var clip3 = new ZeroClipboard.Client();
clip3.glue("wordpress");
clip3.setText($("#widgetCode").val());
},function() { return true; });
$("#wordpress").click(function() {
clip3.setText($("#widgetCode").val());
});

$("#widgetCode").hover(function() {
var clip2 = new ZeroClipboard.Client();
clip2.glue("widgetCode");
clip2.setText($("#widgetCode").val());
},function() { return true; });
$("#widgetCode").click(function() {
$(this).focus();
$(this).select();
clip2.setText($("#widgetCode").val());
});

$("#publisher ul li a").hover(function(){
$(this).parent().append("<span>"+$(this).attr("rel")+"</span>");
},function(){
$("#publisher ul li").find("span").remove();
});

$("#publisher ul li a.urlGO").click(function(){
return true;
});




}


function playerControlers() {
	$("#info p").not(":first-child").hide();
	$(".socials .embed").click(function() {
		if($("#share").is(":visible")) {
			$("#share").slideUp("slow",function() {
			if($("#embed").is(":hidden")) {
				$("#embed").slideDown("slow");
				var clip = new ZeroClipboard.Client();
clip.glue("copyCode1");
clip.setText($("#widgetCode1").val());

$("#widgetCode1").click(function() {
$(this).focus();
$(this).select();
var clip = new ZeroClipboard.Client();
clip.glue("widgetCode1");
clip.setText($("#widgetCode1").val());
});

var clip2 = new ZeroClipboard.Client();
clip2.glue("copyCode2");
clip2.setText($("#widgetCode2").val());

$("#widgetCode2").click(function() {
$(this).focus();
$(this).select();
var clip2 = new ZeroClipboard.Client();
clip2.glue("widgetCode2");
clip2.setText($("#widgetCode2").val());
});
			}
			else {
				$("#embed").slideUp("slow");
			}

		});
		}
		else {
			if($("#embed").is(":hidden")) {
				$("#embed").slideDown("slow");
				var clip = new ZeroClipboard.Client();
clip.glue("copyCode1");
clip.setText($("#widgetCode1").val());

$("#widgetCode1").click(function() {
$(this).focus();
$(this).select();
var clip = new ZeroClipboard.Client();
clip.glue("widgetCode1");
clip.setText($("#widgetCode1").val());
});

var clip2 = new ZeroClipboard.Client();
clip2.glue("copyCode2");
clip2.setText($("#widgetCode2").val());

$("#widgetCode2").click(function() {
$(this).focus();
$(this).select();
var clip2 = new ZeroClipboard.Client();
clip2.glue("widgetCode2");
clip2.setText($("#widgetCode2").val());
});
			}
			else {
				$("#embed").slideUp("slow");
			}
		}
		return false;
	});
	$("#embed .opener").click(function() {
		$("#embed").slideUp("slow");
		return false;
	});

	$(".socials .share").click(function() {
		if($("#embed").is(":visible")) {
			$("#embed").slideUp("slow",function() {
						if($("#share").is(":hidden")) {
				$("#share").slideDown("slow");
			}
			else {
				$("#share").slideUp("slow");
			}

			});
		}
		else {
			if($("#share").is(":hidden")) {
				$("#share").slideDown("slow");
			}
			else {
				$("#share").slideUp("slow");
			}
		}
		return false;
	});
	$("#share .opener").click(function() {
		$("#share").slideUp("slow");
		return false;
	});
	$("#info .opener").click(function() {
		var thisClass = $(this).attr("class").split(" ")[1];
		if(thisClass == "open") {
		$(this).addClass("close").removeClass("open").attr("title","Tancar");
		$("#info p:hidden").slideDown("slow");
		}
		if(thisClass == "close") {
		$(this).addClass("open").removeClass("close").attr("title","Obrir");
		$("#info p").not(":first-child").slideUp("slow");
		}
	return false;
	});

}
