var astonlib = {};

if (window.jQuery)
{
	astonlib.TreeTable = function(elName, surl)
	{
	  var table = $('#'+elName);
	
	  var setClick = function(el)
	  {
		$('img.tree_icon', el).each(function(){ $(this).click(iconClick); });
		$('span.tree_root', el).each(function(){ $(this).click(rootClick); });
	    $('ul.new_content_menu', el).each(contentMenu);
	  }
	  
	  var iconClick = function()
	  {
	  	var img = $(this);
	  	var aktrow = $(this).parents('tr:first');
	  	var rid = aktrow.attr('rowId');
	  	var ilink = img.attr('src');
	  	var show = ilink.match('tree_plus');
	  	img.attr('src', show ? ilink.replace('tree_plus', 'tree_minus') : ilink.replace('tree_minus', 'tree_plus'));
	  	if(show)
	  	{
			$.ajax({
	  			url: surl+'&rowId='+rid+'&show=1',
	  			cache: false,
	  			success: function(html)
	  			{
	  				var a = $(html);
	  				setClick(a);
	  	 			aktrow.after(a);
	  			}
			});
	  	}
	  	else
	  	{
			$.ajax({
	  			url: surl+'&rowId='+rid+'&show=0',
	  			cache: false,
	  			success: function(html)
	  			{
			  	  aktrow.siblings('[rowId^='+rid+']').remove(); 
	  			}
			});
	  	}
	  }
	
	  var rootClick = function()
	  {
	  	var rid = $(this).attr('rowId');
		$.ajax({
	 		url: surl+'&rootId='+rid,
	 		cache: false,
	 		success: function(html)
	 		{
	 			table.html(html);
	 			setClick(table);
	 		}
		});
	  }
	setClick(table);
	}
	
	function initAjaxCombo(id, renderModalFunction, params)
	{
	 var el = document.getElementById(id);
	 var aobj = el.aobj;
	 if(!aobj) { aobj = new astonlib.AjaxCombo(el, renderModalFunction, params); el.aobj = aobj; }
	 setTimeout(function(){aobj.showModal()}, 100);
	}
	
	astonlib.AjaxCombo = function(el, renderModalFunction, params)
	{
	 var oel = $(el);
	 var modal = $('<div class="combo_modal" isModal="true"></div>');
	 modal.hide().css({position: 'absolute', 'z-index': 1000}).appendTo("body");
	 var lastValue = 'undefined';
	
	 this.showModal = function()
	 {
	  if(lastValue!=el.value) { if(renderModalFunction) renderModalFunction(this, params); lastValue = el.value; }
	  var pos = oel.offset();
	  modal.css({top: pos.top+oel.height()+6, left: pos.left});
	  if(modal.height()>180)
	  {
	    modal.css({height:180, overflow: 'scroll'});
	  }
	  else
	  {
	    modal.css({overflow: 'auto'});
	  } 
	  modal.show();
	 }
	  
	 this.close = function() { modal.hide(); }
	 this.setValue = function(val) { el.value = val; }
	 this.getValue = function() { return el.value; }
	 this.setModalContent = function(html) { modal.html(html); }
	
	 $('body').click(function(e) { $('div.combo_modal').hide(); });
	 $('div.combo_modal').click(function(e) { e.stopPropagation(); });
	}
	
	function pathComboRender(combo, params)
	{
	  $.ajax({
	    url: params+'&path='+combo.getValue(),
	    cache: false,
	    success: function(html)
	    {
	     var ohtml = $(html);
	     $('span', ohtml).click(function() { combo.setValue(this.title); combo.showModal(); });
	     combo.setModalContent(ohtml);
	    }
	   });
	}
	
	function codelistComboRender(combo, params)
	{
	 var html = '<div>';
	 var l = params.split("\n");
	 for(i=0; i<l.length; i++) html+= '<span>'+l[i]+'</span><br/>';
	 html+='</div>';
	 var ohtml = $(html);
	 $('span', ohtml).click(function(){ combo.setValue($(this).text()); combo.close(); });
	 combo.setModalContent(ohtml); 
	}
	
	contentMenu = function()
	{
	 var ul_el = $(this);
	 ul_el.attr("class", "content_menu");
	 var btn_el = $('<span class="content_button"> '+$('li:first', ul_el).html()+' <img src="../icons/mbutton.png" alt="" class="content_menu"></span>').insertBefore(ul_el);
			
	 var modal = $('<div class="content_menu_panel"></div>');
	 modal.hide().css({position: 'absolute', 'z-index': 1000}).appendTo('body');
	 modal.append(ul_el);
	 $(modal).click(function(e) { e.stopPropagation(); });
	
	 var clickBtn = function()
	 {
	   var pos = btn_el.offset();
	   modal.css({top: pos.top+btn_el.height(), left: pos.left+btn_el.width()-modal.width()});
	   modal.show();
	 };
	 $('.content_menu', btn_el).click(function(){if(modal.is(':hidden')) setTimeout(clickBtn,100);});
	}
	
	$(document).ready( function()
	{
	 $(document).click(function(e) { $('div.content_menu_panel').hide(); });
	 $('ul.new_content_menu').each(contentMenu);
	});
}
