// GLOBAL OBJECTS

var gvMenuId    = null;
var gvSubMenuId = null;
var gvTimeSync = 0;

var gvMenuLine = 0;
var gvMenuTable = 0;
var gvMenuIdent = 0;

var gvHostName = window.location.host;

// GLOBAL ARRAYS

var gaMonths = new Array(
    'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY',
    'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'
);

var gaDays = new Array(
    'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'
);

var gaWhatsNew = [
	[
		['Announcements',	'http://coast.gmms.ca/cgi-bin/forum/YaBB.pl?board=general']
	],
	[
		['Recent Additions',	'http://coast.gmms.ca/page_addition.html']
	],
	[
		['Show Requests',	'http://coast.gmms.ca/cgi-bin/forum/YaBB.pl?board=request']
	]
];

var gaProgram = [
	[
		['CJOB Audio Vault',		'http://coast.gmms.ca/cgi-bin/cjob.pl'],
		['CoastZone 2009 January',	'http://coast.gmms.ca/page_N0901.html'],
		['CoastZone 2009 July',		'http://coast.gmms.ca/page_N0907.html']
	],
	[
		['Chuck Missler 66/40',		'http://coast.gmms.ca/cgi-bin/khouse.pl'],
		['CoastZone 2009 February',	'http://coast.gmms.ca/page_N0902.html'],
		['CoastZone 2009 August',	'http://coast.gmms.ca/page_N0908.html']
	],
	[
		['',				''],
		['CoastZone 2009 March',	'http://coast.gmms.ca/page_N0903.html'],
		['CoastZone 2009 September',	'http://coast.gmms.ca/page_N0909.html']
	],
	[
		['',				''],
		['CoastZone 2009 April',	'http://coast.gmms.ca/page_N0904.html'],
		['',				'']
	],
	[
		['CoastZone 2008 November',	'http://coast.gmms.ca/page_N0811.html'],
		['CoastZone 2009 May',		'http://coast.gmms.ca/page_N0905.html'],
		['',				'']
	],
	[
		['CoastZone 2008 December',	'http://coast.gmms.ca/page_N0812.html'],
		['CoastZone 2009 June',		'http://coast.gmms.ca/page_N0906.html'],
		['',				'']
	]
];

var gaArchive = [
	[
		['Coast to Coast AM 1993',	'http://coast.gmms.ca/page_C1993.html'],
		['Coast to Coast AM 2002',	'http://coast.gmms.ca/page_C2002.html'],
		['Audio Clips',			'http://coast.gmms.ca/page_A9999.html']
	],
	[
		['Coast to Coast AM 1994',	'http://coast.gmms.ca/page_C1994.html'],
		['Coast to Coast AM 2003',	'http://coast.gmms.ca/page_C2003.html'],
		['Dreamland',			'http://coast.gmms.ca/page_D9999.html']
	],
	[
		['Coast to Coast AM 1995',	'http://coast.gmms.ca/page_C1995.html'],
		['Coast to Coast AM 2004',	'http://coast.gmms.ca/page_C2004.html'],
		['Ghost to Ghost',		'http://coast.gmms.ca/page_G9999.html']
	],
	[
		['Coast to Coast AM 1996',	'http://coast.gmms.ca/page_C1996.html'],
		['Coast to Coast AM 2005',	'http://coast.gmms.ca/page_C2005.html'],
		['Hour of the Time',		'http://coast.gmms.ca/page_H9999.html']
	],
	[
		['Coast to Coast AM 1997',	'http://coast.gmms.ca/page_C1997.html'],
		['Coast to Coast AM 2006',	'http://coast.gmms.ca/page_C2006.html'],
		['Miscellaneous',		'http://coast.gmms.ca/page_Z9999.html']
	],
	[
		['Coast to Coast AM 1998',	'http://coast.gmms.ca/page_C1998.html'],
		['Coast to Coast AM 2007',	'http://coast.gmms.ca/page_C2007.html'],
		['Power of Prophecy',		'http://coast.gmms.ca/page_P9999.html']
	],
	[
		['Coast to Coast AM 1999',	'http://coast.gmms.ca/page_C1999.html'],
		['Coast to Coast AM 2008',	'http://coast.gmms.ca/page_C2008.html'],
		['Radio Liberty',		'http://coast.gmms.ca/page_R9999.html']
	],
	[
		['Coast to Coast AM 2000',	'http://coast.gmms.ca/page_C2000.html'],
		['Coast to Coast AM 2009',	'http://coast.gmms.ca/page_C2009.html'],
		['Somewhere in Time',		'http://coast.gmms.ca/page_S9999.html']
	],
	[
		['Coast to Coast AM 2001',	'http://coast.gmms.ca/page_C2001.html'],
		['Coast to Coast Live',		'http://coast.gmms.ca/page_L9999.html'],
		['Wildcard Page',		'http://coast.gmms.ca/page_wildcard.html']
	],
];

