/**************************************************************************
** $Id: global.js,v 1.6 2008/03/13 16:10:55 stuartg Exp $
**
** Copyright (c) 2000-2007 MassMedia Studios Pty Ltd.
** 68-72 Wentworth Ave, Surry Hills, NSW 2010, Australia.
** All rights reserved.
**
** This software is the confidential and proprietary information of 
** MassMedia Studios Pty Ltd. ("Confidential Information").  You shall not
** disclose such Confidential Information and shall use it only in
** accordance with the terms of the license agreement you entered into
** with MassMedia Studios Pty Ltd.
** ------------------------------------------------------------------------
** 
** Author: Helen Fu
** Release notes:
**
**************************************************************************/

/*-------------------------- BROWSER DETECTION --------------------------*/

function dom_browser() {
	// browser identification based on dom capabilities
	this.myNav = this.navigator;
	this.version = this.navigator.appVersion;
	this.name = this.navigator.appName;
	this.userAgt = this.navigator.userAgent;
	this.ns4 = (document.layers) ? true : false;
	this.ns6 = (this.navigator.userAgent.indexOf("Netscape6") != -1) ? true : false;
	this.dom = (document.getElementById) ? true : false;
	this.ie4 = (document.all) ? true : false;
	this.mac = (this.version.indexOf("Mac") != -1) ? true : false;
	this.ie = (this.version.indexOf("MSIE") != -1) ? true : false;
	this.windows = (this.version.indexOf("Windows") != -1) ? true : false;
	this.hasPlugins = (this.navigator.plugins) ? true : false;
	this.ie6 = (this.version.indexOf("MSIE 6.0") != -1) ? true : false;
	this.ie7 = (this.version.indexOf("MSIE 7.0") != -1) ? true : false;
	this.ie55 = (this.version.indexOf("MSIE 5.5") != -1) ? true : false;
	this.ie5 = (this.version.indexOf("MSIE 5.01") != -1) ? true : false;
	this.ns = (this.userAgt.indexOf("Netscape") != -1) ? true : false;
	this.ff = (this.userAgt.indexOf("Firefox") != -1) ? true : false;
	this.safari = (this.userAgt.indexOf("Safari") != -1) ? true : false;
	this.nsMac = (this.version.indexOf("5.0 (Macintosh; en-US)") != -1) ? true : false; 
		// to detect Netscape browser on the MAC
}

dom_browser();

/*-------------------------- TABULAR MODULE --------------------------*/

// widget tab switcher..
function changeTab(myLinkObj) {
	myDL = myLinkObj.parentNode.parentNode;
	// reset all tabs..
	for (var i=0; i < myDL.childNodes.length; i++) {
		if (myDL.childNodes[i].nodeType == 1) {
			myDL.childNodes[i].className = "";
		}
	}
	// set selected tab..
	myLinkObj.parentNode.className = "selected";
	if (this.ie && this.windows) {
		myLinkObj.parentNode.nextSibling.className = "selected";
	} else {
		myLinkObj.parentNode.nextSibling.nextSibling.className = "selected";
	}
	fixHeight(myLinkObj); // adjust tab container height..
}

// widget tab switcher..
function changeTab(myLinkObj) {
	myDL = myLinkObj.parentNode.parentNode;
	// reset all tabs..
	for (var i=0; i < myDL.childNodes.length; i++) {
		if (myDL.childNodes[i].nodeType == 1) {
			myDL.childNodes[i].className = "";
		}
	}
	// set selected tab..
	myLinkObj.parentNode.className = "selected";
	if (this.ie && this.windows) {
		myLinkObj.parentNode.nextSibling.className = "selected";
	} else {
		myLinkObj.parentNode.nextSibling.nextSibling.className = "selected";
	}
	fixHeight(myLinkObj); // adjust tab container height..
}

// to fix the height of the tabular divs..
function fixHeight(myLinkObj) {
	myWrapper = myLinkObj.parentNode.parentNode.parentNode;
	if (this.ie && this.windows) {
		myHeight = myLinkObj.parentNode.nextSibling.clientHeight + 28;
	} else if(mac) {
		myWrapper.style.height = "0px";
		myHeight = myLinkObj.parentNode.nextSibling.nextSibling.clientHeight + 28;
	} else {
		myHeight = myLinkObj.parentNode.nextSibling.nextSibling.clientHeight + 28;
	}
	myWrapper.style.height = myHeight + "px";
}

