var _top=0;
var _left=0;
var _eventopen=false;

var distancecursorX=15; //Customize x offset of tooltip
var distancecursorY=0; //Customize y offset of tooltip

var offsetdivfrompointerX=5; //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=5; //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

var ie=document.all;
var ns6=document.getElementById && !document.all;

var container_id = new Array(),
	elements = new Array(),
	count = -1;

function _show(_object)
{
	if (document.getElementById)
	{
		subobj=document.getElementById(_object);
		_eventopen=true;
		//subobj.style.width = 200
		//subobj.style.height = 100
		document.onmouseover=mousepos;
		subobj.style.left = _left;
		subobj.style.top = _top;

		subobj.style.display = "block";
		setTimeout("_hideElements(\"SELECT\",subobj)",100);
		return false;
	}
	else
		return true;
}

function _close(subobj)
{
	document.getElementById(subobj).style.display="none";
	_showElements("SELECT",subobj);
	_eventopen=false;
}

function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function mousepos(e)
{
	if (_eventopen)
	{
		var nondefaultpos=false;
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;

		var rightedge=ie&&!window.opera? winwidth-event.clientX-distancecursorX : winwidth-e.clientX-distancecursorX;
		var bottomedge=ie&&!window.opera? winheight-event.clientY-distancecursorY : winheight-e.clientY-distancecursorY;

		var leftedge=(distancecursorX<0)? distancecursorX*(-1) : -1000;

		//if the horizontal distance isn\'t enough to accomodate the width of the context menu
		if (rightedge<subobj.offsetWidth)
		{
			//move the horizontal position of the menu to the left by it\'s width
			subobj.style.left=curX-subobj.offsetWidth+rightedge+"px";
			nondefaultpos=true;
		}
		else if (curX<leftedge)
			subobj.style.left="5px";
		else
		{
			//position the horizontal position of the menu where the mouse is positioned
			subobj.style.left=curX+distancecursorX-offsetdivfrompointerX+"px";
		}

		//same concept with the vertical position
		if (bottomedge<subobj.offsetHeight)
		{
			subobj.style.top=curY-subobj.offsetHeight+bottomedge-distancecursorY+"px";
			nondefaultpos=true;
		}
		else
			subobj.style.top=curY+distancecursorY+offsetdivfrompointerY+"px"
		subobj.style.visibility="visible";
	}
	// when _eventopen is false
	else
	{
		var nondefaultpos=false;
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;

		var rightedge=ie&&!window.opera? winwidth-event.clientX-distancecursorX : winwidth-e.clientX-distancecursorX;
		var bottomedge=ie&&!window.opera? winheight-event.clientY-distancecursorY : winheight-e.clientY-distancecursorY;

		var leftedge=(distancecursorX<0)? distancecursorX*(-1) : -1000;

		//if the horizontal distance isn\'t enough to accomodate the width of the context menu
		if (rightedge<200)
		{
			//move the horizontal position of the menu to the left by it\'s width
			_left=curX+"px";
			nondefaultpos=true;
		}
		else if (curX<leftedge)
			_left="5px";
		else
		{
			//position the horizontal position of the menu where the mouse is positioned
			_left=curX+distancecursorX-offsetdivfrompointerX+"px";
		}

		//same concept with the vertical position
		if (bottomedge<100)
		{
			_top=curY-distancecursorY+"px";
			nondefaultpos=true;
		}
		else
			_top=curY+distancecursorY+offsetdivfrompointerY+"px";

		// end of _eventopen == false
	}
}
document.onmouseover=mousepos;

function _hideElements(tagName,elDiv)
{
	if (nn6) return;

	var divLeft = elDiv.offsetLeft;
	var divRight = divLeft + elDiv.offsetWidth;
	var divTop = elDiv.offsetTop;
	var divBottom = divTop + elDiv.offsetHeight;

	// For all specified elements.
	var els = document.all.tags(tagName);
	for (var i = 0; i < els.length; i++) {
		var el = els.item(i);
		var elParent = el;

		var left = 0;
		var top = 0;
		var width = elParent.offsetWidth;
		var height = elParent.offsetHeight;
		while (elParent)
		{
			left += elParent.offsetLeft;
			top += elParent.offsetTop;
			elParent = elParent.offsetParent;
		}
//alert("SelectLeft("+left +") < DivRight("+ divRight +")\nSelectRight("+ (left + width) +") > DivLeft("+ divLeft +")\nSelectTop("+ top +") < DivBottom("+ divBottom +")\nSelectBottom("+ (top + height) +") > DivTop("+ divTop+")");
		if (left < divRight && (left + width) > divLeft && top < divBottom && (top + height) > divTop) {
			// Hide element.
			el.style.visibility = 'hidden';

			// Find and store hidden element.
			var found = false;
			var free_index = this.elements.length;
			for (var k = 0; k < this.elements.length; k++) {
				if (this.elements[k] == 0) {
					free_index = k;
				}
				if (this.elements[k] == el) {
					found = true;
					break;
				}
			}
			if (!found) {
				this.elements[free_index] = el;
			}
		}
	}
}

