/*******************************************************************************
*                                                                              *
*  Author    :  John Serris                                                    *
*  Web       :  http://justmuzik.com/                                          *
*  E-mail    :  phonophunk ( at ) gmail ( dot ) ( com )                        *
*  Modified  :  19-JUN-2006                                                    *
*                                                                              *
*  Copyright (c) 2006 John Serris. All Rights Reserved.                        *
*  Use of this code is not permitted without permission.                       *
*                                                                              *
*******************************************************************************/

var d = document, dE = d.documentElement;
if (dE) dE.className = "jsenabled";

var JM = new Object();

JM = {
	init: function() {
		if (d.getElementById) {
			JM.Tabs.init();
			JM.Scroller.init();
			JM.linkEmails();
			JM.infoToggles();
		}
	},
	linkEmails: function () {
		spans = d.getElementsByTagName("span");
		for (var i = 0, dL = spans.length; i < dL; i++) {
			if (spans[i].className == "email") {
				text = spans[i].innerHTML.replace(" [AT] ", "@");
				text = text.replace(" [DOT] ", ".");
				spans[i].innerHTML = "<a href=\"mailto:"+text+"\">"+text+"</a>";
			}
		}
	},
	infoToggles: function() {
		spans = d.getElementById("streams").getElementsByTagName("span");
		for (var i = 0, dL = spans.length; i < dL; i++) {
			if (spans[i].className == "options") {
				infolink = d.createElement("a");
				infolink.href = "#";
				infolink.innerHTML = "<img src=\"i/ico-info.gif\" alt=\"info\" title=\"Audio Info\">";				
				infolink.onclick = function() {
					holder = this.parentNode.parentNode;
					holder.className = (holder.className == "") ? "showInfo" : "";
					return false;
				}
				spans[i].appendChild(infolink);
			}
		}
	}
};

JM.Tabs = {
	init: function() {
		uls = d.getElementsByTagName("ul");
		for (var i = 0, ulL = uls.length; i < ulL; i++) {
			if (uls[i].className == "submenu") {
				submenuTabs = uls[i].getElementsByTagName("li");
				submenuTabs[0].className = "current";
				submenuLinks = uls[i].getElementsByTagName("a");
				for (var j = 0, smL = submenuLinks.length; j < smL; j++) {
					submenuLinks[j].onclick = function() {
						JM.Tabs.show(this);
						return false;
					}
				}
			}
		}

		divs = d.getElementsByTagName("div");
		for (var i = 0, dL = divs.length; i < dL; i++) {
			if (divs[i].className == "content") {
				subsection = divs[i].getElementsByTagName("div");
				for (var j = 1, ssL = subsection.length; j < ssL; j++) {
					subsection[j].className = "subsection-hide";
				}
			}
		}
	},
	show: function(tab) {
		tabholder = tab.parentNode.parentNode.parentNode;
		subtabs = tabholder.getElementsByTagName("ul")[0].getElementsByTagName("li");
		for (i = 0, sL = subtabs.length; i < sL; i++) {
			subtabs[i].className = "";
		}

		subsections = tabholder.getElementsByTagName("div");
		for (var i = 0, sL = subsections.length; i < sL; i++) {
			if (subsections[i].className == "subsection") {
				subsections[i].className = "subsection-hide";
			}
		}
		d.getElementById(tab.hash.substring(1)).className = "subsection";
		tab.parentNode.className = "current";
		tab.blur();
		tabholder.getElementsByTagName("div")[0].scrollTop = 0;
	}
};

// Based on http://squidfingers.com/code/dhtml/scrollwin/

JM.Scroller = {
	containerName: "page",
	iex: d.all && !window.opera,
	scrollLoop: false,
	scrollInterval: null,
	currentBlock: null,
	init: function() {
		links = d.getElementsByTagName('a');
		for (var i = 0, lL = links.length; i < lL; i++) {
			if ((links[i].href && links[i].href.indexOf('#') != -1) &&
       			( (links[i].pathname == location.pathname) ||
   				('/'+links[i].pathname == location.pathname) ) &&
       			(links[i].search == location.search) && !links[i].onclick)
			{
				links[i].onclick = function() {
					JM.Scroller.scroll(this.hash.substring(1));
					return false;
				}
			}
		}
	},
	getWindowHeight: function() {
		if (this.iex) return (dE.clientHeight) ? dE.clientHeight : d.body.clientHeight;
		else return window.innerHeight;
	},
	getScrollLeft: function() {
		if (this.iex) return (dE.scrollLeft) ? dE.scrollLeft : d.body.scrollLeft;
		else return window.pageXOffset;
	},
	getScrollTop: function(){
		if (this.iex) return (dE.scrollTop) ? dE.scrollTop : d.body.scrollTop;
		else return window.pageYOffset;
	},
	getElementYpos: function(el) {
		var y = 0;
		while (el.offsetParent) {
			y += el.offsetTop
			el = el.offsetParent;
		}
		return y;
	},
	scroll: function(destId) {
		if (this.scrollLoop) {
			clearInterval(this.scrollInterval);
			this.scrollLoop = false;
			this.scrollInterval = null;
		}
		if (destId == "") {
			this.currentBlock = d.body;
		} else {
			this.currentBlock = d.getElementById(destId);
		}
		var doc = d.getElementById(this.containerName);
		var dHeight = this.getElementYpos(doc) + doc.offsetHeight;
		var windowHeight = this.getWindowHeight();
		var ypos = this.getElementYpos(this.currentBlock);
		if (ypos > dHeight - windowHeight) {
			ypos = dHeight - windowHeight;
		}
		this.scrollTo(0,ypos);
	},
	scrollTo: function(x,y) {
		if (this.scrollLoop) {
			var left = this.getScrollLeft();
			var top = this.getScrollTop();
			if (Math.abs(left-x) <= 1 && Math.abs(top-y) <= 1) {
				window.scrollTo(x,y);
				clearInterval(this.scrollInterval);
				this.scrollLoop = false;
				this.scrollInterval = null;
			} else {
				window.scrollTo(left+(x-left)/2, top+(y-top)/2);
			}
		} else {
			this.scrollInterval = setInterval("JM.Scroller.scrollTo("+x+","+y+")",70);
			this.scrollLoop = true;
		}
	}
};
