//alert("entro amagarForm")

function clipejar(obj,valor,dir)
	{
	var clipv = obj.style.clip.split("rect(")[1].split(")")[0].split("px");
	if (navigator.appVersion.indexOf("MSIE")==-1){
		clipv[1]=clipv[1].substring(2,clipv[1].length);
		clipv[2]=clipv[2].substring(2,clipv[2].length);
		clipv[3]=clipv[3].substring(2,clipv[3].length);
	}
	clipv[2]=(parseInt(valor)*dir)+parseInt(clipv[2]);
	obj.style.clip= "rect("+clipv[0]+"px,"+clipv[1]+"px,"+clipv[2]+"px,"+clipv[3]+"px)";
	}

function tornemClip(obj)
	{
	var clipv = obj.style.clip.split("rect(")[1].split(")")[0].split("px");
	if (navigator.appVersion.indexOf("MSIE")==-1){
		clipv[2]=clipv[2].substring(2,clipv[2].length);
		}
	return clipv[2];
	}

function paddingnejar(obj,valor,dir){
	var pading=parseInt(obj.style.paddingTop)+(valor*dir);
	if (pading>=0) obj.style.paddingTop=pading+"px";
	}

var des=10;
var alcadaMax=0;
var amplada=0;
var cops=0;
var copsAni=0;
var timeAni;
var animant=false;
var alcMenuDesplegat;
var alcContingutDesplegat;
var alcMenuPlegat;
var alcContingutPlegat;
var timeAgafarAlc;

function clipejant(dir){
	clearTimeout(timeAni);
	if (cops-1>copsAni){
		clipejar(document.getElementById('efecteAmagar'),des,dir);
		paddingnejar(document.getElementById('mantenirAlcada'),des,dir);
		document.getElementById("menu").style.height=parseInt(document.getElementById("menu").style.height)+(des*dir)+"px";
		document.getElementById("contingut").style.height=parseInt(document.getElementById("contingut").style.height)+(des*dir)+"px";
		copsAni++;
		timeAni=setTimeout("clipejant("+dir+")",50);
		}
	else posarMidesFinals(dir);
	}

function posarMidesFinals(dir){
	if (dir==1){
		document.getElementById("mantenirAlcada").style.paddingTop="0px";
		treureCapesEfecte();
		document.getElementById("boto").innerHTML='<p><a href="javascript:amagarFormulari()">'+textAmagarCerca+'</a></p>';
		document.getElementById("boto").childNodes[0].childNodes[0].onclick=amagarFormulari;
/*	        document.getElementById("contingut").style.height=alcContingutDesplegat+'px';
		document.getElementById("menu").style.height=alcMenuDesplegat+'px';*/
//		alert('Contingut: '+document.getElementById("contingut").style.height+' Menu: '+document.getElementById("menu").style.height)
		}
	else{
		document.getElementById("mantenirAlcada").style.paddingTop="0px";
		document.getElementById("efecteAmagar").style.clip="rect(0px,"+(amplada)+"px,0px,0px)";
		document.getElementById("boto").innerHTML='<p><a href="javascript:mostrarFormulari();">'+textVeureCerca+'</a></p>';
		document.getElementById("boto").childNodes[0].childNodes[0].onclick=mostrarFormulari;
//		alert("Passo 1 "+alcContingutPlegat)
/*	        document.getElementById("contingut").style.height=alcContingutPlegat;
		document.getElementById("menu").style.height=alcMenuPlegat;*/
//		alert('Contingut: '+document.getElementById("contingut").style.height+' Menu: '+document.getElementById("menu").style.height)
		}
	animant=false;
	}

var notWhitespace =/\S/;
//Elinima el espais en blanc entre nodes en moz
function cleanWhitespace(node) {
  for (var x = 0; x < node.childNodes.length; x++) {
    var childNode = node.childNodes[x]
    if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
// that is, if it's a whitespace text node
      node.removeChild(node.childNodes[x])
      x--
    }
    if (childNode.nodeType == 1) {
// elements can have text child nodes of their own
      cleanWhitespace(childNode)
    }
  }
}


function posarXecs(formu,valorXecs){
	var cont=0;
	for (i=0;i<formu.elements.length;i++){
		if (formu.elements[i].type.toUpperCase() == 'CHECKBOX'){
			formu.elements[i].checked=valorXecs[cont];
			cont++;
			}
		}
	}

