 var requestInfoUrl, actID, requestInfoID, contentBox, contentCell, iBox, iBoxFx, jsIBox, 
	hFrame, iFrame, iMenu, infoRequest, autoHide, sBarWidth;

function init(infoMenuHeight){
	if (/msie/i.test(navigator.userAgent) ) {
		msie = 1
		w = document.body.clientWidth
		h = document.body.clientHeight
		//if(debugging) debug('msie:'+w +'x' +h)
	}
	else	if(innerWidth){
		w = innerWidth
		h = innerHeight	
	}
	sBarWidth = getScrollbarWidth();
	iBox = $('infoBox');
	iBox.dims = iBox.getCoordinates();
	//dumpObject(iBox.dims);
	iFrame = $('infoFrame');
	iFrame.fullHeight = iBox.dims.height;// + infoMenuHeight;
	var extraWidth = 0;
	var iFrameWidth = iBox.dims.width + extraWidth;
	window.onerror = eHandler;
	//########### INIT HIDE FRAME
	hFrame = $('hideFrame');
	var hideSize = 20;
	var hAct = $('hideTop');
	hAct.addEvent('mouseover', initHide);
	hAct.setPosition({x: iBox.dims.left - hideSize - extraWidth/2, y: iBox.dims.top - hideSize});
	hAct.style.width = (iFrameWidth + 2 * hideSize ) +'px';
	hAct.style.height = hideSize +'px'; 
	debug('31:'+hAct.style.height);
	/*hAct = $('hideBottom');
	hAct.addEvent('mouseover', initHide);
	hAct.setPosition({x: iBox.dims.left - hideSize - extraWidth/2, 
		y: iBox.dims.top + iBox.dims.height + infoMenuHeight});
	hAct.style.width = (iFrameWidth + 2 * hideSize) +'px';
	hAct.style.height = hideSize +'px';	*/
	hAct = $('hideLeft');
	hAct.addEvent('mouseover', initHide);
	hAct.setPosition({x: iBox.dims.left - hideSize - extraWidth/2, y: iBox.dims.top });
	hAct.style.width = hideSize +'px';
	hAct.style.height = (iBox.dims.height + infoMenuHeight) +'px';		
	debug('43:'+hAct.style.height);
	hAct = $('hideRight');
	hAct.addEvent('mouseover', initHide);
	hAct.setPosition({x: iBox.dims.left + iBox.dims.width + extraWidth/2, y: iBox.dims.top});
	hAct.style.width = hideSize +'px';
	hAct.style.height = (iBox.dims.height + infoMenuHeight) +'px';	
	debug('49:'+hAct.style.height);
	hFrame.style.display = 'none';
	//###########
	contentBox = $('content');
	contentCell = $('cCell');
	contentBox.style.height = (iBox.dims.height - infoMenuHeight) + 'px';
	debug('54:'+hAct.style.height);
	iMenu = $('infoMenu');
	initInfoMenu();
	iMenu.style.top = (iBox.dims.height - 1) +'px';
	iFrame.style.width = (iBox.dims.width + extraWidth ) + 'px';
	iFrame.style.left = (iBox.dims.left ) + 'px';
	iFrame.iTop = iBox.dims.top + (iBox.dims.height + infoMenuHeight)/2;
	iFrame.style.top = iBox.dims.top + 'px';
	//iFrame.style.top = iFrame.iTop + 'px';
	iFrame.isClosing = iFrame.isOpen = false;
	iBoxFx = new Fx.Tween(iFrame, {property:'height',  duration:500, link:'cancel', transition:'linear'});
	//iBoxFxTop = new Fx.Tween(iFrame, {property:'top',  duration:500, link:'cancel', transition:'linear'});
	//iBoxFx.addEvents({complete:onComplete, start:onStart});
	iBoxFx.addEvents({complete:onComplete});
	infoRequest = new Request.HTML();
	infoRequest.onSuccess = setInfo;
	actID = '';
	//debug('checkPng:'+typeof(checkPng) );
	if(typeof(checkPng)!='undefined')
		checkPng();
	if(initialProject != null){
		//for(item in infoItems){
		debug(infoItems.length);
		for(i = 0;i<infoItems.length;i++){
			debug(initialProject +'==' + i +':' + infoItems[i].id);
			if(initialProject == infoItems[i].id){
				showInfo(initialProject);
				break;
			}
		}
	}
}

function getScrollbarWidth(){
	var inner = document.createElement('p');
	inner.style.width = '100%';
	inner.style.height = '200px';
	var outer = document.createElement('div');
	outer.appendChild(inner);
	outer.style.position = 'absolute';
	outer.style.top = '0px';
	outer.style.visibility = 'hidden';
	outer.style.width = '200px';
	outer.style.height = '150px';
	outer.style.overflow = 'hidden';
	outer = document.body.appendChild(outer);
	outer = document.body.removeChild(outer);
	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';
	var w2 = inner.offsetWidth;
	if (w1 == w2) w2 = outer.clientWidth;     
	//outer.remove();
	return (w1 - w2);
}

