// JavaScript Document
var expand_timer;
var collapse_timer;
var expand_speed = 10;
var current_heigth;
var max_height = 0;

//Crea un pop-up amb el dossier de premsa
function open_dossier() {
	window.open("/files/dossier_popup.html","dossier","location=no,status=no,resizable=no,width=900,height=600");
}


/*------------ FUNCIONS PER TEXTOS DESPLEGAGLES ----------------------------*/
function toggle_expand(div_id) {

  clearInterval(expand_timer);
  clearInterval(collapse_timer);

  if (max_height == 0) max_height = getWrappedDivHeight(div_id) + 20;

  var div = document.getElementById(div_id);
  var height = div.style.height.substring(0,div.style.height.indexOf("px"));
  if (height == max_height) collapse_div(div_id);
  else expand_div(div_id);

}

function expand_div(div_id) {
//alert(div_id);
        var div = document.getElementById(div_id);
        div.style.display = "block";
        div.style.overflow = "hidden";
	expand_timer = setInterval("t_expand('"+div_id+"')",50);
}

function collapse_div(div_id) {
	collapse_timer = setInterval("t_collapse('"+div_id+"')",50);
}

function t_expand(div_id) {
//alert(div_id);
	var div = document.getElementById(div_id);
	tmp =  div.style.height;
        tmp = tmp.substring(0,tmp.indexOf("px"));
        current_height = eval(tmp);
        current_height += expand_speed;
//alert(current_height);
	if (current_height > max_height)  { clearInterval(expand_timer); div.style.height = max_height+'px'; }
	else div.style.height = current_height + "px";
}

function t_collapse(id) {
	var div = document.getElementById(id);
	tmp =  div.style.height;
        tmp = tmp.substring(0,tmp.indexOf("px"));
//alert(tmp);
        current_height = tmp;
	current_height -= expand_speed;
	if (current_height < 1) { clearInterval(collapse_timer); div.style.height = "0px"; }
	else div.style.height = current_height + "px";
}

function showHeight(div_id) {
  alert(document.getElementById(div_id).scrollHeight);
}

function getWrappedDivHeight(div_id) {
    var div = document.getElementById(div_id+"_content");
    return div.scrollHeight;
}


/*----------- FUNCIONS PELS PANELLS MOVIBLES -------------------*/

//Variables de configuracio
var timer = 0;
var wpanel; //ample del panell;
var hpanel; //alcada del panell;
var ttotal; //trigara un segon en moure cada panell

var functionTimer;
var xpos;

var kmov;
var ftotal;
var dxmax;

//L'anomenarem slider! aixo fa uns primers calculs (per que no calculi cada cop)
function initSlider(wide, heigth, duration) {
	wpanel = wide;
	hpanel = heigth;
	ttotal = duration;
   
	kmov = 64 / ttotal;
	ftotal = ttotal / 32;
	dxmax = (2 * wpanel) / ftotal;	   
}

function moveTo(panel) {
	var kpanel = 2; //compensacio per panell
	var ypos = hpanel * (1- panel) - ((panel - 1) * kpanel);
	var container = document.getElementById('container');

	clearTimeout(functionTimer);
	timer = 0;

	container.style["top"] = ypos + "px";
	
	xpos = 0 - wpanel;
	container.style["left"] = xpos + 'px';

	functionTimer = setTimeout("slideContainerTimer()",32);
}

function getDx(t) {
	var dx = 0;
	if (t <= (ftotal / 2)) dx = t * kmov * dxmax;
	else dx = 2 *dxmax - (t * kmov * dxmax)
	return dx;
}

function slideContainerTimer() {
	if (timer <= ftotal) {
		dx = getDx(timer);
		var container = document.getElementById('container');
		xpos += dx;
		container.style["left"] = xpos + 'px';
		timer++;
		functionTimer = setTimeout("slideContainerTimer()",32);
	} else {
		timer = 0;
	} 
}

/*--- funcions pel menu horitzonal de la home ---------------*/
var previous_panel = 0;

function openPanel(panel) {
	var current_menu  = document.getElementById('menu_home_'+panel);
	var previous_menu = document.getElementById('menu_home_'+previous_panel);

	current_menu.className = 'active';
	if (previous_menu!=null) previous_menu.className = 'inactive';
	
	previous_panel = panel;
	
	moveTo(panel);
	
}


/*-------------------------- per truere els accents ---------------*/
String.prototype.renlacc = function(){
	var torem = this;
	alert(this);
	torem = torem.split('');
	toremout = new Array();
	toremlen = torem.length;
	var sec = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž";
	var rep = ['A','A','A','A','A','A','a','a','a','a','a','a','O','O','O','O','O','O','O','o','o','o','o','o','o','E','E','E','E','e','e','e','e','e','C','c','D','I','I','I','I','i','i','i','i','U','U','U','U','u','u','u','u','N','n','S','s','Y','y','y','Z','z'];
	alert(sec);
	alert(rep);
	for (var y = 0; y < toremlen; y++){
		alert(torem[y]);
		alert(sec.indexOf(torem[y]));
		if (sec.indexOf(torem[y]) != -1) {toremout[y] = rep[sec.indexOf(torem[y])];}
		else {toremout[y] = torem[y];}
	}
	toascout = toremout.join('');
	alert(toascout);
	return toascout;
}


/*-- FUNCIO PER DEBUGAR JAVASCRIPT , ES NECESSITA UN DIV HABILITAT PER AIXO -----*/

function debug(text) {
       	var textarea = document.getElementById('debug');
		textarea.innerHTML += text + '<br />'; 
}


/*-- Funció per retardar el la crida a tb_show ---*/
function showbox(command) {
	//alert('tb_show(this,'+command+')');
	setTimeout('tb_show(this,\''+command+'\')',500);
	return true;
}


/*--  Arrangaments generals ----*/
//Fem que quan cliquem sobre el buscar o tingui el focus es seleccioni tot
$(document).ready(function () {$('#edit-keys').click(function() { $(this).select(); }); });

/**
 * Configuracio jQuery 
 */
 
 /** configurant menu esquerra **/
$(document).ready(function() {
	$('.menu-info').fadeTo(0,0);	
	$('.menu a').each( function(menu) {						   
			$(this).mouseover( function() {
					infoId = $(this).attr('id');
					//console.log(infoId);
					//$('.menu-info').stop(true,true);
					$('#desc_'+infoId).fadeTo('normal',1 , function() {
 						   if ($.browser.msie)
							this.style.removeAttribute('filter');
						});
				});
			
			$(this).mouseout( function() {
					infoId = $(this).attr('id');		
					$('#desc_'+infoId).fadeTo('normal',0);
				});
		});
});	