
function ComprovaHash()
{
      var hash = window.location.hash.substr(1);
	  //alert("hash: " + hash);

	  var cadena = hash.split("_");
	  LoadPage(cadena);
	  
}

function LoadPage(cadena)
{
	switch(cadena[0])
	{
		case "default":	$("#IniciPolimedia").delay(500).fadeOut(2000);
						CarregaCos('Inici','','');
						break;
						
		case "":		$("#IniciPolimedia").delay(500).fadeOut(2000);
						CarregaCos('Inici','','');
						break;
						
		case "Inici": 	$("#IniciPolimedia").delay(500).fadeOut(2000);
						CarregaCos('Inici','','');
						break;
						
		case "v":		$("#IniciPolimedia").hide(0);
						ComprovaSiPassword(cadena[1]);						
						break;
		
		case "s":   	$("#FiltreText").val(cadena[1]);
						//alert($("#FiltreText").val());
						FiltraVideo(0);
						break;
		
		default:  		$("#IniciPolimedia").hide(0);
						CarregaCos(cadena[0],cadena[1],cadena[2]);							
						break;
	}
	
				
}

function historyChange(newLocation, historyData) 
{
    //alert("NewLocation: "+newLocation);
	 var cadena = newLocation.split("_");
	 if (cadena != ""){LoadPage(cadena);}else{CarregaCos('Inici','','');}
	window.location.reload;	 
	//alert(historyData);
}


function FuncioEntrada()
{
	$("#MDActiu").val("");
	$("#MEActiu").val("");
	$("#CostatActiu").val("");
	
	$("#FiltreText").val("");
	
//	CarregaSelect("Curs");
//	CarregaSelect("Autor");
//	CarregaSelect("Materia");
	
	//CarregaCos('Inici','','');
	ApuntaEstadistiques();

	dhtmlHistory.initialize();
	if (dhtmlHistory.isFirstLoad()) 
	{
		ComprovaHash();
	}
	else
	{
		dhtmlHistory.addListener(historyChange)
	}
	

}

////////Funció Enter

function submitenter(tipus,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
    {
	    switch (tipus)
	    {
			case 'FiltreText': FiltraVideo(0);
	    }
    } 
}

///////////////////////////Deshabilitar F5
//
//function checkKeyCode(evt)
//{
//
//var evt = (evt) ? evt : ((event) ? event : null);
//var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
//if(event.keyCode==116)
//{
//evt.keyCode=0;
//return false;
//}
//}
//document.onkeydown=checkKeyCode;
//

function InicialitzaMenus()
{
	OcultaMB();
	OcultaMenu('ButtonME','DIVMenuEsquerre','1');
	OcultaMenu('ButtonMD','DIVMenuDret','2');

}

function MostraMB()
{
	$("#DIVMenuBuscador").delay(10).slideDown("slow"); 
	$("#DIVAux1").slideUp("slow");
	document.getElementById("MB").onclick =  function (){OcultaMB();};
	//$("#TDAux1").hide("blind", { direction: "vertical" }, 1000);
	 //$("#heightSI").delay(1000).css("height","458px");
//	 $("#heightSD").delay(1000).css("height","454px");
	 $("#heightSI").slideDown("slow");
     $("#heightSD").slideDown("slow");

}

function OcultaMB()
{
	$("#DIVMenuBuscador").delay(10).slideUp("slow"); 
	$("#DIVAux1").slideDown(1000);
	document.getElementById("MB").onclick =  function (){MostraMB();};
	 //$("#heightSI").delay(1000).css("height","428px");
//	 $("#heightSD").delay(1000).css("height","424px");
	 $("#heightSI").slideUp("slow");
     $("#heightSD").slideUp("slow");

}

