﻿var ie5;
var ie7;
var ns6;
var blnDirtyQuickFixForInternetExplorerThatBehavesLikeAnIdiotAgain = false;

var blnFirstRun = false;
var intRollingImagesCount = 0;
var intFrameWidth = 7;

function startup() {
	var imgDivBg = new Image();
	imgDivBg.src = "img/div_bg_" + jsres + ".jpg";
	ie5=document.all&&document.getElementById&&!window.XMLHttpRequest;
	ns6=document.getElementById&&!document.all;
	ie7=document.all&&document.getElementById&&window.XMLHttpRequest;
	positionThings();
	if(g("divElementCount")) {
		var arrAll = document.getElementsByTagName("*");
		g("divElementCount").innerHTML = arrAll.length;
	}
	if(ie5) {
		g("divMenu").style.width = (g("divMenu").offsetWidth + resValue(2, 3, 2)) + "px";
	}
}

function resValue(r0, r1, r2) {
	switch(jsres) {
		case 0:
			return r0;
			break;
		case 1:
			return r1;
			break;
		case 2:
			return r2;
			break;
	}
}

function delayPositionThings() {
		setTimeout("positionThings()", 750);
}

function positionThings() {
	if(!blnFirstRun && blnDirtyQuickFixForInternetExplorerThatBehavesLikeAnIdiotAgain) {
		return; 
	}
	var pageSize = getPageSize();
	var pageHeight = (ie5 ? pageSize[1] : pageSize[3]);
	
	if(ie5 && !blnFirstRun) {
		addPngFix();
	}
	if(ie5) {
		
	}
	if(!blnFirstRun) {
		blnDirtyQuickFixForInternetExplorerThatBehavesLikeAnIdiotAgain = true;
		doImageReplacements();
		doFrames();
		doParseLinks(false, null);
		doParseLinks(true, g("divContents"));
		doParseLinks(true, g("tdRight"));
		//doUnParseLinks();
		blnDirtyQuickFixForInternetExplorerThatBehavesLikeAnIdiotAgain = false;
	}
	blnFirstRun = true; 
}


function doParseLinks(blnOnlyDiv, objSmth) {
	var arrLinks;
	var blnOkToParse = false;
	if(blnOnlyDiv) {
		arrLinks = objSmth.getElementsByTagName("a");
	} else {
		arrLinks = document.getElementsByTagName("a");
	}
	for(var i = arrLinks.length - 1, len = 0; i >= len; i--) {
		blnOkToParse = false;
		
		if(arrLinks[i].className.indexOf("autobracket") > -1 && !blnOnlyDiv) {
			blnOkToParse = true;
		}
		if(arrLinks[i].className.indexOf("autobracket") <= -1 && blnOnlyDiv) {
			blnOkToParse = true;
		}
		if(arrLinks[i].className.indexOf("leaveit") > -1 || arrLinks[i].className.indexOf("ga_block") > -1) {
			blnOkToParse = false;
		}
		
		if(blnOkToParse) {
			var obj = arrLinks[i];
			var strStyle = obj.className.toString();
			strStyle = strStyle.replace("autobracket", "");
			strStyle = strStyle.replace("  ", " ");
			var objBefore = document.createElement("span");
			var objAfter = document.createElement("span");
			objBefore.innerHTML = "["; objAfter.innerHTML = "]";
			objBefore.className = objAfter.className = strStyle;
			objBefore.className += " autoInsertedBracketThingy";
			objAfter.className += " autoInsertedBracketThingy";
			if(blnOnlyDiv && strStyle == "")
				objBefore.style.color = objAfter.style.color = "#4d83b3";
				
			obj.parentNode.insertBefore(objBefore, obj);
			insertAfter(objAfter, obj);
//			if(obj.target != "") {
//				var objOuttaHere = document.createElement("span");	
//				objOuttaHere.innerHTML = "->";
//				insertAfter(objOuttaHere, objAfter);
//			}
		}
	}
}

function doUnParseLinks() {
	var arrSkipParentBlocks = new Array("ga_block");
	for(var i = 0, ilen = arrSkipParentBlocks.length; i < ilen; i++) {
		var arrSkipLinks = getElementsByClassName("autoInsertedBracketThingy", null, g(arrSkipParentBlocks[i]));
		for(var j = 0, jlen = arrSkipLinks.length; j < jlen; j++) {
			arrSkipLinks[j].parentNode.removeChild(arrSkipLinks[j]);
		}
	}
}

