// This file requires the dojo library to work - you've been warned

var prevHeight = 0;
var accOpen = false;
var currentAcc, ulP, b2offset;
var initializingPageHeight = false;


//making page height global - so it can be used in other scripts
/*
var ciHeight = null;
var ciWidth = null;
*/
var ciHeight, ciWidth;
var side_height_ratio = .50;

//global var to hold sticky link
var gStickylink = "";

//this function sets the height of sidebar and content areas to be 100%
function initalizePageHeight(fixContentArea) {
	if(typeof(fixContentArea) == "undefined") { fixContentArea = true; }
	if(!initializingPageHeight) {
		initializingPageHeight = true;
		/*
		var cHeight = null;*/
		if(window.innerHeight) { // supported in Mozilla, Opera, and Safari 
			ciHeight = (window.innerHeight);
			ciWidth = (window.innerWidth);
		} 
		else if (document.documentElement.clientHeight) { //for IE
			ciHeight = (document.documentElement.clientHeight);
			ciWidth = (document.documentElement.clientWidth);
		}
		
		cHeight = (ciHeight  < 420) ? 420 : ciHeight;
		//offset for bottom spacing and borders
		cHeight -= 5;
		
		
		//IE 6 *sigh* when does it end - adding code to fix min-width and scrolling fun
		if(dojo.isIE == 6) {
			var dContainer =  dojo.byId("container");
			//cWidth = (document.documentElement.clientWidth);
			//cWidth = (ciWidth  < 750) ? 750 : ciWidth;
			cWidth = (ciWidth  < 790) ? 790 : ciWidth;
			document.body.style.width = cWidth;
			dojo.byId("content").style.width = (cWidth - 196); // content border + content margins
			dojo.byId("content_body").style.width = (cWidth - 196); // content border + content margins
		}
		
		//grabbing lots of element heights to figure out excess for open accordion
		var excess_ht = 0;
		var sb = dojo.byId("sidebar");
		var hds = sb.getElementsByTagName("h2");
		for (var h = 0; h < hds.length; h++) {
			var current_ht = dojo._getBorderBox(hds[h]).h;
			//skipp the first one its included in header ht - otherwise figure all h2 heights
			if (h>0) excess_ht+=current_ht;
		}
		
		//fixed heights used inthe layout - grabbed from the page in case they change
		var nh = dojo._getBorderBox(dojo.byId("header")).h;
		var bh = dojo._getBorderBox(dojo.byId("breadcrumb_bar")).h;
		//var uh = dojo._getBorderBox(dojo.byId("vmode_undercarriage")).h + dojo._getBorderBox(dojo.byId("onboard")).h;;
		var uh = dojo._getBorderBox(dojo.byId("vmode_undercarriage")).h;
		var ch = dojo._getBorderBox(dojo.byId("content_head")).h;
			
		//var alltop_ht = 94;
		var alltop_ht = nh + bh + uh + ch;
		
		//var cfoot_ht = 32;
		var cfoot_ht = dojo._getBorderBox(dojo.byId("content_footer")).h;
		var cfoot_sub;
		// var cfoot_sub = dojo.dom.getFirstChildElement(dojo.byId("content_footer"));
		
		if (cfoot_sub === "undefined") { cfoot_ht = 0; }
		
		var c_excess = alltop_ht + cfoot_ht;
		
		//get element heights
		var bar2_ht = dojo._getBorderBox(dojo.byId("bar2")).h;
		
		
		var sidebar_ht = dojo._getBorderBox(dojo.byId("sidebar")).h;
		//figure sidebar excess
		var sidebar_ex = alltop_ht + excess_ht - uh;
		//add one to the height - if using sidebar border or bar border
		//sidebar_ex++;
		//new huge offset for padding and borders
		sidebar_ex += 5;
		
		//new values based on dynamic layout values
		var cbHeight = (cHeight - c_excess - 1);
		dojo.byId("content_body").style.height = cbHeight + "px";
		var side_av_ht = cHeight - sidebar_ex + ch;
		
		if (!accOpen) {
			dojo.byId("bar2").style.height = side_av_ht + "px";
		} else {
			nbot_ht = Math.round(side_av_ht * side_height_ratio);
			ntop_ht = side_av_ht - nbot_ht;
			currentAcc.style.height = nbot_ht + "px";
			dojo.byId("bar2").style.height = ntop_ht + "px";
		}
		// dojo.byId("content_body").style.display = "none";
		// dojo.byId("content_body").style.visibility = "hidden";
		fixMainContentArea();
		/* setTimeout("", 500);
		dojo.byId("content_body").style.display = "block"; */
		initializingPageHeight = false;
	}
}