// to run the fixHeight function onload and determine the tab that is selected..
function fixTabHeight() {
	if(document.links) {
		for(var i=0; i < document.links.length; i++) { // for all links..
			currentLink = document.links[i];
			parentDT = currentLink.parentNode;
			parentDL = currentLink.parentNode.parentNode;
			if(parentDT.className == "selected") { // if selected tab..
				fixHeight(currentLink); 
			}
		}
	}
}

/*-------------------------- ALL POPUPS - open/close functionality --------------------------*/

var loginPopups = Array("Navigation_login","Navigation_forgotpwd"); // if login and forgotten password popups..
var formPopups = Array("Art_sendfriend","Art_yoursay"); // if send to friend and your say forms..
var ftflPopups = Array("ftflLogin","ftflForgotPwd"); // footy tipping and fantasy league side widget login box..

// open/close popup..
function openPopup(myObjId,myStatus) {
	myObj = document.getElementById(myObjId); // current popup object..
	if(myObj != null && typeof(myObj) != "undefined") { // if object exists..
		var myTempArray = null;
		var myLink = null;
		if (myObj.id.indexOf("Art") != -1) { // if your say or send to friend forms..
			myTempArray = formPopups;
		} else if (myObj.className.indexOf("loginBox") != -1 && myObj.id.indexOf("ftfl") != -1) { // for footy tipping and fantasy league login..
			myTempArray = ftflPopups;
		} else if (myObj.className.indexOf("loginBox") != -1 && myObj.id.indexOf("Navigation") != -1) { // if login popups..
			myTempArray = loginPopups;
			myLink = document.getElementById("lnkLogin");
		} else {
			// do nothing..
		}
		
		if(myStatus == "show") { // if to show..
			if(myTempArray != null && typeof(myTempArray) != "undefined") {
				for(var i=0; i < myTempArray.length; i++) {
					myTempObj = document.getElementById(myTempArray[i]); // current popup..
					if(myTempObj != null && typeof(myTempObj) != "undefined") {
						if(myObjId == myTempArray[i]) { // if current popup is same as one we want to show..
							removeClassName(myTempObj,"hide"); // set popup to show..
						} else {
							addClassName(myTempObj,"hide"); // set popup to hide..
						}
					}
					if(myLink != null && typeof(myLink) != "undefined" && myObj.id.indexOf("Navigation") != -1) { // login link..
						myLink.className = "lnkSelLogin"; // display selected login button state..
					}
				}
			}
		} else if(myStatus == "hide") { // if to hide..
			// for all popups..
			if (containsClassName(myObj,"hide") == false) {
				addClassName(myObj,"hide"); // set popup to hide..
				if(myLink != null && typeof(myLink) != "undefined" && myObj.id.indexOf("Navigation") != -1) {
					myLink.className = "lnkLogin"; // display selected login button state..
				}
			}
		} else {
			// do nothing..
		}
	}
}

/*-------------------------- SEARCH FIELD FILTERS --------------------------*/

// toggle to show advanced search and date filter in search forms
function toggleFlds(myChkFld,myId) {
	myObj = document.getElementById(myId);
	if (myObj != null && typeof(myObj) != "undefined") {
		if (myChkFld.checked == true) { // if checkbox is selected..
			removeClassName(myObj,"hide"); // show extra fields..
		} else {
			addClassName(myObj,"hide"); // hide extra fields..
		}
	}
}

/*-------------------------- MAXLENGTH FOR TEXTAREAS --------------------------*/

function checkLength(e, myFieldObj, myLength) {
	// Acts as maxLength attribute. Used with an onkeypress event.
	// Written for textarea fields which do not cater for maxLength.
	var key;
	if (window.event || !e.which) {
		key = e.keyCode;
	} else if (e) {
		key = e.which;
	}
	if ((key >=35 && key <=40) || key == 8 || key == 9 || key == 46)  // allow functional keys eg. arrows, home, end, delete... etc.
		return true;
	if (myFieldObj.value.length >= myLength) {
		// if greater than maximum length, deny key to be written out.
		return false;
	} else {
		// allow key.
		return true;
	}
}

function taCount(myObj, myDiv, myLength) { 
	// Decrements the count value if the amount of characters have not reached max length yet
	// myObj is the form element object that characters are being type into
	// myDiv is the element that the count value is written into
	// myLength is the maximum amount of characters allowed to be entered into myObj
	if(myObj != null && typeof(myObj) != "undefined") {
		if (myObj.value.length > myLength) myObj.value = myObj.value.substring(0,myLength);
		myDivObj = document.getElementById(myDiv);
		if (myDivObj != null && typeof(myDivObj) != "undefined") {
			myDivObj.innerHTML = myLength - myObj.value.length;
		}
	}
}

