////////////////////
// DEFINING VIEWPORT
var shortCut;
function getViewport(which){
	if(navigator.userAgent.indexOf("MSIE")!=-1) {
		var viewPort = document.getElementById(which).nextSibling;
	}
	else var viewPort = document.getElementById(which).nextSibling.nextSibling;
	return viewPort;
}

function winHeight() {
	if (window.innerHeight) return window.innerHeight;
	else if(document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	else if (document.body && document.body.offsetHeight) {
		return document.body.offsetHeight; // IE QUIRKS-MODE?
	}
	else return 0;
}

/////////////////////////////
// LAUNCH PROJECT IN VIEWPORT
var shortCut = ''; // TEMPORARY CACHE FOR CURRENT PROJECT-ID
var shortcutList = new Array('none');

function launchProject(which,mode) {
	document.getElementById('console').innerHTML = ''; // EMPTY CONSOLE-DIV
	var viewPort = getViewport(which);
	if(String(shortcutList).indexOf(which)!=-1 && mode!='keep') {
		viewPort.style.display = 'none';
		document.getElementById(which).style.width = '';
		document.getElementById(which).style.marginBottom='1px';
		document.getElementById(which).style.backgroundColor=resetBg;
		document.getElementById(which).style.color=resetColor;
		var stringDetour = arrayToString(shortcutList); // OPERA DOESN'T GET INDEXOF IN ARRAY!!!
		stringDetour = stringDetour.replace(','+which,'');
		shortcutList = stringDetour.split(',');
		shortCut = '';
	}
	else if(which != shortCut) {
		document.getElementById(which).style.backgroundColor='#FFFFFF';
		document.getElementById(which).style.color='#000000';
		shortCut = which;
		importXML();
		shortcutList.push(shortCut);
	}
}
function showStatic(which) {
	var viewPort = getViewport(which);
	if(viewPort.style.display == 'none') {
//		if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("MSIE 7.0")==-1) viewPort.style.width = eval(340) + 'px'; // SOLVING IE PADDING-PROBLEM FOR FIXED WIDTH CONTENTS BEFORE IE 7.0
//		else if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("MSIE 8.0")!=-1) viewPort.style.width = eval(340) + 'px'; // SOLVING IE PADDING-PROBLEM FOR FIXED WIDTH CONTENTS IN IE 8.0
		if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("MSIE 7.0") != 1) viewPort.style.width = eval(340) + 'px'; // SOLVING IE PADDING-PROBLEM FOR FIXED WIDTH CONTENTS BEFORE IE 7.0
		else viewPort.style.width = eval(340 + 40 - 3) + 'px';
		document.getElementById(which).style.marginBottom='0px';
		document.getElementById(which).style.backgroundColor='#FFFFFF';
		document.getElementById(which).style.color='#000000';
		viewPort.style.display = 'block';
		shortCut = which;
		shortcutList.push(shortCut);
	}
	else {
		viewPort.style.display = 'none';
		document.getElementById(which).style.marginBottom='1px';
		document.getElementById(which).style.backgroundColor=resetBg;
		document.getElementById(which).style.color=resetColor;
		var stringDetour = arrayToString(shortcutList); // OPERA DOESN'T GET INDEXOF IN ARRAY!!!
		stringDetour = stringDetour.replace(','+which,'');
		shortcutList = stringDetour.split(',');
		shortCut = '';
	}
}

