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();
}

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();
}


/* AJAX functions.... */

var AJAX_CACHE=new Array();

var curType=0;

function updateBody(type,id) {
// retrieves information (via AJAX) about a given service/resource/market-sector
// @param type		= 'service','resource','market-sector'
// @param id		= row ID

/*
	var params="t="+type+"&id="+id;
	var text="";

	if (!type.match(/^(services|resources|market.sectors|state.of.the.arts)$/)) { alert(type); return true; }
	if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-type-details.php",params,1,"");
		//alert(params);
		//alert(responseText);
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}

	o=document.getElementById(type+"_content");
	if (o) {
		// set the HTML to the new list, and reset behaviors on click.
		o.innerHTML=text;
	}
*/

	o=document.getElementById("content_"+curType);
	if (o) o.style.display="none";
	curType=id;
	o=document.getElementById("content_"+curType);
	if (o) o.style.display="block";

	return false;
}

function updatePhoto(type,id) {

	var params="t="+type+"&id="+id;
	var text="";

	if (!type.match(/^(services|resources|market.sectors|projects|state.of.the.arts)$/)) return true;
	if (AJAX_CACHE[params]) data=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-big-photo.php",params,1,"");
		if (responseText != '""') {
			data=eval('('+responseText+')');
			AJAX_CACHE[params]=data;
		}
	}
	//alert('here');

	//o=document.getElementById("photo_title");
	//if (o) { o.innerHTML=data["title"]; }

	o=document.getElementById("photo_desc");
	if (o) { o.innerHTML=data["desc"]; }
	o=document.getElementById("main_content");
	if (o) { o.style.backgroundImage="url("+data["image"]+")"; }

	o=document.getElementById("video_window");
	if (o) {
		if ((data["flv"]!="")||(data["embed"]!="")) {
			o2=document.getElementById("main_content");
			o2.style.backgroundImage="none";
			o2.style.backgroundColor="#1E1E1E";
			o.style.backgroundColor="#1E1E1E";
			o.style.display="block";
			if (data["embed"]!="") o.innerHTML=data["embed"];
			else {
			// a SWF needs to be initialized here...

				o.innerHTML='<div id="video_window_swf">'+data["flv"]+'</div>';
				var so=new SWFObject("/RWDIplayer.swf","presentation_swf",525,316,"6","#1E1E1E");
				so.addParam("wmode","transparent");
				so.addParam("FlashVars","flvFile="+data['flv']);
				so.write("video_window_swf");
			}
		}
		else o.style.display="none";
	}

	o=document.getElementById("photo_header");
	if ((o)&&(o.style.display=="none")&&(o.innerHTML!="")) {
		$("div.services_detail_title div.collapsable_content").removeClass("inactive");
		$("div.services_detail_title div.collapsable_content").toggle("slide", { direction: "right" }, 400);
	}
	o=document.getElementById("photo_desc");
	if ((o)&&(o.style.display=="none")&&(o.innerHTML!="")) {
		$("div.services_detail_caption").removeClass("inactive");
		$("div.services_detail_caption div.collapsable_content").toggle("slide", { direction: "right" }, 400);
	}
	if ((o)&&(o.style.display!="none")&&(o.innerHTML=="")) {
		$("div.services_detail_caption div.collapsable_content").toggle("slide", { direction: "right" }, 400);
		$("div.services_detail_caption").addClass("inactive");
	}

	return false;
}

function updateExpert(id,m) {

	var params="id="+id;
	var text="";

	if (m!="hash") window.location.hash=params;//buildParams(r);	// update the hash...
	else {
	// the pollHash() function has detected a change in the hash...

		params=window.location.hash.substr(1);
		if (params.length>0){

			if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
			else {
				var responseText=http_getPage("/ajax/get-expert-details.php",params,1,"");
				//alert(params);
				//alert(responseText);
				if (responseText != '""') {
					text=responseText;
					AJAX_CACHE[params]=text;
				}
			}
			o=document.getElementById("content_right");
			if (o) {
				o.innerHTML=text;
				initGalleryScroller();
				//scroller.init();
			}
		}
	}

	return false;
}

var hpPage=1;

function updateHPStories(pg) {

	var params;
	var text="";

	hpPage++;
	if (hpPage>maxPage) hpPage=1;
	params="pg="+hpPage;

	if (AJAX_CACHE[params]) data=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-hp-stories.php",params,1,"");
		if (responseText != '""') {
			data=eval('('+responseText+')');
			AJAX_CACHE[params]=data;
		}
	}

	o=document.getElementById("main_content");
	if (o) {
		o.innerHTML=data["html"];
		maxStory=data["maxStory"];
		initHomePage();
	}

	return false;
}

function getLanguage(n) {

	var params;
	var text="";

	params="ln="+n;

	if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-language.php",params,1,"");
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}

	o=document.getElementById("language_box");
	if (o) {
		o.innerHTML=text;
		o.style.display="block";
	}

	return false;
}

function javascript_active() {
// updates the session variable to let us know the user has activated Javascript.

	var responseText=http_getPage("/ajax/javascript-active.php","",1,"");
	//alert(responseText);

	return false;
}