// Circus Maniacs Agency : cmaInitialisationFunctions.js
window.onload = function() {
	BrowserDetect.init();
	browserVersion=BrowserDetect.version;
	operatingSystem=BrowserDetect.OS;	
	if (BrowserDetect.browser=="Explorer") {
		IE=true;
		//alert("this browser IS Internet Explorer");
		if (IE && browserVersion<6) {
			//document.location="/oldBrowsers/index.html";
			if (operatingSystem=="Windows") {
				document.location="/oldBrowsers/ie5.html";
			} else {
				document.location="/oldBrowsers/index.html";
			}
		}
	}
	browser=BrowserDetect.browser;
	if (!IE) {
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = getMouseXY;
	}
	// AJAX
	if( !window.XMLHttpRequest && window.ActiveXObject ) {
		window.XMLHttpRequest = function() {
		var a = [ 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.5.0' ],
		i = a.length; 
		while(i--) 
		{
			try {
			return new ActiveXObject( a[i] );
			} catch (e) { }
			}
			return null;
		};
	}
	if( window.XMLHttpRequest ) {
	 xmlhttp = new XMLHttpRequest();  
	}
	
//	if( !xmlhttp ) {
//		var obj=document.getElementById('xhrReport');
//		if (obj != null) { obj.innerHTML=''; }
//	}
	initArt();
	// debug css tool
	if (debugCSS) {
		var allDivArray = document.getElementsByTagName('div');
		for (var i=0; i<allDivArray.length; i++) {
			allDivArray[i].style.border="1px dotted #6699CC";
			if (String(allDivArray[i].id) != 'page' && String(allDivArray[i].id) != 'content') {
			addEvent(allDivArray[i],'click',nameThis);
			addEvent(allDivArray[i],'scroll',nameThis);
			}
		}
	}
}

function initArt() {
	getInterfaceCookie();
	if (createXMLHttpObject() && autoXhrMode) {
		lastLocation="";
		locationTimeoutId=window.setTimeout('locationHandler()',100);
	}
	/* prepare interface for show / hide of each block in the content menu */
//	show('artistMainPicture,actDescription,actPictures,artistNotes,artPictures,artistDetails,artistActs,actArtists');
//	thinTop();
	setCursor();
}

function getNavigators(delay) {
	// called whenever in artist or act solo display and a new block in the tableau ic called
	// lastRecordset is populated whenever a tableau runs - populated by the output controller
	// subSelect attaches a row of filters onto the url and this needs to also be done by this function subSelectChange()
	if (delay) {
		window.setTimeout('getNavigators(false)',200);
	} else {
		if (lastRecordset) {
			var params = "lastRecordset="+escape(lastRecordset);
			params += "&thisPage="+hashString; // is in this page if we just loaded a tableau - not otherwise...
			params += "&pageNum="+lastTableauPageNum;
			//alert("params: "+params+", lastRecordsetParamString: "+lastRecordsetParamString);
			//getInterface("/conf/artActNavigators.php?"+params,"artActNavigators");
			getInterface("/conf/artActNavigators.php?"+params,"evaluate");
		}
	}
}
function navigateFilmStrip (par) {
	var params = "lastRecordset="+escape(lastRecordset);
	params += "&thisPage="+hashString;
	params += par;
	getInterface("/conf/artActNavigators.php?"+params,"evaluate");
}

function getObject(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		return obj;
	} else {
		return false;
	}
}