/////////////
// IMPORT XML
var xmlDoc;
var activeX = false;
function importXML() {
	xmlDoc = false;
    if(window.XMLHttpRequest && !(window.ActiveXObject)) { // BRANCH FOR NATIVE XMLHTTPREQUEST OBJECT
    	try {
			xmlDoc = new XMLHttpRequest();
        } catch(e) {
			xmlDoc = false;
        }
    } else if(window.ActiveXObject) { // BRANCH FOR IE/WINDOWS ACTIVEX VERSION
       	try {
        	xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlDoc = false;
        	}
		}
		activeX = true;
    }
	if(xmlDoc) {
		xmlDoc.onreadystatechange = function() {
			if (xmlDoc.readyState == 4) {
				if (xmlDoc.status == 200) { // ONLY IF "OK"
					parseXML();
				} else {
					writeConsole('<span style="color:#FF0000;padding:2px;background-color:#000000">'+shortCut+' ['+xmlDoc.statusText+']<span>'); // OUTPUT IN CONSOLE-DIV
					if(String(shortcutList).indexOf(shortCut)!=-1) { // ERROR ON OPERA!!!
						shortcutList.splice(String(shortcutList).indexOf(shortCut),1);
						shortCut = '';
					}
				}
			}
		}
		xmlDoc.open("GET", "./projects/"+shortCut+"/pix.xml", true);
		xmlDoc.send("");
	}
	else {
		writeConsole('<span style="color:#FF0000;padding:2px;background-color:#000000">Your browser cannot handle XML-parsing with JavaScript</span>'); // OUTPUT IN CONSOLE-DIV
	}
}

////////////////
// WRITE CONSOLE
var txt;
var console;
var consoleTimer;
function clearConsole(txt) {
	document.getElementById('console').style.backgroundColor = '';
	document.getElementById('console').innerHTML = '';
	consoleTimer = false;
}
function writeConsole(txt) {
	if(consoleTimer) {
		window.clearTimeout(console);
	}
	document.getElementById('console').innerHTML = txt;
	document.getElementById('console').style.backgroundColor = '#000000';
	document.getElementById('console').style.top = '1px'; // GET SCROLLED PIXEL
	if(document.body.scrollTop) document.getElementById('console').style.top = (document.body.scrollTop + 1) + 'px';
	else if(document.documentElement.scrollTop) document.getElementById('console').style.top = (document.documentElement.scrollTop + 1) + 'px';
	console = window.setTimeout("clearConsole()",1500);
	consoleTimer = true;
}


////////////
// PARSE XML
var descriptionList = new Array(); // PROJECT DESCRIPTIONS
var sourceList = new Array(); // PROJECTS FOLDERS
var widthList = new Array(); // IMAGE WIDTHS [GET RID OF IN XML!]
var heightList = new Array(); // IMAGE HEIGHTS [GET RID OF IN XML!]
var captionList = new Array(); // IMAGE CAPTIONS
var pixcountList = new Array(); // CACHE FOR CURRENT IMAGE-COUNT
var pixmaxList = new Array(); // NUMBER OF PROJECT-IMAGES
var readFormat = '';
var width = '';
var height = '';

function parseXML() {
	descriptionList[shortCut] = new Array();
	sourceList[shortCut] = new Array();
	widthList[shortCut] = new Array();
	heightList[shortCut] = new Array();
	captionList[shortCut] = new Array();
	pixcountList[shortCut] = new Array('0');
	pixmaxList[shortCut] = new Array(); 
//	writeConsole('<span style="color:#00FF00;padding:2px;background-color:#000000">'+shortCut+' [parsing XML]</span>'); // OUTPUT IN CONSOLE-DIV
	xmlObj = xmlDoc.responseXML.getElementsByTagName('pix').item(0);
	pixmaxList[shortCut][0] = xmlObj.getElementsByTagName('pic').length;
	if(window.ActiveXObject && pixmaxList[shortCut][0] > 0) {
		for (var i = 0; i<pixmaxList[shortCut][0]; i++) {
			descriptionList[shortCut].push(xmlObj.getElementsByTagName('description')[0].childNodes[0].nodeValue);
			sourceList[shortCut].push(xmlObj.getElementsByTagName('pic')[i].childNodes[0].childNodes[0].nodeValue);
			readFormat = xmlObj.getElementsByTagName('pic')[i].childNodes[0].childNodes[0].nodeValue;
			width = readFormat.substr(readFormat.lastIndexOf('_')+1,readFormat.lastIndexOf('x')-2);
			height = readFormat.substr(readFormat.lastIndexOf('x')+1,readFormat.length);
			widthList[shortCut].push(width);
			heightList[shortCut].push(height);
			if(!xmlObj.getElementsByTagName('pic')[i].childNodes[1].childNodes[0]) {
				captionList[shortCut].push('&nbsp;'); // XML-PARSER DOESNT ACCEPT EMPTY NODE
			}
			else captionList[shortCut].push(xmlObj.getElementsByTagName('pic')[i].childNodes[1].childNodes[0].nodeValue);
		}
	}
	else if(pixmaxList[shortCut][0] > 0) {
		for (var i = 0; i<pixmaxList[shortCut][0]; i++) {
			descriptionList[shortCut].push(xmlObj.getElementsByTagName('description')[0].childNodes[0].nodeValue);
			sourceList[shortCut].push(xmlObj.getElementsByTagName('pic')[i].childNodes[1].childNodes[0].nodeValue);
			readFormat = xmlObj.getElementsByTagName('pic')[i].childNodes[1].childNodes[0].nodeValue;
			width = readFormat.substr(readFormat.lastIndexOf('_')+1,readFormat.lastIndexOf('x')-2);
			height = readFormat.substr(readFormat.lastIndexOf('x')+1,readFormat.length);
			widthList[shortCut].push(width);
			heightList[shortCut].push(height);
			if(!xmlObj.getElementsByTagName('pic')[i].childNodes[3].childNodes[0]) {
				captionList[shortCut].push(' '); // XML-PARSER DOESNT ACCEPT EMPTY NODE
			}
			else captionList[shortCut].push(xmlObj.getElementsByTagName('pic')[i].childNodes[3].childNodes[0].nodeValue);
		}
	}
	else { // OUTPUT IF NO PIX ARE PROVIDED IN XML
		descriptionList[shortCut].push(xmlObj.getElementsByTagName('description')[0].childNodes[0].nodeValue);
	}
//	writeConsole('<span style="color:#00FF00;padding:2px;background-color:#000000">'+shortCut+' [XML parsing done]</span>'); // OUTPUT IN CONSOLE-DIV
	projectShow();
}

