/**
 * Navigation
 * @revision 1.2
 *
 * @author Monty Dickerson 
 * @date 2008-11-04
 * @package none
 *
 */

if (!(typeof baseHttp==="string") || (!baseHttp)) { 
	throw Error("Error: Must define baseHttp string before loading Navigation.js script.");
	var baseHttp = "/";
}//if

var MONTY = {};
MONTY.imgStatus = { inactive: "off_",	active: "on_"		};
MONTY.base		= { idStage: "stage_",	idTab	: "tab_"	};
MONTY.base.imgUrl = baseHttp + "images/";
MONTY.base.ajaxUrl= baseHttp + "?content_type=iframe&iframe_id=";

function Navigation(argObj) {
//	console.log('constructor: Now validating argument(s) --');	////////////////
	if (!(typeof argObj==="object") || (!argObj)) { 
		throw Error("Error: Must pass an object parameter into the Navigation constructor.");
		var argObj = {};
	}//if

	var isEmpty = true;
	var item;
	var currentTab, currentLevel = "";
	for (item in argObj) {
	    if (argObj.hasOwnProperty(item)) {
			isEmpty = false;
			this[item] = argObj[item];
//			console.log(' copied this.'+item);	////////////////
	    }//if
	}//for
	if (isEmpty) { return this }

	if (!(typeof this.level==="object") || (!this.level)) { 
//		console.log(' this.level was not user supplied so making.');	////////////////
		for (currentLevel in this) {
			if (typeof this[currentLevel]==="object" && !(currentLevel==="level")) {
				this.level = {};
				this.level[currentLevel] = [];
				for (currentTab in this[currentLevel]) {
					this.level[currentLevel].push(currentTab);
				}//for
			}//if
		}//for
	}//if
//	console.dir(this.level);	////////////////

	loadXMLHttpObject();
	var currentTab, currentStatus, currentLevel = "";
	this.on = {};
	this.tabLevel = {};
//	console.log(" Now preloading images and building on,tabLevel --");	////////////////
	for (currentLevel in this.level) {
		for (currentTab in this[currentLevel]) {
			if (this[currentLevel][currentTab].on) {
				this.on[currentLevel] = currentTab;
//				console.log(' set this.on.'+currentLevel+' = '+currentTab);	////////////////
			}//if
			for (currentStatus in MONTY.imgStatus) {
				this.tabLevel[currentTab] = currentLevel;
				var i = new Image();
//				console.log('  ' + MONTY.base.imgUrl  + MONTY.imgStatus[currentStatus] + this[currentLevel][currentTab].filename);	////////////////
				i.src = MONTY.base.imgUrl  + MONTY.imgStatus[currentStatus] + this[currentLevel][currentTab].filename;
			}//for
		}//for
	}//for
	for (currentLevel in this.on) {
		this.tabClick(this.on[currentLevel]);
	}
//	console.dir(this);	////////////////
}//function Navigation



Navigation.prototype.tabClick = function(theTab) {
	var that = this;

	var changeTabs = function() {
		var currentTab = "";
		for (currentTab in that[theLevel]) {
			var status = MONTY.imgStatus.inactive;						
			if (currentTab===theTab) { 
				status = MONTY.imgStatus.active;
				that[theLevel][currentTab].on = true;
				that.on[theLevel] = currentTab;
			} else {
				that[theLevel][currentTab].on = false;
			}//if
			document.getElementById(MONTY.base.idTab+currentTab).src = MONTY.base.imgUrl + status + that[theLevel][currentTab].filename;
		}//endfor
	}

	var changeStage = function() {

		function XHR(location) {
			xmlhttp.abort();
			loadXMLHttpObject();
			xmlhttp.open("GET",location,false);
			xmlhttp.send(null);
			return xmlhttp.responseText;
		}//function

		var stageId = MONTY.base.idStage + theLevel;
//		console.log(' new stage for ' + stageId);	////////////////
		document.getElementById(stageId).innerHTML = XHR(MONTY.base.ajaxUrl + that[theLevel][theTab].contentpane);

		//Run scripts in contentpane of the new stage
		var theStage = document.getElementById(stageId);
		if (theStage) {
//			console.log('Checkpoint theStage passed #');	////////////////
			var scrips = theStage.getElementsByTagName('script');
			if (scrips.length) {
//				console.count('Checkpoint scrips passed #');	////////////////
				for (var i = 0; i < scrips.length; i++) {
					if (scrips[i].src) {
//						console.count(scrips[i].src+' #');	////////////////
//						console.log(XHR(scrips[i].src));	////////////////
						scrips[i].innerHTML = XHR(XHR(scrips[i].src));
					}
					eval(scrips[i].innerHTML);
				}//for
			}//if
		}//if

	}//function

	var theLevel	 = this.tabLevel[theTab];
//	console.log(theLevel + '.tabClick(' + theTab + ') invoked. ==');	////////////////

	changeTabs();
	changeStage();

}//endfunction

//////////////////////////////////////////////////////////////////////


/*
 * Example of full constructor invocation:
 *
 *	var gamecenter = new Navigation({
 *		levels: [ "gamecenter","secondlevel" ]
 *		,level: {	gamecenter: ["nextgame","listenlive","fullschedule"]	}
 *		,on:  { gamecenter: "nextgame" }
 *		,gamecenter: {
 *			nextgame	 : { level: "gamecenter",	filename: "nextgame.png",		contentpane: 1,	on:true	}
 *			,listenlive	 : { level: "gamecenter",	filename: "listenlive.png",		contentpane: 2	}
 *			,fullschedule: { level: "gamecenter",	filename: "fullschedule.png",	contentpane: 3	}
 *		}//gamecenter
 *		,secondlevel: {
 *			blogs	: { level: "secondlevel",	filename: "nextgame.png",		contentpane: 4,	on:true	}
 *			,news	: { level: "secondlevel",	filename: "listenlive.png",		contentpane: 7	}
 *			,video	: { level: "secondlevel",	filename: "fullschedule.png",	contentpane: 6	}
 *		}//gamecenter
 *	});
 *
 *
 */
