// JavaScript Document


//initialize the lightview (popup window)
document.observe('lightview:loaded', lightViewLoaded);

function lightViewLoaded() {
	var lvLoaded = true;	
	if(document.URL.include('index.html') || !(document.URL.include('.html'))) {
		if(!(document.URL.include('.asp'))) {
		 	loadHomepage();
		}
	 }
}

//Show the homepage lightview box
function loadHomepage () {
	Lightview.show({
      href: '../home.html',
      rel: 'iframe',
	  radius: 1,
      options: {
      	topclose: true,
      	width: 712,
	    height: 685
      }
  });
}

document.observe("dom:loaded", function() {
	 activateDropdowns();
});

function activateDropdowns () {
	$$(".nav li").each(function(node){

		// if there’s a ul
		var ul = $A(node.getElementsByTagName("ul")).first();
		if(ul != null){

				// toggle it’s visibility on these events
				node.onmouseover = node.onmouseout = function(){
						Element.toggle(ul);
				}
		}
    });
}

function closeHomepage () {
	Lightview.hide();
}

function activateTab (obj) {
	obj.observe('click', respondToClick);
}

function respondToClick(event) {
  var element = Event.element(event);
  element.addClassName('selected');
}

function gotoPage(form){
    var url = form.issues.options[form.issues.selectedIndex].value;
    if (url != "") {
        location.href = url;
    }
}

function loadPage(url) {
	if (url != "") {
		location.href = url;
	}
}

//display charts from the selected issue on the page
function displayCharts(form) {
	var url = "xml/charts.xml";
	var iss = form.charts.options[form.charts.selectedIndex].value;
	var imgString = "<div id='chartDisplay'>";

	//load the charts xml document
  	new Ajax.Request (url, {  
      method: 'get',  
      onSuccess: function (request) {
		var xmlObj = request.responseXML; 
		var charts = getElementsByAttribute("issue", iss, xmlObj);
		for(var i=0; i<charts.length; i++) {
		  imgString += "<img src='" + charts[i].getAttribute("url") + "' alt='chart'/>";
		}
		imgString += "</div>";
		$('chartDisplay').replace(imgString);
	  },
	  onFailure: function(){ alert('Error: could not load the charts xml file')},  
      onException: function(){ alert('There was an error loading or parsing the charts xmlfile')} 
	});  
}
	

//return elements with a specific attribute, or only if that attribute matches a certain value or tagName
function getElementsByAttribute(the_attribute, the_value, the_node){
	var node_tags = the_node.getElementsByTagName('*');
	var results = [];
	for(var i=0;i<node_tags.length;i++){
		if(node_tags[i].getAttribute(the_attribute)){
			if(node_tags[i].getAttribute(the_attribute) == the_value){
				results.push(node_tags[i]);
			}
		}
	}
	return results;
}
