/*!______________________________________________________________________*\
||                                                                      ||
|| __   __					                                            ||			
|| \ \ / /__  _ __   ___ __ _ 		- Fırat Ağdaş                       ||
||  \ V / _ \| '_ \ / __/ _` |		- Yonca New Media                   ||
||   | | (_) | | | | (_| (_| |		- firat@yonca-ad.com                ||
||   |_|\___/|_| |_|\___\__,_|                                          ||
||                                                                      ||
||______________________________________________________________________||
\*                                                                      */

var active_selection = '';
function selection_register(selectionid, onclickfn)
{
	Event.on(selectionid + '-select', 'click', show_selection, selectionid);
	Event.on(document, 'click', function(e, selectionid)
	{
		var menuobject = Dom.get(selectionid + '-select-dom');
		if (active_selection !== '')
		{
			if (Dom.getStyle(menuobject, 'display') == 'none')
			{
				selection_hide();
				return;
			}
			
			var coord = Dom.getXY(menuobject), px = Event.getPageX(e), py = Event.getPageY(e);
			if (px > coord[0] && px < (coord[0] + menuobject.offsetWidth) && py < (coord[1] + menuobject.offsetHeight) && py > coord[1])
			{
				// hiç birşey...
			}
			else
			{
				selection_hide();
				
				// alert('X: ' + px + ' Y: ' + py + ' --> ' + coord + ' OW: ' + menuobject.offsetWidth + ' OH: ' + menuobject.offsetHeight);
			}
		}
	}, selectionid);
	
	onclickfn = Lang.isFunction(onclickfn) ? onclickfn : function(){};
	
	var els = Dom.getElementsBy(function(){ return true; }, 'div', Dom.get(selectionid + '-select-dom'));
	for (var i = 0; i < els.length; i++)
	{
		set_events(els[i], selectionid, onclickfn);
		if (Dom.hasClass(els[i], 'selection-selected'))
		{
			Dom.get(selectionid + '-select-text').innerHTML = els[i].innerHTML;
		}
	}
	
}

function show_selection(e, selectionid)
{
	if (active_selection !== '')
	{
		selection_hide_active();
	}
	
	var coords = Dom.getXY(this);
	var menuobj = Dom.get(selectionid + '-select-dom');
	Dom.setStyle(menuobj, 'display', '');
	
	if (menuobj.offsetHeight > 300)
	{
		Dom.setStyle(menuobj, 'height', '300px');
		if (UA.ie)
		{
			Dom.setStyle(menuobj, 'overflow-y', 'scroll');
		}
		else
		{
			Dom.setStyle(menuobj, 'overflow', 'scroll');
		}
	}
	
	Dom.setXY(menuobj, [coords[0], coords[1] + this.offsetHeight - 1], false);
	setTimeout(function(){ active_selection = selectionid; }, 0);
}

function selection_hide()
{
	if (active_selection !== '')
	{
		var menuobj = Dom.get(active_selection + '-select-dom');
		Dom.setStyle(menuobj, 'display', 'none');
		Dom.setStyle(menuobj, 'top', '-10000px');
		
		setTimeout(function(){ active_selection = ''; }, 0);
	}
}

function selection_hide_active()
{
	var menuobj = Dom.get(active_selection + '-select-dom');
	Dom.setStyle(menuobj, 'display', 'none');
	Dom.setStyle(menuobj, 'top', '-10000px');
		
	active_selection = '';
}

function set_events(targetobj, selectionid, onclickfn)
{
	Event.on(targetobj, 'mouseover', function(e, obj){ Dom.replaceClass(obj, 'selection', 'selection-hover'); }, targetobj);
	Event.on(targetobj, 'mouseout', function(e, obj){ Dom.replaceClass(obj, 'selection-hover', 'selection'); }, targetobj);
		
	Event.on(targetobj, 'click', function(e, args)
	{
		var node = args[1], value = args[0];
		Dom.get(selectionid + '-select-text').innerHTML = node;
		var hidden = Dom.get(args[2] + '-select-hidden');
		if (hidden)
		{
			hidden.value = value;
			if (hidden.errid && dterr)
			{
				dterr(hidden);
			}
		}
		
		try
		{
			onclickfn(node, value);
		}
		catch (e)
		{
		}
		
		selection_hide();

	}, [targetobj.title, targetobj.innerHTML, selectionid]);
	
	targetobj.title = '';
}