var currentmenuid=0;

// function selectMenu(menuid) {
// 	curmidlink=document.getElementById("midlink_"+currentmenuid);
// 	curbotlink=document.getElementById("bottomlinks_"+currentmenuid);
// 	newmidlink=document.getElementById("midlink_"+menuid);
// 	newbotlink=document.getElementById("bottomlinks_"+menuid);
// 	curmidlink.className="midlink";
// 	curbotlink.className="bottomlinks";
// 	newmidlink.className="midlink_selected";
// 	newbotlink.className="bottomlinks_selected";
// 	currentmenuid=menuid;
// }
var niftymoveto=0;
var niftymovefrom=0;
var resizeto=0;
var niftystep=5;
var niftyshown=false;

function nifty_menu_mover(menuid) {
	curmidlink=document.getElementById("midlink_"+currentmenuid);
	thingy=document.getElementById("midlink_mover");
	if (!niftyshown) {
		curmidlink.className="midlink";
		thingy.style.width=curmidlink.offsetWidth+"px";
		thingy.style.left=curmidlink.offsetLeft+"px";
		thingy.style.display="block";
		niftyshown=true;
	}
	newmidlink=document.getElementById("midlink_"+menuid);
	resizeto=newmidlink.offsetWidth;
	niftymoveto=newmidlink.offsetLeft;
	niftymovefrom=curmidlink.offsetLeft;
	nifty_menu_domove();
// 	alert(thingy.style.width);
}

function nifty_menu_domove() {
	thingy=document.getElementById("midlink_mover");
	currentleft=parseInt(thingy.offsetLeft);
	currentwidth=parseInt(thingy.style.width);
	if (Math.abs(currentleft-niftymoveto)<=niftystep) {
		thingy.style.left=niftymoveto+"px";
		thingy.style.width=resizeto+"px";
	} else if (currentleft!=niftymoveto) {
		if (currentleft>niftymoveto) {
			thingy.style.left=(currentleft-niftystep)+"px";
		} else {
			thingy.style.left=(currentleft+niftystep)+"px";
		}
		if (currentwidth>resizeto) {
			thingy.style.width=(currentwidth-1)+"px";
		} else if (currentwidth<resizeto) {
			thingy.style.width=(currentwidth+1)+"px";
		}
		setTimeout("nifty_menu_domove()",2);
	}
}

function showElConditional(sender,searchval,elid) {
	var el=document.getElementById(elid);
	if (sender.value==searchval) {
		el.style.display="block";
	} else {
		el.style.display="none";
	}
}

var updateDateId=0;

function chooseDate(id) {
	updateDateId=id;
	divel=document.createElement("div");
	divel.id="datepopup";
	divel.style.left=(posx-100)+"px";
	divel.style.top=(posy-100)+"px";

	formel=document.createElement("form");

	datelabel=document.createElement("label");
	datelabel.innerHTML="Date";

	dateel=document.createElement("select");
	dateel.name="date";
	dateel.id="date_id";
	dateel.options[dateel.options.length]=new Option("2 July","2009-07-02");
	dateel.options[dateel.options.length]=new Option("3 July","2009-07-03");
	dateel.options[dateel.options.length]=new Option("4 July","2009-07-04");
	dateel.options[dateel.options.length]=new Option("5 July","2009-07-05");
	dateel.options[dateel.options.length]=new Option("6 July","2009-07-06");
	dateel.options[dateel.options.length]=new Option("7 July","2009-07-07");
	dateel.options[dateel.options.length]=new Option("8 July","2009-07-08");
	dateel.options[dateel.options.length]=new Option("9 July","2009-07-09");
	dateel.options[dateel.options.length]=new Option("10 July","2009-07-10");
	dateel.options[dateel.options.length]=new Option("11 July","2009-07-11");

	timelabel=document.createElement("label");
	timelabel.innerHTML="Time";

	timehel=document.createElement("input");
	timehel.type="text";
	timehel.name="timeh";
	timehel.id="timeh_id";

	timesplit=document.createElement("span");
	timesplit.innerHTML=":";

	timemel=document.createElement("input");
	timemel.type="text";
	timemel.name="timem";
	timemel.id="timem_id";

	multidateel=document.createElement("input");
	multidateel.type="checkbox";
	multidateel.name="multidate";
	multidateel.id="multidate_id";
	multidateel.style.width="auto";

	multidatelabel=document.createElement("label");
	multidatelabel.innerHTML="Daily";

	submitel=document.createElement("input");
	submitel.type="button";
	submitel.className="submit";
	submitel.value="Submit";
	submitel.onclick=updateDate;

	br1=document.createElement("br");
	br2=document.createElement("br");
	br3=document.createElement("br");

	closeel=document.createElement("img");
	closeel.src="images/naf_close.png";
	closeel.onclick=closeDate;
	closeel.title="Close";
	closeel.id="closebutton_date";
	divel.appendChild(closeel);

	formel.appendChild(datelabel);
	formel.appendChild(dateel);
	formel.appendChild(br1);
	formel.appendChild(timelabel);
	formel.appendChild(timehel);
	formel.appendChild(timesplit);
	formel.appendChild(timemel);
	formel.appendChild(br2);
	formel.appendChild(multidatelabel);
	formel.appendChild(multidateel);
	formel.appendChild(br3);
	formel.appendChild(submitel);

	divel.appendChild(formel);
	document.getElementById("header_bg").appendChild(divel);
}

