
var isIE = (document.all);
var isNN = ((!document.all) && (document.getElementById));
// additional code from WhatIthinkof.com, used under license from Lightenna.com
wito_ajaxAddEvent(document, 'mousedown', wito_mousedown);
wito_ajaxAddEvent(document, 'mouseup', wito_mouseup);

// thanks to Scott Andrew for hints on the event code (http://www.scottandrew.com/weblog/articles/cbs-events)
function wito_ajaxAddEvent(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 {
		// unable to attach events
		alert("Unable to initialise AJAX events, please use the non-ajax version of this site when available.");
	}
}

function wito_ajaxRemoveEvent(obj, evtype, fn, useCapture) {
	if (obj.removeEventListener) {
		obj.removeEventListener(evtype, fn, useCapture);
		return true;
	} else if (obj.detachEvent) {
		var r = obj.detachEvent("on" + evtype, fn);
		return r;
	} else {
		// unable to remove event, c'est la vie
	}
} 

function wito_ajaxRemoveAllEvents(obj, evtype) {
	obj.onclick = null;
}

// mouse event handlers
var wito_dragging = false;
var wito_dragging_id = -1;
var wito_dragging_div = null;
var wito_dragging_offsetx = -1;
var wito_dragging_offsety = -1;
var wito_dragging_currentx = -1;
var wito_dragging_currenty = -1;
var wito_doubleclick_lastDate = 0;
var wito_dragOuter = "balloon_block_handle";
var wito_catchDrag = "balloon_";
function wito_mousedown( e)
{
	var eventalias = (isIE ? event : e);
	var evtarget = (isIE ? event.srcElement : e.target);
	var dragstr = evtarget.id.substring(0,wito_catchDrag.length);
	// catch drag operations (sustained click)
	if (dragstr == wito_catchDrag) {
		// clicked on titlebar div OR titlebar para (paratbar) OR body (hideoutr) OR arrow (arrowptr)
		wito_dragging_id = evtarget.id.substring(wito_catchDrag.length+1);
		wito_dragging_div = document.getElementById(wito_dragOuter);
		// alert("starting drag ["+evtarget.id.substring(wito_catchDrag.length+1)+"]");
		wito_dragging = true;
	    wito_dragging_offsetx = eventalias.clientX;
		wito_dragging_offsety = eventalias.clientY;
		if (isIE) {
			wito_dragging_div.style.filter = 'alpha(opacity=50)';
		} else {
			wito_dragging_div.style.MozOpacity = '0.5';
		}
		wito_dragging_currentx = parseInt(wito_dragging_div.style.left);
		wito_dragging_currenty = parseInt(wito_dragging_div.style.top);
		wito_ajaxAddEvent(document, 'mousemove', wito_mousedrag);
		return(false);
	} else {
		return(true);
	}
}

function wito_mousedrag( e)
{
	if (wito_dragging)
	{
		eventalias = (isIE ? event : e);
		// update css properties based on drag distance
		wito_dragging_div.style.left = "" + (wito_dragging_currentx + eventalias.clientX - wito_dragging_offsetx) + "px";
		wito_dragging_div.style.top = "" + (wito_dragging_currenty + eventalias.clientY - wito_dragging_offsety) + "px";
		return(false);
	}
}

function wito_mouseup()
{
	if (wito_dragging)
	{
		// reset opacity
		if (isIE) {
			wito_dragging_div.style.filter = 'alpha(opacity=100)';
		} else {
			wito_dragging_div.style.MozOpacity = '1.0';
		}
		// detach event
		wito_ajaxRemoveEvent(document, 'mousemove', wito_mousedrag);
		wito_dragging = false;
		return(false);
	}
}


