//
// soccer.js - Global JavaScript functions for AYSO 208 Web pages
//
// Michael Urban
// AYSO Region 208, Wichita, Kansas
//

////////////////////////////////
// Global variables/constants //
////////////////////////////////
//
var fontSize;
var defaultSize = '10pt';				// Default page font size
var minSize     = 7;				// Minimum font size available
var maxSize     = 20;				// Maximum font size available

var browser = new Object();			// Create global browser object for determining 
							// navigator platform, version, etc.

//var strWinProp;					// global string containing window properties for popup windows.

//////////////////////
// Global Functions //
//////////////////////
//
// changeFont - Change the font size within the page
//
// increment: Amount to increase or decrease the font
//
function changeFont(increment) {

	// Extract the current size of the body font
	//
	if (browser.isMicrosoft)
		var bodyElement = document.all["body"];
	else
		var bodyElement = document.getElementsByTagName("body");

	var currentSize = bodyElement[0].style.fontSize;

	// If no size available, use the default
	//
	if (currentSize == "" || !currentSize)
		currentSize = defaultSize;

	// Set the new size of the font
	//
	var size = parseInt(currentSize);
	fontSize = size + increment;
	if (fontSize < minSize)
		fontSize = minSize;
	if (fontSize > maxSize)
		fontSize = maxSize;

	fontSize = fontSize.toString() + "pt";

	// Apply the font size to the specified elements
	//
	bodyElement[0].style.fontSize = fontSize;

//document.body.style.fontSize = fontSize;

	// Initialize a cookie string to write.  Set the expiration to be one year from now.
	//
	var cookieString;
	var nextYear = new Date();
	nextYear.setFullYear(nextYear.getFullYear() + 1);

	cookieString = "fontsize=" + fontSize + "; expires=" + nextYear.toGMTString();

	// Write the cookie
	//
	document.cookie = cookieString;
}

// getPrefs - Read the cookie associated with this document.  Invoke from the onload action
//            of the body tab within the documents
//
function getPrefs() {
	var docCookies = document.cookie;

	// Get the font size specified in the cookie
	//
	var pos = docCookies.indexOf("fontsize=");
	if (pos != -1) {
		var start = pos + 9;					// Start of cookie value
		var end   = docCookies.indexOf(";", start);	// End of cookie value

		if (end == -1)
			end = docCookies.length;

		var value = docCookies.substring(start, end);
		value = unescape(value);

		// Extract the body element from the document
		//
		var bodyElement = document.getElementsByTagName("body");
		bodyElement[0].style.fontSize = value;
//document.body.style.fontSize = fontSize;
	}
}
		
// getBrowserInfo - Extract various browser parameters.  Use the global browser Object (initialized
//                  above to hold the values.
//
function getBrowserInfo() {

	// Determine the major version of the browser
	//
	browser.majorVersion = parseInt(navigator.appVersion);
	browser.minorVersion = parseFloat(navigator.appVersion);

	// Determine browser type
	//
	browser.type = navigator.userAgent;
	var agent = browser.type.toLowerCase();

	browser.isBeonex = false;
	browser.isChimera = false;
	browser.isFirefox = false;
	browser.isMicrosoft = false;
	browser.isMozilla = false;
	browser.isNetPositive = false;
	browser.isNetscape = false;
	browser.isOpera = false;
	browser.isPhoenix = false;
	browser.isSafari = false;
	browser.isSkipStone = false;
	browser.isStarOffice = false;

	if (agent.indexOf("beonix") != -1) browser.isBeonix = true;
	if (agent.indexOf("chimera") != -1) browser.isChimera = true; 
	if (agent.indexOf("firefox") != -1) browser.isFirefox = true;
	if (agent.indexOf("msie") != -1) browser.isMicrosoft = true;
	if (agent.indexOf("mozilla") != -1) browser.isNetscape = true;
	if (agent.indexOf("mozilla/5.0") != -1) browser.isMozilla = true;
	if (agent.indexOf("netpositive") != -1) browser.isNetPositive = true;
	if (agent.indexOf("netscape") != -1) browser.isNetscape = true;
	if (agent.indexOf("opera") != -1) browser.isOpera = true;
	if (agent.indexOf("phoenix") != -1) browser.isPhoenix = true;
	if (agent.indexOf("safari") != -1) browser.isSafari = true;
	if (agent.indexOf("skipstone") != -1) browser.isSkipStone = true;
	if (agent.indexOf("staroffice") != -1) browser.isStarOffice = true;

	// Determine the platform type
	//
	browser.platform = navigator.platform;
}

// imageOver
// Update the navigation bar (left side) images when the mouseover/mouseout events occur.  Each 
// navigation bar element should consist of an <a> tag (pointing to the document to load) and an 
// <img> tag, which contains the default image for the appropriate page.  Each <a> and <img> 
// tag must contain a matching "name" attribute, which corresponds to a key within the 
// imageArray associative array.
//
// objName:	Name of the object invoking the function.  Specified by the name attribute of the 
//           <a> tag (corresponding <img> tag must have the same name).
// flag:	Flag to determine which image to load from imageArray array.  
//		0 = off image (mouseover), 1 = on image (mouseout), -1 = don't update image
//
// To invoke, ensure the following events are present within the <a> tag:
//	onmouseover="imageOver('name', 1); return true;"
//	onmouseout="imageOver('name', 0); return true;"
//
// Notes: 
//	1) Ensure function call is followed by "return true;" in order for the browser status 
//	   bar to be updated (not really necessary in IE, but it won't hurt to be there).
//
//function imageOver(objName, flag) {
//
////	// Extract the name attribute from the passed <a> object.  There should be an image 
////	// with the same name.  Will reference the <img> element in order to modify the "src" 
////	// attribute.
////	var imageName = objName.name;
//
//	// Extract the "title" text from the <img> tag to use as the browser status text, if supported.  
//	// If not, use array value
//	//
//	if (document[objName].title)
//		var statusText = document[objName].title;
//	else
//		var statusText = "-- " + statusArray[objName] + " --";
//
////	if (browser.isNetscape && (browser.majorVersion <= 4)) {
////		var statusText = statusArray[objName];
////	} else {
////		var statusText = document[objName].title;
////	}
//
//	// Extract the desired image from the imageArray given the passed object name (objName) and the flag
//	// value (0 = off, 1 = on, -1 = don't update the image...use default).  Concatentate extracted array
//	// element with the imageBase value.
//	//
//	if (flag > -1) {
//		document[objName].src = imageBase + imageArray[objName][flag];
//	}
//
//	// Update the status line with text defined above
//	//
//	if ((flag == 0) || (statusText == undefined))
//		window.status = "";
//	else {
//		window.status = statusText;
//	}
//
//	return true; 
//}

