// Javascript file for eXonyte's Realm 4.0

function toggleitem(id) {
  if (document.getElementById) {
    if ((document.getElementById(id).style.display == "none") || (document.getElementById(id).style.display == ""))
      document.getElementById(id).style.display = "block";
    else
      document.getElementById(id).style.display = "none";
  }
  else if (document.all) {
    if ((document.all[id].style.display == "none") || (document.all[id].style.display == ""))
      document.all[id].style.display = "block";
    else
      document.all[id].style.display = "none";
  }
    
}

function getcookie(name) {
  var search = name + "=";

  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
  }
}

function setcookie(name,value) {
  if (name) {
    var expire = new Date();

    if (value)
      //sets cookie to expire in 90 days
      //(1000ms times 60s times 60m times 24h times 90d)
      expire.setTime(expire.getTime() + 1000*60*60*24*90);

    else
      expire.setTime(expire.getTime() - 1);

    document.cookie = name + "=" + escape(value) + "; expires=" + expire.toGMTString();
  }
}

function applytheme(newtheme) {
  setcookie("theme",newtheme);
  if (document.styleSheets) {
    for(i = 0; i < document.styleSheets.length; i++) {
      if (document.styleSheets[i].href.indexOf("/" + newtheme + ".css") > 0) {
        document.styleSheets[i].disabled = false;
      }
      else {
        document.styleSheets[i].disabled = true;
      }
    }
  }
  else
    document.location.href=document.location.href;
}

if (getcookie("theme") == "Blue (Default)") applytheme("blue");
if (getcookie("theme") == "Calm Blue") applytheme("calmblue");

//V2.01.A<p></p> - http://www.openjs.com/scripts/jx/
jx = {
	http:false, //HTTP Object
	format:'text',
	callback:function(data){},
	handler:false,
	error: false,
	opt:new Object(),
	//Create a xmlHttpRequest object - this is the constructor. 
	getHTTPObject : function() {
		var http = false;
		//Use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
		if (XMLHttpRequest) {
			try {http = new XMLHttpRequest();}
			catch(e) {}
		} else if(typeof ActiveXObject != 'undefined') {
			//Use IE's ActiveX items to load the file.
			try {http = new ActiveXObject("Msxml2.XMLHTTP");}
			catch(e) {
				try {http = new ActiveXObject("Microsoft.XMLHTTP");}
				catch(E) {}
			}
		}
		return http;
	},
	// This function is called from the user's script. 
	//Arguments - 
	//	url	- The url of the serverside script that is to be called. Append all the arguments to 
	//			this url - eg. 'get_data.php?id=5&car=benz'
	//	callback - Function that must be called once the data is ready.
	//	format - The return type for this function. Could be 'xml','json' or 'text'. If it is json, 
	//			the string will be 'eval'ed before returning it. Default:'text'
	//	method - GET or POST. Default 'GET'
	load : function (url,callback,format,method) {
		this.init(); //The XMLHttpRequest object is recreated at every call - to defeat Cache problem in IE
		if(!this.http||!url) return;
		//XML Format need this for some Mozilla Browsers
		if (this.http.overrideMimeType) this.http.overrideMimeType('text/xml');

		this.callback=callback;
		if(!method) var method = "GET";//Default method is GET
		if(!format) var format = "text";//Default return type is 'text'
		this.format = format.toLowerCase();
		method = method.toUpperCase();
		var ths = this;//Closure
		
		//Kill the Cache problem in IE.
		var now = "uid=" + new Date().getTime();
		url += (url.indexOf("?")+1) ? "&" : "?";
		url += now;

		var parameters = null;

		if(method=="POST") {
			var parts = url.split("\?");
			url = parts[0];
			parameters = parts[1];
		}
		this.http.open(method, url, true);

		if(method=="POST") {
			this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.http.setRequestHeader("Content-length", parameters.length);
			this.http.setRequestHeader("Connection", "close");
		}

		if(this.handler) { //If a custom handler is defined, use it
			this.http.onreadystatechange = this.handler;
		} else {
			this.http.onreadystatechange = function () {//Call a function when the state changes.
				if(!ths) return;
				var http = ths.http;
				if (http.readyState == 4) {//Ready State will be 4 when the document is loaded.
					if(http.status == 200) {
						var result = "";
						if(http.responseText) result = http.responseText;

						//Give the data to the callback function.
						if(ths.callback) ths.callback(result);
					} else {
						if(ths.opt.loadingIndicator) document.getElementsByTagName("body")[0].removeChild(ths.opt.loadingIndicator); //Remove the loading indicator
						if(ths.opt.loading) document.getElementById(ths.opt.loading).style.display="none"; //Hide the given loading indicator.
						
						if(ths.error) ths.error(http.status);
					}
				}
			}
		}
		this.http.send(parameters);
	},
	init : function() {this.http = this.getHTTPObject();}
}

