var j = jQuery.noConflict();


j(document).ready(function(){
	//initialize and size tabs
	tsizer.init();
	tsizer.size();
	j(window).resize(tsizer.size);
	
	//size the header title
	sizeHeaderTitle();
	j(window).resize(sizeHeaderTitle);
	
	//turn sidebar boxes into links
	sideBoxLinks();
	
	//preload images
	preloadImages();
});	

/* Preload some images */
function preloadImages(imgList){
	if(imgList && imgList.length >0){
		for(var i=0; i<imgList.length; i++){
			var tmpImg = new Image();
			tmpImg.src = imgList[i];
		}
	}	
}


function sizeHeaderTitle(){
	var globalNav = j("div#globalNavDiv");
	var headerTitle = j("div#HeaderTxtDiv");
	var headerTagline = j("div#taglineDiv");
	
	var newWidth = j(window).width() - (headerTagline.offset().left + headerTagline.width()+20);
	headerTitle.css({width:newWidth});
	
	var topPos = globalNav.offset().top - headerTitle.height()-5;
	headerTitle.css({top:topPos});
}


/*
	Convert sidebar boxes into links where they contain a single link
*/
function sideBoxLinks(){
	j("div.side-box").each(function(i, b){
		var box = j(b);									
		var boxLink = box.find("a");
		//only convert the box if it has one link
		if(boxLink.length==1){
			box.css({cursor:"pointer"});
			box.click(function(evt){
				window.location = boxLink[0].href;			   
  		    });
		}		
	});
}



/* 
	Tab resizing for liquidity
*/
var tsizer = new Object();

tsizer.init=function(){
	tsizer.tabs =  j("li.TabbedPanelsTab");
	tsizer.panelContainer = j("div.TabbedPanels");
	tsizer.tabsOrigWidth = 0;
	//calculate original width of tabs
	tsizer.tabs.each(function(i, t){
		var tab = j(t);
		var tmpWidth = tab.outerWidth();			
		//tab.data({owidth:tmpWidth});
		tsizer.tabsOrigWidth += tmpWidth;
	});
}

tsizer.size=function(){
	var tabWidthMax = tsizer.panelContainer.width();
	
	var tabHeight=0;
	/*console.log("tsizer.tabs: "+tsizer.tabs.length);
	console.log("tsizer.tabsOrigWidth: "+tsizer.tabsOrigWidth);
	console.log("tabWidthMax: "+tabWidthMax);*/
	
	//loop through the tabs
	tsizer.tabs.each(function(i, t){
		  var tab = j(t);
		  //reset dims 
		  tab.css({height:"",width:""});
		  
		  var newWidth = Math.floor(tabWidthMax/tsizer.tabs.length - (tab.outerWidth()-tab.width()));
		  //set their widths, if needed
		  if(tsizer.tabsOrigWidth > tabWidthMax) tab.width(newWidth-5);
		  
		  //get height		  
		  var tmpHeight = tab.height();
		  
		  //get the tallest tab
		  if(tmpHeight>tabHeight) tabHeight=tmpHeight;

	});
	//set the tab heights
	/*tsizer.tabs.each(function(i, t){
		j(t).height(tabHeight);
	});	*/
	tsizer.tabs.height(tabHeight);
}