//Animated Collapsible DIV- Author: Dynamic Drive (http://www.dynamicdrive.com)

//Last updated June 27th, 07'. Added ability for a DIV to be initially expanded.



var uniquepageid=window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, "") //get current page path and name, used to uniquely identify this page for persistence feature



function animatedcollapse(divId, animatetime, persistexpand, initstate){

	this.divId=divId

	this.divObj=document.getElementById(divId)

	this.divObj.style.overflow="hidden"

	this.timelength=animatetime

	this.initstate=(typeof initstate!="undefined" && initstate=="block")? "block" : "contract"

	this.isExpanded=animatedcollapse.getCookie(uniquepageid+"-"+divId) //"yes" or "no", based on cookie value

	this.contentheight=parseInt(this.divObj.style.height)

	var thisobj=this

	if (isNaN(this.contentheight)){ //if no CSS "height" attribute explicitly defined, get DIV's height on window.load

		animatedcollapse.dotask(window, function(){thisobj._getheight(persistexpand)}, "load")

		if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes") //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)

			this.divObj.style.visibility="hidden" //hide content (versus collapse) until we can get its height

	}

	else if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes") //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)

		this.divObj.style.height=35+"px" //just collapse content if CSS "height" attribute available

		this.divObj.style.overflow = "visible";

	if (persistexpand)

		animatedcollapse.dotask(window, function(){animatedcollapse.setCookie(uniquepageid+"-"+thisobj.divId, thisobj.isExpanded)}, "unload")

}



animatedcollapse.prototype._getheight=function(persistexpand){

	this.contentheight=this.divObj.offsetHeight

	if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes"){ //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)

		this.divObj.style.height=35+"px"//collapse content

		this.divObj.style.visibility="visible"

	}

	else //else if persistence is enabled AND this content should be expanded, define its CSS height value so slideup() has something to work with

		this.divObj.style.height=this.contentheight+"px"

}





animatedcollapse.prototype._slideengine=function(direction){

	var elapsed=new Date().getTime()-this.startTime //get time animation has run

	var thisobj=this

	if (elapsed<this.timelength){ //if time run is less than specified length

		var distancepercent=(direction=="down")? animatedcollapse.curveincrement(elapsed/this.timelength) : 1-animatedcollapse.curveincrement(elapsed/this.timelength)

	this.divObj.style.height=distancepercent * this.contentheight +"px"

	this.runtimer=setTimeout(function(){thisobj._slideengine(direction)}, 10)

	}

	else{ //if animation finished

		this.divObj.style.height=(direction=="down")? this.contentheight+"px" : 35+"px";

		this.isExpanded=(direction=="down")? "yes" : "no" //remember whether content is expanded or not

		this.runtimer=null

		if (this.isExpanded=="yes")

		{this.divObj.className="sfondoBoxAperto";

		this.divObj.style.overflow = "visible";

		}

		else {this.divObj.className="sfondoBoxChiuso";

		

		}

			

	}

}





animatedcollapse.prototype.slidedown=function(){

	if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running

		if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)

			alert("Please wait until document has fully loaded then click again")

		else if (parseInt(this.divObj.style.height)==35){ //if content is collapsed

			this.startTime=new Date().getTime() //Set animation start time

			this._slideengine("down")

		}

	}

}



animatedcollapse.prototype.slideup=function(){

	if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running

		if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)

			alert("Please wait until document has fully loaded then click again")

		else if (parseInt(this.divObj.style.height)==this.contentheight){ //if content is expanded

			this.startTime=new Date().getTime()

			this._slideengine("up")

		}

	}

}



animatedcollapse.prototype.slideit=function(){

	if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)

		alert("Please wait until document has fully loaded then click again")

	else if (parseInt(this.divObj.style.height)==35)

	

		this.slidedown()

	else if (parseInt(this.divObj.style.height)==this.contentheight)

{	this.divObj.style.overflow = "hidden";

		this.slideup()}

}



// -------------------------------------------------------------------

// A few utility functions below:

// -------------------------------------------------------------------



animatedcollapse.curveincrement=function(percent){

	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input

}





animatedcollapse.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)

	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype

	if (target.addEventListener)

		target.addEventListener(tasktype, functionref, false)

	else if (target.attachEvent)

		target.attachEvent(tasktype, functionref)

}



animatedcollapse.getCookie=function(Name){ 

	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair

	if (document.cookie.match(re)) //if cookie found

		return document.cookie.match(re)[0].split("=")[1] //return its value

	return ""

}



