
var navImages = new Array();						 // a list of all navigation images
var reg = /(_selected|_sibling)*\.gif$/; // replace the state info of the nav images

// allows multiple calls to onload without overwriting the existing onloads
function loadHandler(func) {
	if(window.onload) {
		var f = window.onload;

		return function() {
			f();
			func();
		}
	}
	else return func;
}


// handle the image swap for the previous sibling
function handleSibling(sibling_id, hover, neighbour) {
	var idx = navImages.indexOf(sibling_id);

	if(neighbour) {
		if(idx + 1 < navImages.length) {
			var img     = document.images[navImages[idx]];
			var sibling = document.images[navImages[(idx + 1)]];

			if(sibling.id == currentChannel) {
				img.src = img.src.replace(reg, neighbour);
			}
		}
	}

	if(--idx >= 0) {
		var img = document.images[navImages[idx]];

		if(img.id == currentChannel) {
			hover = (hover == "_sibling.gif") ? "_selected_sibling.gif" : "_selected.gif";
		}

		img.src = img.src.replace(reg, hover);
	}
}


// returns the index position of an element or -1
Array.prototype.indexOf = function(item) {
for(var i = 0; i < this.length; i++) {
		if(this[i] == item) return i;
	}

return -1;
};


window.onload = loadHandler(function() {
// init();
if(!document.getElementById) return;

	// load up the flah movie of the placeholder element is available
if(document.getElementById("flashcontent")) {
	var so = new SWFObject("media/swf/zenith-homepage-animation.swf", "zenith", "544", "207", "6", "#ffffff");		so.addParam("quality", "high");
		so.write("flashcontent");
	}

	// sort out the top nav rollover images
	var d = document.getElementById("header");

	if(!d) return;

	var ul   = d.getElementsByTagName("ul")[0];
	var imgs = ul.getElementsByTagName("img");

	for(var i = 0; i < imgs.length; i++) {
		navImages[navImages.length] = imgs[i].id;

		imgs[i].onmouseover = function() {
			if(this.id == currentChannel) return;
			this.src = this.src.replace(reg, "_selected.gif");
			handleSibling(this.id, "_sibling.gif", "_selected_sibling.gif");
		};
		imgs[i].onmouseout = function() {
			if(this.id == currentChannel) return;
			this.src = this.src.replace(reg, ".gif");
			handleSibling(this.id, ".gif", "_sibling.gif");
		};

		// make this one selected
		if(imgs[i].id == currentChannel) {
			imgs[i].src = imgs[i].src.replace(reg, "_selected.gif");
			handleSibling(imgs[i].id, "_sibling.gif");
		}
	}
});