function fixMainContentArea() {
	if(dojo.isIE) { // a proprietary solution to a proprietary issue with missing content
		dojo.byId("content_body").style.zoom = ".99";
		dojo.byId("content_body").style.zoom = "1";
	}
}

function resizePageHeight() {
	window.onresize = initalizePageHeight;
}

/* Start temp table striper */
// this function is need to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}

function stripe(id) {
	var even = false;
	var evenCName = arguments[1] ? arguments[1] : "evenRow";
	var oddCName = arguments[2] ? arguments[2] : "oddRow";
	
	var table = document.getElementById(id);
	if (! table) { return; }
	var tbodies = table.getElementsByTagName("tbody");
	for (var h = 0; h < tbodies.length; h++) {
		var trs = tbodies[h].getElementsByTagName("tr");
		for (var i = 0; i < trs.length; i++) {
			var mytr = trs[i];
			if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
				mytr.className = even ? evenCName : oddCName;
			}
			even = ! even;
		}
	}
}
/* End temp table striper */

//this function figures if a subnode of the bar2 list is has a 'display: none' or not
function myblockDisplay(tnode) {
	cdisplay = dojo.getComputedStyle(tnode, "display");
	
	if (cdisplay == "none") {
		return false;
	} else if (tnode.id == "bar2") {
		return true;
	}
	
	pn = tnode.parentNode;
	return myblockDisplay(pn);
}

//this function stripes the bar2 list
//20070322 - unused right now for "performance" issues
function stripe_list(ul) {
	var even = false;
	var evenCName = arguments[1] ? arguments[1] : "evenListRow";
	var oddCName = arguments[2] ? arguments[2] : "oddListRow";
	
	if (! ul) { return; }
	var lis = ul.getElementsByTagName("li");
	for (var h = 0; h < lis.length; h++) {
		var myli = lis[h];
		//dojo.removeClass(myli, evenCName);
		//dojo.removeClass(myli, oddCName);
		
		pdisplay = dojo.getComputedStyle(myli.parentNode, "display");
		//if (pdisplay != "none") {
		if(myblockDisplay(myli)) {
			myli.style.backgroundColor = even ? "#ffffff" : "#f1f9fb";
			//cclass = even ? evenCName : oddCName;
			//dojo.addClass(myli, cclass);
			even =  ! even;
		}
	}
}

//this function toggles the visibility of the accordion divs
function toggleVis(node, hnode) {
	hnode.className = (hnode.className == "") ? "openheader" : "";
	var b2 = dojo.byId("bar2");
	var bot_ht = (node.style.display == "none") ? 0 : dojo._getBorderBox(node).h;
	
	if ((!accOpen) || (node == currentAcc)) {
		var top_ht = dojo._getBorderBox(b2).h;
		var av_ht = top_ht + bot_ht;
		if (bot_ht == 0) {
			bot_ht = Math.round(av_ht * side_height_ratio);
			top_ht = av_ht - bot_ht;
			//decrement for 1px bottom border on h2's
			bot_ht--;
			//if (node != currentAcc) { bot_ht-- };
			node.style.height = bot_ht + "px";
		} else {
			//fix for decrement issue
			av_ht++;
			top_ht = av_ht;
		}
		
		
		//top offset
		//top_ht -= 4;
		top_ht--;
		b2.style.height = top_ht + "px";
		node.style.display = (node.style.display == "none") ? "block" : "none";
		currentAcc = node;
		accOpen = ! accOpen;
	} else {
		var prev_ht = dojo._getBorderBox(currentAcc).h;
		currentAcc.style.display = "none";
		currentAcc_hdr = currentAcc.parentNode.getElementsByTagName("h2")[0];
		currentAcc_hdr.className = "";
		var top_ht = dojo._getBorderBox(b2).h + prev_ht;
		var av_ht = top_ht + bot_ht;
		
		if (bot_ht == 0) {
			bot_ht = Math.round(av_ht * side_height_ratio);
			top_ht = av_ht - bot_ht;
			node.style.height = bot_ht + "px";
		} else {
			top_ht =av_ht;
		}
		
		//top offset
		//top_ht -= 4;
		top_ht--;
		b2.style.height = top_ht + "px";
		node.style.display = (node.style.display == "none") ? "block" : "none";
		currentAcc = node;
	}
	
	//IE 6 will add space unless you force it back
	if(dojo.isIE == 6) {
		initalizePageHeight();
	}
	
}

