/*
#VERSION_HISTORY_START

V1.03 - 05/07/11 AC
- added NewWindow Function

V1.02 - 12/07/10 TS
- added optional height parameter to OpenPopup() method

V1.01 - 23/02/10 TS
- when a gadget is how shown / hidden from the display, PostShow() and PostHide() methods of the gadget objects are executed IF they are defined

#VERSION_HISTORY_END
*/

function Page() {
	// object variables
	this.objectList = new Array();
	this.gadgetTypes = new Array();
	this.hiddenGadgetTypes = new Array();
	this.tagLookup = new Array();
	this.currentObjectPointer = -1;
	this.uniqueCount = 0; // generic variable for generating unique values
	this.win=null;
	
	// gadget object methods
	this.SetCurrentGadget = SetCurrentGadget;
	this.NewPageObject = NewPageObject;
	this.PageObject = PageObject;
	this.CurrObject = CurrObject;
	this.GadgetObject = GadgetObject;
	
	// layout variables
	this.boxTop = 0;
	this.boxBottom = 0;
	
	// page layout methods
	this.GetObjectHeight = GetObjectHeight;
	this.GetChildDiv = GetChildDiv;
	this.AdjustCol = AdjustCol;
	this.AdjustTable = AdjustTable;
	this.ResizeDivs = ResizeDivs;
	this.ResizeTables = ResizeTables;
	this.AdjustImageSrc = AdjustImageSrc;
	this.ChangeLanguage = ChangeLanguage;
	this.ShowHiddenGadget = ShowHiddenGadget;
	this.ShowGadgetByTag = ShowGadgetByTag;
	this.HideGadgetByTag = HideGadgetByTag;
		
	// misc methods
	this.OpenPopup = OpenPopup;
	this.NewWindow = NewWindow;
	this.ShowSmallLoader = ShowSmallLoader;
	this.GetUniqueValue = GetUniqueValue;
	
	/* define class methods */
	
	// sets the css to show a gadget on screen
	this.ShowGadget = function(gadgetId) {
		document.getElementById('PageGadget'+gadgetId).style.display = '';
		// if there is a gadget class method to execute after the gadget is shown, execute it
		if (this.PageObject(gadgetId) != null) {
			if (typeof(this.PageObject(gadgetId).PostShow) == 'function') {
				this.PageObject(gadgetId).PostShow();
			}
		}
	}
	
	// sets the css to hide a gadget on screen
	this.HideGadget = function(gadgetId) {
		document.getElementById('PageGadget'+gadgetId).style.display = 'none';
		// if there is a gadget class method to execute after the gadget is hidden, execute it
		if (this.PageObject(gadgetId) != null) {
			if (typeof(this.PageObject(gadgetId).PostHide) == 'function') {
				this.PageObject(gadgetId).PostHide();
			}
		}
	}
}

function SetCurrentGadget(gadgetId, gadgetType, startHidden, tagName) {
	this.currentObjectPointer = gadgetId;
	if (typeof(this.gadgetTypes[gadgetType]) == 'undefined') {
		this.gadgetTypes[gadgetType] = new Array();
	}
	this.gadgetTypes[gadgetType].push(gadgetId);
	if (startHidden) {
		if (typeof(this.hiddenGadgetTypes[gadgetType]) == 'undefined') {
			this.hiddenGadgetTypes[gadgetType] = new Array();
		}
		this.hiddenGadgetTypes[gadgetType].push(gadgetId);
	}
	if (tagName != '') this.tagLookup[tagName] = gadgetId;
}

function NewPageObject(objectData) {
	objectData.gadgetId = this.currentObjectPointer;
	objectData.currentPage = this;
	this.objectList[this.currentObjectPointer] = objectData;
}

function CurrObject() {
	return this.objectList[this.currentObjectPointer];
}