function recollirValorXecs(formu){
	var valorXecs=new Array();
	for (i=0;i<formu.elements.length;i++){
		if (formu.elements[i].type.toUpperCase() == 'CHECKBOX'){
			valorXecs[valorXecs.length]=formu.elements[i].checked;
			}
		}
	return valorXecs;
	}

function treureCapesEfecte(){
	var efecteAmagar=document.getElementById("formulari").childNodes[0];
	var formu=document.getElementById("formulari").childNodes[0].childNodes[0];
	var xecs=recollirValorXecs(formu);
	document.getElementById("formulari").removeChild(efecteAmagar);
	document.getElementById("formulari").appendChild(formu);
	var mantenirAlcada=document.getElementById("mantenirAlcada");
	document.getElementById("contingut").removeChild(mantenirAlcada);
	posarXecs(formu,xecs);
}

function crearCapesEfecte(){
	var formu=document.getElementById("formulari").childNodes[0];
	var efecteAmagar=document.createElement("div");
	var xecs=recollirValorXecs(formu);
	efecteAmagar.setAttribute("id","efecteAmagar");
	efecteAmagar.appendChild(formu);
	document.getElementById("formulari").appendChild(efecteAmagar);
	document.getElementById("efecteAmagar").style.position="absolute";
	posarXecs(formu,xecs);
	var mantenirAlcada=document.createElement('div');
	mantenirAlcada.setAttribute('id', 'mantenirAlcada');
	insertAfter(mantenirAlcada,document.getElementById("formulari"));
	document.getElementById("mantenirAlcada").style.paddingTop=alcadaMax+"px";
	document.getElementById("efecteAmagar").style.clip="rect(0px,"+amplada+"px,"+alcadaMax+"px,0px)";
}

function amagarFormulari(){
	if (animant==false){
		animant=true;
		crearCapesEfecte();
		copsAni=0;
		cops=Math.abs(alcadaMax/des);
		if(parseInt(document.getElementById("mantenirAlcada").style.paddingTop)!=0){
			clipejant(-1);
		}
	}
}

function mostrarFormulari(){
	if (animant==false){
		animant=true;
		copsAni=0;
		cops=Math.abs(alcadaMax/des);
		if(parseInt(document.getElementById("mantenirAlcada").style.paddingTop)!=alcadaMax){
			clipejant(1);
		}
	}
}
	
function insertAfter(newElement,targetElement) {
	//target is what you want it to go after. Look for this elements parent.
	var parent = targetElement.parentNode;
	//if the parents lastchild is the targetElement...
	if(parent.lastchild == targetElement) {
	//add the newElement after the target element.
		parent.appendChild(newElement);
		}
	 else {
	// else the target has siblings, insert the new element between the target and it's next sibling.
		parent.insertBefore(newElement, targetElement.nextSibling);		
		}
	}

function agafarAlc(){
	clearTimeout(timeAgafarAlc);
	if (typeof ddequalcolumns!="undefined" && typeof filtresCarregats!="undefined"){
 	  if (typeof ddequalcolumns.initMides!="undefined"){
	    if (ddequalcolumns.initMides && filtresCarregats){
	      alcMenuDesplegat=parseInt(document.getElementById("contingut").style.height);
	      alcContingutDesplegat=parseInt(document.getElementById("menu").style.height);
	      alcMenuPlegat=alcMenuDesplegat-alcadaMax+'px';
	      alcContingutPlegat=alcContingutDesplegat-alcadaMax+'px';
	      setTimeout("amagarFormulari()",500);
	      }
	    }
	    else {
		timeAgafarAlc=setTimeout("agafarAlc()",1000); 
		}	
	  }
        else {
	     timeAgafarAlc=setTimeout("agafarAlc()",1000); 
	     }	

}

function initAmagarFormFeina(){
	if (document.getElementById("resultados")){
	  var boto=document.createElement("DIV");
	  boto.setAttribute("id","boto");
	  boto.innerHTML='<p><a href="#">'+textVeureCerca+'</a></p>';
  	  document.getElementById("contingut").insertBefore(boto,document.getElementById("formulari"))
	  document.getElementById("boto").childNodes[0].childNodes[0].onclick=mostrarFormulari;
 	  alcadaMax=document.getElementById("formulari").offsetHeight;
	  amplada=document.getElementById("formulari").offsetWidth;
	  if (navigator.appVersion.indexOf("MSIE")==-1) cleanWhitespace(document.getElementById("formulari"))
	  setTimeout("agafarAlc()",500);
	}
}

addLoadEvent(initAmagarFormFeina);
//alert("surto amagarForm")

