/* -------------------------------------------------------
 * konstansok
 * -------------------------------------------------------
 */

var BOX_ANIM_TIME = 400;		// ennyi ido az anim
var ANIM_TIMER_SLEEP = 15;		// ennyi msec van ket anim fazis kozt
var NAVIGATION_BAR_HEIGHT = 30; // navigacios csik magassaga
var boxIntervals = new Array();


var boxOpenIconFileSrc = '';
var boxCloseIconFileSrc = '';

/* -------------------------------------------------------
 * korrekcio
 * -------------------------------------------------------
 */


function correctCentererParagraphHeight()
{
	// IE alatt nem lesz 100% magas az egesz kep, sucks...
	var centererParagraph = getElementWithId('centererParagraph');
	if(centererParagraph != null)
	{
		var windowHeight = getViewportHeight();
		var pHeight = centererParagraph.getHeight();
		if(pHeight <= windowHeight)
		{
			// rendbetesszuk, mivel kisebb sose lehetne...
			centererParagraph.setStyle({height: windowHeight+'px'});
		}
	}
}


/* -------------------------------------------------------
 * boxok
 * -------------------------------------------------------
 */

var boxAnimStructs = new Array();
var animTimerID = null;

function initBoxHeights_static()
{
	for(var i=0; i<boxIds.length; i++)
	{
		var boxId = boxIds[i];
		
		if(boxClosed[boxId])
		{
			setBoxHeight(boxId, 1, 1);
		}
		else
		{
			var boxHeight = boxHeights[boxId];
			var boxContentHeight = getBoxContentHeight(boxId);
			
			//debugLog("box: "+boxId+", boxHeight = "+boxHeight+", boxContentHeight = "+boxContentHeight);
			if(boxContentHeight > boxHeight)
				boxHeight = boxContentHeight;
			
			setBoxHeight(boxId, boxHeight, NAVIGATION_BAR_HEIGHT);
		}
	}
}


function setBoxHeight(boxId, height, navHeight)
{
	var hideContent = false;
	var hideNav = false;
	if(height <= 1)
		hideContent = true;
	if(navHeight <= 1)
		hideNav = true;

	var boxContentAndNavObj = $('contentAndNavigation_' + boxId);
	var boxContentContainerObj = $('contentContainer_'+boxId);
	var boxNavObj = $('nav_' + boxId);
	
	boxContentContainerObj.setStyle({height: height+'px'});
	if(boxNavObj)
		boxNavObj.setStyle({height: navHeight+'px'});
	
	if(hideContent && hideNav)
	{
		boxContentAndNavObj.hide();
	}
	else
	{
		boxContentAndNavObj.show();
	}


	/*
	if(hideContent)
	{
		boxContentContainerObj.hide();
	}
	else
	{
		boxContentContainerObj.setStyle({height: height+'px'});
		boxContentContainerObj.show();
	}

	if(hideNav)
	{
		boxNavObj.hide();
	}
	else
	{
		boxNavObj.setStyle({height: navHeight+'px'});
		boxNavObj.show();
	}
	*/
}

function getBoxHeight(boxId)
{
	var boxObj = $('contentContainer_'+boxId);
	if(boxObj)
		return boxObj.getHeight();
	
	return -1;
}

function getBoxContentHeight(boxId)
{
	var boxContentObj = $('content_'+boxId);
	if(boxContentObj)
		return boxContentObj.getHeight();
	
	return -1;
}

function getBoxNavHeight(boxId)
{
	var boxNavObj = $('nav_'+boxId);
	if(boxNavObj)
		return boxNavObj.getHeight();
	
	return 0;
}

// visszaadja az anim struct indexét vagy -1 ha nincs
function findBoxAnimStruct(boxId)
{
	for(var i=0; i<boxAnimStructs.length; i++)
	{
		var struct = boxAnimStructs[i];
		if(struct['boxId'] == boxId)
			return i;
	}
	return -1;
}