//this function initializes the sidebar accordion
function naccCreate() {
	var hold = dojo.byId("sidebar");
	var divs = hold.getElementsByTagName("div");
	var flexdivs = new Array();
	var subhds = new Array();
	var subcontainers = new Array();
	var fc_hd, fc_sub;
	
	for (var x = 0; x < divs.length; x++) {
		var mydiv = divs[x];
		if (mydiv.className == "naccContainer_flex") {
			flexdivs.push(mydiv);
		}
	}
	
	for (var x = 0; x < flexdivs.length; x++) {
		var cdiv = flexdivs[x];
		//being lazy here for speed - before you are done write a function to parse for correct id
		var chd = cdiv.getElementsByTagName("h2")[0];
		var csub = cdiv.getElementsByTagName("div")[0];
		
		csub.style.display = "none";
		
		chd.onclick = function () { 
			toggleVis(this.parentNode.getElementsByTagName("div")[0], this);
			return false;
		}
		
		if (x == 0) {
			fc_hd = chd;
			fc_sub = csub;
		}
	}
	
	//when done open the first div
	toggleVis(fc_sub, fc_hd);
	
}

function isSubNode(masternode, subnode) {
	return (subnode.parentNode == masternode);
}

function getFirstChildElementsByTagName(node, tagname) {
	var tags = new Array();
	var tList = node.getElementsByTagName(tagname);
	
	for (var x = 0; x < tList.length; x++) {
		var citem = tList[x];
		if (isSubNode(node, citem)) {tags.push(citem)};
	}
	
	return tags;
}

//this function puts the down and side arrows on lists with sub items
//and sets their initial display to none
function downList(ulH) {
	//var ulH = pnode.getElementsByTagName("ul")[0];
	if (! ulH) { return };
	//console.log("in downList(" + ulH + ")");
	var ul = new Array();
	ul = getFirstChildElementsByTagName(ulH, "li");
	
	for (var x = 0; x < ul.length; x++) {
		li = ul[x];
		var ulN = li.getElementsByTagName("ul")[0];
		
		a = li.getElementsByTagName("a")[0];
		if (ulN) {
			if (li.className !=  "expanded") {
				if (!(li.className == "arrow_d" || li.className == "arrow_r")) {
					li.className = "arrow_r";
					ulN.style.display = "none";
				}
			} else {
				li.className = "arrow_d";
				
				/*if (ulN.firstChild == null) {
					alert("no children!");
				}*/
			}
			
			a.onclick = function() {
				console.log("downList " + this.innerHTML + " clicked");
				var ccli = this.parentNode;
				var ccul = ccli.getElementsByTagName("ul")[0];
				var ulAID = 0;
				if((ccul.style.display == "none") && (ccul.id.substr(0,4) == "aid_")) { // If the ul is closed and has an id that starts with "aid_"
					ulAID = ccul.id.substr(4);
					var genreFilter = "";
					if(ccli.parentNode.parentNode.getElementsByTagName("a")[0].id.substring(0,4) == "gid_") {
						genreFilter = ccli.parentNode.parentNode.getElementsByTagName("a")[0].id.substring(4);
					}
					if(genreFilter) { // If we're in the genre tree and have a genre to filter on
						ccul.innerHTML = dumpArtistAlbums(ulAID,genreFilter);
					} else {
						ccul.innerHTML = dumpArtistAlbums(ulAID);
					}
				}
				ccul.style.display = (ccul.style.display == "none") ? "block" : "none";
				ccli.className = (ccli.className == "arrow_r") ? "arrow_d" : "arrow_r";
				if(dojo.isIE) {
					if (!ccul.firstChild &&(ccli.className == "arrow_d")) {
						//alert("no items open");
						//ccul.style.height = "0";
						ccul.style.display = (ccul.style.display == "none") ? "block" : "none";
					} else if (!ccul.firstChild ) {
						//alert("no items close");
						//ccul.style.height = "";
						ccul.style.display = (ccul.style.display == "none") ? "block" : "none";
					}
				}
				
				//alert("ccul.firstChild.nodeName: " + ccul.firstChild.nodeName);
				//stripe_list(ulP);
				return false;
			}
			downList(ulN);
		}
		else 
		{
			/*
			a.onclick = function() {
				if (gStickylink != "" || gStickylink == null) {
					gStickylink.className = "";
				}
				this.className = "stickylink";
				gStickylink = this;
			}
			
			a.onclick = function() {
				gStickyToggle(this);
			}*/
		}
	}
	
	//
}

function gStickyToggle(node) {
	if (gStickylink != "" || gStickylink == null) {
		// gStickylink.className = "";
		dojo.removeClass(gStickylink, "stickylink");
	}
	// node.className = "stickylink";
	dojo.addClass(node, "stickylink");
	gStickylink = node;
}

//this function initializes the bar2 tree
function ntreeCreate(treeId) {
	var hold = dojo.byId(treeId);
	ulP = hold.getElementsByTagName("ul")[0];
	downList(ulP);
	//stripe_list(ulP);
}

function block() {
	stripe('tracks');
}