function MostraMenu(C,N)
{
	var temps = 0;
	var CA = $("#CostatActiu").val();
	if (CA != "")
	{
		if(CA == 'E')
		{
			OcultaMenu('E','1');
			temps = 500;
				
		}
		else
		{
			OcultaMenu('D','2');
			temps = 500;	
		}	
	}
	
	$("#CostatActiu").val(C);
	//Part Superior
	$("#DIVB"+N ).delay(temps).hide("blind", { direction: "horizontal" }, 500);
	$("#DIVA"+N).delay(temps).show("blind", { direction: "horizontal" }, 500);
	
	//Part Inferior
	$("#DIVCaux"+N).delay(temps).hide("blind", { direction: "horizontal" }, 500);
	$("#DIVC"+N).delay(temps).show("blind", { direction: "horizontal" }, 500);
	
	//Part Lateral
	$("#CA1").hide(500);
	$("#CA2").hide(500);

	
	$("#DIVMenu"+C).delay(temps).show(500);
	
	
	//$("#ContCont").delay(400).effect("size",{from : {width: 855}, to: {width: 718 }  }, 1);
	//$("#"+DIV2).effect("size",{ to: {width: 148,height: 390}  }, 1000);
	
	document.getElementById("ButtonM"+C).onclick =  function (){OcultaMenu(C,N);};
	document.getElementById("P"+C+"1").onclick =  function (){};
	document.getElementById("P"+C+"2").onclick =  function (){};
	document.getElementById("P"+C+"3").onclick =  function (){};
	
}


function OcultaMenu(C,N)
{	
	//$("#ContCont").effect("size",{ from : {width: 718} , to: {width: 855}  }, 1);
	//Part Superior
	$("#DIVA"+N).delay(1).hide("blind", { direction: "horizontal" }, 500);
	$("#DIVB"+N).delay(1).show("blind", { direction: "horizontal" }, 500);

//Part Inferior
	$("#DIVC"+N).delay(1).hide("blind", { direction: "horizontal" }, 500);
	$("#DIVCaux"+N).delay(1).show("blind", { direction: "horizontal" }, 500);
	
	//Part Lateral
	$("#CA1").show(500);
	$("#CA2").show(500);

	$("#DIVMenu"+C).delay(1).hide(500);	
	
	for (var i=1; i<4; i++)	
	{
		$("#Menu"+C+i).slideUp("slow");
	}
	
	document.getElementById("ButtonM"+C).onclick =  function (){MostraMenu(C,N);};
	document.getElementById("P"+C+"1").onclick =  function (){ MostraMenuL(C,'1');};
	document.getElementById("P"+C+"2").onclick =  function (){ MostraMenuL(C,'2');};
	document.getElementById("P"+C+"3").onclick =  function (){ MostraMenuL(C,'3');};
	
	$("#CostatActiu").val("");
	$("#M"+C+"Actiu").val("");
}

function CanviaColorPestanyes(N)
{
	var aux = $("#IMGAactiuE").val();
	
	$("#IMGE"+aux).html('<img src="NewIMG/E'+aux+'0.png">');
	$("#IMGE"+N).html('<img src="NewIMG/E'+N+'1.png">');
	$("#IMGAactiuE").val(N);
	
}

function MostraMenuL(C,N)
{
	if ($("#M"+C+"Actiu").val()!= N)
	{
		CarregaMenuL(C,N);
		
		aux = $("#M"+C+"Actiu").val();
		
		$("#Menu"+C+aux).slideUp("slow");		
		
		$("#Menu"+C+N).slideDown("slow");
		$("#M"+C+"Actiu").val(N);
				
	}	
	
}

function CarregaMenuL(C,N)
{
	switch (C)
	{
		case 'E': 	
					switch (N)
					{
						case '1':	
									$.get("PHP/CarregaMenu.php",{N:N,C:C,taula:'Curs'},LlegadaCarregaMenuL);
									break;
						case '2':	$.get("PHP/CarregaMenu.php",{N:N,C:C,taula:'Autor'},LlegadaCarregaMenuL);
									break;
						case '3':	$.get("PHP/CarregaMenu.php",{N:N,C:C,taula:'Materia'},LlegadaCarregaMenuL);
									break;
					}
					break;
		case 'D': 
					switch (N)
					{
						case '1':	$.get("PHP/CarregaMenuQueEsPolimedia.php",{N:N,C:C},LlegadaCarregaMenuL);
									break;
						case '2':	$.get("PHP/CarregaMenuElsMesVistos.php",{N:N,C:C},LlegadaCarregaMenuL);
									break;
						case '3':	$.get("PHP/CarregaMenuLinks.php",{N:N,C:C},LlegadaCarregaMenuL);
									break;
					}
					break;	
	}
}