function _showElements(tagName,elDiv) {
	if (nn6) return;
	// For all hidden elements.

	var els = document.all.tags(tagName);
	for (var i = 0; i < els.length; i++) {
		var el = els.item(i);
		if (el) {
			var elParent = el;

			var left = 0;
			var top = 0;
			var width = elParent.offsetWidth;
			var height = elParent.offsetHeight;
			while (elParent) {
				left += elParent.offsetLeft
				top += elParent.offsetTop
				elParent = elParent.offsetParent
			}

			var show = true;

			var divLeft = elDiv.offsetLeft;
			var divRight = divLeft + elDiv.offsetWidth;
			var divTop = elDiv.offsetTop;
			var divBottom = divTop + elDiv.offsetHeight;

			if ((left < divRight) && (left + width > divLeft) && (top < divBottom) && (top + height > divTop))
				show = false;
			if (show) {
				el.style.visibility = 'visible';
			}
		}
	}
}

function setCities(o)
{
	var city = document.getElementsByName("city");
	if (o.value == -1)
		city[0].disabled = true;
	else
		city[0].disabled = false;
	city[0].length = 1;
	for (i in aStateCity)
	{
		if (i == o.value)
		{
			for (var j = 0; j < aStateCity[i].length; j++)
			{
				var oOption = document.createElement("option");
				city[0].options.add(oOption);
				if (aStateCity[i][j] == "")
				{
					oOption.innerHTML = "unknown";
					oOption.value = "unknown";
				}
				else
				{
					oOption.innerHTML = aStateCity[i][j];
					oOption.value = aStateCity[i][j];
				}
			}
		}
	}
}

/******************** AJAX *********************/

var Event = {};

	Event.offsetFlag = 0,
	Event.aParams = [],
	Event.pValue = [];

Event.showItems = function (URL, Params)
{
	Event.customBlock = arguments.length > 2 ? arguments[2] : '';
	Event.aParams = Params.split("&");
	Event.pValue = Event.aParams[2].split("=");

	Event.pleaseWait();

	if(Params.indexOf("offset") != -1)
		Event.offsetFlag = 1;

	var MainContainer = $("AjaxBlock" + Event.pValue[1]),
		Element = $("AjaxItems" + Event.pValue[1]),
		Waiting = $("Waiting" + Event.pValue[1]);

	if(Element && Waiting)
		Event.getClassRequest(URL,Params);
}

Event.getClassRequest = function (URL,Params)
{
	this.myAjax = new Ajax.Request(
			URL,
			{
				method: 'get',
				parameters: Params,
				onComplete: this.showResponse
			}
		);
}

Event.showResponse = function (originalRequest)
{
	var itemsNode = $(Event.customBlock ? Event.customBlock : "AjaxItems" + Event.pValue[1]);
	itemsNode.innerHTML = originalRequest.responseText;
	
	var node = $("AjaxBlock" + Event.pValue[1]);
	node.onmouseover = null;
	node.onmouseout = null;
	
	var Waiting = $("Waiting" + Event.pValue[1]);
	if(Waiting)
		Waiting.style.display = "none";
	
	Event.offsetFlag = 0;
	
	try
	{
		initWidgets();
	}
	catch(e)
	{}
}

Event.pleaseWait = function ()
{
	var Waiting = $("Waiting" + Event.pValue[1]);
	if(Waiting)
	{
		Waiting.style.position = "absolute";
		
		var node = $("AjaxBlock" + Event.pValue[1]);
		node.onmouseover = function(e)
		{
			Waiting.style.left = (10 + (e ? e.pageX : event.clientX))+'px';
			Waiting.style.top = (10 + (e ? e.pageY : event.clientY))+'px';
			Waiting.style.display = "block";
		}
		node.onmouseout = function(event)
		{
			Waiting.style.display = "none";
		}
	}
}

Event.clean = function ()
{
	document.getElementsByName("category")[0].value = -1;
	document.getElementsByName("state")[0].value = -1;
	document.getElementsByName("city")[0].value = -1;
	document.getElementsByName("category")[1].value = -1;
	document.getElementsByName("state")[1].value = -1;
	document.getElementsByName("city")[1].value = -1;
}