var version = '';
var agent   = '';
var os      = '';
//this may seem odd to have a variable for this, however since an isSafari
//check is need in function getPagePos this saves searching the user agent
//string on every call
var isSafari = false;
if (navigator.userAgent.search('MSIE') >= 0) {
	if (navigator.userAgent.search('MSIE 4') >= 0 || navigator.userAgent.search('MSIE 3') >= 0) {
		agent = '';
	} else {
		agent = 'ie';
		version = parseFloat(navigator.userAgent.substr(navigator.userAgent.indexOf('MSIE ')+5));
	}
} else if (navigator.userAgent.search('Konqueror') >= 0 || navigator.userAgent.search('Safari') >= 0) {
	agent = 'konqueror';
	if (navigator.userAgent.search('Safari') >= 0)
		isSafari = true;
} else if (navigator.userAgent.search('Gecko') >= 0) {
	agent = 'gecko';
} else if (navigator.appName == 'Netscape' && navigator.userAgent.search('Mozilla/4') >= 0) {
	agent = 'ns4';
} else {
	agent = '';
}

if (navigator.userAgent.search('Mac') >= 0) {
	os = 'Mac';
} else {
	os = '';
}

var haveFlash = 0;
var ranVb = "";
if(agent == "ie" && os == ''){
	document.write('<SCR'+'IPT LANGUAGE="VBScript" src="/fdetection.vbs"></SCR'+'IPT>\n');
}

function detectFlash() {
	var fallback = arguments.length > 0 ? arguments[0] : false;
	if (navigator.plugins) {
		if (navigator.plugins["Shockwave Flash"]) {
			return true;
		}
	}
	if (ranVb && haveFlash) return true;
	return fallback;
}

function flash_or_image(flashsrc, imgsrc, width, height) {
	if (detectFlash()) {
		document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="'+width+'" height="'+height+'" id="fbiradio"><param name="movie" value="'+flashsrc+'" /><param name="quality" value="high" /><param name="bgcolor" value="#BDBCAF" /><param name="loop" value="true" /><param name="menu" value="false" /><param name="wmode" value="opaque" /><embed src="'+flashsrc+'" loop="true" menu="false" quality="high" wmode="opaque" bgcolor="#BDBCAF" width="'+width+'" height="'+height+'" swLiveConnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>');
		//alert('<EMBED src="'+flashsrc+'" loop=true menu=false quality=high wmode=opaque swLiveConnect=true TYPE="APPLICATION/X-SHOCKWAVE-FLASH" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" NAME="host" border=0></EMBED>');
	} else {
		document.writeln('<div style="position: absolute"><IMG SRC="'+imgsrc+'" style="border: solid #000000 2px;" BORDER=0></div>');
		//alert('<IMG SRC="'+imgsrc+'" BORDER=0>');
	}
}

function flash_or_nothing(flashsrc, width, height) {
	if (detectFlash()) {
		document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="'+width+'" height="'+height+'" id="fbiradio"><param name="movie" value="'+flashsrc+'" /><param name="quality" value="high" /><param name="bgcolor" value="#BDBCAF" /><param name="loop" value="true" /><param name="menu" value="false" /><param name="wmode" value="opaque" /><embed src="'+flashsrc+'" loop="true" menu="false" quality="high" wmode="opaque" bgcolor="#BDBCAF" width="'+width+'" height="'+height+'" swLiveConnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>');
	}
}

