<!--
/* JavaScript Document for all common functions for the KPS site,
	should only contain those functions which are used all the time
	throughout the KPS site.
	Specialist functions for specific sections or pages should be
	separated out into other files.
	
	Created by Simon Wilkinson - 08/2009
*/

/*****************************************/
/**** developer information displayer ****/
/*****************************************/
function dev_display(devlinkObj){
	sqlbox = devlinkObj.parentNode;
	//console.log(sqlbox.style.display);
	//console.log(displayval);
	if(sqlbox.style.overflow == 'hidden'){
		sqlbox.style.overflow = 'visible';
		sqlbox.style.height = 'auto';
		sqlbox.style.width = 'auto';
		sqllink.innerHTML = 'hide developer info';
	}else {
		sqlbox.style.overflow = 'hidden';
		sqlbox.style.height = '12px';
		sqlbox.style.width = '99%';
		sqllink.innerHTML = 'show developer info';
	}
}

/***************************************/
/********* date/time functions *********/
/***************************************/
// comparison local date
var checktime = new Date();

// output time string for header
function showTime(calculate){
	// if clock element exists
	if(document.getElementById('clock')){
		// calculate local time differential between now and last showt function run
		// then add this to server time/date
		if(calculate){
			//current local date
			var nowtime = new Date();
			timediff = ( nowtime.getTime() - checktime.getTime() );
			checktime.setTime( nowtime.getTime() );
			
			today.setMilliseconds(today.getMilliseconds() + timediff);
		}
		
		//get date details
		var h=today.getHours();
		var m=today.getMinutes();
		var s=today.getSeconds();
		var y=today.getYear();
		if (y < 1000)
			y+=1900;
		var d=today.getDay();
		var mth=today.getMonth();
		var dt=today.getDate();
		
		// create word output arrays for month and day names
		var weekday=new Array(7);
		weekday[0]="Sunday";
		weekday[1]="Monday";
		weekday[2]="Tuesday";
		weekday[3]="Wednesday";
		weekday[4]="Thursday";
		weekday[5]="Friday";
		weekday[6]="Saturday";
		
		var month=new Array(12);
		month[0]="January";
		month[1]="February";
		month[2]="March";
		month[3]="April";
		month[4]="May";
		month[5]="June";
		month[6]="July";
		month[7]="August";
		month[8]="September";
		month[9]="October";
		month[10]="November";
		month[11]="December";
		
		// add abbreviation suffix to dates
		function num_abbrev_str(n){
			return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; 
		}
		var date = num_abbrev_str(dt);	
		
		// add the zeros in front of numbers<10
		m=checkTime(m);
		s=checkTime(s);
		dt=checkTime(dt);		
		
		document.getElementById('clock').innerHTML = weekday[d] + " " + month[mth] + " " + date + ", " + y + ". " + h + ":" + m + ":" + s;
		
		// repeat every half second
		t=setTimeout('showTime(true)',500);
	}
}
// small function to add leading zeros to numbers less than ten
function checkTime(i){
	if (i<10){
		i="0" + i;
	}
	return i;
}


/****************************************/
/**** menu opening/closing functions ****/
/****************************************/
// initial variable to hold which menu is currently open
var openmenuObj = null;

// show a menu
function showSubMenu(menuname, depth, openerObj){
	// hide all submenus (of this depth) first
	hideSubMenus(depth, openerObj.parentNode);
	
	// show the required sub menu (change styles)
	showObj = document.getElementById(menuname+'_sub');
	if(showObj){
		showObj.style.display = 'inline';
		if(showObj.className != 'sub_2') showObj.style.marginLeft = (openerObj.offsetWidth / 2)+'px';
		openmenuObj = showObj;
	}
}
// hide all menus of a specific depth
function hideSubMenus(depth, parentObj){
	menuObjList = parentObj.children;
	
	//alert(parentObj.id);
	//console.log(menuObjList);
	
	// iterate through menu children and select all menus at specified depth and hide them
	for(index = 0;index <= menuObjList.length;index++){
		menuObj = menuObjList[index];
		if(typeof(menuObj) == 'object' && menuObj != null){
			//console.log(menuObj.firstChild);
			if(typeof(menuObj.firstChild) == 'object' && menuObj.firstChild != null && menuObj.firstChild.nodeName == "DIV"){
				menuObj.firstChild.style.display = 'none';
			}
		}
	}
	openmenuObj = null;
}
// make sure that when anywhere apart from the submenu, that it is hidden
document.onclick = function(e) {
	if(openmenuObj != null){
		var clicked;
		if (!e)
			var e = window.event;
		
		if (e.target) 
			clicked = e.target;
		else if (e.srcElement) 
			clicked = e.srcElement;
		
		if (clicked.nodeType == 3) // defeat Safari bug
			clicked = clicked.parentNode;
		
		if(clicked != openmenuObj && clicked != openmenuObj.parentNode)
			openmenuObj.style.display = 'none';
	}
	return true;
};