function insertAfter(newElement,targetElement) {  
    var parent = targetElement.parentNode;  
    if (parent.lastChild == targetElement) {  
        parent.appendChild(newElement);  
    } else {  
        parent.insertBefore(newElement,targetElement.nextSibling);  
    }  
}  

function doImageReplacements() {
	var arrImgs = document.getElementsByTagName("img");
	for(var i = arrImgs.length - 1, len = 0; i >= len; i--) {
		if(arrImgs[i].className.indexOf("autoscale") > -1) {
			var obj = arrImgs[i];
			var strImageName = obj.src;
			var strExtension = strImageName.substr(strImageName.lastIndexOf("."));
			var strNameOnly = strImageName.substr(0, strImageName.lastIndexOf("."));
			if(obj.getAttribute("height") + "x" != "x")
				obj.setAttribute("height", p(p(obj.getAttribute("height")) * crf));
			if(obj.getAttribute("width") + "x" != "x")
				obj.setAttribute("width", p(p(obj.getAttribute("width")) * crf));
			strNameOnly = strNameOnly.substr(strNameOnly.lastIndexOf("/") + 1);
			// if second chr from the right is an underscore then test if imageref is correct
			if(strNameOnly.substr(strNameOnly.length - 2, 1) == "_") {
				var intImageIndex = p(strNameOnly.substr(strNameOnly.length - 1));
				if(p(intImageIndex) != p(jsres)) {
					obj.src = strImageName.substr(0, strImageName.lastIndexOf("/") + 1) + strNameOnly.substr(0, strNameOnly.length - 1) + jsres.toString() + strExtension;
				}
			} else {
				obj.src = strImageName.substr(0, strImageName.lastIndexOf("/") + 1) + strNameOnly + "_" + jsres.toString() + strExtension;
			}
		}
	}
}

function doFrames() {
	var arrImgs = document.getElementsByTagName("img");
	for(var i = arrImgs.length - 1, len = 0; i >= len; i--) {
		if(arrImgs[i].className.indexOf("frameon") > -1) {
			var div = document.createElement("DIV");
			div.style.margin = "0px";
			if(arrImgs[i].className.indexOf("framefloat") > -1) {
				if(arrImgs[i].className.indexOf("framefloatright") > -1) {
					div.style.cssFloat = "right";
					div.style.styleFloat = "right";
				}
			}
			var obj = arrImgs[i];
			var tbl = document.createElement("TABLE");
			tbl.cellPadding = "0"; tbl.cellSpacing = "0";
			tbl.insertRow(0); tbl.insertRow(1); tbl.insertRow(2);
			for(var j = 0; j < 3; j++) {
				tbl.rows[j].insertCell(-1);
				tbl.rows[j].insertCell(-1);
				tbl.rows[j].insertCell(-1);
			}
			tbl.rows[0].style.height = intFrameWidth + "px";
			tbl.rows[2].style.height = intFrameWidth + "px";
			tbl.rows[0].cells[0].style.width = intFrameWidth + "px";
			tbl.rows[0].cells[2].style.width = intFrameWidth + "px";
			
			div.appendChild(tbl);
			div.style.width = obj.offsetWidth + (intFrameWidth * 2) + "px";
			div.style.height = obj.offsetHeight + (intFrameWidth * 2) + "px";
			
			var imgTopLeft = document.createElement("IMG");
			imgTopLeft.src = "img/frame_top_left.jpg";
			tbl.rows[0].cells[0].innerHTML = "<img src=\"img/frame_top_left.jpg\">";
			tbl.rows[2].cells[0].innerHTML = "<img src=\"img/frame_bot_left.jpg\">";
			tbl.rows[0].cells[2].innerHTML = "<img src=\"img/frame_top_right.jpg\">";
			tbl.rows[2].cells[2].innerHTML = "<img src=\"img/frame_bot_right.jpg\">";
			tbl.rows[0].cells[1].className = "frameTop";
			tbl.rows[1].cells[2].className = "frameRight";
			tbl.rows[2].cells[1].className = "frameBot";
			tbl.rows[1].cells[0].className = "frameLeft";
			
			obj.parentNode.insertBefore(div, obj);
			obj.parentNode.removeChild(obj);
			tbl.rows[1].cells[1].appendChild(obj);
		}
	}
	//alert(g("divContents").innerHTML);
}