function LlegadaCarregaMenuL(data)
{
//	alert(data);
	var cadena = data.split("|");
	$("#Menu" + cadena[1] + cadena[0]).html(cadena[2]);
	//alert(cadena[2]);
}

function FiltraMenuL()
{
	
}

function CarregaCos(Taula,id, page)
{
	var N;
	switch (Taula)
	{
		case 'Curs': N="1";
					break;		
		
		case 'Autor': N="2";
					break;		
		
		case 'Materia': N="3";
					break;		
	}
	CanviaColorPestanyes(N);
	///Aquí s'ha de sel·leccionar la modalitat en la que es veura la informació (llista, resum, previsualització)
	$.get("PHP/CarregaCos.php",{Taula:Taula,id:id,page:page},LlegadaCarregaCos);	
}

function LlegadaCarregaCos(data)
{
	//alert(data);
	var cadena = data.split("|");
	$("#ContCentral").html(cadena[0]);
	
	var hash = cadena[1];
	 
	window.location.hash = cadena[1]+"_"+cadena[2]+"_"+cadena[3];
	if (cadena[2] != "")
	{
		hash = hash+"_"+cadena[2];
	}

	if (cadena[3] != "")
	{
		hash = hash+"_"+cadena[3];
	}


}

function cambiar_color_over(celda){ 
   celda.style.backgroundColor="#e3be9e" ;
} 

function cambiar_color_out(celda,index){ 
  
   var color;
   
   if (index == 1)
	{
		color = "#eeeeee";		
	}
	else
	{
		color = "#cccccc";		
	}
   
   celda.style.backgroundColor =  color; 
} 

function F1_over(DIV,id,m)
{
	$("#IMGRed"+id).show();//html('<img src="NewIMG/IMGRed2.png" />');
	DIV.style.background='url(NewIMG/FondoFichaVideo2'+m+'.jpg)';
}

function F1_out(DIV,id,m)
{
	$("#IMGRed"+id).hide();//html('<img src="NewIMG/IMGRed.png" />');	
	DIV.style.background='url(NewIMG/FondoFichaVideo'+m+'.jpg)';
}

function MostraVideo(id)
{
	$("#Loading").fadeIn("slow");
	$.get("PHP/MostraVideo.php",{id:id},LlegadaMostraVideo);	
}

function LlegadaMostraVideo(data)
{
	var cadena = data.split("|"); 
	
	$("#TitolVideo").html(cadena[0]);
	$("#NomProfeVideo").html(cadena[2]);
    
	var video = '<a href="http://fmstream.uab.es/media/cr-med/Polimedia/'+ cadena[1] +'.mp4" style="display:block;width:564px;height:361px"  id="player"> </a> <script>flowplayer("player", "flowplayer/flowplayer-3.2.7.swf");</script>';
	
	$("#VideoEnFitxa").html(video);
	
		$f("player", "http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf",  {
		
	// clip does not start automatically
	clip: {
		url: 'http://fmstream.uab.es/media/cr-med/Polimedia/'+ cadena[1] +'.mp4',
		autoPlay: true,
		autoBuffering: true,
		scaling: 'orig'
	},
	
	// default controls with the same background color as the page background
	plugins:  {  
		controls:  {
			backgroundColor: '#000000',
			all: false,
			play:true,
			mute:true,
			volume:true,
			fullscreen:true,
			scrubber: true,
			height: 40,
			sliderColor: '#333333',
			progressColor: '#ca000a',
			bufferColor: '#666666',
			autoHide: true,
			autoplay:true, 
			time:true
			
		}
	},
	
	// fiercy red background color with a little gradient and curving
	canvas: {
		backgroundColor: '#ffffff',
		backgroundGradient: [0.3, 0],
		borderRadius: 10
	},
	
	// set screen dimensions so that it goes near the canvas borders
	screen: {
		width: 564,
		height: 352, 
		top: 0
	}
	
});
	
	$("#Loading").fadeOut("slow");
	$("#LlencaVideo").fadeIn(1000);	
	$("#VideoEnFitxa").delay(1000).fadeIn(1000);
	$("#URLVideo").val("http://polimedia.uab.cat/#v_"+cadena[1]);
	
}