/////////////////
// PROJECT VIEWER
function projectShow() {
	var viewPort = getViewport(shortCut);
	document.getElementById(shortCut).style.marginBottom = '0px';
	if(widthList[shortCut][pixcountList[shortCut][0]] && viewPort.style.width != parseInt(widthList[shortCut][pixcountList[shortCut][0]]) + 'px') viewPort.style.width = parseInt(widthList[shortCut][pixcountList[shortCut][0]]) + 'px';
	if(pixmaxList[shortCut][0] == 0) {
		viewPort.innerHTML = '<div style="background-color:#FFFFFF;"><div class="diaText" style="margin-bottom:2px;text-align:left;">' + descriptionList[shortCut] + '<\/div><\/div>';
		document.getElementById(shortCut).style.width = 340 + 'px';
		if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("MSIE 7.0") == -1) viewPort.style.width = eval(340) + 'px'; // SOLVING IE PADDING-PROBLEM FOR FIXED WIDTH CONTENTS BEFORE IE 7.0
		else viewPort.style.width = eval(340 + 40 - 3) + 'px';
	}
	else {
		if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("MSIE 7.0")==-1) document.getElementById(shortCut).style.width = parseInt(widthList[shortCut][pixcountList[shortCut][0]])+6 + 'px'; // SOLVING IE PADDING-PROBLEM BEFORE IE 7.0
		else document.getElementById(shortCut).style.width = parseInt(widthList[shortCut][pixcountList[shortCut][0]])-40+3 + 'px';
		var captionText = captionList[shortCut][parseInt(pixcountList[shortCut][0])];
		var altText=captionText;
		if(altText.indexOf('at <')!=-1) { // STRIP CAPTION FROM EVTL. HTML TAGS
			altText = altText.substr(0,altText.indexOf('at <')-1);
		}
		else if(altText.indexOf('<')!=-1) {
			altText = altText.substr(0,altText.indexOf('<')-1);
		}
		if(pixmaxList[shortCut][0] <= 1) {
			viewPort.innerHTML = '<div style="width:' + parseInt(widthList[shortCut][pixcountList[shortCut][0]]) + 'px;background-color:#FFFFFF;"><div class="diaText" style="margin-bottom:2px;text-align:left;">' + descriptionList[shortCut][pixcountList[shortCut][0]] + '<\/div><div id="pixFrame" style="width:' + parseInt(widthList[shortCut][pixcountList[shortCut][0]]) + 'px;height:' + parseInt(heightList[shortCut][pixcountList[shortCut][0]]) + 'px;"><img src="projects/' + shortCut + '/' + sourceList[shortCut][pixcountList[shortCut][0]] + '" width="'+parseInt(widthList[shortCut][pixcountList[shortCut][0]])+'" height="'+parseInt(heightList[shortCut][pixcountList[shortCut][0]])+'" alt="'+altText+'" /></div><div class="diaText" style="margin-top:2px;float:left;text-align:left;color:#000000;">'+ captionList[shortCut][pixcountList[shortCut][0]] + '<\/div><div class="diaText" style="margin-top:2px;float:right;"><\/div><div class="clearfloat"><\/div><\/div>';
		}
		else {
			viewPort.innerHTML = '<div style="width:' + parseInt(widthList[shortCut][pixcountList[shortCut][0]]) + 'px;background-color:#FFFFFF;"><div class="diaText" style="margin-bottom:2px;text-align:left;">' + descriptionList[shortCut][pixcountList[shortCut][0]] + '<\/div><div id="pixFrame" style="width:' + parseInt(widthList[shortCut][pixcountList[shortCut][0]]) + 'px;height:' + parseInt(heightList[shortCut][pixcountList[shortCut][0]]) + 'px;"><img src="projects/' + shortCut + '/' + sourceList[shortCut][pixcountList[shortCut][0]] + '" width="'+parseInt(widthList[shortCut][pixcountList[shortCut][0]])+'" height="'+parseInt(heightList[shortCut][pixcountList[shortCut][0]])+'" alt="'+altText+'" /></div><div class="diaText" style="margin-top:2px;float:left;text-align:left;color:#000000;">'+ captionList[shortCut][pixcountList[shortCut][0]] + '<\/div><div class="diaText" style="margin-top:2px;float:right;"><a href="javascript:projectStep(\'minus\',\''+shortCut+'\');" style="text-decoration:none;">previous<\/a> | ' + eval(pixcountList[shortCut][0]+1) + ' OF ' + pixmaxList[shortCut][0] + ' | <a href="javascript:projectStep(\'plus\',\''+shortCut+'\');" style="text-decoration:none;">next<\/a><\/div><div class="clearfloat"><\/div><\/div>';
		}
	}
	viewPort.style.display = 'block';
	window.setTimeout("scrollViewport()",200);
}