function colorizeArrow(obj, blnIsOver, strVari) {
	obj.src = (blnIsOver ? "img/arrow_red_" + strVari + "_" + jsres + ".png" : "img/arrow_gray_" + strVari + "_" + jsres + ".png");
}

function rollImages(dir) {
	// dir  =  prev / next
	
	if(blnSliderMoving) {
		return;
	}
	var intImageMoveWidth = 0;
	var intImageMoveCount = 3;
	switch(jsres) {
		case 0:
			intImageMoveWidth = 303;
			break
		case 1:
			intImageMoveWidth = 378;
			break
		case 2:
			intImageMoveWidth = 473;
			break
	}
	
	if(intFrontImageCount > 3) {
		if(dir=="next") {
			intImageMoveCount = intFrontImageCount - intRollingImagesCount - 3;
			if(intImageMoveCount > 3 ) {
				intRollingImagesCount += 3;
				intImageMoveCount = 3;
			} else {
				intRollingImagesCount += intImageMoveCount;
			}
		} else {
			if(intRollingImagesCount > 0) {
				if(intRollingImagesCount <= 3) {
					intImageMoveCount = intRollingImagesCount;
					intRollingImagesCount = 0;
				} else {
					intImageMoveCount = 3;
					intRollingImagesCount-= 3;
				}
			}
		}
	}
	if(dir=="next") {
		xy_scroller('divFrontImageContainer', p(g('divFrontImageContainer').style.left), (intImageMoveWidth * intRollingImagesCount * -1), 50, 8);
	} else {
		xy_scroller('divFrontImageContainer', p(g('divFrontImageContainer').style.left), (intImageMoveWidth * intRollingImagesCount * -1), 50, 8);
	}
}

function addPngFix() {
	var arr = document.getElementsByTagName("img");
	for(var i = 0; i < arr.length; i++) {
		if(arr[i].src) {
			if(arr[i].src.toLowerCase().indexOf("png") > 0) {
				if(arr[i].className == "") {
					arr[i].className = "overlay";
				} else {
					if(arr[i].className != "overlay" || arr[i].className != "overlaystay") {
						arr[i].className += " overlay";
					}
				}
			}
		}
	}
}

function showFrontText(objSource, idno) {
	var obj = g("divFrontText" + idno);
	if(obj.innerHTML==""||blnSliderMoving) {
		return;
	}
	obj.style.display = "block";
	obj.style.top = "0px";
	obj.style.left = "0px";
	obj.style.top = ryposObj(objSource.parentNode) + objSource.parentNode.offsetHeight + "px";
	obj.style.left = rxposObj(objSource.parentNode) + "px";
	obj.style.width = objSource.parentNode.offsetWidth - (p(objSource.parentNode.style.paddingLeft) * 2) - 2 + "px";
}

function hideFrontText(idno) {
	g("divFrontText" + idno).style.display = "none";
}

function toggleInfo() {
	if(g('masterContainer1').style.display=="block") {
		g('masterContainer1').style.display = "none";
		g('documentContainer1').style.display = "none";
		g('arrowContainer1').style.display = "none";
		g('imgTextArea').style.display = "none";
		g('lukknap').style.display = "none";
	}
	else {
		g('masterContainer1').style.display = "block";
		g('documentContainer1').style.display = "block";
		g('arrowContainer1').style.display = "block";
		g('imgTextArea').style.display = "block";
		g('lukknap').style.display = "block";
	}
}
var intCurrImg = 0;

function nextImage() {
	if(typeof(arrBGimg)!="undefined") {
		intCurrImg += 1;
		if(intCurrImg>=arrBGimg.length) {
			intCurrImg = 0;
		}
		g('docCell').style.backgroundImage = "url('" + arrBGimg[intCurrImg] + "')";
	}
}

function prevImage() {
	if(typeof(arrBGimg)!="undefined") {
		intCurrImg -= 1;
		if(intCurrImg<0) {
			intCurrImg = arrBGimg.length - 1;
		}
		g('docCell').style.backgroundImage = "url('" + arrBGimg[intCurrImg] + "')";
	}
}


function printThis(v)
{ 
	if(v!=0) {
		document.forms.printform.printboat.value = v;
	}
	document.forms.printform.printwhat.value = document.getElementById("documentContainer").innerHTML;
	document.forms.printform.submit();
}

