var xmlHttp;
var page;
var tag;
var section;

function showInfo(str,id) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	document.getElementById("loadAnimation").style.display='block';
	if (str=='search') {
		var url='sphider/search.php';
		var params='query='+id;
	} else {
		var url="process.php";
		var params="q="+str+"&id="+id;
	}
	page=str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		switch(page) {
			case "blog":
				document.getElementById("content").innerHTML=xmlHttp.responseText;
				break;
			case "wall":
				document.getElementById("wallcomments").innerHTML=xmlHttp.responseText;
				break;
			case "calendar":
				document.getElementById("calendar").innerHTML=xmlHttp.responseText;
				break;
			case "search":
				document.getElementById("content").innerHTML=xmlHttp.responseText;
				break;
			case "comment":
				document.getElementById("styled_popup").innerHTML=xmlHttp.responseText;
				document.getElementById("styled_popup").style.top=((Window.getWindowHeight()/2)-218+(Window.getScrollTop())) + 'px';
				document.getElementById("styled_popup").style.left=((Window.getWindowWidth()/2)-249) + 'px';
				document.getElementById("styled_popup").style.display='block';
				var theHandle = document.getElementById("handle");
				var theRoot = document.getElementById("styled_popup");
				Drag.init(theHandle, theRoot);
				break;
			case "submit":
				if (section=='blog') {
					showInfo('blog',tag);
				} else if (section=='wall') {
					showInfo('wall','void');
				}
				document.getElementById("styled_popup").style.display='none';
				break;
		}
	document.getElementById("loadAnimation").style.display='none';
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function searchRequest() {
	var s_q = document.getElementById('s_query').value;
	var s_s = document.getElementById('s_search').value;
	showInfo('search',s_q+'&search='+s_s);
	return false;
}
function submitForm(id) {
	document.getElementById("loadAnimation").style.display='block';
	var path = document.getElementById(id);
	var comment = path.comment.value;
	comment = escape(comment);
	var name = path.name.value;
	name = escape(name);
	path.name.disabled = true;
	path.comment.disabled = true;
	path.submit.disabled = true;
	path.name.value = "";
	path.comment.value = "Your Comment is Now Posting..."
	if (id=="wall_comment") {
		section = 'wall';
		showInfo("submit","wall&name="+name+"&comment="+comment);
	} else if (id=="blog_comment") {
		var b_id = path.blog_id.value;
		tag = path.blog_time.value;
		section = 'blog';
		showInfo("submit",b_id+"&name="+name+"&comment="+comment);
	}
}

function toggleSpecs(item,tag) {
	var path = "blog_content"+tag;
    if (document.getElementById(path).style.fontSize == "13px" || document.getElementById(path).style.fontSize == "") {
        document.getElementById(path).style.fontSize = "16px";
		item.innerHTML = "View Smaller";
    } else {
        document.getElementById(path).style.fontSize = "13px";
		item.innerHTML = "View Larger";
    }
}

function loginForm(action) {
	if (action == "cancel") {
		document.getElementById("darkContainer").style.display = "none";
		document.getElementById("loginContent").style.display = "none";
	} else {
		document.getElementById("darkContainer").style.display = "block";
		document.getElementById("loginContent").style.display = "block";
		document.getElementById("user").focus();
	}
}

function imageViewer(item,action,text,w,h) {
	if (action == "close") {
		document.getElementById("darkContainer").style.display = "none";
		document.getElementById("imageContent").style.display = "none";
		document.getElementById("imageContent").innerHTML = "";
	} else if (action == "load") {
		var obj = document.getElementById("imageContent");
		obj.style.width = w+'px';
		obj.style.height = h+'px';
		obj.innerHTML = '<img id="imageHandle" src="'+item+'" />';
		obj.style.top = ((Window.getWindowHeight()/2)) +'px';
		obj.style.left = ((Window.getWindowWidth()/2)) +'px';
		obj.style.marginTop = '-'+(h/2)+'px';
		obj.style.marginLeft = '-'+(w/2)+'px';
		document.getElementById("darkContainer").style.display = "block";
		obj.style.display = "block";
		var theHandle = document.getElementById("imageHandle");
		var theRoot = document.getElementById("imageContent");
		Drag.init(theHandle, theRoot);
	}
}

function styledPopupClose() {
	document.getElementById("styled_popup").style.display = "none";
}

function ClearValue(item) {
    if (item.value == "COMMENT..." || item.value == "NAME...") {
        item.value = "";
    }
}

function popup(url,windowname) {
	var width  = screen.width;
	var height = screen.height;
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=yes';
	params += ', location=yes';
	params += ', menubar=yes';
	params += ', resizable=yes';
	params += ', scrollbars=yes';
	params += ', status=yes';
	params += ', toolbar=yes';
	newwin=window.open(url,windowname,params);
	if (window.focus) {newwin.focus()}
	return false;
}

function emoticon(text,item) {
	var txtarea = document.getElementById(item+"_comment").comment;
	text = ' ' + text + ' ';
	txtarea.value  += text;
	txtarea.focus();
}

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

var Window = {	
	//Returns an integer representing the width of the browser window (without the scrollbar).
	getWindowWidth : function() {
	return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},
 
	//Returns an integer representing the height of the browser window (without the scrollbar).
	getWindowHeight : function() {	
	return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},	
 
	//Returns an integer representing the scrollWidth of the window. 
	getScrollWidth : function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},
 
	//Returns an integer representing the scrollHeight of the window. 
	getScrollHeight : function(){		
		return document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
	},			
 
	//Returns an integer representing the scrollLeft of the window (the number of pixels the window has scrolled from the left).
	getScrollLeft : function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},
 
	//Returns an integer representing the scrollTop of the window (the number of pixels the window has scrolled from the top).
	getScrollTop : function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	}
};
