/**
 * @author dennis.odell
 */

/*
window.addEvent('domready', function() {	
	new Accordion($$('.toggler'), $$('.element'), {
		duration:800,
		onComplete: function(){DOsetHeightDivSxDx();}

	});
});
*/

function init_accordion(iddivsx, iddivdx)
{
	if (iddivsx && iddivsx)
	{
		piddivsx = iddivsx;
		piddivdx = iddivdx;
		pdasottrarre = 10;
		DOsetHeightDivSxDx();
		window.setTimeout("init_accordion()", 1);
	}
	else
	{
		try
		{
			window.setTimeout(function() {
				//DOsetHeightDivSxDx();
				aggiusta_spalla('div_sx', 'div_dx', 'WebPartPromotional', 'WebPartPromotionalFiller');
				aggiusta_filler('div_dx', 'WebPartPromotional', 'WebPartPromotionalFiller');
				new Accordion($$('.toggler'), $$('.element'), {
					duration:800,
					onComplete:function(){
						aggiusta_filler('div_dx', 'WebPartPromotional', 'WebPartPromotionalFiller');
					}
				});			
			}, 1);
	/*new Accordion($$('.toggler'), $$('.element'), {
				duration:800,
				onComplete: function(){DOsetHeightDivSxDx();}
			});*/
		}
		catch (e)
		{}
	}
}


function aggiusta_filler(_sIDSpalla, _sIDPod, _sIDFiller)
{
	try 
	{
		var oSpalla = document.getElementById(_sIDSpalla);
		var oPod = document.getElementById(_sIDPod);
		var oFiller = document.getElementById(_sIDFiller);

		var nBorder = Math.ceil((oSpalla.offsetWidth - oPod.offsetWidth) / 2);
		//alert(nBorder);

		var nAvailable = oPod.offsetTop + oPod.offsetHeight;
		if (oPod.offsetParent != oSpalla)
			nAvailable -= oSpalla.offsetTop;
		nAvailable = oSpalla.offsetHeight - nBorder - nAvailable
		if (nAvailable == 0)
			return;

		var n = oFiller.offsetHeight;
		
		var x;
		for (x = 1; x * (x + 1)  / 2 < Math.abs(nAvailable); x++)	// 1+2+3+4+...
			;
		x = Math.min(x, Math.floor(Math.abs(nAvailable) / 20));		// gli ultimi 20 px vado a step 1

		n = n + x * nAvailable / Math.abs(nAvailable);
		if (!isNaN(parseInt(oFiller.style.height)))
			if (nAvailable > 0)
				n = Math.max(n, parseInt(oFiller.style.height) + 1);
			else
				n = Math.min(n, parseInt(oFiller.style.height) - 1);
		n = Math.max(n, 0);
		oFiller.style.height = n + "px";
		if (n == 0)
			return;
		window.setTimeout(function(){aggiusta_filler(_sIDSpalla, _sIDPod, _sIDFiller);}, 1);
	}
	catch(e)
	{
		// do nothing
	}
}

function aggiusta_spalla(_sIDBody, _sIDSpalla, _sIDPod, _sIDFiller)
{
	try 
	{
		var ACCORDION_MIN_HEIGHT = 200;

		var oBody = document.getElementById(_sIDBody);
		var oSpalla = document.getElementById(_sIDSpalla);
		var oPod = document.getElementById(_sIDPod);
		var oFiller = document.getElementById(_sIDFiller);

		if (oFiller == null)
		{
			oFiller = document.createElement("DIV");
			oFiller.id = _sIDFiller;
			oFiller.style.height = "0px";
			oFiller.style.margin = "0px";
			//oFiller.style.backgroundColor = "yellow";
			oPod.insertBefore(oFiller, oPod.firstChild);
		}
		var nBorder = Math.ceil((oSpalla.offsetWidth - oPod.offsetWidth) / 2);

		var nAvailable = oPod.offsetTop + oPod.offsetHeight;
		if (oPod.offsetParent != oSpalla)
			nAvailable -= oSpalla.offsetTop;
		nAvailable = oSpalla.offsetHeight - nBorder - nAvailable;

		// prima di attivare l'accordion, le tabelline con i "see all" sono tutte visibili, dopo solo una per volta lo sara' ...
		var cSeeAll = $$('.fw_see-all')
		var nMaxSeeAll = 0;
		for (var i = 0; i < cSeeAll.length; i++)
		{
			nAvailable += cSeeAll[i].offsetHeight;
			nMaxSeeAll = Math.max(nMaxSeeAll, cSeeAll[i].offsetHeight); 
		}
		nAvailable -= nMaxSeeAll; 

		//nAvailable = Math.max(nAvailable, 0);
		if (nAvailable < ACCORDION_MIN_HEIGHT)		// div_sx troppo corto, allungo...
		{
			if (oBody != null)
			{
				oBody.style.height = (parseInt(oBody.style.height) + ACCORDION_MIN_HEIGHT - nAvailable) + "px";
				oSpalla.style.height = (parseInt(oSpalla.style.height) + ACCORDION_MIN_HEIGHT - nAvailable) + "px";
				nAvailable = ACCORDION_MIN_HEIGHT;
			}
		}
		var nHeight = 0;
		if (document.getElementById('accordion_forum'))
		{
			nHeight = Math.min(nAvailable, document.getElementById('accordion_forum_scroll').offsetHeight);
			document.getElementById('accordion_forum').style.height = nHeight + "px";
		}
		if (document.getElementById('accordion_blog'))
		{
			nHeight = Math.min(nAvailable, document.getElementById('accordion_blog_scroll').offsetHeight);
			document.getElementById('accordion_blog').style.height = nHeight + "px";
		}
		if (document.getElementById('accordion_cars_event'))
		{
			nHeight = Math.min(nAvailable, document.getElementById('accordion_cars_event_scroll').offsetHeight);
			document.getElementById('accordion_cars_event').style.height = nHeight + "px";
		}
		if (document.getElementById('accordion_news'))
		{
			nHeight = Math.min(nAvailable, document.getElementById('accordion_news_scroll').offsetHeight);
			document.getElementById('accordion_news').style.height = nHeight + "px";
		}
	}
	catch(e)
	{
		// do nothing
	}
}