function GadgetObject(gadgetType, gadgetFunction) {
	var functionArguments = new Array();
	var argumentValue;
	for (i = 2; i < arguments.length; i++) {
		argumentValue = arguments[i];
		if (typeof(argumentValue) == 'string') {
			argumentValue = '"'+argumentValue+'"';	
		}
		functionArguments.push(argumentValue);
	}
	
	if (typeof(this.gadgetTypes[gadgetType]) != 'undefined') {
		for (j = 0; j < this.gadgetTypes[gadgetType].length; j++) {
			gadgetId = this.gadgetTypes[gadgetType][j];
			if (typeof(this.objectList[gadgetId]) != 'undefined') {
				eval("this.objectList["+gadgetId+"]."+gadgetFunction+"("+functionArguments.join(',')+")");
			}
		}
	}
}

function PageObject(gadgetId) {
	if (typeof(this.objectList[gadgetId])) {
		return this.objectList[gadgetId];
	}
	return null;
}

function GetObjectHeight(obj) {
	return obj.offsetHeight;
}

function GetChildDiv(parentObject, childPos) {
	var objectCount = 0;
	if (childPos > 0) {
		for (i = 0; i < parentObject.childNodes.length; i++) {
			if (typeof(parentObject.childNodes[i].tagName) != 'undefined') {
				if (parentObject.childNodes[i].tagName == 'DIV') {
					objectCount++;
				}
			}
			if (objectCount == childPos) {
				return parentObject.childNodes[i];
			}
		}
	} else {
		for (i = parentObject.childNodes.length-1; i >= 0; i--) {
			if (typeof(parentObject.childNodes[i].tagName) != 'undefined') {
				if (parentObject.childNodes[i].tagName == 'DIV') {
					objectCount++;
				}
			}
			if (objectCount == (childPos * -1)) {
				return parentObject.childNodes[i];
			}
		}
	}
}

function AdjustCol(colObject, tableHeight) {
	var divObj;
	var colTotal = 0;
	var divCount = 0;
	
	for (currentDiv = 0; currentDiv < colObject.childNodes.length; currentDiv++) {
		if (typeof(colObject.childNodes[currentDiv].tagName) != 'undefined') {
			divObj = colObject.childNodes[currentDiv];
			colTotal += this.GetObjectHeight(divObj);
			divCount++;
		}
	}
	
	if (divCount > 0 && colTotal < tableHeight) {
		var colDifference = tableHeight - colTotal;
		this.GetChildDiv(divObj, 3).style.height = colDifference + 'px';
		/*
		var contentDiv = this.GetChildDiv(divObj, 2);
		var newHeight = this.GetObjectHeight(contentDiv) + colDifference;
		contentDiv.style.height = newHeight + 'px';
		*/
	}
}

function AdjustTable(tableObject) {
	var tableHeight = this.GetObjectHeight(currentTable);
	
	var tableRow = tableObject.getElementsByTagName('tr')[0];
	for (currentCol = 0; currentCol < tableRow.childNodes.length; currentCol++) {
		if (typeof(tableRow.childNodes[currentCol].tagName) != 'undefined') {
			this.AdjustCol(tableRow.childNodes[currentCol], tableHeight);
		}
	}
}

function ResizeDivs() {
	var i, topParent, borderSize, thisHeight, topParentHeight, newSize, topPadding;
	var divList = document.getElementsByTagName("DIV");
	for (i = 0; i < divList.length; i++) {
		if (typeof(divList[i].className) != 'undefined') {
			if (divList[i].className.indexOf('maxHeight') > -1 || divList[i].className.indexOf('centreVertical') > -1) {
				topParent = divList[i].parentNode.parentNode.parentNode;
				borderSize = 0;
				if (typeof(topParent.childNodes[0].tagName) == 'undefined') {
					var spacerDiv = topParent.childNodes[7];
					borderSize += this.GetObjectHeight(topParent.childNodes[1]);
					borderSize += this.GetObjectHeight(topParent.childNodes[3]);
					borderSize += this.GetObjectHeight(topParent.childNodes[9]);
					borderSize += this.GetObjectHeight(topParent.childNodes[11]);
				} else {
					var spacerDiv = topParent.childNodes[3];
					borderSize += this.GetObjectHeight(topParent.childNodes[0]);
					borderSize += this.GetObjectHeight(topParent.childNodes[1]);
					borderSize += this.GetObjectHeight(topParent.childNodes[4]);
					borderSize += this.GetObjectHeight(topParent.childNodes[5]);
				}
				
				topParentHeight = this.GetObjectHeight(topParent);
				newSize = topParentHeight - borderSize;
				thisHeight = this.GetObjectHeight(divList[i]);
				
				if (newSize > this.GetObjectHeight(divList[i])) {
					spacerDiv.style.height = '0px';
					if (divList[i].className.indexOf('centreVertical') > -1) {
						topPadding = (newSize / 2) - (thisHeight / 2);
						divList[i].style.paddingTop = topPadding+'px';
						newSize -= topPadding;
					}
					divList[i].style.height = newSize+'px';
				}
			}
		}
	}
}