// initializeImages
// Pre-load the images into the browser cache for faster loading
//
function initializeImages() {

//	for (var item in imageArray) {
//		(new Image(135, 35)).src = imageArray[item][0];
//		(new Image(135, 35)).src = imageArray[item][1];
//	}
//
//	// Instantiate the browser info object
//	//
	getBrowserInfo();
}

// initPopup
// Initialize the popup window with default parameters.  Make modifications to default values here.  
// Could be a bit more robust, but will suffice for now....
//
// winWidth:		Width of the window in pixels (optional)
// winHeight:	Height of the window in pixels (optional)
// winTop:		Offset of window's top edge from screen, in pixels (optional)
// winLeft: 		Offset of window's left edge from screen, in pixels (optional)
//
//function initPopup(winWidth, winHeight, winTop, winLeft) {
//
//	// Internal width and height.  Adjust intWidth for the end of the screen, and intHeight for the icon bar
//	//  at the bottom of the window.  Should only need to use these parameters if creating full screen window
//	//
//	var intWidth = screen.width - 10;
//	var intHeight = screen.height - 80;
//
//	// Set width and height to default values, or values specified in function call
//	//
//	var width = 500;
//	if (winWidth) width = winWidth;
//
//	var height = 400;
//	if (winHeight) height = winHeight;
//
//	// Set offset values to default, or to value specified in function call
//	//
//	var topOffset = 0;
//	if (winTop) topOffset = winTop;
//
//	var leftOffset = 0;
//	if (winLeft) leftOffset = winLeft;
//
//	// Set the properties of the popup window
//	// 
//	strWinProp = "width=" + width		// Width.  Set to intWidth for entire screen.
//			+ ",height=" + height 	// Height. Set to intHeight for entire screen.
//			+ ",resizable=yes"		// Resizable attribute
//			+ ",scrollbars=yes"		// Displays scrollbars if document is larger than window.
//			+ ",menubar=yes"		// Menubar at top of window.
//			+ ",toolbar=yes"		// Browser toolbar, Back; Forward, etc...
//			+ ",directories=yes"		// "What's New".  Netscape only (??).
//			+ ",location=yes"		// URL field.
//			+ ",status=yes"		// Status Bar at bottom of window.
//			+ ",titlebar=yes"		// Enable/Disable titlebar resize capability.
//			+ ",top=" + topOffset	// Offset of window's top edge from screen.
//			+ ",left=" + leftOffset	// Offset of window's left edge from screen.
//			+ "";  
//}

// sectionCollapse - Collapse the given element ID (frameID)
//
function sectionCollapse(frameID) {
	var frameName = 'section' + frameID;
	var c_image   = 'collapse' + frameID;
	var e_image   = 'expand' + frameID;

	var frameElement = document.getElementById(frameName);

	frameElement.style.visibility = 'hidden';
	frameElement.style.display    = 'none';

	showElement(c_image, true);
	showElement(e_image);
}

// sectionExpand - Expand the given element ID (frameID)
//
function sectionExpand(frameID) {
	var frameName = 'section' + frameID;
	var c_image   = 'collapse' + frameID;
	var e_image   = 'expand' + frameID;

	var frameElement = document.getElementById(frameName);

	frameElement.style.visibility = 'visible';
	frameElement.style.display    = 'block';

	showElement(c_image);
	showElement(e_image, true);
}

// showElement - Show/Hide the given element
//
// elementID: Document element to manipulate
// hide:      If true, hide the affected element
//
function showElement(elementID, hide) {
	var element = document.getElementById(elementID);

	if (hide) {
		element.style.visibility = 'hidden';
		element.style.display    = 'none';
	} else {
		element.style.visibility = 'visible';
		element.style.display    = 'inline';
	}
}

//
//// showPopup
//// Display a popup window.  Must call the initPopup() function, passing necessary parameters.
////
//// targetURL: 	URL displayed in window
////
//function showPopup(targetURL) {
//
//	// Initialize the popup window.  Make any sizing attribute changes here.
//	//
//	initPopup(500, 400, 30, 30);
//
//	// Create new window containing "targetURL" document.  Name the target window so all subsequent calls will place
//	//  their content here.
//	//
//	window.open(targetURL, "newwin", strWinProp);
//}
//
//
//

function FP_preloadImgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

function swapImage(objName, flag) {

	// Extract the id attribute from the passed <img> object.  
	//
	var imageName = objName.id;

	// Rewrite the source of the image, if flag is set.  If not, set to default
	//
	if (flag)
		newImage = buttonBase + imageName + "_" + flag + ".jpg";
	else
		newImage = buttonBase + imageName + ".jpg";

	objName.src = newImage;
}

function FP_swapImg() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_getObjectByID(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}