function emailToFriend(id) {
	divel=document.createElement("div");
	divel.id="emailpopup";
	divel.style.left=(posx-100)+"px";
	divel.style.top=(posy-100)+"px";

	formel=document.createElement("form");
	formel.action="events/email_event/"+id;
	formel.method="post";

	emaillabel=document.createElement("label");
	emaillabel.innerHTML="Friend's email";

	emailel=document.createElement("input");
	emailel.name="email";
	emailel.id="emailid";
	emailel.type="text";

	namelabel=document.createElement("label");
	namelabel.innerHTML="My name";

	nameel=document.createElement("input");
	nameel.name="name";
	nameel.id="nameid";
	nameel.type="text";

	submitel=document.createElement("input");
	submitel.type="submit";
	submitel.className="submit";
	submitel.value="Submit";

	br1=document.createElement("br");
	br2=document.createElement("br");
	br3=document.createElement("br");

	closeel=document.createElement("img");
	closeel.src="images/naf_close.png";
	closeel.onclick=closeEmailToFriend;
	closeel.title="Close";
	closeel.id="closebutton_email";

	divel.appendChild(closeel);
	formel.appendChild(namelabel);
	formel.appendChild(nameel);
	formel.appendChild(br1);
	formel.appendChild(emaillabel);
	formel.appendChild(emailel);
	formel.appendChild(br2);
	formel.appendChild(submitel);

	divel.appendChild(formel);
	document.getElementById("header_bg").appendChild(divel);
}

function removeChildren(node) {
	if((node==undefined) || (node==null) || (node.className=="floater")) {
		return;
	}
	var len = node.childNodes.length;
	while (node.hasChildNodes()) {
		if (node.firstChild.hasChildNodes()) {
			removeChildren(node.firstChild);
		}
		node.removeChild(node.firstChild);
	}
}

function closeEmailToFriend(e) {
	el=document.getElementById("emailpopup");
	if ((el) && (el.hasChildNodes())) {
		removeChildren(el);
	}
	el.innerHTML="";
	document.getElementById("header_bg").removeChild(el);
}

function closeDate(e) {
	el=document.getElementById("datepopup");
	if ((el) && (el.hasChildNodes())) {
		removeChildren(el);
	}
	el.innerHTML="";
	document.getElementById("header_bg").removeChild(el);
}

function updateDate(e) {
	dateval=document.getElementById("date_id").value;
	timehval=document.getElementById("timeh_id").value;
	timemval=document.getElementById("timem_id").value;
	dailyval=document.getElementById("multidate_id").checked;
	if ((dateval!=="") && (timehval!=="") && (timemval!=="") && IsNumeric(timehval) && IsNumeric(timemval) && (timehval<=23) && (timemval<=59)) {
		if (dailyval) {
			var t=" "+timehval+":"+timemval;
			var s="";
			for(var x=26;x<=30;x++) {
				s+="2008-06-"+x+t+", ";
			}
			for(var x=1;x<5;x++) {
				s+="2008-07-"+x+t+", ";
			}
			s+="2008-07-05"+t;
		} else {
			var s=dateval+" "+timehval+":"+timemval;
		}
		document.getElementById(updateDateId).value=s;
	} else {
		alert("Please input a valid date and time");
	}
	el=document.getElementById("datepopup");
	while (el.hasChildNodes()) {
		el.removeChild(el.firstChild);
	}
	topel=document.getElementById("header_bg");
	topel.removeChild(el);
}