///////////////////////////
// SCROLL PROJECT INTO VIEW
function scrollViewport() {
	var viewPort = getViewport(shortCut);
	if(navigator.userAgent.indexOf("MSIE")!=-1) {
		var fromTop = viewPort.offsetParent.offsetTop;
		if(navigator.userAgent.indexOf("MSIE 7.0")==-1) var scrolled = document.body.scrollTop;
		else var scrolled = document.documentElement.scrollTop;
	}
	else {
		var fromTop = viewPort.offsetTop;
		var scrolled = window.pageYOffset;
	}
	if((fromTop+document.getElementById(shortCut).offsetHeight+viewPort.offsetHeight) > (scrolled+winHeight())) {
		window.scrollBy(0, ((fromTop+document.getElementById(shortCut).offsetHeight+viewPort.offsetHeight) - (scrolled+winHeight()-10)));
	}
}

function projectStep(direction, projectClicked) {
	shortCut = projectClicked;
	if (direction == 'plus') pixcountList[shortCut].push(eval(pixcountList[shortCut][0]+1));
	else if (direction == 'minus') pixcountList[shortCut].push(eval(pixcountList[shortCut][0]-1));
	pixcountList[shortCut].shift();
	if (pixcountList[shortCut][0] < pixmaxList[shortCut][0] && pixcountList[shortCut][0] >= 0) {
		viewPort = getViewport(shortCut);
		projectShow();
	}
	else if (pixcountList[shortCut][0] < 0) {
		pixcountList[shortCut][0] = pixmaxList[shortCut][0]-1;
		viewPort = getViewport(shortCut);
		projectShow();
	}
	else if (pixcountList[shortCut][0] >= pixmaxList[shortCut][0]) {
		pixcountList[shortCut][0] = 0;
		viewPort = getViewport(shortCut);
		projectShow();
	}
}
