/* ============================================ */
/*			Title: JS Utils						*/
/*			Author: Jim M. & Aaron G.			*/
/*			Usage: Base Structure				*/
/* ============================================ */




/*	Onload Handling 							*/
/* ============================================ */

// USAGE: onloadHandlers[onloadHandlers.length] = 'functionName();';

var onloadHandlers = [];
window.onload = function(){
	for(var i=0;i<onloadHandlers.length;i++){
		eval(onloadHandlers[i]);
	}
}


/*	GetElement Shortcut							*/
/* ============================================ */

// USAGE: var myElement  = $('myElementID');
//		  var myElements = $('myElementID-1', 'myElementID-2');

// RETURNS: DOM element for single argument or array of elements
//			if you pass multiple arguments

function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}




/*	Visual Effects								*/
/* ============================================ */

var visFX = {
	appear: function(){},
	fade: function(){},
	init: function(){}
}
onloadHandlers[onloadHandlers.length] = 'visFX.init();';



/*	Page Tracking								*/
/* ============================================ */
/* page handling for bookmarking, report etc.	*/

var ext=".php"
var page = "index";

function changeURL(me){
	window.location = "#"+me;
}

function redirectPage(){
	if (window.location.hash.length>1) {
		var newLoc = new String(window.location);
		newLoc = newLoc.slice(newLoc.indexOf('#')+1);
		newLoc = newLoc.split("|");
		var redir = "";
		for (i=0;i<newLoc.length;i++){
			redir = redir+"/"+newLoc[i];
		}
		if (newLoc.length==1){
			window.location.href=redir
		}else{
			window.location.href=redir+ext;
		}
	}
}

function callTrackingPage(page){
	if(window.XMLHttpRequest){
    	req = new XMLHttpRequest();
		req.onreadystatechange = requestStateChange;
		req.open("GET", "/tracking.php?page="+page, true);
		req.send(null);
	}else if(window.ActiveXObject){ //IE
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if(req){
			req.onreadystatechange = requestStateChange;
			req.open("GET", "/tracking.php?page="+page, true);
			req.send();
		}
	}
}

function requestStateChange(){
    /*
    readyState values
    0 = uninitialized
    1 = loading
    2 = loaded
    3 = interactive
    4 = complete
    */
    if(req.readyState == 1){
        //displayWaitingStatus(callingElement);
    }
	if(req.readyState == 4){
		eval(req.responseText);
	}
}

function trackme(me){
	//alert("tracking: "+me);
}

//onloadHandlers[onloadHandlers.length] = 'back = new BackButton();';
//onloadHandlers[onloadHandlers.length] = 'redirectPage()';

/*	External Links 								*/
/* ============================================ */
/* handling or target new	*/

function externalLinks(){
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++) {
		
		var anchor = anchors[i];
		
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "ext") {
			
			anchor.target = "_blank";
			
			// XiTi tagging
			anchor.onclick = function() {xt_med('C',xtn2,this.id,'S');}
			
			// detect older versions of Safari (before Tiger)
			// which do not display PDFs inside the browser window
			// and prevent the creation of a blank pop-up window
			if (anchor.href.indexOf('.pdf')!=-1){
					browserId = navigator.userAgent;
					if (browserId.indexOf('AppleWebKit')!=-1) {
						//alert("this is Safari");
						var regxpWebkitVersion= /AppleWebKit\/(\d+)/;
						webkitVersion = browserId.match(regxpWebkitVersion)[1];
						if (webkitVersion < 412) {
							//alert("This is not Tiger")
							anchor.target = "_self";
						} 
					}
				}
		}
	}
}
//onloadHandlers[onloadHandlers.length] = 'externalLinks()';


/*	Nomadic Link 								*/
/* ==================================================== */
/* This link and it's parent li needs special attention	*/
/* So we find it in the nav and give it a unique ID 	*/
/* We could move this to the structure.xsl but whatevs. */

function isolateMuseumLink(){
	var museumLink = $('nav-exhibition-nomadic-museum');
	if(!museumLink) return;
	var museumLinkLI = museumLink.parentNode;
	if(!museumLinkLI) return;
	museumLinkLI.id = 'museumLinkLI';
}
//commented out for now  onloadHandlers[onloadHandlers.length] = 'isolateMuseumLink()';


//cookie

//cookies for choosing language
function checkPop(){
	var pop = readCookie('pop');
	if(!pop){
		openPopup();
		setPop('pop');
	}
}

function setPop(pop){
	createCookie('pop',pop,31);
	//alert('lang = ' + readCookie('lang'));
}

function clearPop(){
	eraseCookie('pop');
	//alert('lang = ' + readCookie('lang'));
}

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 closePopup(){
	document.getElementById('coming-soon-container').style.display = "none";
	document.getElementById('coming-soon').style.display = "none";
}
function openPopup(){
	if ((document.getElementById('coming-soon-container')) != null) {
		document.getElementById('coming-soon-container').style.display = "block";
	}
	if ((document.getElementById('coming-soon')) != null) {
		document.getElementById('coming-soon').style.display = "block";
	}
}
onloadHandlers[onloadHandlers.length] = 'checkPop()';


/*	Button Active State 								*/
/* ============================================ */

function setButtonStates() {
	if (document.getElementById('submit-button')) {
		submitButton = document.getElementById('submit-button');
		submitButton.onmousedown = function() { this.style.color = "#433222"; }
		submitButton.onmouseup = function() { this.style.color = "#994400"; }
		submitButton.onmouseout = function() { this.style.color = "#994400"; }
	}
}

onloadHandlers[onloadHandlers.length] = 'setButtonStates()';


var anim = [];
var animateTimer;

function prepAnimation(){
	anim = document.getElementById('codex-entry').getElementsByTagName('a');
	animateTimer = setInterval('animate()',100);
}

function killAnimation(){
	anim[0].className='anim1';
	clearInterval(animateTimer);
}

function animate() {
	var count = anim[0].className.charAt(4);
	count++;
	anim[0].className = 'anim'+count;
	if (count == 5)	clearInterval(animateTimer);
}

function redirectHome(){
	window.location.href='/index.php';
	clearTimeout(rTimer);
}

function setRTimer(){
	if (document.getElementById('flash-download')){
		var FlashDownloadDiv = document.getElementById('flash-download');
		var links = FlashDownloadDiv.getElementsByTagName('a');
		for (var i=0; i<links.length; i++) {
			var link = links[i];
			link.onclick = function(){rTimer = setTimeout('redirectHome()',1500);}
		}
	}
}

onloadHandlers[onloadHandlers.length] = 'setRTimer()';

function redirectAfterXiti(url){
	var img_is_loaded = 0;
	var redirect = function() {if (img_is_loaded==0){document.location = url; img_is_loaded = 1;}}
	xt_img.onload = function(){redirect;}
	self.setTimeout(redirect, 2000)
}

/*	Error Message Suppression 					*/
/* ============================================ */

function handleError() {
	return true;
}

window.onerror = handleError;