function nameThis() {
	var obj;
	if (obj=getObject('features01')) {
		obj.style.position='absolute';
		obj.style.top='380px';
		this.style.border="1px solid";
		obj.innerHTML=this.id;
	}
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

// this shuld be somewhere else but hey..
function webSiteFeedback(n) {
	// provide a window that allows the user to enter data that is submitted by web2.0 to the ARIA.doc record as specified in the argument n
	noAnimation=false;
	var obj='';
	if (obj=getObject('feedbackDiv')) {
		var objStyle=obj.style;
		objStyle.width='400px';
		objStyle.height='600px';
		objStyle.left='230px';
		objStyle.top='10px';
		objStyle.borderTop='2px solid #FFFFFF';
		objStyle.borderRight='2px solid #999999';
		objStyle.borderBottom='2px solid #333333';
		objStyle.borderLeft='2px solid #E9E9E9';
		objStyle.backgroundColor='#E9E9E9';
		objStyle.padding='10px';
		objStyle.visibility='visible';
		getFeedback(n);
	}
	// If this is Firefox then set all background Divs to overflow:hidden and record what it was set to so it can be reset.
	firefoxPatch_OverflowHidden();
}
var firefoxPatch_DivOverflowArray = new Array('content1','content2','listContent');
var firefoxPatch_ResetOverflowArray = new Array();
function firefoxPatch_OverflowHidden() {
	firefoxPatch_ResetOverflowArray = new Array();
	for(var i=0; i<firefoxPatch_DivOverflowArray.length; i++) {
		var obj=document.getElementById(firefoxPatch_DivOverflowArray[i]);
		if (obj!=null) {
			obj=obj.style;
			firefoxPatch_ResetOverflowArray.push(String(obj.overflow));
			obj.overflow='hidden';
			//alert(String(obj.overflow));
		}
	}
}
function firefoxPatch_OverflowReset() {
	for(var i=0; i<firefoxPatch_DivOverflowArray.length; i++) {
		var obj=document.getElementById(firefoxPatch_DivOverflowArray[i]);
		if (obj!=null) {
			obj=obj.style;
			obj.overflow=firefoxPatch_ResetOverflowArray[i];
			//alert(String(obj.overflow));
		}
	}
}
function getFeedback(n) {
	if (n) {
		var path= '/conf/dataSourceFeedback.php?docid='+n;
		getInterface(path,"feedbackDiv");
	}
}

function saveFeedback(n) {
	var obj='';
	if (obj=getObject('feedback')) {
		var feedback = obj.value;
		feedback = escape(feedback);
		var string='feedback='+feedback+'&docid='+n;
		// ariaFilePath,type,params
		postData('web_site_development.php','article',string);
		closeFeedback();
	}
}
function closeFeedback() {
	var obj='';
	if (obj=getObject('feedbackDiv')) {
		var objStyle=obj.style;
		objStyle.width='1px';
		objStyle.height='1px';
		objStyle.left='930px';
		objStyle.top='0px';
		objStyle.borderTop='none';
		objStyle.borderRight='none';
		objStyle.borderBottom='none';
		objStyle.borderLeft='none';
		objStyle.padding='0px';
		objStyle.backgroundColor='';
		objStyle.visibility='hidden';
		obj.innerHTML="";
	}
	firefoxPatch_OverflowReset();
}

function dragDiv(obj,e) {
	switch(e) {
		case "start":
			animatePageActive=true;
			currentDragDiv=obj;
			dragDivStartPositionX = parseInt(currentDragDiv.style.left);
			dragDivStartPositionY = parseInt(currentDragDiv.style.top);		
			offsetX = tempX-dragDivStartPositionX;
			offsetY = tempY-dragDivStartPositionY;
		break;
		case "stop":
			animatePageActive=false;
			currentDragDiv=obj;
		break;
	}
}

function getMouseXY(e) {
	if (IE) { 
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else { 
		tempX = e.pageX
		tempY = e.pageY
	}  
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	
	if (animatePageActive) {
		animatePage();
	}
	if (animateActive) {
		animateArtistDataInterface();
	}
	return true
}

function animatePage() {
	if (!noAnimation) {
	currentDragDiv.style.left = (tempX-offsetX)+'px';
	currentDragDiv.style.top = (tempY-offsetY)+'px';
	}
}

/*
		With many thanks to Quirksmode for the following excellent tool
		www.quirksmode.org
*/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

var traceCalls=0;
function trace(t) {
	/*
	var traceOutputDiv = document.getElementById('debugDiv');

	var traceString = traceOutputDiv.innerHTML;
	if (traceCalls>50) {
		var strArray = traceString.split('|');
		strArray.shift();
		traceString = strArray.join('|');
	}
	if (traceOutputDiv != null) {
		traceOutputDiv.innerHTML = traceString+'<br/>| '+t;
		traceCalls++;
							traceOutputDiv.style.width="900px";
							traceOutputDiv.style.height="600px";
							traceOutputDiv.style.overflow="auto";
							traceOutputDiv.style.backgroundColor="#FFFFFF";
	}
	*/
}