//=============================================================================

function fnDebug() {
	var str = "";
	if (self.innerHeight) {
		str += "innerHeight = " + self.innerHeight + "<br>";
	}
	if (document.documentElement && document.documentElement.clientHeight) {
		str += "d.clientHeight = " + document.documentElement.clientHeight + "<br>";
	}
	if (document.body) {
		str += "b.clientHeight = " + document.body.clientHeight + "<br>";
	}
	str += "scrollHeight = " + document.body.scrollHeight + "<br>";
	
	var el = fnGetElement('idDebug');
	el.innerHTML = str;
}

function fnGetElement(id) {
	if (document.getElementById) {
		return(document.getElementById(id));
	}
	if (document.all) {
		return(document.all[id]);
	}
	if (document.layers) {
		return(document.layers[id]);
	}
	return(null);
}

function fnGetWndHeight() {
	if (self.innerHeight) {
		return(self.innerHeight);
	}
	if (document.documentElement && document.documentElement.clientHeight) {
		return(document.documentElement.clientHeight);
	}
	if (document.body) {
		return(document.body.clientHeight);
	}
	return(null);
}

function fnGetWndWidth() {
	if (self.innerWidth) {
		return(self.innerWidth);
	}
	if (document.documentElement && document.documentElement.clientWidth) {
		return(document.documentElement.clientWidth);
	}
	if (document.body) {
		return(document.body.clientWidth);
	}
	return(null);
}

function fnSetVisibility(obj, state) {
	if ((document.getElementById) || (document.all)) {
		obj.style.visibility = (state == 1) ? 'visible' : 'hidden';
		return(state);
	}
	if (document.layers) {
		obj.visibility = (state == 1) ? 'show' : 'hide';
		return(state);
	}
	return(null);
}

function fnGetVisibility(obj) {
	if (obj == null) {
		return(0);
	}
	if ((document.getElementById) || (document.all)) {
		var state = (obj.style.visibility == 'visible') ? 1 : 0;
		return(state);
	}
	if (document.layers) {
		var state = (obj.visibility == 'show') ? 1 : 0;
		return(state);
	}
}

function fnSetOpacity(obj, value) {
	if (obj == null) {
		return(0);
	}
	obj.style.opacity = value / 10;
	obj.style.filter = 'alpha(opacity=' + (value * 10) + ')';
}

//=============================================================================

function fnMenuSwitch(menuId, submenuId, state) {
    var obj = null;
    
    // Mouse leaves a menu button
    if (state == 0) {
    }
    // Mouse enters a menu button
    if (state == 1) {
        if (gvSubMenuId != null) {
            if (gvSubMenuId != submenuId) {
                obj = fnGetElement(gvSubMenuId);
                fnSetVisibility(obj, 0);
                gvSubMenuId = null;
            }
        }
        if (gvSubMenuId == null) {
            if (submenuId != null) {
                obj = fnGetElement(submenuId);
                fnSetVisibility(obj, 1);
            }
        }
        gvMenuId = menuId;
        gvSubMenuId = submenuId;
    }
    // Mouse leaves a sub menu
    if (state == 2) {
    	obj = fnGetElement(submenuId);
    	fnSetVisibility(obj, 0);
    	gvSubMenuId = null;
    }
    // Mouse enters a sub menu
    if (state == 3) {
        if (gvSubMenuId == null) {
            if (submenuId != null) {
                obj = fnGetElement(submenuId);
                fnSetVisibility(obj, 1);
            }
        }
        gvMenuId = menuId;
        gvSubMenuId = submenuId;
    
    }
}

function fnMenuBuild(menu, id) {
	var squote = "'";
	var str = '<table border="0" cellpadding="0" cellspacing="0">';
	var ident = 0;
	for (var i = 0; i < menu.length; i++) {
		str += '<tr>';

		for (var j = 0; j < menu[i].length; j++) {
			ident++;
			str += '<td>';
			if (menu[i][j][1] != '') {
				str += '<a id="' + id + ident + '" class="dcsmItem" href="#" ';
				str += 'onMouseOver="window.status=' + squote + squote + '; return true;"';
				str += 'onClick="fnLoadPage(' + squote + menu[i][j][1] + squote + '); ';
				str += 'this.blur(); return false;">';
				str += menu[i][j][0];
				str += '</a>';
			}
			str += '</td>';
			ident++;
			if ((j < (menu[i].length - 1)) && ((menu[i][j][1] != '') || (menu[i][j+1][1] != ''))) {
				str += '<td id="' + id + ident + '" class="dcsmDivider"></td>';
			}
			else {
				str += '<td></td>';
			}
		}
		str += '</tr>';
	}
	str += '</table>';
	var obj = fnGetElement(id);
	obj.innerHTML = str;
}

