dojo.require("dojo.parser");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.form.Button");
dojo.require("dojo.fx");
dojo.require("dojox.timing");
dojo.require("dojox.fx.flip");
//These need to be global for setTimer
mainTabs = {};
dojo.addOnLoad(function () {
	/* Make the widgets */
	var tabs = [];
	if(dojo.byId("print-button")) {
		dojo.connect(dojo.byId("print-button"), "onclick", function (evt) { evt.preventDefault(); window.print(); });
	}
	if(dojo.byId("mainTabContainer")) {				 
		//This needs to happen before we start making the widget! Important!
		var randomTab = Math.floor(Math.random()*dojo.query("#mainTabContainer div").length);
		//Turn each of the children of the tab container into a ContentPane
		dojo.query("#mainTabContainer div").forEach(function(childNode){
			tabs.push(new dijit.layout.ContentPane({
				// just pass a title: attribute, this, we're stealing from the node
				title: dojo.attr(childNode,"title")
			}, childNode));
		});
		
		//Provide a little hack to get the tab box to 
		//left align in IE, as opposed to magically growing a 300px 
		//left margin
		extraTabStyle = "height: 220px;";
		if(dojo.isIE) {
			extraTabStyle=";float: left; width: 100%; height: 220px;";
		}
		
		//Create the tab container, which will scarf up those content panes
		mainTabs = new dijit.layout.TabContainer({
			style:dojo.attr("mainTabContainer", "style")+extraTabStyle
		},"mainTabContainer");
		mainTabs.startup();
	
		//Hook up an event to tab clicks to get the tab ID into the clicking
		dojo.query(".dijitTab").onclick(function(evt) { 
			//we either got tabLabel (which has a useful ID), or its parent and need 
			//to traverse to get the tabLabel
			var appendableId = dojo.hasAttr(evt.target, "id") ? evt.target.id : dojo.query(".tabLabel", evt.target)[0].id;
			window.location = String(window.location).split('#')[0]+"#"+ appendableId;										 
		});
		var tabToSelect= randomTab;
	
		
		//select a random tab on page load
		/* Begin setup for tab box access by anchor */
		//Find the anchors attached to the URL
		var curLoc = String(window.location);
		var anchors = curLoc.split('#');
		
		if(anchors.length > 1) {
			var section_name = anchors[1];
			//Build selectors for the content and panel
			var lastChar = section_name.charAt(section_name.length-1);
			//If the last character of the anchor was a number, select that tab
			var maybeNumber = parseInt(lastChar);
	
			if(maybeNumber != NaN) {
				tabToSelect = maybeNumber;
			}
	
		}
		mainTabs.selectChild(tabs[tabToSelect]);
	}
	
	/*Setup the random facts*/
	var randomFacts = [
	{heading: "Did you know?" , body: "Seven pounds of plastic waste has the energy equivalent of a gallon of JP-8 jet fuel."},
	{heading: "Did you know?", body: "More than 70% of military trucks in convoys are carrying fuel supplies."}
	];
	
	var cycleNumber = 0;
	var randomFactTimer = new dojox.timing.Timer(8000);
	randomFactTimer.onTick = function () {
		cycleNumber = (cycleNumber + 1) % randomFacts.length;
		dojo.fadeOut({
			node: "random_fact",
			onEnd: function () {
				dojo.fadeIn({node: "random_fact", delay: 300}).play();
				dojo.query("#random_fact h3")[0].innerHTML = randomFacts[cycleNumber]["heading"];				dojo.query("#random_fact p")[0].innerHTML = randomFacts[cycleNumber]["body"];
				}
			}).play();
	}
	
	
	//Disabled to remove random facts entirely
	if(dojo.byId("random_fact") != null) {
		randomFactTimer.start();
		dojo.connect(dojo.byId("random_fact"), "onmouseover", function () { 
					randomFactTimer.stop();
		});
		dojo.connect(dojo.byId("random_fact"), "onmouseout", function () { 
					randomFactTimer.start();
		});
	}
	
		
 	

});