if (agent == 'ns4') {
	document.write('<script language="Javascript" src="/js/nogginmenus-ns4.js"></script>');
} else {
	function NogginMenu(id) {
		this.items = new Array();
		this.parentIdx = null;
		this.currentItem = null;
		this.id = id;

		this.add = function _add(title, url, html) {
			this.items[this.items.length] = new NogginMenuItem(title, url, this);
		}

		this.install = function _install() {
			this.lyr = document.getElementById(this.id);
			for (var i = 0; i < this.lyr.rows[0].getElementsByTagName('td').length-1; i++) {
				this.items[i].setTr(this.lyr.rows[0].getElementsByTagName('td')[i]);
				if (this.items[i].sub != null) {
					this.items[i].sub.lyr = document.createElement('table');
					document.body.appendChild(this.items[i].sub.lyr);
				}
			}
			this.lyr.onmouseover = function _over(event) {
				if (agent == 'konqueror') {
					document.onmouseover = new Function('event', this.id+'.hide();');
				} else {
					document.body.onmouseover = new Function('event', this.id+'.hide();');
				}
			}
			var tmp = getPagePos(document.getElementById('slider'));
			initDot(tmp.x+10, tmp.y+9);
			this.lyr.onmousemove = moveDot;
		}

		this.hide = function _hide() {
			if (this.currentItem != null) {
				if (this.currentItem.sub != null) this.currentItem.sub.hide();
				this.currentItem.out();
			}
			if (agent == 'konqueror') {
				document.onmouseover = function _donothing() {};
			} else {
				document.body.onmouseover = function _donothing() {};
			}
		}

	}

	function NogginSubMenu(parentMenu, parentIdx) {
		parentMenu.items[parentIdx].sub = this;
		this.parentMenu = parentMenu;
		this.parentItem = parentMenu.items[parentIdx];
		this.parentIdx = parentIdx;
		this.items = new Array();
		this.lyr = null;
		this.currentItem = null;
		this.built = false;

		this.add = function _add(title, url) {
			this.items[this.items.length] = new NogginMenuItem(title, url, this);
		}

		this.build = function _build() {
			if (this.built) return false;
			this.lyr.setAttribute('cellpadding', '5', 0);
			this.lyr.setAttribute('cellspacing', '0', 0);
			if (os == 'Mac' && agent == 'ie') {
				this.lyr.cellPadding = 5;
				this.lyr.cellSpacing = 0;
			}
			if (agent == 'ie') {
				this.lyr.style.cursor = 'hand';
			} else {
				this.lyr.style.cursor = 'pointer';
			}

			var tmp, tbody, tr, maintr;
			this.lyr.appendChild(tbody = document.createElement('tbody'));
			for (var i = 0; i < this.items.length; i++) {
				tbody.appendChild(maintr = document.createElement('tr'));
				maintr.appendChild(tmp = document.createElement('td'));
				tmp.className = 'topnav';
				tmp.appendChild(document.createTextNode(this.items[i].title));
				this.items[i].setTr(tmp);
			}
			this.lyr.onmouseover = detectTarget;
			this.lyr.style.backgroundColor = '#ff8201';
			this.lyr.style.position = 'absolute';
			this.lyr.style.top = this.parentItem.subpos.y+'px';
			this.lyr.style.left = '0px';
			this.lyr.over = function _over(event) {}
			this.hide();
			this.built = true;
			return true;
		}

		this.hide = function _hide() {
			if (!this.built) return false;
			//this.lyr.style.top = (-1*this.lyr.offsetHeight-10)+'px';
			//this.lyr.style.left = (-1*this.lyr.offsetWidth-10)+'px';
			this.lyr.style.visibility = 'hidden';
			if (this.currentItem != null) this.currentItem.out();
		}

		this.show = function _show() {
			if (!this.built) return false;
			this.parentItem.setSubPos();
			this.lyr.style.visibility = 'visible';
			//this.lyr.style.top = this.parentItem.subpos.y+'px';
			this.lyr.style.left = this.parentItem.subpos.x+'px';
		}

	}

	function NogginMenuItem(title, url, parentMenu) {
		this.title = title;
		this.url = url;
		this.tr = null;
		this.parentMenu = parentMenu;
		this.sub = null;

		this.setTr = function _setTr(tr) {
			this.tr = tr;
			this.tr.item = this;
			this.tr.onmouseover = detectTarget;
			this.tr.onclick = detectTarget;
			if (this.sub != null) this.setSubPos();
		}

		this.setSubPos = function _setSubPos() {
			this.subpos = getPagePos(this.tr);
			this.subpos.y += this.tr.offsetHeight;
		}

		this.over = function _over(event) {
			if (this.parentMenu.currentItem != null) {
				if (this.parentMenu.currentItem.sub != null) this.parentMenu.currentItem.sub.hide();
				this.parentMenu.currentItem.out();
			} else if (this.parentMenu.parentIdx == null) {
				this.parentMenu.lyr.onmouseover(event);
			}
			if (this.tr != null) {
				this.tr.style.backgroundColor = '#f55f00';
				this.parentMenu.currentItem = this;
				if (this.sub != null) {
					this.sub.build();
					this.sub.show();
				}
			}
		}

		this.out = function _out() {
			if (this.tr != null) {
				this.tr.style.backgroundColor = '#ff8201';
				if (this.sub != null) {
					this.sub.hide();
				}
			}
			if (this.parentMenu.currentItem == this) this.parentMenu.currentItem = null;
		}

		this.click = function _click() {
			window.location = this.url;
		}

	}

	function detectTarget(event) {
		if (agent == 'gecko') {
			var item = event.currentTarget.item;
		} else {
			var event = window.event;
			var item = this.item;
		}
		event.cancelBubble = true;
		if (item) {
			switch (event.type) {
				case 'mouseover':
					item.over(event);
					break;
				case 'click':
					item.click(event);
					break;
			}
		}
	}
}

