var docWidth = "";
var docHeight = "";
var curfly = "";

var timer = {
	timerID : 0,
	tStart : null,
	updateTimer : function() {
		if(timer.timerID) {
		  clearTimeout(timer.timerID);
		}
		
		if(!timer.tStart)
			timer.tStart   = new Date();
		
		var   tDate = new Date();
		var   tDiff = tDate.getTime() - timer.tStart.getTime();
		tDate.setTime(tDiff);
		if(tDate.getSeconds()>0.75) {
			menuCleanup();
			timer.Stop();
			timer.Reset();
		}
		
		timer.timerID = setTimeout("timer.updateTimer()", 100);
	},
	Start: function() {
		timer.tStart   = new Date();
		timer.timerID  = setTimeout("timer.updateTimer()", 100);
	},
	Stop: function() {
		if(timer.timerID) {
		  clearTimeout(timer.timerID);
		  timer.timerID  = 0;
		}
   		timer.tStart = null;
   
	},
	Reset: function() {
		timer.tStart = null;
	}
}

$(function(){
	// Document is ready
	document.onclick = function() {
		menuCleanup();	
	}
	$("#tabs li").each(function(i){
		this.onmouseover = function() {
			if((curfly=="")||(curfly!=this.id)) {
				tabMouse(this,"over");
				timer.Stop();
				timer.Reset();
			}
		}
	});

	$(".flyout li").not(".nohover").each(function(i) {
		this.onmouseover = function() {
			$(this).addClass("hover");
			timer.Stop();
		}
		this.onmouseout = function() {
			$(this).removeClass("hover");
			timer.Reset();
			timer.Start();
		}
		this.onclick = function() {
			location.href=$(this).children("a").attr("href");
			$(".flyout").remove();
		}
	});
	$("#masthead h1").each(function() {
		this.onclick = function() {
			location.href="/index.php?id=home";
		}						
	});
	repositionTabs();
	docWidth = $(document).width();
	docHeight = $(document).height();
	if( $("#feature").text()=="") {
		$("#feature").remove();
	}
	techClassrooms();
	if($.browser.safari) {
		var ran_number = Math.floor(Math.random()*7);
		$('#flash').html("<img src=\"/fileadmin/media/banners/safaribanner"+ran_number+".jpg\" alt=\"Our Mission: To provide comprehensive multimedia and instructional support services that promote MSU's commitment to advancing knowledge and transforming lives.\" />");
		
	} else {
		$('#flashcontent').flash(
			{ 
				src: '/fileadmin/templates/images/BANNERanimation.swf',
				width: 762,
				height: 232,
				wmode: "transparent",
				flashvars: { path: '/fileadmin/media/' }
			}
			,{ version: '8' }
		);
	}
	$("img.centered").each(function(i) {
		//console.log("centered image");
		$(this).after("<div align='center' id='tmpDiv'><div>");
		$("#tmpDiv").attr("id","").prepend(this);
	});
});

function tabMouse(tab,mode) {
	if((curfly!="")&&(curfly!=tab.id)) {
		menuCleanup();
	}
	if(mode=="over") {
		$(tab).addClass("active_tab");
		showMenu(tab);
		curfly = tab.id;
	} else {
		menuCleanup();
	}
	showMenu(tab);
}

function menuCleanup() {
	if(curfly!="") {
		curfly = "";
		$(".active_tab").removeClass("active_tab");
		$(".flyout_holder").hide();
	}
}

function showMenu(tab) {
	var tabname = tab.id.replace("tab_","nav_");
	$("#"+tabname).show();
	// reposition the tabs if the page has been resized
	if(docWidth!=$(document).width()) {
		docWidth = $(document).width();
		docHeight = $(document).height();
		repositionTabs();
	} else if(docHeight!=$(document).height()) {
		docWidth = $(document).width();
		docHeight = $(document).height();
		repositionTabs();
	}
	if(tabname=="nav_creative") {
		var av_height = $("#nav_av").height();
		av_height = av_height - (av_height*2);
		$("#nav_creative ul#nav_image li.reset").css("margin-top",av_height-9); 
	}
}

function positionNav(str,bumpLeft) {
	var navOffset = $("#tab_"+str+" a").offset();
	var tabHeight = $("#tab_"+str+" a").height();
	var verticalOffset = tabHeight - 9;
	if(str=="creative") {
		verticalOffset = verticalOffset + 5;
	}
	$("#nav_"+str).css({ left: navOffset.left-bumpLeft, top: navOffset.top + verticalOffset });
}

function repositionTabs() {
	positionNav("classroom",8);
	positionNav("technical",12);
	positionNav("creative",150);
	positionNav("ip",12);
	positionNav("forms",23);
}

/* Text re-sizing */
function init()  {
	var iBase = TextResizeDetector.addEventListener(onFontResize,null);
}
function onFontResize(e,args) {
	repositionTabs();
}
TextResizeDetector.TARGET_ELEMENT_ID = 'dex_'; //id of element to check for and insert control
TextResizeDetector.USER_INIT_FUNC = init; //function to call once TextResizeDetector has init'd

isInteger = function(s) {
	return !isNaN( parseInt( s ) );
}

function techClassrooms() {
	var theBuildings = document.getElementById("building");
	if(theBuildings) {
		var tmpBuildingNum = 0;
		var num_moved = 0;
		var num_options = theBuildings.length;
		for(i=0;i<num_options;i++) {
			if(	isInteger(theBuildings.options[i-num_moved].value) ) {
				theSelect = document.getElementById("building"+tmpBuildingNum);
				if(theSelect) {
					theSelect.appendChild(theBuildings.options[i-num_moved]);
					num_moved++;
				}
			} else {
				tmpBuildingNum++;
				newSelect = document.createElement("select");
				newSelect.id = "building"+tmpBuildingNum;
				newSelect.className = "hidden";
				document.body.appendChild(newSelect);
			}
		}
		doClassrooms(1);
	}	
}
function doClassrooms(buildingNum) {
		$("#classroom").empty();
		if(document.getElementById("building"+buildingNum)) {
			$("#building"+buildingNum).clone().appendTo("#classroom");
		} else {
			$("#classroom").append("<option value='0'>No Classrooms</option>");
		}
}
function goTo(id) {
	if(id>0) {
		location.href = "/index.php?id="+id;
	}
}
function missingPic(img,url) {
	img.src = url;		
}