animatedcollapse.setCookie=function(name, value, days){

	if (typeof days!="undefined"){ //if set persistent cookie

		var expireDate = new Date()

		var expstring=expireDate.setDate(expireDate.getDate()+days)

		document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()

	}

	else //else if this is a session only cookie

		document.cookie = name+"="+value

}



// Imposta la classe al caricametno della pagina



function setBoxClass (boxToSet){

 	if (boxToSet.isExpanded!="no")

	  {boxToSet.divObj.className="sfondoBoxAperto";}

  else {boxToSet.divObj.className="sfondoBoxChiuso";}

}



function openAddMore(element) {

	var spanPopup = element.getElementsByTagName('span')

	var popup= spanPopup[0];

	// setto la larghezza del popup uguale a se stessa per ie7 (che altrimenti non dimensiona la roba correttamente)

	popup.style.width = popup.offsetWidth +"px";

	var thisPopped = popup.popped;

	var allSpans = document.getElementsByTagName('*');

	

	/* Hide al the popups*/

	for (loop=0; loop<allSpans.length; loop++) 

	{

		if (allSpans[loop].className == "addMore") 

		  {

			

				var allSpanPopup = allSpans[loop].getElementsByTagName('span')

	      var allPopup= allSpanPopup[0];

				allPopup.style.visibility="hidden";

        allPopup.popped = 0;}

	}



	/* Show the actual popup */

	if ( thisPopped != 1){

	popupLeft = popup.offsetWidth/2 -8;

	popup.style.left = -popupLeft + "px";

	popup.style.visibility="visible";

  popup.popped = 1;

	}

	else { 

	popup.style.visibility="hidden";

  popup.popped = 0;

	}

}





function larghezzaLabel(area) {	

	// prendo tutti i div della pagine a scelgo area-miei-dati

	var div_list = document.getElementsByTagName("div");



	for (loop=0; loop<div_list.length; loop++) 

	{

		if (div_list[loop].className == area) 

		{

		

		  // al suo interno cerco i label

	    var lab_list = div_list[loop].getElementsByTagName("label");

			var max_larghezza_label = 0;

			for (loop2=0; loop2<lab_list.length; loop2++) 

			{

				//se la larghezza del label è superiore a quella che ho memorizzato la salvo

				if (lab_list[loop2].offsetWidth > max_larghezza_label) 

					{

						max_larghezza_label = lab_list[loop2].offsetWidth;

					} 

			}

			

			//aumento di qualche pixel la larghezza finale

			var max_larghezza_label = max_larghezza_label;

			

			// imposto la larghezza di tutti i label pari a quella del piu largo

			for (loop3=0; loop3<lab_list.length; loop3++) 

			{

				lab_list[loop3].style.width = max_larghezza_label + "px";



			}

			

			//setto anche il margin degli elementi allienati ai label

			var altri_list = div_list[loop].getElementsByTagName("*");

			

			for (loop4=0; loop4<altri_list.length; loop4++) 

			{			

				if (altri_list[loop4].className == "fieldWarning")

				{

					altri_list[loop4].style.paddingLeft = max_larghezza_label + 20 + "px";

				}

			}

			

			// allieno i warning

			for (loop4=0; loop4<altri_list.length; loop4++) 

			{			

				if (altri_list[loop4].className.indexOf('allinea') != -1)

				{

					altri_list[loop4].style.paddingLeft = max_larghezza_label + 20 + "px";

				}

			}

		

		}

	}

}





function larghezzaCampi(area) {	

	// prendo tutti i div della pagine a scelgo area-miei-dati

	var div_list = document.getElementsByTagName("div");



	for (loop=0; loop<div_list.length; loop++) 

	{

		if (div_list[loop].className == area) 

		{

		

		  // al suo interno cerco tutto

	    var lab_list = div_list[loop].getElementsByTagName("p");

			var max_larghezza_campo = 0;

			for (loop2=0; loop2<lab_list.length; loop2++) 

			{

				if (lab_list[loop2].className == "centrato") 

				{ 

				

					if (lab_list[loop2].offsetWidth > max_larghezza_campo) 

						{

							max_larghezza_campo = lab_list[loop2].offsetWidth;

						} 

				}

			}

	

		

			

			// imposto la larghezza di tutti i label pari a quella del piu largo

			for (loop3=0; loop3<lab_list.length; loop3++) 

			{

				if (lab_list[loop3].className == "centrato") 

				{

				  lab_list[loop3].style.width = max_larghezza_campo + "px";

				}

			}

			

			//setto anche il margin degli elementi allienati ai label

			var altri_list = div_list[loop].getElementsByTagName("*");

			for (loop4=0; loop4<altri_list.length; loop4++) 

			{			

				if (altri_list[loop4].className.indexOf('allinea') != -1)

				{

					altri_list[loop4].style.paddingLeft = max_larghezza_label + 20 + "px";

				}

			}

		}

	}

}





