$(document).ready(function() {
	setLevel2();
	/* Highslide */
	//hs.registerOverlay({ overlayId:'closebutton', position:'top right', fade:2 });
	//$('.accordion').accordion();
	//$(".accordion #open").trigger('click');
	sfHover();
	ipGrab(); // initiate IP check (ipgrab.js.php), test and write Intranet link
	initNonEmptyTextHandlers();
	initSearchForm();
});

/* ----------------------
   Scripts
---------------------- */

function sfHover() {
	if (document.all&&document.getElementById) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" over";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" over\\b"), "");
			}
		}
	}
}

/* Popup:
popup(URL, Width, Height, Left, Top, AutoCenter[0=False, 1=True], FullScreen[0=False, 1=True])
<a href="javascript:popUp('domain', 640, 480, 50, 50, 1, 0)">Popup!</a>
*/
function popup(URL, popWidth, popHeight, popLeft, popTop, autoCenter, fullScreen) {
	day = new Date();
	id = day.getTime();
	if ( autoCenter == 1 ) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	if ( fullScreen == 1 ) {
		// Open in Full Screen window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+(screen.width-10)+",height="+(screen.height-26)+",left=0,top=0');");
	} else {
		// Open in normal window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
	}
}


/* Auto Highlight Main Menu */
function extractLevelName(hrefString) {
	var arr = hrefString.split('/');
	arr = arr[arr.length-2]+'/'+arr[arr.length-1];
	return arr.toLowerCase();	
}

function setActiveLevel2Menu(arr, crtPage) {
	for( var i=0; i<arr.length; i++ ) {
		if(extractLevelName(arr[i].href) == crtPage) {
			if ( arr[i].className == "" ) {
				arr[i].className = "active";
			}
		}
	}
}

function setLevel2() {
	if( document.location.href ) {
		hrefString = document.location.href;
	} else {
		hrefString = document.location;
	}
	if ( document.getElementById("subnav") != null )  {
		setActiveLevel2Menu(document.getElementById("subnav").getElementsByTagName("a"), extractLevelName(hrefString));
	}
}

function initNonEmptyTextHandlers(){
	$(".non-empty").each(function(){
		this.defaultValue = this.value;
		$(this).addClass("non-empty-empty");
		$(this).focus(function(){
			if(this.value == this.defaultValue){
				this.value = "";
				$(this).removeClass("non-empty-empty");
			}
		});
		$(this).blur(function(){
			if(!this.value.length){
				$(this).addClass("non-empty-empty");
				this.value = this.defaultValue;
			}
		});
	});
}

function initSearchForm(){
	
	var searchForm = document.getElementById("search-form");
	var searchFieldset = document.getElementById("search-fieldset");
	var searchFieldsetClose = document.getElementById("search-fieldset-close");
	var queryInput = document.getElementById("search-query-input");
	var radioSiteSearch = document.getElementById("site-search");
	var radioCourseSearch = document.getElementById("course-search");
	
	radioSiteSearch.onclick = function(){
		searchForm.action = "search/";
		queryInput.name = "m_query";
	}
	
	radioCourseSearch.onclick = function(){
		searchForm.action = "http://www.leeds.ac.uk/site/custom_scripts/course_index.php";
		queryInput.name = "query";
	}
	
	searchFieldsetClose.onclick = function(){
		$(searchFieldset).fadeOut("fast");
	}
	
	$(queryInput).focus(function(){
		$(searchFieldset).fadeIn("fast");
	});
}

function expandThumbnail(anchor){
	
	
	
	return false;
}
