/* =====================================================================
 * scripts for /community/
 * Created: 	2008-07-24 	Gabriel Mcgovern
 * Modified:	
 * ===================================================================== */


/* =====================================================================
 * functions for term based tab interface
 * Requires following schema:
 * <div id="tabs" class="worldview">
 *   <div class="indexlist" id="Term200804">
 *     <h3 class="term">Fall 2008</h3>
 *	   ...
 *	 </div>
 * </div>  
 * ===================================================================== */

$(document).ready(function() { 
		//alert("asd");
		var termNum = $("div.indexlist").size(); 							// find number of available terms
		var termList = '';
		for(var i=0; i<termNum; i++){										// build link text
			var termThis = $("div.indexlist:eq("+i+") h3").text(); 			// get  h3 text 
			var termCode = $("div.indexlist:eq("+i+")").attr("id");			// get indexlist id
			$("div.indexlist:eq("+i+") h3").remove();						// remove h3
			termList+= '<li><a href="#'+termCode+'" onclick="return updateHash(\''+termCode+'\');">'+termThis+'</a></li>';
		}
		$("#tabs").prepend('<ul id="tab-list">'+termList+'</ul>');			// insert list
		
		$.getScript('/_source/scripts/jquery.hashchange.js', function(){        // require hashchange
			$(window).hashchange( function(){
				var hash = location.hash;
				if( hash.substr(1,4) == "tab-" ){								// if hash "tab-term", use that tab
					showTerm( hash.substr(5));
				}
				else{
					showTerm($("div.indexlist:eq(0)").attr("id"), false );		   // or, use first
				}
			});
	
			$(window).hashchange();
		});	
});


function showTerm(termCode){
	$("div.indexlist:not(#"+termCode+")").hide();						// animate
	$("div.indexlist#"+termCode).fadeIn("slow");
	$("#tab-list  li").removeClass("current"); 							// set current state
	$("#tab-list li:has(a[href$="+termCode+"])").addClass("current");   // href=#"+termCode+" seems to bork in ie. Selector bug? Special char?
	return false;	
}	

function updateHash(termCode){
		// This will trigger hashchange
	 	window.location.hash = "tab-"+termCode; 							// update url
		return false;	
}