var shoutbox_page = 1;
var shoutbox_perpage = 5;
var shoutbox_dragging = false;

function shoutboxSubmit() {
	if (shoutbox_page != 1) {
		shoutboxPage(1 - shoutbox_page);
		return;
	}
        msg = document.getElementById("message").value;
	if (msg.length < 2) {
	  alert('Your message is too short.');
	  document.getElementById("message").value = '';
          return;
        }
	if (msg.length > 150) {
	  alert('Your message is too long.');
	  document.getElementById("message").value = '';
          return;
	}
	var ul = document.getElementById("shoutboxul"), li = document.createElement("li");
	ul.insertBefore(li, ul.getElementsByTagName("li")[0]);
        document.getElementById("message").value = '';
	li.innerHTML = '<i>Please wait...</i>';
        jx.load('shout.php?message=' + escape(msg), function(data) {
        	li.innerHTML = data;
	}, null, 'POST');
	items = ul.getElementsByTagName("li");
	items[items.length - 1].innerHTML = '<a href="#"><i>Archiving...</i></a>';
	Effect.Squish(items[items.length - 1]);
}

function deleteShout(draggable, droppable, e) {
	cont = draggable.innerHTML;
	shoutid = draggable.id;
	shoutid = shoutid.substring(5, shoutid.length);
	draggable.innerHTML = '<a href="#"><i>Deleting shout ' + shoutid + '...</i></a>';
        jx.load('shout.php?delete=' + shoutid, function(data) {
        	if (data == "1") {
			Effect.Squish(draggable);
			shoutboxPage(0);
		       }
		else {
			alert('That shout does not belong to you.');
			draggable.innerHTML = cont;
		}
	}, null, 'POST');
}

function shoutboxPage(offset) {
	shoutbox_page = shoutbox_page + offset;
	if (shoutbox_page < 1) {
		shoutbox_page = 1;
		alert('You can not go back any further.');
		return;
	}
	offset = (shoutbox_page * shoutbox_perpage) - shoutbox_perpage;
	pg = document.getElementById('shoutboxpg');
	pg.innerHTML = '<i>Loading...</i>';
	ul = document.getElementById('shoutboxul');
	//Effect.BlindUp(ul,{queue:'front'});
        jx.load('shout.php?extend=' + offset, function(data) {
		if (data == "0") {
			shoutbox_page--;
			pg.innerHTML = 'Page ' + shoutbox_page;
                        alert('You can not go forward any further.');
			return;
		}
		pg.innerHTML = 'Page ' + shoutbox_page;
		ul.innerHTML = data;
		items = ul.getElementsByTagName("li");
		for (i = 0; i < items.length; i++) {
			li = items[i];
			new Draggable(li, {revert:true,change:shoutboxDrag,revert:shoutboxDragEnd});
		}
	}, 'text', 'POST');
	//Effect.BlindDown(ul,{queue:'end'});
}

function shoutboxUpdate() {
	if (!shoutbox_dragging)	shoutboxPage(0);
	setTimeout('shoutboxUpdate()', 15000);
}

function shoutboxDrag(obj) {
	if (obj.dragging) {
		shoutbox_dragging = true;
		document.getElementById('deleteshout').style.display = '';
	}
}

function shoutboxDragEnd(obj) {
	shoutbox_dragging = false;
	document.getElementById('deleteshout').style.display = 'none';
	return true;
}

setTimeout('shoutboxUpdate()', 15000);