function check_spalla(_sIDBody, _sIDSpalla, _sIDPod, _sIDFiller, _sIDPPRoot)
{
	try 
	{
		var oBody = document.getElementById(_sIDBody);
		var oSpalla = document.getElementById(_sIDSpalla);
		var oFillerDx = document.getElementById(_sIDFiller);
		var oPPRoot = document.getElementById(_sIDPPRoot);
		var oFillerSx = document.getElementById(_sIDPPRoot + "_Filler");

		if (oFillerSx == null)
		{
			oFillerSx = document.createElement("DIV");
			oFillerSx.id = _sIDPPRoot + "_Filler";
			oFillerSx.style.height = "0px";
			oFillerSx.style.margin = "0px";
			//oFillerSx.style.backgroundColor = "yellow";
			oPPRoot.insertBefore(oFillerSx, oPPRoot.firstChild);
		}

		var nHeightSx = oBody.offsetHeight;
		var nHeightDx = oSpalla.offsetHeight;						

		if (nHeightSx > nHeightDx)
		{
			if (oFillerSx.offsetHeight > 0)
			{
				// accorcio il body
				oFillerSx.style.height = Math.max(0, parseInt(oFillerSx.style.height) - (nHeightSx - nHeightDx)) + "px";
			}
			else
			{
				// allungo la spalla
				oSpalla.style.height = (parseInt(oSpalla.style.height) + (nHeightSx - nHeightDx)) + "px";
				oFillerDx.style.height = (parseInt(oFillerDx.style.height) + (nHeightSx - nHeightDx)) + "px";
			}
		}
		else if (nHeightSx < nHeightDx)
		{
			if (oFillerDx.offsetHeight > 0)
			{
				// accorcio la spalla
				var n = Math.min(oFillerDx.offsetHeight, nHeightDx - nHeightSx);
				oSpalla.style.height = (parseInt(oSpalla.style.height) - n) + "px";
				oFillerDx.style.height = (parseInt(oFillerDx.style.height) - n) + "px";
			}
			else
			{
				// devo allungare il body, altrimenti sminchio l'accordion
				oFillerSx.style.height = (parseInt(oFillerSx.style.height) + (nHeightDx - nHeightSx)) + "px";
			}
		}
		var nSpreco = Math.min(oFillerSx.offsetHeight, oFillerDx.offsetHeight);
		if (nSpreco > 0)
		{
			// non dovrebbe mai capitare...
			oSpalla.style.height = (oSpalla.offsetHeight - nSpreco) + "px";
			oFillerSx.style.height = (oFillerSx.offsetHeight - nSpreco) + "px";
			oFillerDx.style.height = (oFillerDx.offsetHeight - nSpreco) + "px";
		}
	}
	catch(e)
	{
		//alert(e.message);
	}
}

var s_nScrollSpeed;

function do_scroll(_sID, _nAmount)
{
	s_nScrollSpeed += Math.abs(_nAmount / 100);
	_nAmount *= Math.floor(s_nScrollSpeed);
	var oElem = document.getElementById(_sID);
	var nTop = parseFloat(oElem.style.top);
	if (isNaN(nTop))
		nTop = 0;
	nTop += _nAmount;
	nTop = Math.max(nTop, -oElem.offsetHeight + oElem.parentNode.offsetHeight);
	nTop = Math.min(nTop, 0);
	oElem.style.top = nTop + "px";
}

function start_scroll(_sID, _nAmount)
{
	
	s_nScrollSpeed = 1;
	var oElem = document.getElementById(_sID);
	oElem.scrollTimerID = window.setInterval("do_scroll('" + _sID + "', " + _nAmount + ")", 1);
}

function stop_scroll(_sID)
{
	
	var oElem = document.getElementById(_sID);
	window.clearInterval(oElem.scrollTimerID);
}


