var $ = jQuery.noConflict(),
	$window = $(window),
	$document = $(document),
	$body = null; //created in DOM ready func
	

//determine IE version
if($.browser.msie){
	var isIE = true,
		IEVersion = parseInt($.browser.version.replace(/^(\d+).+$/, "$1"));
		
	/*document.write("<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>");*/
}

//Give us console logging/debug messages if it doesn't exist, so debugging doesn't throw errors.
if(typeof console === "undefined"){
	
	(function(){
		var $debug;
		
		window["console"] = {		
			_history:[],
			log:function(msg){
				this._history.push(msg);				
				showMsg(msg);					
			}
		}
		//Make these console functions degrade to log()
		console.debug = console.dir = console.info = console.trace = console.error = console.log;
		//Make these console functions do nothing
		console.group = console.groupEnd = console.groupCollapsed = console.time = console.timeEnd = console.profile = console.profileEnd = function(){}
		//Do message output
		var $debug=null;		
		function showMsg(msg){
			if($debug!==null){
				var newRow = $("<div class='debug-row'>"+msg+"</div>").appendTo($debug);
			}
		}
			
		
		//When DOM is ready...
		$(function(){
			$debug = $("#debug");
			if(!$debug.length){
				//Create the debug div if it does not exist.
				//$debug = $("<div id='debug'></div>").appendTo("body");
			}
			console.isDOMReady = true;
			if(console._history.length>0){
				for(var h=0; h<console._history.length; h++){
					showMsg(console._history[h]);
				}
			}
		});
		
		
	
	})();
	
}


//When DOM is ready...
$document.ready(function(){
	$body = $("body");
	
	//initialize and size tabs
	tsizer.init();
	tsizer.size();
	$window.resize(tsizer.size);
	

	if(IEVersion < 8) {
		$window.resize(pageMinWidth);
		pageMinWidth();
		
		
	}
	
	//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 pageMinWidth(evt){
	var min = 995;
	if($body.width() < min){
		//$body.css({width:980});	
		$("#sitePage,#siteHeader,#siteNav,#siteFooter").css({width:min});
	}else{
		//$body.css({width:""});
		$("#sitePage,#siteHeader,#siteNav,#siteFooter").css({width:""});
	}
	//console.log($body.css("width"));
}


/*
	Convert sidebar boxes into links where they contain a single link
*/
function sideBoxLinks(){
	$("div.side-box:has(a)").click(function(evt){
		window.location = boxLink[0].href;			   
	});
}



/* 
	Tab resizing for liquidity
*/
var tsizer = {
	init:function(){
		tsizer.tabs =  $("li.TabbedPanelsTab");
		tsizer.panelContainer = $("div.TabbedPanels");
		tsizer.tabsOrigWidth = 0;
		//calculate original width of tabs
		tsizer.tabs.each(function(i, t){
			var tab = $(t);
			var tmpWidth = tab.outerWidth();			
			//tab.data({owidth:tmpWidth});
			tsizer.tabsOrigWidth += tmpWidth;
		});
	},
	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 = $(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){
			$(t).height(tabHeight);
		});	*/
		tsizer.tabs.height(tabHeight);
	}
};


