/********** constants ************/
var _NOSCROLL_POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,resizable=no,scrollbars=no,top=100,left=100,screenX=100,screenY=100';
var _SCROLL_POPUP_FEATURES = 'location=0,statusbar=1,menubar=0,resizable=yes,scrollbars=yes,top=100,left=100,screenX=100,screenY=100';

/********** neatly degrading popup code, props to AListApart ********/
function raw_popup(url, target, can_scroll, w_width, w_height) {
  // define base set depending on whether the window can scroll or not
  var features = _SCROLL_POPUP_FEATURES;
  if (can_scroll==false) features = _NOSCROLL_POPUP_FEATURES;
  // define target attr
  if (typeof(target)=='undefined') target = '_blank';
  // add width and height to features
  if (typeof(w_width)=='undefined') w_width = 400;
  if (typeof(w_height)=='undefined') w_height = 300;
  features += ',width='+w_width+',height='+w_height;
  // call the window
  var theWindow = window.open(url, target, features);
  // setTimeout('theWindow.focus();',250);
  return theWindow;
}
function link_popup(src, can_scroll, w_width, w_height) {
  return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', can_scroll, w_width, w_height);
}
if (!window.console) console = {log: function() {}};

var DomLoaded =
{
	onload: [],
	loaded: function()
	{
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		for (i = 0;i < DomLoaded.onload.length;i++) DomLoaded.onload[i]();
	},
	load: function(fireThis)
	{
		this.onload.push(fireThis);
		if (document.addEventListener) 
			document.addEventListener("DOMContentLoaded", DomLoaded.loaded, null);
		if (/KHTML|WebKit/i.test(navigator.userAgent))
		{
			var _timer = setInterval(function()
			{
				if (/loaded|complete/.test(document.readyState))
				{
					clearInterval(_timer);
					delete _timer;
					DomLoaded.loaded();
				}
			}, 10);
		}
		/*@cc_on @*/
		/*@if (@_win32)
		var proto = "src='javascript:void(0)'";
		if (location.protocol == "https:") proto = "src=//0";
		document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
		    if (this.readyState == "complete") {
		        DomLoaded.loaded();
		    }
		};
		/*@end @*/
	   window.onload = DomLoaded.loaded;
	}
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function initRollovers() 
{
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		} else if (aImages[i].className == 'imgon') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);
			
			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			
			sTempSrc = aImages[i].getAttribute('src');
			aImages[i].setAttribute('src', aImages[i].getAttribute('hsrc'));			
			
		}
	}
	
}

function link_popup(src, can_scroll, w_width, w_height) {
  return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', can_scroll, w_width, w_height);
}
function img_popup(src, can_scroll, w_width, w_height) {
  return raw_popup("image.php?i="+escape(src.getAttribute('href')), src.getAttribute('target') || '_blank', can_scroll, w_width, w_height);
}

function toggleDiv( idName )
{
	var el = document.getElementById(idName);
	if (!el) return false;
	
	// default checking is none since the AUTO value is block.
	if (el.style.display=='none') el.style.display = 'block';
	else el.style.display = 'none';
}

function confirmAction( checkMessage, theURL ) { if (confirm(checkMessage)) window.location = theURL; }

function imposeMaxLength(obj, maxLength) {
	if (obj.value.length>maxLength) obj.value = obj.value.substring(0,maxLength);
}

function checkOtherField( el, fname )
{
	// get the selected value
	var sv = el.options[el.selectedIndex].value;
	
	if (sv=='@@other') {
		$('new_'+fname).style.visibility = 'visible';
		$('new_'+fname).focus();
	}
	else {
		$('new_'+fname).style.visibility = 'hidden';
	}
}

function subsetAdd( el )
{
	$('subset_right').innerHTML += '<option value="'+el.value+'" ondblclick="subsetDel(this);">'+el.innerHTML+'</option>';
	subsetRecalc();
}

function subsetDel( el )
{
	el.parentNode.removeChild( el );
	subsetRecalc();
}

function subsetRecalc()
{
	var opts = $('subset_right').getElementsByTagName('option');
	var css = new Array();
	for (var i in opts) {
		if (typeof(opts[i])=='undefined' || typeof(opts[i])!='object') continue;
		css.push(opts[i].value);
	}
	$('saved_subset_val').value = css.join(',');
}

function pop_up(pop_up_url) 
{
	w = 720;
	h = 418;
	// Centre the window
	LeftPosition = (screen.width)?(screen.width-w)/2:100;
	TopPosition = (screen.height)?(screen.height-h)/2:100;
	// Hide all the scrollbars and stuff
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	// Create the new window
	window.open(pop_up_url, 'pop_up', settings);
}

var accordion;
var accordionClicks;
var accordionContents;

function initAccordion()
{
	accordionClicks = document.getElementsByClassName('accordion-click');
	accordionContents = document.getElementsByClassName('accordion-content');
	/*
	console.log("contents of accordionClicks: " + accordionClicks);
	console.log("contents of accordionContents: " + accordionContents);
	*/
	accordion = new fx.Accordion(accordionClicks, accordionContents);
}

function window_init()
{
	if (!document.getElementById) return;
	
	// initialise any rollovers
	initRollovers();
	
	// initialize accordion
	initAccordion();
}

DomLoaded.load(window_init);