function fnMenuDraw(menu, id) {
	gvMenuLine = 0;
	gvMenuTable = menu;
	gvMenuIdent = id;
	fnMenuDrawCycle();
}

function fnMenuDrawCycle() {
	var ident = 0;
	for (var i = 0; i < gvMenuTable.length; i++) {
		for (var j = 0; j < gvMenuTable[i].length; j++) {
			ident++;
			var obj = fnGetElement(gvMenuIdent + ident);
			if (obj) {
				if (i < gvMenuLine) {
					obj.className = 'dcsmItem';
				}
				else if (i == gvMenuLine) {
					obj.className = 'dcsmLite';
				}
				else {
					obj.className = 'dcsmHide';
				}
			}
			ident++;
			if ((j < (gvMenuTable[i].length - 1)) && ((gvMenuTable[i][j][1] != '') || (gvMenuTable[i][j+1][1] != ''))) {
				var obj = fnGetElement(gvMenuIdent + ident);
				if (obj) {
					if (i <= gvMenuLine) {
						obj.className = 'dcsmDivider';
					}
					else {
						obj.className = 'dcsmDivHide';
					}
				}
			}
		}
	}

	gvMenuLine++;
	if (gvMenuLine <= gvMenuTable.length) {
		setTimeout("fnMenuDrawCycle()", 100);
	}
}

function fnSizePage() {
	var caObj = fnGetElement('idContent');
	caObj.style.height = '0px';
	var pvObj = fnGetElement('idPreview');
	pvObj.style.height = '0px';

	// Content area = Window size - Heading size - Margins

	var height = fnGetWndHeight();
	var hwObj = fnGetElement('idHeadWrap');

	height -= hwObj.offsetHeight;
	height -= 10; 	// 5 pixel top margin + 5 pixel bottom margin

	// Scale menu and content area height

	var mwObj = fnGetElement('idMenuWrap');
	var cwObj = fnGetElement('idContentWrap');

	mwObj.style.height = height + 'px';
	cwObj.style.height = height + 'px';
	caObj.style.height = height + 'px';
}

function fnLoadPage(page) {
	var oPage = new fnLocation(page);
	var el = fnGetElement('idContent');
	el.src = oPage.href;

	var oHost = new fnLocation(window.location.href);
	var hash = "";
	if (oHost.hostname == oPage.hostname) {
		var url = oPage.pathname.substring(1);
//		if (url.indexOf("page_home") == -1) {
			if (oPage.search) {
				url += "?" + oPage.search;
			}
			hash = url;
//		}
	}
	window.location.hash = hash;

	var cwObj = fnGetElement('idContentWrap');
	var caObj = fnGetElement('idContent');
	var pvObj = fnGetElement('idPreview');

	var height = cwObj.offsetHeight;
	caObj.style.height = '0px';
	pvObj.style.height = height + 'px';

	if (gvSubMenuId != null) {
		var obj = fnGetElement(gvSubMenuId);
		fnSetVisibility(obj, 0);
		gvSubMenuId = null;
	}
}

function fnLivePage() {
	var cwObj = fnGetElement('idContentWrap');
	var caObj = fnGetElement('idContent');
	var pvObj = fnGetElement('idPreview');

	var height = cwObj.offsetHeight;
	pvObj.style.height = '0px';
	caObj.style.height = height + 'px';
}

function fnPageInitialize() {
	fnSizePage();
	fnClock();
	fnLastUpdate();

	var oLoc = new fnLocation(window.location.href);
	var url = 'page_home.html';
	if (oLoc.hash) {
		url = oLoc.hash;
	}
	oLoc = new fnLocation(url);
	fnLoadPage(oLoc.href);
}

//	protocol://host/pathname#hash?search
//	hash      returns the URL from the hash sign (#)
//	host      returns the hostname and port number of the current URL
//	hostname  returns the hostname of the current URL
//	href      returns the entire URL
//	pathname  returns the path of the current URL
//	port      returns the port number of the current URL
//	protocol  returns the protocol of the current URL
//	search    returns the URL from the question mark (?)

