//
// Carnegie Mellon Univerity, Human Computer Interaction Institute
// Copyright 2005
// All Rights Reserved
//
// Author: Benjamin Billings
// Version: $Revision: 1.21 $
// Last modified by: $Author: bleber $
// Last modified on: $Date: 2010/01/07 16:15:59 $
// $KeyWordsOff: $
//
//general tab switcher
 function showAvailableDatasets(event){
	 var myDataText = "My Datasets";
	 var publicDataText = "Public Datasets";
	 var availDataText = "Other Datasets";
	 var advancedDataText = "Advanced";
	 var adminDataText = "Admin";
	 var helpDataText = "Help";

	 if (getTextContent(this) == myDataText) {
	 	 myDataSets = document.getElementById('my-data-sets');
	 	 if (myDataSets!=null) {
	 	 	myDataSets.style.display = "block";
	 	 }
		 document.getElementById('public-data-sets').style.display = "none";
		 document.getElementById('available-data-sets').style.display = "none";

	 } else if (getTextContent(this) == publicDataText) {
	 	 myDataSets = document.getElementById('my-data-sets');
	 	 if (myDataSets!=null) {
		 	myDataSets.style.display = "none";
		 }
		 document.getElementById('public-data-sets').style.display = "block";
		 document.getElementById('available-data-sets').style.display = "none";

	 } else if (getTextContent(this) == availDataText) {
	 	 myDataSets = document.getElementById('my-data-sets');
	 	 if (myDataSets!=null) {
		 	myDataSets.style.display = "none";
		 }
		 document.getElementById('public-data-sets').style.display = "none";
		 document.getElementById('available-data-sets').style.display = "block";

	 } else if (getTextContent(this) == helpDataText) {
	 	 myDataSets = document.getElementById('my-data-sets');
	 	 if (myDataSets != null) {
		 	myDataSets.style.display = "none";
		 }
		 document.getElementById('public-data-sets').style.display = "none";
		 document.getElementById('available-data-sets').style.display = "none";
	 } else if (getTextContent(this) == advancedDataText) {
	 	// do nothing
	 } else if (getTextContent(this) == adminDataText) {
	 	// do nothing
	 } else {
		alert("Tab not recognized by JavaScript.");	 
	 }

	 var primaryNav = document.getElementById("primarynav");
	 var selectedTab = primaryNav.getElementsByTagName("span")[0];
	 //replace <span> with new <a>; keep text the same
	 if (document.getElementById && document.createElement) {
		 a_new = document.createElement("a");
		 a_new.onclick = showAvailableDatasets;
		 //a_new.style.cursor = "pointer";
		 a_new.setAttribute("href","#")
		 theText = getTextContent(selectedTab);
		 aText = document.createTextNode(theText);
		 aTextId = theText.replace(/\s+/,'');
		 a_new.setAttribute("id",aTextId);
		 a_new.appendChild(aText);
		 var parent = selectedTab.parentNode;
		 parent.replaceChild(a_new, selectedTab);
		 //replace <a> with <span>; keep text the same
		 span_new = document.createElement("span");
		 var sText = document.createTextNode(getTextContent(this));
		 span_new.appendChild(sText);
		 this.parentNode.replaceChild(span_new, this);
	 }
 }
 
function initDatasetSelection() {
	 var primaryNav = document.getElementById("primarynav");
	 if (primaryNav!=null) {
		 var linkArray = primaryNav.getElementsByTagName("a");
		 for( i = 0; i < linkArray.length; i++) {
		     if (linkArray[i].innerHTML != 'Advanced' && linkArray[i].innerHTML != 'Admin') {
		         linkArray[i].onclick = showAvailableDatasets;
		     }
		 }
	 }
	//init the dataset selector dropdown
	var theSelect = document.getElementById("dsSelectorSelect");
	if (theSelect!=null) {
		theSelect.onchange = selectChanged.bindAsEventListener(theSelect);
	}
 }

//Handles selection changes on the Dataset Selector combo box
function selectChanged(theElement) {
    var theSelect;
    theSelect = this;

    //Build the form.
    var newForm = document.createElement('FORM');
    newForm.setAttribute('name', 'ds_select_form');
    newForm.setAttribute('id', 'ds_select_form');
    newForm.setAttribute('form', 'text/plain');
    newForm.setAttribute('method', 'POST');

    var newInput = document.createElement('input');
    newInput.name="datasetId";
    newInput.type="hidden";
    newInput.value="?datasetId=" + theSelect.value;
    newForm.appendChild(newInput);
    document.getElementsByTagName('body').item(0).appendChild(newForm);
    
    if (theSelect.value == "Other..." || theSelect.value == "") {
        location.replace("index.jsp");
    } else {
        currentURL = window.location.href;
        // if we are at index.jsp,  login or help, change URL to go to DatasetInfo
        if ((currentURL.search("index.jsp") != -1)
                || (currentURL.search("login") != -1) 
                || (currentURL.search("WebServicesCredentials") != -1) 
                || (currentURL.search("LoggingActivity") != -1) 
                || (currentURL.search("help") != -1) ) {
            newURL = currentURL.substring(0, currentURL.lastIndexOf("/")+1);
            newURL += "DatasetInfo?datasetId=" + theSelect.value;
            newForm.setAttribute('action', newURL);
            newForm.submit();
        // otherwise, go to the same report with the new dataset
        } else {
            lastSlashPos = currentURL.lastIndexOf("/")+1;
            questionPos = currentURL.indexOf("?");
            if (questionPos == -1) {
                questionPos = currentURL.length;
            }
            newURL = currentURL.substring(0, currentURL.lastIndexOf("/")+1);
            newURL += currentURL.substring(lastSlashPos,questionPos);
            newURL += "?datasetId=" + theSelect.value;
            newForm.setAttribute('action', newURL);
            newForm.submit();
        }
    }
    return true;
}

//We don't use the next three functions currently
function selectClicked() {
	this.changed = true;
}

function selectFocussed() {
	this.initValue = this.value;
	return true;
}

function selectKeyed(e) {
	var theEvent;
	var keyCodeTab = "9";
	var keyCodeEnter = "13";
	var keyCodeEsc = "27";
	
	if (e) {
		theEvent = e;
	} else {
		theEvent = event;
	}

	if ((theEvent.keyCode == keyCodeEnter || theEvent.keyCode == keyCodeTab) && this.value != this.initValue) {
		this.changed = true;
		selectChanged(this);
	} else if (theEvent.keyCode == keyCodeEsc) {
		this.value = this.initValue;
	} else {
		this.changed = false;
	}

	return true;
}

//Returns textcontent. should work in any W3C DOM compliant browser
function getTextContent(node) {
	if (node.nodeType == 3) {
		return node.nodeValue;
		} // text node
	if (node.nodeType == 1) { // element node
		var text = [];
		for (var chld = node.firstChild; chld; chld=chld.nextSibling) {
			text.push(getTextContent(chld));
		} return text.join("");
	} return ""; // some other node, won't contain text nodes
}
onloadObserver.addListener(initDatasetSelection);