var posx=0;
var posy=0;

function trackMouse() {
	document.onmousemove=updateMousePos;
}

function updateMousePos(e) {
	if (!e) {
		e=window.event;
	}
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
}

function IsNumeric(strString) {
//  check for valid numeric strings
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;
//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++) {
	strChar = strString.charAt(i);
	if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

window.onload=function(){trackMouse();}

/* Ajax stuff */

/**
/* Ajax object for multiple Ajax instances
*/

function callAjax() {
	var xmlreqs=new Array();
	//this.xmlreqs=xmlreqs;
	this.CXMLReq=CXMLReq;
	this.xmlreqGET=xmlreqGET;
	this.xmlhttpChange=xmlhttpChange;

	function CXMLReq(type,xmlhttp) {
		this.type=type;
		this.xmlhttp=xmlhttp;
	}

	function xmlreqGET(url) {
		this.xmlhttp=false;
		if (window.XMLHttpRequest) { // Mozilla, etc.
			this.xmlhttp=new XMLHttpRequest();
			this.xmlhttp.onreadystatechange = xmlhttpChange;
			this.xmlhttp.open("GET",url,true);
			this.xmlhttp.send(null);
		} else if (window.ActiveXObject) { // IE
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			if (this.xmlhttp) {
				this.xmlhttp.onreadystatechange = xmlhttpChange;
				this.xmlhttp.open("GET",url,true);
				this.xmlhttp.send();
		}
	}
	xmlreq = new CXMLReq('', this.xmlhttp);
	this.xmlreq=xmlreq;
	xmlreqs.push(this.xmlreq);
}

	function xmlhttpChange() {
		//if (typeof(window['xmlreqs']) == "undefined") return;
		for (var i=0; i < xmlreqs.length; i++) {
			if (xmlreqs[i].xmlhttp.readyState == 4) {
				if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) {
					// 200 OK
					// get response info here before splicing - see below on creating an xml object
					this.result=xmlreqs[i].xmlhttp.responseText;
					//alert("Got result");
					eval(this.result);
					//Process result here
					//alert(this.result);
					xmlreqs.splice(i,1);
					i--;
				} else {
					// error
					xmlreqs.splice(i,1);
					i--;
				}
			}
		}
	}
}

function catchClick(url) {
	ajax=new callAjax();
	ajax.xmlreqGET(url);
}

function changeInner(senderid,newval) {
	sender=document.getElementById(senderid);
	sender.innerHTML=newval;
}

function changeImg(senderid,newval) {
	sender=document.getElementById(senderid);
	sender.src=newval;
}

function switchInner(sender,val1,val2) {
	if (sender.innerHTML==val1) {
		changeInner(sender,val2);
	} else {
		changeInner(sender.val1);
	}
}

//Search form thingy
function makeSearchForm() {
	val1=document.getElementById("choose_day").value;
	val2=document.getElementById("choose_fest").value;
	val3=document.getElementById("choose_cat").value;
	if ((val2==-1)) {
		alert("Please select Main or Fringe");
	} else {
		document.getElementById('findshowid').submit();
	}
}

function closeAjaxEventWarn(e) {
	el=document.getElementById("ajaxwarn");
	if ((el) && (el.hasChildNodes())) {
		removeChildren(el);
	}
	el.innerHTML="";
	document.getElementById("header_bg").removeChild(el);
}

function ajaxEventWarn(id,msg) {
	divel=document.createElement("div");
	divel.id="ajaxwarn";
	divel.style.left=(posx-100)+"px";
	divel.style.top=(posy-100)+"px";

	msgel=document.createElement("div");
	msgel.innerHTML=msg+"Do you want to add anyway?";

	okel=document.createElement("input");
	okel.type="button";
	okel.id="okid";
	okel.value="Okay";
	okel.onclick=function() { catchClick("events/ajax_addtoschedule/"+id+"/1"); closeAjaxEventWarn(); }

	cancelel=document.createElement("input");
	cancelel.type="button";
	cancelel.id="cancelid";
	cancelel.value="No thanks";
	cancelel.onclick=closeAjaxEventWarn;

	closeel=document.createElement("img");
	closeel.src="images/naf_close.png";
	closeel.onclick=closeAjaxEventWarn;
	closeel.title="Close";
	closeel.id="closebutton_email";

	divel.appendChild(closeel);
	divel.appendChild(msgel);
	divel.appendChild(okel);
	divel.appendChild(cancelel);

	document.getElementById("header_bg").appendChild(divel);
}