function fnLocation(url) {
	var ptr = 0;

	// Extract search string - anything following the '?' character
	var search = "";
	if ((ptr = url.indexOf("?")) != -1) {
		search = url.substring(ptr + 1);
		url = url.substring(0, ptr);
	}
	this.search = search;

	// Extract hash string - anything following the '#' character
	var hash = "";
	if ((ptr = url.indexOf("#")) != -1) {
		hash = url.substring(ptr + 1);
		url = url.substring(0, ptr);
	}
	this.hash = hash;

	// Extract protocol - anything preceding the '://' character sequence
	var protocol = "http";
	if ((ptr = url.indexOf("://")) > 0) {
		protocol = url.substring(0, ptr);
		url = url.substring(ptr + 3);
	}
	this.protocol = protocol;

	var host = "";
	if ((ptr = url.indexOf("/")) > 0) {
		var temp = url.substring(0, ptr);
		if ((temp.indexOf(".") > 0) || (temp == "localhost")) {
			host = temp;
			url = url.substring(ptr);
		}
	}
	if (host.length == 0) {
		var oUrl = new fnLocation(window.location.href);
		host = oUrl.host;
	}
	this.host = host;

	var hostname = host;
	var port = "";
	if ((ptr = host.indexOf(":")) > 0) {
		hostname = host.substring(0, ptr);
		port = host.substring(ptr + 1);
	}
	this.hostname = hostname;
	this.port = port;

	var pathname = "/";
	if (url.length > 0) {
		pathname = (url.substring(0,1) == "/") ? url : "/" + url;
	}
	this.pathname = pathname;
	
	var href = protocol + "://" + host + pathname;
	if (hash.length > 0) {
		href += "#" + hash;
	}
	if (search.length > 0) {
		href += "?" + search;
	}
	this.href = href;
}

//=============================================================================

function fnClock() {
	var uTime    = new Date();
	var hours    = uTime.getUTCHours();
	var minutes  = uTime.getUTCMinutes();
	var seconds  = uTime.getUTCSeconds();
	gvLocHour = hours;
    
	if (gvTimeSync == 0) {
		gvTimeSync =  ((gvUTCHour * 60 + gvUTCMins) * 60 + gvUTCSecs) * 1000;
		gvTimeSync -= ((hours * 60 + minutes) * 60 + seconds) * 1000;
		if (gvTimeSync == 0) {
			gvTimeSync++;
		}
	}

	var uMs      = uTime.getTime();
	var pTime    = new Date(uMs + gvTimeSync - 28800000);

	var year     = pTime.getFullYear();
	var month    = pTime.getUTCMonth() + 1;
	var day      = pTime.getUTCDate();
	var weekday  = pTime.getUTCDay();
	hours    = pTime.getUTCHours();

	var daylight = 'PST';
	var sundays  = 0;

	if (day > weekday) {
		sundays = Math.floor((day - weekday) / 7 + 1);
	}

	if (((month == 3) && (sundays == 3) && (weekday == 0) && (hours >= 2)) ||
		((month == 3) && (sundays == 3) && (weekday > 0))                  ||
		((month == 3) && (sundays > 2))                                    ||
		((month > 3) && (month < 11))                                      ||
		((month == 11) && (sundays < 1))                                   ||
		((month == 11) && (sundays == 1) && (weekday == 0) && (hours < 2))) {
			pTime    = new Date(uMs + gvTimeSync - 25200000);
			daylight = 'PDT';
			hours    = pTime.getUTCHours();
	}

	minutes = pTime.getUTCMinutes();

	var status = 'OFF AIR';
	if ((hours >= 22) || (hours < 2)) {
		status = 'ON THE AIR';
	}
 
 	var ampm = (hours < 12) ? 'AM' : 'PM';
	hours = (hours <= 12) ? ((hours > 0) ? hours : 12) : hours - 12;
	minutes = (minutes < 10) ? '0' + minutes : minutes;

	var obj = fnGetElement('idsbTime');
	obj.innerHTML = hours + ':' + minutes + ' ' + ampm + ' ' + daylight;
	obj = fnGetElement('idsbDate');
	obj.innerHTML = gaMonths[month - 1] + ' ' + day + ' ' + year;
	obj = fnGetElement('idsbWeekDay');
	obj.innerHTML = gaDays[weekday];
	obj = fnGetElement('idsbBroadcast');
	obj.innerHTML = status;
	obj = fnGetElement('idsbVisitors');
	obj.innerHTML = gvVisitors + ' ENCOUNTERS';
	obj = fnGetElement('idsbDownloads');
	obj.innerHTML = gvDownloads + ' ABDUCTIONS';
    
	setTimeout("fnClock()", 1000);
}

function fnLastUpdate() {
	if (gvFileUpdate == 0) {
		return;
	}

	var time = gvFileUpdate / 60;
	var type = 'MINUTE';
   
	if (time > 60) {
		time = time / 60;
		type = 'HOUR';

		if (time > 24) {
			time = time / 24;
			type = 'DAY';

			if (time > 7) {
				time = time / 7;
				type = 'WEEK';

				if (time > 4) {
					time = time / 4;
					type = 'MONTH';
				}
			}
		}
	}
	time = Math.floor(time);
	if (time != 1) {
		type = type + 'S';
	}
	var str = 'UPDATED ' + time + ' ' + type + ' AGO';
	var obj = fnGetElement('idsbUpdate');
	obj.innerHTML = str;
}