function ng_dump(obj) {
	var result = '';
	for (var i in obj) if (arguments.length == 1 || i.search(arguments[1]) >= 0) result += '.' + i + ' = ' + obj[i] +'<br />\n';
	return result;
}

function Point(x,y) {
	this.x=x; this.y=y; return this;
}

function getPagePos(node) {
	if (agent == 'ns4') return new Point(node.pageX, node.pageY);
	//added the test for the Mac since it things Safari is Konqueror 
	if (agent == 'konqueror' && !isSafari) return new Point((node.scrollLeft*-1)+document.scrollLeft, (node.scrollTop*-1)+document.scrollTop);
	var str = '';
	var x = 0;
	var y = 0;
	while (node && node.tagName != 'BODY') {
		str += '\n'+node.tagName+' = '+node.offsetTop;
		x += node.offsetLeft;
		y += node.offsetTop;
		if (os == 'Mac' && agent == 'ie' && node.tagName == 'TD') y += node.parentNode.offsetTop;
		node = node.offsetParent;
	}
	// Bug is fixed on MacOS X IE5.21 (maybe earlier?)
	
	if (!node && os == 'Mac' && agent == 'ie' && version < 5.21) {
		x += (document.body.leftMargin*1);
		y += (document.body.topMargin*1);
	}
	return new Point(x, y);
}

function initDot(x, y) {
	if (agent == 'ns4') {
		var dot = new Layer(15, window);
		document.topnavdot = dot;
		dot.pageX = x;
		dot.pageY = y;
		dot.document.open();
		dot.document.write('<img src="/img/topnavdot.gif" width="15" height="15" alt="" id="topnavdot" />');
		dot.document.close();
		dot.visibility = 'show';
	} else {
		var dot = document.createElement("img");
		dot.style.width = '15px';
		dot.style.height = '15px';
		dot.src = '/img/topnavdot.gif';
		dot.id = 'topnavdot';
		dot.style.position = 'absolute';
		dot.style.top = y+'px';
		dot.style.left = x+'px';
		document.body.appendChild(dot);
	}
}

function moveDot(event) {
	if (agent == 'ns4') {
		var curspos = (event.pageX-7);
		if (curspos < document.images['slider'].x+10) curspos = document.images['slider'].x+10;
		if (curspos > document.images['slider'].x+450) curspos = document.images['slider'].x+450;
		document.topnavdot.pageX = curspos;
	} else {
		if (!event) event = window.event;
		var curspos = (event.clientX-7);
		var slider = getPagePos(document.getElementById('slider'));
		if (curspos < slider.x+10) curspos = slider.x+10;
		if (curspos > slider.x+450) curspos = slider.x+450;
		document.getElementById('topnavdot').style.left = curspos+'px';
	}
}


function popup(name, url) {
	var newwin = window.open(url, name, 'width=275,height=300,scrollbars=yes,personalbar=no,status=no,menubar=no,toolbar=no,resizable,dependent');
	newwin.focus();
	return !(newwin && newwin.document);
}

function paymentGW(obj) {
	obj.form.target='nogginpaymentgw';
	var gwwin = window.open('', 'nogginpaymentgw', 'innerHeight=430,Height=460,innerWidth=535,Width=570,directories=no,toolbar=no,menubar=no,personalbar=no,resizeable=yes,scrollbars=yes,status=yes');
	return false;
}

function resetfrm(obj) {
	obj.form.target='';
	return true;
}

function submitfrm() {
	var frm = document.forms[0];
	frm.target='nogginpaymentgw';
	var gwwin = window.open('', 'nogginpaymentgw', 'innerHeight=430,Height=460,innerWidth=535,Width=570,directories=no,toolbar=no,menubar=no,personalbar=no,resizeable=yes,scrollbars=yes,status=yes');
	gwwin.focus();
	frm.submit();
}

function popUp(id) {
	var newwin = window.open('/program_closeup.php?programtimeid=' + id,'ProgramCloseUp','width=240,height=500,locationbar=yes,menubar=no,personalbar=no');
	newwin.focus();
	return false;
}