function abilitaGiocare(check) {

	checklist = check.parentNode.parentNode.getElementsByTagName("input");

	if (checklist.length >1)

	{

		if (check.checked == true) 

			{

				checklist[1].disabled = false;

				checklist[1].checked = true;

			}

		else 

		 {

			 checklist[1].checked = false;

			 checklist[1].disabled = true;

		 }

	}

}



function abilitaColonna(starter,columName) {

	  starter.status = !!starter.status;

		starterParente = starter.parentNode.parentNode.parentNode

		chosenColumnInputList = starterParente.getElementsByTagName("span");

		

		//chosenColumnInputList = starterParente.getElementsByClassName(columName);

		for (loop=0; loop<chosenColumnInputList.length; loop++)

		{

			if(chosenColumnInputList[loop].className == columName)

			 {

				pino = chosenColumnInputList[loop].getElementsByTagName("input");

				if (starter.status == false)

				{

					if (pino[0].disabled == false)	pino[0].checked = true;

					if ( columName == 'opzioniInteressa')

					abilitaGiocare(pino[0]);

				}

				

				else {

					if (pino[0].disabled == false)	pino[0].checked = false;

					if ( columName == 'opzioniInteressa')

					abilitaGiocare(pino[0]);

				}

			 }

		}

	

	starter.status = !starter.status;

	if (starter.status == true) 

	{

		starter.innerHTML = "deseleziona <strong>tutti</strong>"

	}

	else 

	{

		starter.innerHTML = "seleziona <strong>tutti</strong>"

	}

	

	

	// mette la colonna gioca a seleziona tutti se si è si è deselezionata la colonna mi piace

	if (chosenColumnInputList[2].getElementsByTagName("a").length > 0) {

	chosenColumnInputList[2].getElementsByTagName("a")[0].status = !!chosenColumnInputList[2].getElementsByTagName("a")[0].status

	

	

		if ( columName == 'opzioniInteressa' && starter.status == false && chosenColumnInputList[2].getElementsByTagName("a")[0].status == true ) {

			abilitaColonna(chosenColumnInputList[2].getElementsByTagName("a")[0],'opzioniGiocare')

		}

		

			if ( columName == 'opzioniInteressa' && starter.status == true && chosenColumnInputList[2].getElementsByTagName("a")[0].status == false ) {

			abilitaColonna(chosenColumnInputList[2].getElementsByTagName("a")[0],'opzioniGiocare')

		}

		

	}

}

function visualizzaCommentoNascosto(commento) {
	commento.visualizzato = !!commento.visualizzato;
	
	if (commento.visualizzato == false) {
		commento.parentNode.parentNode.getElementsByTagName("div")[0].style.display = "block";
		commento.innerHTML= "Nascondi commento";
		commento.visualizzato = true;
	}
	
	else
	{
		commento.parentNode.parentNode.getElementsByTagName("div")[0].style.display = "none";
		commento.innerHTML= "Visualizza ugualmente";
		commento.visualizzato = false;
	}
	
	}


	/**
	* @function  popup
	*    Creates an accessible JavaScript pop up window link. Allows JavaScript-
	*    enabled browsers to open links in a new window and control its
	*    appearance, while allowing non JavaScript browsers to follow the link.
	*
	* @param   el (object, required)
	*    Node reference to the clicked link.
	*
	* @param   opts (string, optional)
	*    A string containing options to control the appearance of the new
	*    window.
	*
	* @return  boolean
	*    If an invalid node reference is passed, true is returned to allow the
	*    mouse click or pressed key event to continue. If all is well, a false
	*    value is returned to nullify the link click.
	*/
	function popup(el, opts) {
	    if (!el) {
	        return true;
	    }
	    var target = '_blank', url = el.href;
	    if (!opts) {
	        opts = '';
	    }
	    if (el.target !== '') {
	        target = el.target;
	    }
	    else {
	        var w = 850;
	        var h = 850;
	        var l = Math.floor((screen.width - w) / 2);
	        var t = Math.floor((screen.height - h) / 2);
	        target = 'width=' + w + ',height=' + h + ',top=' + t + ',left=' + l + ' ,resizable = yes, scrollbars = yes, location= no, menubar= no, status = no, titlebar = no';
	    }
	    window.open(url, opts, target);
	    return false;
	}
