//Adds a method to arrays to find where a specific item is in the array
Array.prototype.indexOf = function (query) {
	var val = -1;
	for (var i=0; i<this.length; i++)
	{
		if (this[i] == query)
		{
			return i;
			break;
		}
	}
}

function disableTextSelect(element)
{
	if (typeof element.onselectstart != "undefined") {
		//Internet Explorer
		element.onselectstart = function(){ return false;};		
	} else if (typeof element.style.MozUserSelect!="undefined"){
		//Mozila ... FF ... and the like.
		element.style.MozUserSelect="none"	
	} else {
		element.onmousedown=function(){return false}
	};
	
	element.style.cursor = "default"
}

function Updating(containerID, text, mods, solo) 
{
	//first, check if there is already an updating el.
	var updatingChk = document.getElementById("UpdatingEl");
	var containerEl = document.getElementById(containerID);	
	var updatingEl
	
	if (updatingChk == null){		
		updatingEl = document.createElement("div");
	} else {
		updatingEl = updatingChk;
	};
	
	var _parent; var _top;
	if (mods){
		if (mods.get){
			_parent = mods.get("_parent");
			_top = mods.get("_top");
		};
	};
	
	if (!containerEl){ 
		alert("container not here: " + containerID);
		return; 
	};

	if (text == null || text == "")
	{
		text = "Updating ...";	
	}
	updatingEl.innerHTML = "<img src='/_images/updatingB.gif' style='position:relative;top:3px;' /> &nbsp;" + text;
	updatingEl.id = "UpdatingEl";
	if (solo){
	    updatingEl.id += "_" + containerID;
	};
	updatingEl.className= "UpdatingEl";
	
	var contElHeight = containerEl.offsetHeight;

	var contTop = getElementTop(containerEl);
	var contLeft = getElementLeft(containerEl);

	updatingEl.style.paddingTop = ((contElHeight / 4) - 10).toString() + "px";
	updatingEl.style.height = (contElHeight - ((contElHeight / 4) - 10)).toString()+"px";
	updatingEl.style.width = containerEl.offsetWidth.toString()+"px";
	if (_top != null){
		updatingEl.style.top = _top + "px";
	} else {			
		updatingEl.style.top = contTop.toString()+"px";
	};
	updatingEl.style.left = contLeft.toString()+"px";
		
	//containerEl.parentNode.appendChild(updatingEl);
	//document.getElementById('main_body').appendChild(updatingEl);
	if (_parent){
		_parent.appendChild(updatingEl);
	} else {
		document.body.appendChild(updatingEl);
	};
	
	updatingEl = null;
}

function Update(soloID)
{
    var updatingElId = "UpdatingEl";
    if (soloID != null){ updatingElId += "_" + soloID; };  
    
	var updatingEl = document.getElementById(updatingElId);
	
	if (updatingEl)	updatingEl.parentNode.removeChild(updatingEl);
	
	/*
	var arrUpEls = new Array();
	elementGetElementsByClass(document.documentElement, "div", "UpdatingEl", arrUpEls);
	for (i=0;i<arrUpEls.length;i++)
	{
		arrUpEls[i].parentNode.removeChild(arrUpEls[i]);
	}
	*/
}


function CheckCR(e){
    var result = false;
    if (e != null) {
		//Firefox
		//el = e.target;
		//target = el.id;
	} else {
		//el = event.srcElement;
		//target = el.id;
		e = event;
	}

    if((e.which ? e.which : e.keyCode) == 13) result = true;
    
    return result;
}


if (window.Node && Node.prototype)
{
	Node.prototype.__defineGetter__("innerXml", function () {
        var oSerializer = new XMLSerializer();
        var frag = this.ownerDocument.createDocumentFragment();
        for(var i = 0; i < this.childNodes.length; i++) {
         frag.appendChild(this.childNodes[i]);
        }
        return oSerializer.serializeToString(frag, "text/xml");
    });
}

function sortNumber(a,b)
{
	return a - b
}

function RegisterEvent (el, ev, func) {
	if (el.attachEvent != null) {
		el.attachEvent("on" + ev, func);
	} else {
		el.addEventListener(ev, func, true);
	}
}

function UnregisterEvent (el, ev, func) {
	if (el.attachEvent != null) {
		el.detachEvent("on" + ev, func);
	} else {
		el.removeEventListener(ev, func, true);
	}
}