function printerfocus()
{
	printframe.focus();
	printframe.print();
}

function formatLineNumbers() {
	var obj = g("divLineNumbers");
	var arrLines = obj.innerHTML.split("<BR>");
	var indentCount = 0;
	obj.innerHTML = "";
	for(var i = 0; i < arrLines.length; i++) {
		if(arrLines[i].indexOf("}") > -1) {
			indentCount--;
		}
		if(indentCount < 0 ) indentCount = 0;
		
		obj.innerHTML += i+1 + ": " + rs(indentCount) + arrLines[i] + "<br>";
		if(arrLines[i].indexOf("{") > -1) {
			indentCount++;
		}
	}
}

function formatLineNumbersToTable() {
	var obj = g("divLineNumbers");
	var arrLines = obj.innerHTML.split("<BR>");
	var indentCount = 0;
	var output = "";
	output = "<table cellpadding='0' cellspacing='0' border='0'>";
	for(var i = 0; i < arrLines.length; i++) {
		if(arrLines[i].indexOf("}") > -1) {
			indentCount--;
		}
		if(indentCount < 0 ) indentCount = 0;
		
		output += "<tr><td>" + (i+1) + ": &nbsp;</td><td>" + rs(indentCount) + arrLines[i] + "</td></tr>";
		if(arrLines[i].indexOf("{") > -1) {
			indentCount++;
		}
	}
	output += "</table>";
	obj.innerHTML = output;
}

var arrIndentPlus = new Array("{", "Then", "While", "Function", "Else", "For", "Public", "Private", "Protected", "<html>", "<head>", "<body", "&lt;html&gt;", "&lt;head&gt;", "&lt;body", "&lt;script");
var arrIndentMinus = new Array("}", "End If", "Wend", "End Function", "Else", "Next", "End Sub", "</html>", "</head>", "</body>", "&lt;/html&gt;", "&lt;/head&gt;", "&lt;/body&gt;", "&lt;/script&gt");
var arrSkipLines = new Array("'", "//", "/*");
var arrSingleComment = new Array("'", "//");
var arrMultiComments = new Array("/*","*/");

function oc(a)
/// http://snook.ca/archives/javascript/testing_for_a_v/
{
  var o = {};
  for(var i=0;i<a.length;i++) {
    o[a[i]]='';
  }
  return o;
}

function executeFormatLineNumbers() {
	var arrDivs = document.getElementsByTagName("div");
	for(var i = 0, len = arrDivs.length; i < len; i++) {
		if(arrDivs[i].className.indexOf("codelines") > -1) {
			formatLineNumbersTheHardWay(arrDivs[i]);
		}
	}
}

