var loading = '<div id="loading"><img src="/images/busy.gif" alt="loading..." /><br />Please wait while loading.</div>';
var loadingImage = '<img src="/images/busy.gif" alt="loading..." />';
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;
}
function blackOut (goal){
	if(goal=='in'){
		var htmlheight = document.body.parentNode.scrollHeight;  
		var windowheight = window.innerHeight;  
		if ( htmlheight < windowheight ) {
			myHeight = windowheight;
		} else {
			myHeight = htmlheight;  
		} 
		
		document.body.style.height = myHeight + "px";
		
		try {
		  black = document.createElement("<div id='blackOut' style='height:"+myHeight+"px' />");
		} catch (e) {
		  black = document.createElement('div');
		  black.setAttribute('id', 'blackOut');
		  black.setAttribute('style', "height:"+myHeight+"px");
		}
		document.body.appendChild(black);
	}else if(goal == 'out'){
		var d = document.body;
		var olddiv = document.getElementById('blackOut');
		d.removeChild(olddiv);
	}
}
function callback(serverData, serverStatus) {
	fullUrl = window.location.href;
	if(fullUrl.substring(fullUrl.indexOf('?')+1, fullUrl.indexOf('?')+6) == 'debug') alert(serverData);
	if(serverData.indexOf('^' != -1)) {
		arrays = serverData.split('^');
		i=0;
		while(i<arrays.length){
			array = arrays[i].split('~');
			var element = document.getElementById(array[1]);
			if(array[3] == 'innerHTML') element.innerHTML = array[5];
			if(array[3] == 'src') element.src = array[5];
			if(array[3] == 'redirect') window.location = array[5];
			if(array[3] == 'refresh') location.reload(true);
			if(array[3] == 'class') element.className = array[5];
			if(array[3] == 'chat') showMessage(array[5], array[1]);
			if(array[3] == 'chatLog') chatLog = array[5];
			if(array[3] == 'chatTimestamp') chatTimestamp = array[5];
			if(array[3] == 'alertMSG') alert(array[5]);
			i = i+1;
		}
	}
	//urchinTracker("/ajax" );
}
function ajaxRequest(request, post) {
	var AJAX = null;
	if (window.XMLHttpRequest) {
		AJAX=new XMLHttpRequest();
	} else {
		AJAX=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (AJAX==null) {
		alert("Your browser doesn't support AJAX.");					
		return false
	}
	AJAX.onreadystatechange = function() {
		if (AJAX.readyState==4 || AJAX.readyState=="complete") {
			callback(AJAX.responseText, AJAX.status);
		}
	}
	if(post){
		urlBase = window.location.protocol + '//' + window.location.hostname;
		AJAX.open('post', urlBase+'/ajax.php', true);
  		AJAX.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		AJAX.send('request='+request);
	}else{
		var url='/ajax.php?request='+request;
		AJAX.open("GET", url, true);
		AJAX.send(null);
	}
}
function insertAtCursor(myField, myValue) {
	if (document.selection) {
		$(myField).focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	} else if ($(myField).selectionStart || $(myField).selectionStart == 0) {
		var startPos = $(myField).selectionStart;
		var endPos = $(myField).selectionEnd;
		$(myField).value = $(myField).value.substring(0, startPos) + myValue + $(myField).value.substring(endPos, $(myField).value.length);
	} else {
		$(myField).value += myValue;
	}
}
function postMessage (for_id, for_sort, div){
	if(window.textArray) $('sendMessageButton').innerHTML = textArray['submittingMessage'];
	else $('sendMessageButton').innerHTML = 'Sending..';
	if($('msg_textarea').value == 'no'){
		ajaxRequest('messages&post=true&sort='+for_sort+'&for='+for_id+'&div='+div+'&content='+escape(FCKeditorAPI.__Instances['msg_content'].GetXHTML(true)));
	}else{
		ajaxRequest('messages&post=true&simple=true&sort='+for_sort+'&for='+for_id+'&div='+div+'&content='+escape($('msg_content').value));

	}
}
function quoteMessage (for_id, for_sort, div, msg_id){
	ajaxRequest('createfck&div='+div+'&name=msg_content&msg_for='+for_id+'&msg_sort='+for_sort+'&quote_message='+msg_id)
}
function checkForScreenSize (){
	if ((screen.width>=1024) && (screen.height>=768)){
		document.getElementById('bannerRight').style.display = 'block';
	}else{
		document.getElementById('bannerRight').style.display = 'none';
		document.getElementById('content').style.padding = '10px 0';
		document.getElementById('content').style.marginLeft = '0';
		document.getElementById('content').style.backgroundPosition = '-103px 0';
		document.getElementById('subHeader').style.width = '780px';
	}
}