// common client side functions

function ajax_execute(target, method) {
	ajax2(target, method, true);
}
function ajax_get(target, method) {
	ajax2(target, method, false);
}

function ajax2(target, method, execute) {
	var oajax;
	if (typeof method == 'undefined') {
		method = 'GET';
	}
	if (window.XMLHttpRequest) {
		oajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		oajax = new ActiveXObject('Microsoft.XMLHTTP');
	} else {
		alert('Unable to set up ajax');
	}
	if (execute == true) {
		oajax.open(method, target, true);
		oajax.onreadystatechange = function() {
			if (oajax.readyState == 4) {
//if (oajax.responseText != '') { alert(oajax.responseText); }
				try {
					eval(oajax.responseText);
				} catch(e) {
					alert('ajax error on:\n\n'+oajax.responseText+'\n\n'+e);
				}
			}
		}
		oajax.send(null);
	} else {
//		alert('sync');
		oajax.open(method, target, false);
		oajax.send(null);
		return oajax.responseText;
	}
	return true;
}


/*
function loadexternal(filename){ // load external javascript after pageload
	var fileref = document.createElement('script');
	fileref.type = "text/javascript";
	fileref.src = filename;
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}
*/





/////////////////////////
// some UI shortcuts
/////////////////////////
function innerhtml(id, val) {
	document.getElementById(id).innerHTML = val;
}
function innerhtml_append(id, val) { // adds a message to the user's history
	document.getElementById(id).innerHTML += ''+val;
}
function scrollbottom(id, loop_num) { // scrolls to the bottom of a div
	// scrolls a div to the bottom, if the object hasn't been created yet then loop until it exists
	loop_num = loop_num || 0;

	// first try it, if it's already created we don't need to do a loop
	height = -1;
	try {
		height = document.getElementById(id).scrollHeight;
	} catch(e) {
		if (loop_num < 100) { // prevent infinite loops
			setTimeout('scrollbottom("'+id+'", '+(loop_num+1)+')', 10);
		}
	}
	if (height != -1) {
		setTimeout('document.getElementById("'+id+'").scrollTop = '+height, 100);
		setTimeout('document.getElementById("'+id+'").scrollTop = '+height, 200); // sometimes these don't take effect until it's shown
		setTimeout('document.getElementById("'+id+'").scrollTop = '+height, 300);
	}
}


function show(id) {
	document.getElementById(id).style.display = 'inline';
}
function hide(id) {
	document.getElementById(id).style.display = 'none';
}
function src(id, val) {
	document.getElementById(id).src = val;
}
function alt(id, val) {
	document.getElementById(id).alt = val;
}
function value(id, val) {
	document.getElementById(id).value = val;
}
function zindex(id, val) {
	document.getElementById(id).style.zIndex = val;
}
function movetotop(id) {
	document.getElementById(id).style.zIndex = 1*get_max_z()+1;
}

function get_max_z() {
	// get the current top floating div
	var max_z = 0;
	var divs = document.getElementsByTagName('div');
	var length = divs.length;
	for (var i = 0; i < length; i++) {
		if (divs[i].id.indexOf('box_') == 0) {
			if (divs[i].style.zIndex > max_z) {
				max_z = divs[i].style.zIndex;
			}
		}
	}

	return max_z;
}

function div_exists(id) {
	var divs = document.getElementsByTagName('div');
	var length = divs.length;
	for (var i = 0; i < length; i++) {
		if (divs[i].id == id) {
			return true;
		}
	}
	return false;
}

function debug(msg) {
	show('div_debug');
	innerhtml_append('div_debug', '<li>'+msg+'</li>');
	scrollbottom('div_debug');
}



/////////////////////////
// chat
/////////////////////////
var timer_loop = 0;
//setTimeout('timer_loop2();', 2000)
function reset_timer() { // resets the check messages timer
	timer_loop = 0;
}
function timer_loop2() {
	timer_loop++;
	get_new_messages();

	if (timer_loop < 2*60/2) { // idle < 2 minutes
		time = 2000;
	} else if (timer_loop < 3*60/2) { // 3 minutes
		time = 5000;
	} else if (timer_loop < 4*60/2) { // 4 minutes
		time = 10000;
	} else if (timer_loop < 5*60/2) { // 5 minutes
		time = 20000;
	} else if (timer_loop < 10*60/2) { // 10 minutes
		time = 30000;
	} else {
		time = 60000; // every minute if they leave it open, 1400 times a day still though
	}
	setTimeout('timer_loop2();', time);
}
function get_new_messages() {
	// check the server for pending messages
//debug('get_new_messages');

	url = "chat_pending_ajax.php?current_chat_open="+current_chat_open+"&channel_id="+current_channel_open;
//alert(url);
	ajax_execute(url);
}
function add_msg(her_user_id) {
	msg = document.getElementById('msg_'+her_user_id).value;

	innerhtml_append('history', '<br><span class="msg_from">'+msg+'</span>');
	scrollbottom('history');

	url = "chat_add_ajax.php?to_user_id="+her_user_id+"&msg="+msg;
//alert(url);
	ajax_execute(url, 'GET');

	document.getElementById('msg_'+her_user_id).value = '';

	reset_timer();
}
function chat2(her_user_id, default_value) {
	// checks for the text in the box, if it's the default, erase it before they start typing
	if (document.getElementById('msg_'+her_user_id).value == default_value) {
		document.getElementById('msg_'+her_user_id).value = ''; // erase
	}
}



function closefloat(id) { // close a float box
	innerhtml(id, '');
	hide(id);
}



// update the unread_messages box
function update_unread_messages() {
	url = "chat_unread_messages_ajax.php";
//alert(url);
	ajax_execute(url, 'GET');
}




// chat room functions
function channel_open(channel_id) {
	// opens the chat div
	url = "channel_open_ajax.php?channel_id="+channel_id;
//	alert(url);
	ajax_execute(url);
}
function channel_users(channel_id) {
	// shows the users in the room list
	url = "channel_users_ajax.php?channel_id="+channel_id;
//	alert(url);
	ajax_execute(url);
}
function channel_history(channel_id) {
	// shows the users in the room list
	url = "channel_history_ajax.php?channel_id="+channel_id;
//	alert(url);
	ajax_execute(url);
}
function channel_add(channel_id, msg) {
	url = "channel_add_ajax.php?channel_id="+channel_id+"&msg="+msg;
//	alert(url);
	ajax_execute(url);
}
function channel_close() {
	if (current_channel_open != '') { // global
		url = "channel_close_ajax.php?channel_id="+current_channel_open;
	//	alert(url);
		ajax_execute(url);

		current_channel_open = ''; 
	}
}









/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * http://www.youngpup.net/projects/dom-drag/
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/
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;
	}
};