function addNode(n, p){
	p.appendChild(n);
}

function setInfo(responseTree, responseElements, responseHTML, responseJavaScript){
	//debug('setInfo');
	if(gIsloaded)
		GUnload();
	contentCell.empty();
	iLength = responseTree.length;
	while(iLength--){
		//debug(iLength + ':' + responseTree[0].nodeName+'responseTree.length:'+responseTree[0].childNodes.length);
		contentCell.appendChild(responseTree[0]);
	}
	//debug(new XMLSerializer().serializeToString(contentBox));
	//debug(responseJavaScript);
	if(responseJavaScript != ''){
		startInfo();
	}
	if(requestInfoID == 'farming')
		load();
	//debug(responseHTML);
	actID = requestInfoID;
	setCurrentIndex(actID);
}

function eHandler(txt, url, line){
	debug('error:' + txt + ' on line:' + line +'\n')
	return true
}

function showInfo(linkID){
	//debug(linkID + ' showInfo:iFrame.isOpen' +iFrame.isOpen+ ' iFrame.isClosing:'+ iFrame.isClosing);
	requestInfoID = linkID;
	//debug(requestInfoUrl);
	openInfoBox();
}

function openInfoBox(){
	//debug(autoHide + '<- openInfoBox::iFrame.isClosing:' + iFrame.isClosing + ' :iFrame.isOpen '+iFrame.isOpen);
	if (autoHide){
		clearTimeout(autoHide);
		autoHide = 0;
	}	
	if(iFrame.isClosing){
		iBoxFx.cancel();
		//iBoxFxTop.cancel();
		iFrame.isOpen = iFrame.isClosing = false;		
	}
	if(!iFrame.isOpen){
		iBoxFx.start(iFrame.fullHeight);
		if(requestInfoID != actID)
			contentCell.empty();
		iFrame.style.display = 'block';
		//iBoxFxTop.start(iBox.dims.top);
	}
}

function requestInfo(rID){
	//debug('requestInfo::rID:'+rID+' actID:' + actID)
	if(rID == actID)
		return;
	requestInfoID = rID;
	infoRequest.get( 'info/'+rID+'.php');
}

function startHide(){
	//debug('startHide');
	//contentBox.empty();
	if(infoHideFunction != startHide)
		infoHideFunction();
	hFrame.style.display = 'none';
	autoHide = 0;
	iFrame.isOpen = iFrame.isClosing = true;
	iBoxFx.start(0);
	//iBoxFxTop.start(iFrame.iTop);
}

function initHide(){
	//debug('initHide');
	if (autoHide)
		return;
	iFrame.addEvent('mouseover', openInfoBox);
	autoHide = setTimeout("startHide()", 3000);
}

function onComplete(){
	//debug('onComplete::iFrame.isOpen:'+iFrame.isOpen);
	if(iFrame.isOpen){
		iFrame.style.display = 'none';
		iFrame.isOpen = iFrame.isClosing = false;
		//iFrame.removeEvent('mouseout', initHide);
		iFrame.removeEvent('mouseover', openInfoBox);
	}
	else{
		//debug('request:'+requestInfoID);
		iFrame.isOpen = true
		requestInfo(requestInfoID);
		hFrame.style.display = 'block';
		dumpLayout(iFrame);
		dumpLayout(hFrame);
		dumpLayout($('hideTop'));
	}
}
//################ INFO MENU HANDLING

var currentInfoIndex, infoNavImgEl, infoMenuImagesDisabled, infoMenuImages, linkList;
var infoItems, infoHideFunction, changeInfoFunction, currentInfoIndexMain;

function initInfoMenu(){
	//debug('initInfoMenu');
	infoMenuImages = new Array();
	infoItems = new Array();
	setItems(projectItems);
	/*for(i=0;i<projectItems.length;i++)
		infoItems[i] = projectItems[i];*/
	changeInfoFunction = requestInfo;
	infoHideFunction = startHide;
	infoMenuImagesDisabled = new Array();
	var infoImg = iMenu.getElementsByTagName('img');
	infoNavImgEl = new Array();
	linkList = new Array();
	var actImg;
	for(i=0;i<infoImg.length-1;i++){
		infoNavImgEl.push(infoImg[i]);
		//linkList.push(infoNavImgEl[i].parentNode.getAttribute('href'));
		linkList.push(infoNavImgEl[i].parentNode.href);
		actImg = new Image();
		infoNavImgEl[i].src = actImg.src = "../design/icons/resultset_" +infoImg[i].id+ ".png";
		infoMenuImages.push(actImg);
		actImg = new Image();
		actImg.src = "../design/icons/resultset_" +infoImg[i].id+ "_grey.png";
		infoMenuImagesDisabled.push(actImg);
		//debug(infoMenuImages[i].src + ' \\n' + infoMenuImagesDisabled[i].src);
	}
}