function ResizeTables() {
	var i;
	var tableObjects = document.getElementsByTagName('table');
	for (i in tableObjects) {
		currentTable = tableObjects[i];
		if (currentTable.className == 'gadgetTable') {
			this.AdjustTable(currentTable);
		}
	}
	this.ResizeDivs();
}

// adjust the source path for user images when viewed in admin mode
function AdjustImageSrc() {
	var srcSplit;
	var pageImages = document.getElementsByTagName('img');
	if (pageImages) {
		for (i = 0; i < pageImages.length; i++) {
			if (pageImages[i].src.indexOf('userimages') > -1) {
				srcSplit = pageImages[i].src.split('userimages');
				srcSplit[0] += '../';
				pageImages[i].src = srcSplit.join('userimages');	
			}
		}
	}
}

// change the current view language
function ChangeLanguage(langCode) {
	LanguageForm.lang.value = langCode;
	LanguageForm.submit();	
}

// opens a popup page
function OpenPopup(url, width, height, returnReference) {
	if (typeof(returnReference) == 'undefined') returnReference = true;
	width += 18;
	if (typeof(height) == 'undefined') height = 0;
	if (height == 0) height = width;
	var winRef = window.open(url, "window1","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height="+height+",width="+width);
	if (returnReference) return winRef;
}

// opend a popup window
function NewWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
		LeftPosition=(screen.width)?(screen.width-w)/2:100;
		TopPosition=(screen.height)?(screen.height-h)/2:100;
	}else if((pos!="center" && pos!="random") || pos==null){
		LeftPosition=0;
		TopPosition=20
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	this.win=window.open(mypage,myname,settings);
}

function ShowSmallLoader(loaderDivId) {
	if (loaderDivId) {
		loaderDIV = document.getElementById(loaderDivId);
	} else {
		var loaderDivId = "loaderDIV"+this.GetUniqueValue();
		var loaderDIV = document.createElement("DIV");
		loaderDIV.id = loaderDivId;
		document.body.appendChild(loaderDIV);
	}
	
	var so = new SWFObject("swf/loading_tiny.swf", "mymovie", "100", "16", "6,0,0,0", "#ffffff");
	so.write(loaderDivId);	
}

function GetUniqueValue() {
	this.uniqueCount++;
	return this.uniqueCount;	
}

function ShowHiddenGadget(gadgetType, firstInstanceOnly, hideElement) {
	var i, hiddenGadgetId;
	var instanceCount = 0;
	if (typeof(this.hiddenGadgetTypes[gadgetType]) != 'undefined') {
		for (i = 0; i < this.hiddenGadgetTypes[gadgetType]; i++) {
			hiddenGadgetId = this.hiddenGadgetTypes[gadgetType][i];
			this.ShowGadget(hiddenGadgetId);
			instanceCount++;
			if (firstInstanceOnly && instanceCount == 1) {
				break;
			}
		}
	}
	if (typeof(hideElement) != 'undefined') {
		document.getElementById(hideElement).style.display = 'none';
	}
}

function ShowGadgetByTag(tagName) {
	if (typeof(this.tagLookup[tagName]) != 'undefined') {
		this.ShowGadget(this.tagLookup[tagName]);
	}
}

function HideGadgetByTag(tagName) {
	if (typeof(this.tagLookup[tagName]) != 'undefined') {
		this.HideGadget(this.tagLookup[tagName]);
	}
}