/*-------------------------- CLASS MANIPULATION --------------------------*/

function trim(str) {
	// removes white spaces from beginning and end of given string, str.
	if (str != null && typeof(str) != "undefined") {
		return str.replace(/^\s*|\s*$/g,"");	
	} else {
		return "";
	}
}

function containsClassName(myElement, myClass) {
	// checks to see whether class is assigned to current element..
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	if (myReg.test(currentClassName)) {
		return true;
	} else {
		return false;
	}
}

function addClassName(myElement, myClass) {
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	if (!myReg.test(currentClassName)) {
		// not currently set on element, so add class..
		if (currentClassName != "") {
			myElement.className += " " + myClass;
		} else {
			myElement.className = myClass;
		}
	}
}

function removeClassName(myElement, myClass) {
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	myElement.className = currentClassName.replace(myReg, "");
}

/*-------------------------- MODULE HIGHLIGHT STATE --------------------------*/

// hover state for fixtures and results rounds (i.e. green glow modules)..
function highlightModule(myModuleObj, type, highlight) {
	if (type == "open") { // if module is open..
		if (highlight) { // to turn hover state (i.e. green glow) on..
			removeClassName(myModuleObj, "openModule");
			addClassName(myModuleObj, "openGlow");
		} else { // to turn normal state on..
			removeClassName(myModuleObj, "openGlow");
			addClassName(myModuleObj, "openModule");
		}
	} else if (type == "closed") { // if module is closed..
		if (highlight) { // to turn hover state (i.e. green glow) on..
			removeClassName(myModuleObj, "closedModule");
			addClassName(myModuleObj, "closedGlow");
		} else { // to turn normal state on..
			removeClassName(myModuleObj, "closedGlow");
			addClassName(myModuleObj, "closedModule");
		}
	} else {
		// do nothing..
	}
}

// set up onmouseover and onmouseout events for fixtures and results rounds (i.e. green glow modules)..
function setupHighlights() {
	var allDivs = document.getElementsByTagName("DIV");
	for (var i=0; i<allDivs.length; i++) {
		if (containsClassName(allDivs[i], "closedModule")) { // for closed modules..
			allDivs[i].onmouseover = function() {
					highlightModule(this, "closed", true);
				};
			allDivs[i].onmouseout = function() {
					highlightModule(this, "closed", false);
				};
		}
		if (containsClassName(allDivs[i], "openModule")) { // for open modules..
			allDivs[i].onmouseover = function() {
					highlightModule(this, "open", true);
				};
			allDivs[i].onmouseout = function() {
					highlightModule(this, "open", false);
				};
		}
	}
}


/*-------------------------- MODULE OPEN/CLOSE --------------------------*/

// open/close round.. on fixtures and results page
function toggleRound(myId) {
	myObj = document.getElementById(myId);
	if (myObj != null && typeof(myObj) != "undefined") {
		addClassName(myObj,"hide"); // hide open or closed module..
		if (myId.indexOf("c") != -1) { // if current module is closed..
			myObj2 = document.getElementById("o" + myId.substring(1)); // set myObj2 to openModule..
		} else { // if current modules is open..
			myObj2 = document.getElementById("c" + myId.substring(1)); // set myObj2 to closedModule..
		}
		if (myObj2 != null && typeof(myObj2) != "undefined") {
			removeClassName(myObj2,"hide"); // display myObj2..
		}
	}
}

// expand all rounds, open all modules..
function expandAllRounds() {
	var allDivs = document.getElementsByTagName("DIV");
	for (var i=0; i<allDivs.length; i++) {
		myObj = allDivs[i];
		if (myObj != null && typeof(myObj) != "undefined") {
			if (myObj.className.indexOf("closedGlow") != -1) { // if closed module, hide
				addClassName(myObj,"hide");
			}
			if (myObj.className.indexOf("openGlow") != -1) { // if open module, show
				removeClassName(myObj,"hide");
			}
			if (myObj.className.indexOf("closedModule") != -1) { // if closed module, hide
				addClassName(myObj,"hide");
			}
			if (myObj.className.indexOf("openModule") != -1) { // if open module, show
				removeClassName(myObj,"hide");
			}
		}
	}
}

