var tmr;

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

window.onload = function() {

	modifyNewWindowLinks();
	hideDottedLines();
	setModalHeight();
	initSWF();
}

function initSWF() {
	o=document.getElementById("swf_object");
	if (o) swfobject.registerObject("swf_object", "6.0.0");
}

function modifyNewWindowLinks() {
	//This function looks for all the links with a class of "NewWindow"
	//and will cause them to open in a new window.

	var aTags = document.getElementsByTagName("a");
	for (var x = 0; x < aTags.length; x++) {
		var currTag = aTags[x];
		if (String(currTag.className).indexOf('NewWindow') > -1) {
			currTag.onclick = function(){ window.open(this.href); return false;}
		}
	}
}

function findPos(obj) {
// gets the 'top' and 'left' positions of the object.

  var curtop = 0;
  var curleft = 0;
  if (obj.offsetParent) {
	  while (obj.offsetParent) {
		  curtop += obj.offsetTop;
		  curleft += obj.offsetLeft;
		  obj = obj.offsetParent;
	  }
  }
  else {
	  if (obj.y) curtop += obj.y;
	  if (obj.x) curleft += obj.x;
  }
  var r=Array();
  r[0]=curtop;
  r[1]=curleft;
  return r;
}

function setModalHeight() {
	var o=document.getElementById("modalDiv");
	if (o) {
		var eop1=document.getElementById("eopDiv_lhs");
		var eop2=document.getElementById("eopDiv_rhs");
		var pos1=(eop1)?findPos(eop1):0;
		var pos2=(eop2)?findPos(eop2):0;
		var max=(pos1[0]>pos2[0])?pos1[0]:pos2[0];
		//alert(pos1[0]+' '+pos2[0]+' = '+max);

		o.style.height=(max+50)+"px";
	}
}

function hideDottedLines() {
// hide the 'dotted lines' around the link for links with image-replacement.

	var theahrefs = document.getElementsByTagName("a");

	// fix dotted line thing when link is OnClicked
	for(var x=0;x!=theahrefs.length;x++) {
		//if (theahrefs[x].className!="") alert(theahrefs[x].className);
		if (theahrefs[x].className.indexOf("hideOutline")>-1) {
			theahrefs[x].onfocus = function stopLinkFocus(){ this.hideFocus=true; };
		}
	}
}

function form_autoSubmit() {
// used to submit the "event type" select-box on the schedule page...

	f=document.forms["modeSel"];
	if (f) f.submit();
}

function showHelp(o,html) {

	fr=document.getElementById("farRight");
	hd=document.getElementById("helpDiv");

	if ((fr)&&(hd)) {
		frPos=findPos(fr);
		pos=findPos(o);
		offsetX=7;
		if ((pos[1]+300)>frPos[1]) offsetX=-100;

		hd.style.top=(pos[0]+20)+"px";
		hd.style.left=(pos[1]+offsetX)+"px";
		hd.innerHTML=html.replace(/#QUOTE#/g,'"');
		hd.style.display="block";
	}
}

function hideHelp() {
	if (hd=document.getElementById("helpDiv")) {
		hd.style.display="none";
	}
}

function favourites_remove(p,id) {

	// we are ready to send our request to the server...
		responseText=http_getPage("/ajax/ajax-favourites.php",p+"&rm="+id,1,"");
		if (responseText != '""') {
			o=document.getElementById("favourites_current");
			if (o) {
				// set the HTML to the new list, and reset behaviors on click.
				o.innerHTML=responseText;
			}
		}
		return false;
}

function reminderShow() {

	$('<div id="message_overlay"></div>').appendTo('html');
	$("#message_overlay").css({height:$('body').height()+'px'});
	$('<div id="reg_message"></div>').appendTo('#wrapper');
	$('#reg_message').load('/templates/block-registration-message.tpl');		
	$("#reg_message a.close").live("click", reminderClose);
}

function reminderClose() {
	$('#message_overlay, #reg_message').fadeOut(150, function() {
		$(this).remove();
	});
}