function setItems(items){
	//debug('setItems');
	for(i=0;i<items.length;i++){
		infoItems[i] = items[i];
		//dumpObject(infoItems[i]);
	}
	//dumpObject(infoItems, true);
}

function updateInfoMenu(idx){
	//debug('updateInfoMenu:'+idx);
	for(i=0;i<infoNavImgEl.length;i++){
		if((idx == 0 && i< 2) || (idx == infoItems.length-1 && i > infoNavImgEl.length - 3)){
			infoNavImgEl[i].src = infoMenuImagesDisabled[i].src;
			//debug('disabled:' + infoNavImgEl[i].src +' \\n' +infoMenuImagesDisabled[i].src);
			infoNavImgEl[i].parentNode.removeAttribute('href');
			//infoNavImgEl[i].className = 'disabled';
			infoNavImgEl[i].title = '';
		}
		else{
			infoNavImgEl[i].src = infoMenuImages[i].src;
			//infoNavImgEl[i].className = 'enabled';
			switch(i){
				case 0:
				infoNavImgEl[i].title = infoItems[0].title
				break;
				case 1:
				infoNavImgEl[i].title = infoItems[idx - 1].title
				break;				
				case 2:
				infoNavImgEl[i].title = infoItems[idx + 1].title
				break;
				case 3:
				infoNavImgEl[i].title = infoItems[infoItems.length - 1].title
			}			
			infoNavImgEl[i].parentNode.setAttribute('href', linkList[i]);
			//debug('enabled:' + infoNavImgEl[i].src +' \\n' +infoMenuImages[i].src)
		}
	}
}

function setCurrentIndex(id){
	//debug('setCurrentIndex:'+id);
	for(i=0;i<infoItems.length;i++){
		//debug(i+':'+ infoItems[i].id);
		if(infoItems[i].id == id){
			currentInfoIndex = i;
			break;
		}
	}
	updateInfoMenu(currentInfoIndex);
}

function changeInfo(which){
	switch(which){
		case 'first':
		changeInfoFunction(infoItems[0].id);
		break;
		case 'last':
		changeInfoFunction(infoItems[infoItems.length-1].id);
		break;
		case 'previous':
		changeInfoFunction(infoItems[currentInfoIndex-1].id);
		break;
		case 'next':
		changeInfoFunction(infoItems[currentInfoIndex+1].id)
		break;
	}
}	

function hideInfo(){
	infoHideFunction();
}
/*########################
	SAMPLE IMAGE FUNCTIONS
########################*/
var hiResBox, sampleBox, sampleItems, hiresMarginTop, _samplesReady = false;

function setSampleBoxes(sListId, hiresId){
	//debug("setSampleBoxes");
	hiResBox = $(hiresId);
	hiResBox.fade('out');
	hiResBox.style.display = 'block';
	hiResBox.style.width = contentBox.offsetWidth + 'px';
	hiResBox.style.height = contentBox.style.height + 'px';
	debug('31:'+hiResBox.style.height);
	hiresMarginTop = Math.round((contentBox.offsetHeight - 413)/2);
	sampleBox = $(sListId);
	var samples = sampleBox.getElementsByTagName('img');
	sampleItems = new Array();
	//debug('setSampleBoxes:' + samples.length)
	for(i=0;i<samples.length;i++){
		aLink = samples[i].parentNode;
		aLink.href = "javascript:showHiRes("+i+")";
		sampleItems.push(
			{
				id: i,
				title: samples[i].getAttribute('title'),
				source:samples[i].src.replace(/\.\d+/, ''),
				liveUrl: aLink.rel 
			}
		);
	}
}

function initSamples(){
	//debug('initSamples');
	currentInfoIndexMain = currentInfoIndex;
	setItems(sampleItems);
	changeInfoFunction = showHiRes;
	infoHideFunction = hideHires;
	_samplesReady = true;
}

//function showHiRes(source, liveUrl){
function showHiRes(sampleId){
	//debug('showHiRes:' + sampleId);
	if(!_samplesReady)
		initSamples();
	setCurrentIndex(sampleId);
	hiResBox.innerHTML = "<center><a href='" + infoItems[currentInfoIndex].liveUrl + 
	"' target='_blank' onmousedown='hideHires()'><img src='" + infoItems[currentInfoIndex].source +
	"' style='margin-top:" + hiresMarginTop + "px;' title='Visit this Site' /></a></center>";
	hiResBox.fade('in');
}

function hideHires(){
	currentInfoIndex = currentInfoIndexMain;
	setItems(projectItems);
	changeInfoFunction = requestInfo;
	infoHideFunction = startHide;
	updateInfoMenu(currentInfoIndex);
	_samplesReady = false;
	hiResBox.fade('out');
}