function TancaVideo()
{
	$("#VideoEnFitxa").fadeOut(1000);
	$("#VideoEnFitxa").delay(1000).html("");
	$("#LlencaVideo").fadeOut(1000);
	$("#TitolVideo").html("");
	$('#URLVideo').hide(1000);
		
}

function CarregaSelect(Taula)
{
	$.get("PHP/CarregaSelect.php",{Taula:Taula},LlegadaCarregaSelect);	
}

function LlegadaCarregaSelect(data)
{
	var cadena = data.split("|");
	//alert(cadena[1]);
	
	$("#Filtre"+cadena[0]).html(cadena[1]);	
}
 
function FiltraVideo(page)
{
	var f1,f2,f3,f4;
	
	f1 = $("#FiltreText").val();
	f2 = $("#FiltreCurs option:selected").val()	;
	f3 = $("#FiltreAutor option:selected").val();
	f4 = $("#FiltreMateria option:selected").val();
	
	$.get("PHP/Cercador.php",{f1:f1,f2:f2,f3:f3,f4:f4,page:page},LlegadaCarregaCos);
} 

function CarregaMenuPolimedia()
{
	$.get("PHP/PolimediaCarregaMenu.php",{},LlegadaCarregaMenuPolimedia);
}

function LlegadaCarregaMenuPolimedia(data)
{	
	$("#ContCentral").html(data);
	CarregaCosPolimedia(1);
}

function CarregaCosPolimedia(id)
{
	var idAntiga = $("#IdAntiga").val();
	$("#TMP"+idAntiga).css("background-color","#404040");
	$("#TMP"+id).css("background-color","#961500");
	$("#IdAntiga").val(id);
	
	$.get("PHP/PolimediaCarregaCos.php",{id:id},LlegadaCarregaCosPolimedia);
}

function LlegadaCarregaCosPolimedia(data)
{	
	$("#ContPolimedia").html(data);
}

function MostraFitxaContacte()
{
	$.get("PHP/ContacteCarrega.php",{},LlegadaMostraFitxaContacte);
}

function LlegadaMostraFitxaContacte(data)
{
	$("#DadesContacte").html(data);	
	$("#FitxaContacte").fadeIn("slow");

}

function TancaFitxaContacte()
{
		$("#FitxaContacte").fadeOut("slow");
}

function MostraPassword(id)
{
	$("#pass").val("");
	$("#VideoRestringit").fadeIn("slow");
	document.getElementById("AcceptPassword").onclick =  function (){ ComprovaPassword(id);};
}

function ComprovaPassword(id)
{
	var pass = $("#pass").val();
	
	$.get("PHP/ComprovaPassword.php",{pass:pass,id:id},LlegadaComprovaPassword);		
}

function LlegadaComprovaPassword(data)
{
	var cadena = data.split("|");
	if (cadena[1]==1)
	{
		$("#VideoRestringit").fadeOut("slow");
		MostraVideo(cadena[0]);
	}else
	{
		$("#pass").val("");
		$("#ErrorMessage").html();
		$("#ErrorMessage").fadeIn(1000);
		$("#ErrorMessage").delay(3000).fadeOut(1000);

		//MostraPasswordIncorrecte	
	}	
}

function ApuntaEstadistiques()
{
	$.get("PHP/EstadistiquesEntrada.php",{},function(){});		
}

function ComprovaSiPassword(id)
{
	$.get("PHP/ComprovaSiPassword.php",{id:id},LlegadaComprovaSiPassword);		
}

function LlegadaComprovaSiPassword(data)
{
	var cadena = data.split("|")
	
	if (cadena[1] == "0") MostraPassword(cadena[0]);
	else MostraVideo(cadena[0]);	
	
	
}