function addBoxAnimStruct(boxId, stopHeight, stopNavHeight)
{
	// debugLog("addBoxAnimStruct(): boxId = "+boxId+", stopHeight = "+stopHeight);
	
	// ha volt kivesszuk
	removeBoxAnimStruct(boxId);
	
	var currHeight = getBoxHeight(boxId);
	if(currHeight == stopHeight)
	{
		// nincs mit csinalni
		//debugLog("addBoxAnimStruct(): currHeight = "+currHeight+", cancelled!");
		return;
	}
	
	var currNavHeight = getBoxNavHeight(boxId);
	
	var struct = new Array();
	struct['boxId'] = boxId;
	struct['startHeight'] = currHeight;
	struct['stopHeight'] = stopHeight;
	struct['startNavHeight'] = currNavHeight;
	struct['stopNavHeight'] = stopNavHeight;
	struct['animTime'] = BOX_ANIM_TIME;
	struct['startTime'] = null;
	
	boxAnimStructs.push(struct);
	
	/*
	var currHeight = 0;
	if (isNavigationBarIncluded) {
		currHeight = getBoxContentAndNavigationHeight(boxId);
	} else {
		currHeight = getBoxHeight(boxId);
	}
	if(currHeight == stopHeight)
	{
		// nincs mit csinalni
		// debugLog("addBoxAnimStruct(): currHeight = "+currHeight+", cancelled!");
		return;
	}
	
	var struct = new Array();
	struct['boxId'] = boxId;
	struct['startHeight'] = currHeight;
	struct['stopHeight'] = stopHeight;
	if (isNavigationBarIncluded) {
		var navigationBarHeight = (stopHeight == 0? 0: NAVIGATION_BAR_HEIGHT);
		struct['stopHeight'] += navigationBarHeight;
	}
	struct['animTime'] = BOX_ANIM_TIME;
	struct['startTime'] = null;
	struct['isNavigationBarIncluded'] = isNavigationBarIncluded;
	
	boxAnimStructs.push(struct);
	*/
	
	// debugLog("addBoxAnimStruct(): added!");
}

function removeBoxAnimStruct(boxId)
{
	var index = findBoxAnimStruct(boxId);
	if(index != -1)
	{
		// kivesszuk
		boxAnimStructs.splice(index, 1);
	}
}

function startAnimTimer()
{
	if(animTimerID != null || boxAnimStructs.length == 0)
		return;
	
	animTimerID = window.setTimeout("doAnimPhase()", ANIM_TIMER_SLEEP);
}

function stopAnimTimer()
{
	// lelojuk
	window.clearTimeout(animTimerID);
	animTimerID = null;
}

function doAnimPhase()
{
	stopAnimTimer();
	
	// feldolgozzuk a dolgokat
	//debugLog("----- start");

	for(var i=0; i<boxAnimStructs.length; i++)
	{
		var struct = boxAnimStructs[i];
		var boxId = struct['boxId'];
		
		var currTime = new Date().getTime();
		var startTime = struct['startTime'];
		if(startTime == null)
		{
			struct['startTime'] = currTime;
			startTime = currTime;
		}
		
		var timeFactor = (currTime - startTime) / struct['animTime'];
		if(timeFactor > 1)
			timeFactor = 1;
		// sinus lassulashoz
		timeFactor = Math.sin(Math.PI/2 * timeFactor);
		if(timeFactor > 1)
			timeFactor = 1;
		
		var currHeight = struct['startHeight'] + Math.round((struct['stopHeight'] - struct['startHeight']) * timeFactor);
		var navHeight = struct['startNavHeight'] + Math.round((struct['stopNavHeight'] - struct['startNavHeight']) * timeFactor);
		
		setBoxHeight(boxId, currHeight, navHeight);
		
		if(timeFactor == 1)
			// vege
			removeBoxAnimStruct(boxId);
	}

	//debugLog("----- stop");
	
	// ha van elem meg indul megint
	startAnimTimer();
}


//megjott a box a valasz
function boxXmlArrived(originalRequest)
{
	var newBoxContent = null;
	var boxId = null;
	
	if (originalRequest.responseXML)
	{
		var xh = new tXMLHelper(originalRequest.responseXML);
		
		if (!xh.nodeExists('superbrands')) {
			//alert("there is no <superbrands> section in the response xml");
		}
		else {
			// ok, dolgozzuk fel az xml-t!

			if (xh.nodeExists('boxContent')) {
				newBoxContent = xh.nodeToString('boxContent');
			}
			if(xh.nodeExists('boxId')) {
				boxId = parseInt(xh.nodeToString('boxId'));
			}
		}
	}
	else
	{
		//alert("response came from the server was not an xml response");
	}
	
	if(boxId != null)
	{
		//alert(boxId+", "+newBoxContent);
		var boxObj = $('box_'+boxId);
		if(boxObj)
		{
			var targetBoxHeight = boxHeights[boxId];
			var currBoxHeight = getBoxHeight(boxId);
			
			boxObj.replace(newBoxContent);

			// hogy nyitva/csukva van-e az már lokálisan döl el és nem a letöltött html-böl!
			// de vigyazz, nem csukhato dobozoknal ezek nincsenek!
			var td = $('openbox_' + boxId);
			var img = $('openboximg_' + boxId);
			if(td && img)
			{
				if (!boxClosed[boxId]) {
					img.src = boxCloseIconFileSrc;
					td.removeClassName('openBox');
					td.addClassName('closeBox');
				} else {
					img.src = boxOpenIconFileSrc;
					td.removeClassName('closeBox');
					td.addClassName('openBox');
				}
				
			}
			

			setBoxHeight(boxId, currBoxHeight, NAVIGATION_BAR_HEIGHT);
			
			var boxContentHeight = getBoxContentHeight(boxId);
			
			//debugLog("box: "+boxId+", boxHeight = "+boxHeight+", boxContentHeight = "+boxContentHeight);
			if(boxContentHeight > targetBoxHeight)
				targetBoxHeight = boxContentHeight;

			
			addBoxAnimStruct(boxId, targetBoxHeight, NAVIGATION_BAR_HEIGHT);
			startAnimTimer();
		}

		startBoxAutoNavigation(boxId);
	}
	
}