function ajaxEventWarnOk() {
	closeAjaxEventWarn();
}

function window_size() {
// 	win=top_window();
	if (window.innerHeight) {
		winW=window.innerWidth;
		winH=window.innerHeight;
// 		alert("Win.innerHeight:"+winH);
	} else {
// 		winW=Math.max(win.document.documentElement.clientWidth, win.document.body.clientWidth);
// 		winH=Math.max(win.document.documentElement.clientHeight, win.document.body.clientHeight);
			winW=window.document.documentElement.clientWidth;
			winH=window.document.documentElement.clientHeight;
// 		alert("win.doc.body.offsetHeight:"+winH);
	}
	retarray=new Array(2);
	retarray[0]=winW;
	retarray[1]=winH;
	return(retarray);
}

function getScrollY() {
	var scrOfY = 0;
	if( typeof(window.pageYOffset) == 'number' ) {
		//Netscape compliant
		scrOfY=window.pageYOffset;
	} else if(document.body && (document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY=document.body.scrollTop;
	} else if(window.document.documentElement && (window.document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
// 		alert("IE6");
		scrOfY=window.document.documentElement.scrollTop;
	}
	return scrOfY;
}

function closeImgpopup() {
	bgel=document.getElementById("imgpopup_bg");
	imgel=document.getElementById("imgpopup_img");
	closeel=document.getElementById("closeimg");
	container=document.getElementById("header_bg");
	if (document.getElementById("titleel")) {
		titleel=document.getElementById("titleel");
		container.removeChild(titleel);
	}
	container.removeChild(bgel);
	container.removeChild(imgel);
	container.removeChild(closeel);
}

function getIEVersion() {
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function imgpopup(imgid) {
//Create a floating window
	winsize=window_size();
	catchClick("image/ajax_draw_popup/"+imgid+"/"+winsize[0]+"/"+winsize[1]);
// 	topel=document.getElementById("toplevel");
// 	floaterel=document.getElementById("floater1");
// // 	document.body.style.overflow="hidden";
// // 	floaterel.style.zIndex="600";
// // 	floaterel.style.backgroundColor="#FFF";
//
// 	//Grey out background
// 	topel.className="show";
// 	document.getElementById("toplevel").style.display="block";
// // 	alert(doc.body.parentNode.scrollHeight);
// 	topel.style.height=doc.body.parentNode.scrollHeight+"px";
//
// 	//Create our new element
// 	el=doc.createElement("div");
// 	el.setAttribute("id","myfloater");
// 	ver=getIEVersion();
// 	if ((ver<7) && (ver>-1)) {
// 		//For IE6
// 		el.style.position="absolute";
// 		y=y+getScrollY();
// 	}
// 	el.style.width=w+"px";
// 	el.style.height=h+"px";
// 	el.style.top=y+"px";
// 	el.style.left=x+"px";
// 	el.style.opacity="1";
//
// 	el.style.zIndex="600";
// 	el.style.backgroundColor="#FFF";
// 	el.className="floater";
// 	el.innerHTML=floater_content;
//
// 	//Create a close icon
// 	closeel=doc.createElement("div");
// 	closeel.className="closefloater";
// 	//closeel.innerHTML="<img src='images/winclose.png' />";
// 	closeelimg=doc.createElement("img");
// 	closeelimg.src="images/winclose.png";
// 	closeelimg.onclick=kill_floater;
//
// 	//Create a title div
// 	titlediv=doc.createElement("div");
// 	titlediv.className="title";
// 	titlediv.innerHTML=floater_title;
//
// 	floaterel.appendChild(el);
// 	doc.getElementById("myfloater").display="block";
// 	el.appendChild(closeel);
// 	el.appendChild(titlediv);
//
// 	closeel.appendChild(closeelimg);
// 	//Just for IE
// 	hide_selects(doc);
//
// 	return(el);
}