function formatLineNumbersTheHardWay(obj) { // format data into table with line numbers 
	var blnException = false;
	
	checkBackupObject(obj.id); //creates copy of current obj (div with classname "codelines")
	obj.style.fontFamily = "courier new";
	obj.style.fontSize = "12px";
	var arrBreaks = new Array();
	var strSplit = "<br>";
	var thisText = obj.innerHTML.toLowerCase();
	var intTempStartValue = 0;
	var output = "";
	var indentCount = 0;
	var iii = 0;
	
	while (thisText.indexOf(strSplit,intTempStartValue) > -1) { // populate array of text index values where the "strSplit" character is present
		intTempStartValue = thisText.indexOf(strSplit, intTempStartValue) + 1; // find next "strSplit" location
		arrBreaks[arrBreaks.length] = intTempStartValue; // add found location to end of array
	}
		
	thisText = obj.innerHTML;
	obj.innerHTML = "";
	
	if(thisText.indexOf("[cdata]") == 0) { // if first line of data is string literal "[cdata]" when set var blnException to "true"
		blnException = true;
	}
	
	var tbl = document.createElement("table");  // create a table with zero cellpadding and cellspacing
	tbl.cellPadding = "0";
	tbl.cellSpacing = "0";
	obj.appendChild(tbl); // append the newly created table for the DOM
	var row = tbl.insertRow(tbl.rows.length); // add a new row to the table
	var cellLeft = row.insertCell(0); // add the left cell (for line numbers)
	cellLeft.style.textAlign = "right";
	var cellRight = row.insertCell(1); // add the right cell (for text)
	
	intTempStartValue = 0; // reset the start value variable
	var i = 0; // declare counter variable (to keep track of "strSplit" index)
	
	while(intTempStartValue <= arrBreaks[arrBreaks.length-1]) { // loop through "strSplit" locations
		var strPart = thisText.substring(intTempStartValue, parseInt(arrBreaks[i])-1); // substring part of the text from start value to index of next "strSplit"
		
		for(var j=0;j<arrIndentMinus.length;j++) { // loop through strings that increase indentation
			// if current indent character is found in text and the first one or two character are not present in array "arrSkipLines" then increase indentation count
			if(strPart.indexOf(arrIndentMinus[j]) > -1 && !(strPart.substr(0,1) in oc(arrSkipLines)) && !(strPart.substr(0,2) in oc(arrSkipLines))) {
					indentCount = (blnException ? indentCount : indentCount -= 1); // increate indentation count only if this is not an exception
			}
		}
		
		if(indentCount < 0 ) indentCount = 0; // simple check to ensure a zero+ indentation count

		cellLeft.innerHTML += (i + 1 - (blnException ? 1 : 0)) + ":&nbsp;&nbsp;<br>"; // add line number to left cell
		cellRight.innerHTML += returnSpaces(indentCount) + (blnException && i == 0 ? strPart.replace("[cdata]", "") : strPart) + "<br>"; // add text to right cell and remove "[cdata]" if present
		
		if (blnException && i == 0) { // delete first row if right cell is empty or just contains "<br>"
			var cellRightText = cellRight.innerHTML.replace(returnSpaces(indentCount), "").toLowerCase();
			if(cellRightText.length == 0 || cellRightText == "<br>") {
				tbl.deleteRow(row);
			}
		}
		row = tbl.insertRow(tbl.rows.length);
		cellLeft = row.insertCell(0);
		cellLeft.style.textAlign = "right";
		cellRight = row.insertCell(1);
		
		intTempStartValue = parseInt(arrBreaks[i]) + (strSplit.length - 1);
		
		for(var j=0;j<arrIndentPlus.length;j++) { // loop through strings that decrease indentation
			if(strPart.indexOf(arrIndentPlus[j]) > -1 && !(strPart.substr(0,1) in oc(arrSkipLines)) && !(strPart.substr(0,2) in oc(arrSkipLines))) {
				indentCount = (blnException ? indentCount : indentCount += 1); // decreate indentation count only if this is not an exception
			}
		}
		i++;
	}

	cellLeft.innerHTML += (i + 1 - (blnException ? 1 : 0)) + ":&nbsp;&nbsp;<br>";
	cellRight.innerHTML += returnSpaces(indentCount-1) + (thisText.substr(arrBreaks[arrBreaks.length-1]+strSplit.length-1));
	if(tbl.rows.length > 0) {
		if(tbl.rows[0].cells.length < 1) {
			return;
		}
		var lnkToggleLineNumbers = document.createElement("a");
		var strBreak = document.createElement("br");
		obj.appendChild(strBreak);
		obj.appendChild(lnkToggleLineNumbers);
		lnkToggleLineNumbers.innerHTML= "Toggle line numbers";
		lnkToggleLineNumbers.style.cursor = "pointer";
		lnkToggleLineNumbers.onclick = function() { toggleLineNumbers(tbl); };
	}
}

function toggleLineNumbers(tbl) {
	var strDisplay = "";
	var cell = tbl.rows[0].cells[0];
	strDisplay = cell.style.display;
	if(strDisplay != "none") {
		for(var i = 0, len = tbl.rows.length; i < len; i++) {
			tbl.rows[i].cells[0].style.display = "none";
			tbl.rows[i].cells[0].innerHTML = "";
		}
	} else {
		for(var i = 0, len = tbl.rows.length; i < len; i++) {
			tbl.rows[i].cells[0].style.display = "block";
			tbl.rows[i].cells[0].innerHTML = (i + 1) + ":&nbsp;&nbsp;<br>";
		}
	}
}

function checkBackupObject(v) {
	if(!g(v + "_backup")) {
		var d = document.createElement("div");
		d.id = v + "_backup";
		d.style.display = "none";
		document.body.appendChild(d);
		d.innerHTML = g(v).innerHTML;
	}
}


function returnSpaces(v) {
	var strTmp = "";
	for(var i = 0; i < v*4; i++) {
		strTmp += "&nbsp;";
	}
	return strTmp;
}