function getBoxContent(url, boxId)
{
	//alert("getBoxContent to: "+url);
	
	stopBoxAutoNavigation(boxId);
	
	this.ttAjax = new Ajax.Request(url, {
		method: 'get',
		onComplete: boxXmlArrived
	});
}

function openOrCloseBox(boxId) {
	var targetBoxHeight = 1;
	var targetBoxNavHeight = 1; 
	if (boxClosed[boxId]) {
		
		var img = $('openboximg_' + boxId);
		img.src = boxCloseIconFileSrc;

		var td = $('openbox_' + boxId);
		td.removeClassName('openBox');
		td.addClassName('closeBox');
		
		var boxContentAndNavObj = $('contentAndNavigation_' + boxId);
		boxContentAndNavObj.show();
		
		boxClosed[boxId] = false;
		
		var targetBoxHeight = boxHeights[boxId];
		//var currBoxHeight = getBoxHeight(boxId);
		//setBoxHeight(boxId, currBoxHeight, NAVIGATION_BAR_HEIGHT);
		var boxContentHeight = getBoxContentHeight(boxId);
		if(boxContentHeight > targetBoxHeight)
			targetBoxHeight = boxContentHeight;
		
		//targetBoxHeight = getBoxContentHeight(boxId);
		targetBoxNavHeight = NAVIGATION_BAR_HEIGHT;
		startBoxAutoNavigation(boxId);
		
	} else {
		
		var img = $('openboximg_' + boxId);
		img.src = boxOpenIconFileSrc;
		
		var td = $('openbox_' + boxId);
		td.removeClassName('closeBox');
		td.addClassName('openBox');
		
		boxClosed[boxId] = true;
		stopBoxAutoNavigation(boxId);
	}
	addBoxAnimStruct(boxId, targetBoxHeight, targetBoxNavHeight);
	startAnimTimer();
}

function startBoxAutoNavigation(boxId)
{
	stopBoxAutoNavigation(boxId);
	
	if (boxAutoNext[boxId]) {
		boxIntervals[boxId] = window.setTimeout("autoNextBox(" + boxId + ")", boxAutoNextTime[boxId] * 1000);
	}
}

function stopBoxAutoNavigation(boxId)
{
	clearTimeout(boxIntervals[boxId]);	
}

function autoNextBox(boxId) {
	if (! boxClosed[boxId]) {
		getBoxContent(boxAutoNext[boxId]);
	}
}

function autoNext() {
	
	for(var i=0; i<boxIds.length; i++)
	{
		var boxId = boxIds[i];
		var qqq = boxAutoNext[boxId];
		if(qqq)
		{
			boxIntervals[boxId] = window.setTimeout("autoNextBox(" + boxId + ")", boxAutoNextTime[boxId] * 1000);
		}
	}

	/*
	for (var boxId in boxAutoNext) {
		boxIntervals[boxId] = window.setTimeout("autoNextBox(" + boxId + ")", boxAutoNextTime[boxId] * 1000);
	}
	*/
}

/* -------------------------------------------------------
 * oldal betoltve
 * -------------------------------------------------------
 */

function pageResized()
{
	 correctCentererParagraphHeight();
}

function pageLoaded()
{
	
}

function doAfterPageLoaded() {
	correctCentererParagraphHeight();

	// boxok
	if(!isArchiveYear)
		initBoxHeights_static();
}

addHtmlLoadedCallback(new tJsCallback(doAfterPageLoaded));