/*-------------------------- MULTIMEDIA POPUP PAGE TOGGLE --------------------------*/

// multimedia popup toggle.. show one module at a time only..
function toggleMM(myLnk,myId) {
	var allDivs = document.getElementsByTagName("DIV");
	var myMultiObj = document.getElementById(myId);
	if (myMultiObj != null && typeof(myMultiObj) != "undefined") {
		for (var i=0; i<allDivs.length; i++) { // for all divs..
			myObj = allDivs[i];
			if (myObj != null && typeof(myObj) != "undefined") {
				if (myObj.className.indexOf("multiList") != -1) { // if multimedia wrapper..
					myLink = document.getElementById("multiLnk" + myObj.id.substring(5)); // set link object..
					if (myLink != null && typeof(myLink) != "undefined") {
						if (myMultiObj == allDivs[i]) { // if current object equals object to be shown..
							removeClassName(myObj,"hide"); // show object..
							myLink.className = "btnMultiSel"; // set link to have selected state..
						} else { // otherwise if not object to be shown..
							addClassName(myObj,"hide"); // hide object..
							myLink.className = "btnMulti"; // set link to have normal state..
						}	
					}
				}
			}
		}
	}
}

/*-------------------------- MULTIMEDIA PAGE POPUP WIN CALL --------------------------*/

// to open multimedia popup in a new window with fixed dims..
function openPage(myPage) {
	myWin = window.open(myPage,'myWin','width=674,height=445,scrollbars=yes,resizeable=no,menubar=no,statusbar=no');
	myWin.focus();
}

/*-------------------------- POPULATE SEARCH FIELD WITH LABEL --------------------------*/

// populates search field with default "search" text of field remains empty..
function populateFldDesc(myFld,defaultTxt,myStatus) {
	if (myStatus == true && myFld.value == defaultTxt) { // on field so need to empty text..
		myFld.value = "";
	}
	if (myStatus == false && myFld.value == "") { // moving off field so need to repopulate field with default text if empty..
		myFld.value = defaultTxt;
	}
}

/*-------------------------- NAVIGATION SUBNAV --------------------------*/

// for showing the sub nav..
function showNav(myObj) {
	var myHome = document.getElementById("myHome"); // my home button..
	var myMenu = document.getElementById("myMenu"); // my navigation list..
	if (myHome != null && typeof(myHome) != "undefined" && myMenu != null && typeof(myMenu) != "undefined") {
		// NORMAL STATE...
		// set normal state for home button..
		myHome.className = "btnHome";
		// hide all sections and set normal state for nav buttons..
		for (var i=0; i < myMenu.childNodes.length; i++) { // for each LI item..
			if (myMenu.childNodes[i].nodeName == "LI") {
				if (containsClassName(myMenu.childNodes[i],"selectedSec")) { // if contains section selected..
					removeClassName(myMenu.childNodes[i],"selectedSec"); // hide section..
				}
				// hide all selected states for first level buttons..
				var myCurrentBtn;
				for (var j=0; j < myMenu.childNodes[i].childNodes.length; j++) { // for all items in current list item..
					if (myMenu.childNodes[i].childNodes[j].nodeName == "A") { // if anchor tag.. 
						myCurrentBtn = myMenu.childNodes[i].childNodes[j];
						if (containsClassName(myCurrentBtn,"selnav")) { // if anchor contains selected nav class then remove..
							removeClassName(myCurrentBtn,"selnav");
						}
					}
				}
			}
		}
		// SELECTED STATE SET..
		// show selected button and sections..
		if (myObj == myHome) { // must be home icon..
			myObj.className = "selhome";
		} else { // must be other nav item..
			// show selected section..
			try {
				myTempObj = myObj.nextSibling.nextSibling; // get subnav.. if it exists..
				if (myTempObj.nodeName == "UL") {
					addClassName(myObj.parentNode,"selectedSec"); // add selectedSec class to element..
				}
			} catch (exception) {
				// no error alert
			}
			// show selected button..
			addClassName(myObj,"selnav"); // added selected nav class to current anchor tag selected..
		}
	}
}

/*-------------------------- EXTEND AREA (CLUB SELECTOR) --------------------------*/

function extendArea(myDiv,myH) {
	myObj = document.getElementById(myDiv);
	if (myObj != null && typeof(myObj) != "undefined") {
		myObj.style.height = myH + "px";
	}
}