var MDS_CMS = {
    Utilities:{
        addClass:function(el, className)
        {
	        if (el.className.indexOf(className) == -1)
	        {
		        el.className = el.className + " " + className;
	        }
        },
        removeClass: function(el, className)
        {
	        var result = el.className;
	        var loc = el.className.indexOf(className);
	        if (loc != -1)
	        {
		        result = el.className.substring(0, loc) + el.className.substring(loc + className.length);
	        }
	        el.className = result;
        }
   }
}

function getElementsByClassExclude (tagname, classname, ar, exclude)
{
	var alltags = document.all? document.all : document.getElementsByTagName(tagname);
	_allTagsIterate(classname, alltags, exclude, ar);
}

function getElementsByClass (tagname, classname, ar)
{
		getElementsByClassExclude (tagname, classname, ar, "");
}

function elementGetElementsByClass (element, tagname, classname, ar)
{
	var alltags = new Array();
	alltags = element.getElementsByTagName(tagname);
	_allTagsIterate(classname, alltags, "", ar);
}

function _allTagsIterate (classname, alltags, exclude, ar)
{
	var Classes;
	for (i=0; i<alltags.length; i++){
		Classes = new Array();
		Classes = alltags[i].className.split(" ");	
		if (Classes.indexOf(classname) > -1)
		{
			ar.push(alltags[i]);
		}
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function windowWidthGet () {
	var windowWidth;
	
	if (window.innerWidth)
	{
		windowWidth = window.innerWidth;
	}
	else
	{
		windowWidth = document.documentElement.offsetWidth;
	}
	
	return windowWidth;
}

function windowHeightGet () {
	var windowHeight;
	
	if (window.innerWidth)
	{
		windowHeight = window.innerHeight;
	}
	else
	{
		windowHeight = document.documentElement.offsetHeight;
	}
	
	return windowHeight;
}

function getMouseXY(e, el) {
	if (e.clientX) { 
		// grab the x-y pos.s if browser is IE
		tempX = e.clientX; // + document.body.scrollLeft;
		tempY = e.clientY; // + document.body.scrollTop;		
	} else {  
		// grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	
	if (el != undefined)
	{
		var elX = getElementLeft(el);
		var elY = getElementTop(el);  
		
		//window.status = "tX: " + tempX + " elX: " + elX + " | tY: " + tempY + " elY: " + elY;
		
		tempX = tempX - elX;
		tempY = tempY - elY;
	}
  
  
  	var cPair = tempX + "," + tempY
	return cPair;
}

function getElementLeft (element) {
	xPos = eval(element).offsetLeft;
	tempEl = eval(element).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	
	xPos -= f_scrollLeft();
	return xPos;
}

function getElementTop (element) {
	yPos = eval(element).offsetTop;
	tempEl = eval(element).offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	
	yPos -= f_scrollTop();
	return yPos;
}


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



function killBubble(event)
{
	if (event)
	{
		if (event.stopPropagation){
			event.stopPropagation();
		} else if (event.cancelBubble){
			event.cancelBubble = true;
		};
	} else if (window.event) {
		window.event.cancelBubble = true;	
	}	
}

function clearBadCharacters(str)
{
	//str = str.replace(/</g, "");
	//str = str.replace(/>/g, "");
	//str = str.replace(/\\/g, "");
	//str = str.replace("/", "");
	
	return str;
}

/* ---------------TODAYS DATE ------------------------------ */
Style = 2; //pick a style from below

/*------------------------------
Style 1: March 17, 2000
Style 2: Mar 17, 2000
Style 3: Saturday March 17, 2000
Style 4: Sat Mar 17, 2000
Style 5: Sat March 17, 2000
Style 6: 17 March 2000
Style 7: 17 Mar 2000
Style 8: 17 Mar 00
Style 9: 3/17/00
Style 10: 3-17-00
Style 11: Saturday March 17
--------------------------------*/

months = new Array();
months[1] = "January";  months[7] = "July";
months[2] = "February"; months[8] = "August";
months[3] = "March";    months[9] = "September";
months[4] = "April";    months[10] = "October";
months[5] = "May";      months[11] = "November";
months[6] = "June";     months[12] = "December";

months2 = new Array();
months2[1] = "Jan"; months2[7] = "Jul";
months2[2] = "Feb"; months2[8] = "Aug";
months2[3] = "Mar"; months2[9] = "Sep";
months2[4] = "Apr"; months2[10] = "Oct";
months2[5] = "May"; months2[11] = "Nov";
months2[6] = "Jun"; months2[12] = "Dec";

days = new Array();
days[1] = "Sunday";    days[5] = "Thursday";
days[2] = "Monday";    days[6] = "Friday";
days[3] = "Tuesday";   days[7] = "Saturday";
days[4] = "Wednesday";

days2 = new Array();
days2[1] = "Sun"; days2[5] = "Thu";
days2[2] = "Mon"; days2[6] = "Fri";
days2[3] = "Tue"; days2[7] = "Sat";
days2[4] = "Wed";

function GetTodaysDate()
{
	todaysdate = new Date();
	date  = todaysdate.getDate();
	day  = todaysdate.getDay() + 1;
	month = todaysdate.getMonth() + 1;
	yy = todaysdate.getYear();
	year = (yy < 1000) ? yy + 1900 : yy;
	year2 = 2000 - year; year2 = (year2 < 10) ? "0" + year2 : year2;
	
	dateline = new Array();
	dateline[1] = months[month] + " " + date + ", " + year;
	dateline[2] = months2[month] + " " + date + ", " + year;
	dateline[3] = days[day] + " " + months[month] + " " + date + ", " + year;
	dateline[4] = days2[day] + " " + months2[month] + " " + date + ", " + year;
	dateline[5] = days2[day] + " " + months[month] + " " + date + ", " + year;
	dateline[6] = date + " " + months[month] + " " + year;
	dateline[7] = date + " " + months2[month] + " " + year;
	dateline[8] = date + " " + months2[month] + " " + year2;
	dateline[9] = month + "/" + date + "/" + year2;
	dateline[10] = month + "-" + date + "-" + year2;
	dateline[11] = days[day] + " " + months[month] + " " + date;

	return dateline[Style];
}

  //Generic function Argument passing class
Arguments.prototype.constructor = Arguments;
function Arguments ()
{
	this._args = new Array();
}
Arguments.prototype.push = function (arg)
{
	this._args.push(arg);
}
Arguments.prototype.get = function (name)
{
	for (var i=0; i<this._args.length; i++)
	{
		if (this._args[i].name == name)
		{
			return this._args[i].value;
		}
	}
}
Arguments.prototype.set = function (name, value)
{
	for (var i=0; i<this._args.length; i++)
	{
		if (this._args[i].name == name)
		{
			this._args[i].value = value;
			return true;
		}
	}
	
	//alert("Argument " + name + " does not exist in argument list.");
	return false;
}
Arguments.prototype.serialize = function ()
{
	_args = "";
	for (var i=0; i<this._args.length; i++)
	{
		val = encodeURIComponent(this._args[i].value);
		val = val.replace(/'/g, "%27");
		_args += "&" + encodeURIComponent(this._args[i].name) + "=" + val;
	}
	return _args.substring(1);
}
Arguments.prototype.serializeValues = function ()
{
	_args = "";
	for (var i=0; i<this._args.length; i++)
	{
		val = eval(this._args[i].name);
		val = encodeURIComponent(val);
		val = val.replace(/'/g, "%27");
		_args += "&" + encodeURIComponent(this._args[i].value) + "=" + val;
	}
	return _args.substring(1);
}

//Generic function Argument passing class
Argument.prototype.constructor = Argument;
function Argument (name, value)
{
	this.name = name;
	this.value = value;
}

// ------------------------------------- Menus.js --------------------------------------------

function LoadMenus(){
	//main menu element:
	var mainmenu = document.getElementById("mainmenu");
	var firsts = new Array(); //top level menu items.
	var thirds = new Array(); //Third level menu blocks.
	
	elementGetElementsByClass(mainmenu, "div", "thirdLevel", thirds);
	elementGetElementsByClass(mainmenu, "div", "link", firsts);
	
	
	//hide third level menus:
	for (var i=0;i<thirds.length;i++){
		var third = thirds[i];
		var first = third.parentNode;		
				
				
		var l = first.offsetLeft + 60;
		var h = first.offsetTop;
		third.style.left = l + "px";
		third.style.top = h + "px";
		
				
		if (first.className.indexOf("selected") < 0){
			MDS_CMS.Utilities.addClass(third, "hidden");			
			MDS_CMS.Utilities.addClass(third, "absolute");
		} else {
			MDS_CMS.Utilities.removeClass(third, "hidden");
		};
		
		var thirdMenus = new Array();  //third level menu items.
		elementGetElementsByClass(third, "li", "thirdLevelMenu", thirdMenus);
	
		for (var t=0;t<thirdMenus.length;t++){
			var tm = thirdMenus[t];	
			
			tm.onmouseover = function(){
				//MDS_CMS.Utilities.addClass(this, "over");
			};
			
			tm.onmouseout = function(){
				//MDS_CMS.Utilities.removeClass(this, "over");
			};
		};
	};
	
	//set first level mouse overs:
	for (var i=0;i<firsts.length;i++){
		if (firsts[i].className.indexOf("selected") < 0){
			firsts[i].onmouseover = function(){
				//get thirdLevel el.
				var first = this;
				var thirds = new Array();
				elementGetElementsByClass(first, "div", "thirdLevel", thirds);
				var third = thirds[0];
				if (third != null){
					MDS_CMS.Utilities.removeClass(third, "hidden");
					
					var l = first.offsetWidth + first.offsetLeft - 30;
					var h = first.offsetTop;
					third.style.left = l + "px";
					third.style.top = h + "px";
				};
				
				//MDS_CMS.Utilities.addClass(first, "over");
			};
			
			firsts[i].onmouseout = function(){
				var first = this;
				//MDS_CMS.Utilities.removeClass(first, "over");
				
				var thirds = new Array();
				elementGetElementsByClass(first, "div", "thirdLevel", thirds);
				var third = thirds[0];
				if (third != null){
					MDS_CMS.Utilities.addClass(third, "hidden");
				};
			};
		};
	};
	
};

// JavaScript Document
var Worldview = {	
	worldview_el : document.getElementById("worldView"),

	Show : function () {	
		if (this.worldview_el == null) {this.worldview_el = document.getElementById("worldView");};
		var url = document.location.toString();
		var strippedUrl = url.split("#");
		if (strippedUrl.length > 1){
			//anchor value already exists.
			var anchorvalue = strippedUrl[1];
			if (anchorvalue = "hw"){
				document.location = document.location.toString().replace("hw", "showworld");
			};
		} else {
			document.location += "#showworld";
		};
		
		MDS_CMS.Utilities.removeClass(this.worldview_el, "hidden");
	},
	
	Hide : function () {
		this.selectedPoint = "";
		MDS_CMS.Utilities.addClass(this.worldview_el, "hidden");
		var allItems = document.getElementById("allitems");
		if (allItems) MDS_CMS.Utilities.addClass(allItems, "hidden");
		
		var url = document.location;
		var strippedUrl = url.toString().split("#");
		if (strippedUrl.length > 1){
			//remove anchor marker.
			var url = document.location.toString();
			url = url.replace("showworld", "hw");
			
			document.location = url;			
		} else {
			
		};
	},
	
	AddPoint : function (metaDataID, coordinatePair, caption, contentData_ids) {
		if (this.worldview_el == null) {this.worldview_el = document.getElementById("worldView");};
		
		this.worldview_el.appendChild(CreateNewPoint(metaDataID, coordinatePair, caption, contentData_ids));
	},
	
	highlightClose : function() {
		var closeBox = document.getElementById("worldView_close");
		
		MDS_CMS.Utilities.addClass(closeBox, "closeOver");
	},
	
	unhighlightClose : function() {
		var closeBox = document.getElementById("worldView_close");
		
		MDS_CMS.Utilities.removeClass(closeBox, "closeOver");
	}
};

Worldview.selectedPoint = "";

function CreateNewPoint(metaDataID, coordinatePair, caption, contentData_ids){
	coordinatePair = coordinatePair.split(",");
	var x = coordinatePair[0];
	var y = coordinatePair[1];

	var newElement = document.createElement('div');
	newElement.className = "mapPoint";

	//newElement.setAttribute("_metaDataID", metaDataID);
	//newElement.setAttribute("_coordinatePair", coordinatePair);
	//newElement.setAttribute("_contentData_ids", contentData_ids);
	newElement.style.top = y + "px";
	newElement.style.left = x + "px";

	newElement.innerHTML = "<div class='pointTarget'><!-- empty --></div>" + 
		"<div id='mP_" + metaDataID + "' _metaDataID='" + metaDataID + "' " +
		" _coordinatePair='" + coordinatePair + "' _contentData_ids='" + contentData_ids + "' class='mp_label'>" +
		caption + "</div>";
		
	newElement.onmouseover = function(e){hoverMapPoint(e, metaDataID, caption, contentData_ids)};	
	newElement.onclick = function(e){clickMapPoint(e, metaDataID, caption, contentData_ids)};
	
	return newElement;                                                                                                                                                               
};

function clickMapPoint (e, metaDataID, caption, contentData_ids){
	//window.location= "http://" + domainPath + "/Default.aspx?searchquery=" + contentData_ids + "&searchtitle=" + caption;
	killBubble(e);
};

function hoverMapPoint(e, metaDataID, caption, contentData_ids){
	if (e != null && e.target != null) {
		//Firefox
		el = e.target;
	} else {
		//IE group.
		if (e == null) e = event;
		el = event.srcElement;		
	};
	
	//Open small window of listed items.
	var smallMenuInnerHTML = "<div class='caption'>" + caption + "</div>";
	var cdArray = contentData_ids.split(",");
	
	for (var i=0;i<cdArray.length;i++){		
		var cd_data_Array = cdArray[i].split("|");
		if ((cd_data_Array[2] != null) && (cd_data_Array[2] != "")){
			smallMenuInnerHTML += "<a href='" + cd_data_Array[2] + "'>" + unescape(cd_data_Array[1]) + "</a>";			
		};
	};	

	var smallMenuEl = document.getElementById("smallMenu");
	if (!(smallMenuEl)){
		smallMenuEl = document.createElement('div');
		smallMenuEl.id = "smallMenu";
		el.parentNode.parentNode.appendChild(smallMenuEl);
	};
	
	MDS_CMS.Utilities.removeClass(smallMenuEl, "hidden");
	
	smallMenuEl.innerHTML = smallMenuInnerHTML;
	
	var mpEl = el.offsetParent;
	while (mpEl.className != "mapPoint" && mpEl.offsetParent){
		mpEl = mpEl.offsetParent;	
	};
	
	smallMenuEl.style.left = mpEl.style.left;
	smallMenuEl.style.top = mpEl.style.top;
	
	
	//Set roll out functions to hide smallMenu	
	smallMenuEl.onmouseout = function(e){
		if (e != null && e.target != null) {
			//Firefox
			el = e.target;
		} else {
			//IE group.
			if (e == null) e = event;
			el = event.srcElement;		
		};
				
		if (el.id == "smallMenu"){
			var coordinatePair = getMouseXY(e, null);
		        var tempCPair = coordinatePair.split(",");
	            if (tempCPair.length != 2) return false;
            	
	            var coordX = tempCPair[0];
	            var coordY = tempCPair[1];
	            
		    //Is mouse in smallMenu?
		    var sMenu = document.getElementById('smallMenu');
		        var smLeft = getElementLeft(sMenu);
		        var smTop = getElementTop(sMenu);
    		    var smRight = smLeft + sMenu.offsetWidth;
    		    var smBottom = smTop + sMenu.offsetHeight;
    		        
		    window.status = "exitSML smL: " + smLeft + " x: " + coordX + " r: " + smRight + " || " + " smT: " + smTop + " y: " + coordY + " b: " + smBottom;
		    
		    if ((coordX < smRight) && (coordX > smLeft + 1) && (coordY < smBottom) && (coordY > smTop + 1)){
		        //mouse coordinates are within smallMenu, do not hide it.
		    } else {
		    	MDS_CMS.Utilities.addClass(sMenu, "hidden");
		    	this.onmouseover = "";
		    };
		};
		killBubble(e);
	};
	smallMenuEl.parentNode.onmouseover = function(e){
		if (e != null && e.target != null) {
			//Firefox
			el = e.target;
		} else {
			//IE group.
			if (e == null) e = event;
			el = event.srcElement;		
		};
		
		while (el.id != "smallMenu" && el.parentNode != this && el.parentNode){
			el = el.parentNode;
		};
						
		if (el.id == "smallMenu"){		
		} else {
		    var coordinatePair = getMouseXY(e, null);
		        var tempCPair = coordinatePair.split(",");
	            if (tempCPair.length != 2) return false;
            	
	            var coordX = tempCPair[0];
	            var coordY = tempCPair[1];
	            
		    //Is mouse in smallMenu?
		    var sMenu = document.getElementById('smallMenu');
		        var smLeft = getElementLeft(sMenu);
		        var smTop = getElementTop(sMenu);
    		    var smRight = smLeft + sMenu.offsetWidth;
    		    var smBottom = smTop + sMenu.offsetHeight;
    		        
		    window.status = "over smL: " + smLeft + " x: " + coordX + " r: " + smRight + " || " + " smT: " + smTop + " y: " + coordY + " b: " + smBottom;
		    
		    if ((coordX < smRight) && (coordX > smLeft) && (coordY < smBottom) && (coordY > smTop)){
		        //mouse coordinates are within smallMenu, do not hide it.
		    } else {
		    	//MDS_CMS.Utilities.addClass(sMenu, "hidden");
		    	this.onmouseover = "";
		    };
		};
		
		killBubble(e);
	};
	
	killBubble(e);
};




