
// FIND ALL FOLDERS, SET LINKS ON ANCHORS IN TABS
function initFolderLinks() {
	// init defdiv
	defdiv = new Array();
	
	viewing= document.location.hash.substr(1);
	//msg("viewing: " + viewing, 1);

	if (!document.getElementsByTagName) return null;
	var folders = getElementsByClass("folder");
	for (var j=0; j < folders.length ; j++ ) {  // for each folder
		def=false;
		var anchors = folders[j].getElementsByTagName("a");
		for(var i=0; i < anchors.length; i++) { // for each anchor in folder
			var a = anchors[i];
			var href = a.href;
			var id = a.id;
								
			if ((href.indexOf("#") != -1) && (href.indexOf("nav") != -1)) { // jump ref
				var index = href.indexOf("#") + 1;
				var t = href.substr(index);  // t is the sheet name
				
				// look for default tab specified by style
				if (a.className.indexOf("defTab")>=0 || t==viewing) {
					defdiv.push(t);
					def = true;
				} 
				
				
				// now set the href and title, preserve original string in rel
				href = "javascript:showSheet('" + t + "','" + folders[j].id + "');doPlayer('" + t + "');";
				a.setAttribute("href",href);  // set the javascript for the link
				a.setAttribute("rel",t); // store the sheet name in the rel attribute for later
				a.setAttribute("id","tab" + t); // set the id of the tab anchor
				a.setAttribute("title","View the " + t.substr(3) + " sheet in this group"); // helpful title
			} //endif jumpref
		} // end anchors in folder
		if (!def) { // if no defaults were specified for this folder, use the first tab
			defdiv.push(anchors[0].rel);
		}
	
	
	}// end folderloop
	
	
// collapse defdivs
defdiv = defdiv.toString();
//msg("defdiv: " + defdiv,1);
return defdiv;
};


// SHOWS A SHEET, HIDES OTHERS IN ITS PARENT FOLDER
function showSheet(sheetID,folderID) { 
	if (!document.getElementById) return null;
	d = $(sheetID);
	if (d.style.display == "none") {
		d.style.display = "block";
	}
	hideSheets(sheetID,folderID); // hide with our target exempt, and its parent folder as the node
};


function hideSheets(exemptID,folderID) {
	if (!document.getElementsByTagName) return null;
	node=$(folderID);
	if (!exemptID) exemptID = "";
	var divs = getElementsByClass("sheet",node);
	for(var i=0; i < divs.length; i++) {
		var div = divs[i];
		var id = div.id;
		if ((id.length>1) && (id.indexOf("nav")>=0) && (exemptID.indexOf(id)<0)) {
			// hide the sheets
			div.style.display = "none";
			
			// check for audio player and delete node if it exists
			playerdiv = $(id + 'Player');
			if(playerdiv) playerdiv.parentNode.removeChild(playerdiv);
		}
	}
	if (divs.length) clearOtherTabs(exemptID,folderID); // only hide sheets if we found any
};

function clearOtherTabs(exemptID,folderID) {
	tabs = getTabs(folderID);
    for (j=0 ; j<tabs.length; j++ ) {
		tab = tabs[j];
		// clear all tabs in the folder(s), if it's not our tab
		if (tab.href.indexOf(exemptID)<0) clearTab(tab.id);
		// find the tabs in our folder not already hilited, and hilite
		if ((exemptID.indexOf(tab.rel)>=0) && (tab.className.indexOf('ontab')<0)) hilite(tab.id);
    }
    clearTab(getElementsByClass("showall",$(folderID))[0]); // clear showall tab
};

function getTabs(folderID) {
	var tabs = new Array();
	// if no node specified, use whole doc
	folderID ? node=$(folderID) : node=document;
	var anchors = node.getElementsByTagName("a");
	for(var i=0; i < anchors.length; i++) {
		if (anchors[i].id.indexOf("tab") == 0) tabs.push(anchors[i]);
	}
	return tabs;
};

function clearTab(tabID) {
	t=$(tabID);
	if(t)t.className = t.className.replace('ontab','');
};

function hilite(tabID) {
	$(tabID).className += " ontab";
};

 // SHOW ALL SHEETS IN A FOLDER
function showFolder(folderID) {
 	node=$(folderID);
 	// GET THE SHEETS
 	var divs = getElementsByClass("sheet",node);
	for(var i=0; i < divs.length; i++) {
		var div = divs[i];
		var id = div.id;
		// MAKE ALL SHEETS VISIBLE
		if ((id.length>1) && (id.indexOf("nav")>=0)) {
			div.style.display = "block";
		}
	}
	// GET TABS
	var tabs = getTabs(folderID);
	for (j=0 ; j<tabs.length; j++ ) {
		tab = tabs[j];
		// clear all tabs in the folder(s)
		clearTab(tab.id);
    }
    // hilite the show all tab (reqires "showall" class)
	hilite(getElementsByClass("showall",node)[0]);
};


// ---------------------- AUDIO PLAYER WRITE

function doPlayer(div) {
	
	xpath='/audio/' + div + '.xspf'; // set playlist location
	
	
	switch(div) {
		case 'navTwoTea':
			ptitle = "Two for Tea";
			break;
		case 'navTartufo':
			ptitle = "Tartufo";
			break;
		case 'navTrio':
			ptitle = "Folie a Trois";
			break;
		case 'navQuartino':
			ptitle = "Quartino";
			break;
		case 'navFour':
			ptitle = "Quartet illy";
			break;
		case 'navSolaria':
			ptitle = "Solaria";
			break;
	}
	
	ptitle += " Sample Tracks - Click to Start";
	
	$(div).innerHTML += '<div id="' + div + 'Player"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="400" height="15" id="xspf_player" align="left"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/xspf_player_slim.swf?playlist_url=' + xpath + '&player_title=' + ptitle + '" /><param name="quality" value="high" /><param name="bgcolor" value="#e6e6e6" /><embed src="/xspf_player_slim.swf?playlist_url=' + xpath + '&player_title=' + ptitle + '" quality="high" bgcolor="#e6e6e6" width="400" height="15" name="xspf_player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object></div>';

	
	
};






// ---------------------- GENERAL FUNCS


// GENERIC SHOW/HIDE FUNCTION
function showhide(what) {
  if (!document.getElementById) return null;
  showWhat = document.getElementById(what);
  if (showWhat.style.display == "none") {
  	showWhat.style.display = "block";
  } else {
  	showWhat.style.display = "none";
  }
};

// SHOWS ALL DIVS WITH "nav" IN THE ID
function showall() {
	var divs = document.getElementsByTagName("div");
  for(var i=0; i < divs.length; i++)
  {
    var div = divs[i];
    var id = div.id;
    if (id.indexOf("nav")>=0)
    {
      div.style.display = "block";
    }
  }
 };

// CALL THE INIT FUNCTIONS

tabinit = function() {
	hideSheets(initFolderLinks());
};

addLoadEvent(tabinit);