/****************************************/
/******** Log in/out functions **********/
/****************************************/
function conf(loginSession){
	if(!loginSession) return true;
	return confirm("This will log you out of your account. Do you want to continue?");
}

/****************************************/
/**** Custom select list replacement ****/
/****************************************/
function selectReplacement(obj) {
	obj.className += ' replaced';
	var ul = document.createElement('ul');
	ul.className = 'selectReplacement';
	var opts = obj.options;
	var selectedOpt = (!obj.selectedIndex) ? 0 : obj.selectedIndex;
	for (var i=0; i<opts.length; i++) {
		var li = document.createElement('li');
		var txt = document.createTextNode(opts[i].text);
		li.appendChild(txt);
		li.selIndex = i;
		li.selectID = obj.id;
		li.onclick = function() {
			selectMe(this);
		};
		if (i == selectedOpt) {
			li.className = 'selected';
			li.onclick = function() {
				this.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this);
				};
			};
		}
		if (window.attachEvent) {
			li.onmouseover = function() {
				this.className += ' hover';
			};
			li.onmouseout = function() {
				this.className = 
					this.className.replace(new RegExp(" hover\\b"), '');
			};
		}
		ul.appendChild(li);
	}
	obj.onfocus = function() {
		ul.className += ' selectFocused';
	};
	obj.onblur = function() {
		ul.className = 'selectReplacement';
	};
	obj.onchange = function() {
		var idx = this.selectedIndex;
		selectMe(ul.childNodes[idx]);
	};
	obj.onkeypress = obj.onchange;
	obj.parentNode.insertBefore(ul,obj);
}
function selectMe(obj) {
	var lis = obj.parentNode.getElementsByTagName('li');
	for (var i=0; i<lis.length; i++) {
		if (lis[i] != obj) {
			lis[i].className='';
			lis[i].onclick = function() {
				selectMe(this);
			};
	 } else {
			setVal(obj.selectID, obj.selIndex);
			obj.className='selected';
			obj.parentNode.className = 
				obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
			obj.onclick = function() {
				obj.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this);
				};
			};
		}
	}
}
function setVal(objID,val) {
	var obj = document.getElementById(objID);
	obj.selectedIndex = val;
}
function setForm() {
	var s = document.getElementsByTagName('select');
	for (var i=0; i<s.length; i++) {
		selectReplacement(s[i]);
	}
}

var alertObj = null;
var alertDivObj = null;
var alertHeight = null;
var alertTime = null;
function reduceSize(){
	console.log(alertHeight);
	if(alertHeight <= 0){
		//clearTimeout(alertTime);
		alertObj.parentNode.removeChild(alertObj);
	} else {
		alertDivObj.style.height = alertHeight - 2+'px';
		alertHeight = alertHeight - 2;
	}
}
	
function closeAlert(linkObj){
	alertObj = linkObj.parentNode.parentNode;
	alertDivObj = linkObj.parentNode;
	alertHeight = (alertDivObj.offsetHeight - 32);
	alertDivObj.style.overflow = 'hidden';
}

/*****************************************/
/**** AJAX searches - requires jQuery ****/
/*****************************************/
function doSearch(resultbox, searchurl, data, headtext){
	if(typeof(headtext) == "undefined"){
		var headtext = false;
	}
	$("#"+resultbox).slideUp("fast");
	
	if(headtext != false){
		$("#"+resultbox).prev().fadeOut("fast", function(){
			$(this).text(headtext)
				.fadeIn("fast");
		});
	}
	
	$("#"+resultbox).load(searchurl, data, function(){
		$(this).slideDown("fast");
		// make sure that colorbox ajax links are re-initiated
		$(".cb_ajax").colorbox();
	});
}

function searchChange(resultbox, searchurl, data){
	var resheight = Math.floor(($("#"+resultbox+" .result_grid").height() / 2) - 33);
	$("#"+resultbox+" .result_grid").append("<div style=\'text-align:center; width:100px; height: 65px; position:absolute; top: "+resheight+"px; left: 50%; margin-left: -50px; background-color: white; border: 1px solid #999;\'><img src=\'/images/d2/loading_cb.gif\' /><br />Loading...</div>");
	$("#"+resultbox).load(searchurl, data, function(){
		// make sure that colorbox ajax links are re-initiated
		$(".cb_ajax").colorbox();
	});
}

/*********************************************/
/*** all stuff that should happen on load! ***/
/*********************************************/
// this should happen last!
window.onload = function() {
	showTime;
	//(document.all && !window.print) ? null : setForm();
};
-->