jQuery(document).ready(function(){	
	jQuery.each(jQuery('li.ajaxSideMenu'), function(){
		jQuery(this).css({'position':'relative'});
		
		//show menu
		jQuery(this).hover(
			function(){
				if(jQuery('.ajaxMenu', this).length==0){
					jQuery('a:first', this).after(
						'<div class="ajaxMenu"></div>'						
					);
				}//end if
				jsMenu.over = true;
				jsMenu.load( jQuery('a:first', this)[0]);
			},
			function(){
				jQuery('.ajaxMenu', this).hide();
				jsMenu.over = false;
			}	
		);
		
	});
	
	jQuery('head').append('<style>.ajaxMenu{'+
		'position:absolute;display:none;top:0px;left:100%;'+
		'border:1px solid #2B74B1;margin-left:-1px;'+
		'background-color:#D6E7F9;width:185px;padding:5px;'+
		'padding-right:15px;}'+
		'</style>'
	);
	
});

/************************************************************************************************************************/
var jsMenu = {
	over:false,
	storage : {},
	page:'fileadmin/templates/includes/ajaxMenu.php',
	
	/**
	 * @param Object obj: the 'a' object
	 */
	load : function(obj){
		var url = document.URL.replace(/(http)(s)?(\:\/\/)/,"").toLowerCase().split("/");
		var urlPart = url[0].split('.');
		var tbl = url[1];
		var page = obj.href;
		var id = null;
			
		if(page.match(/\=/)){
			id = page.split("?")[1].split("=")[1];
		}else{
			page = page.split("/")[page.split("/").length-1];
			id = page.split(".")[0];
		}//end else

		var menuId = id.replace(/\W/g,'').toLowerCase();
		if( typeof(jsMenu.storage[menuId]) == 'undefined' ){
			var _obj = {id:escape(id),
						table:escape(tbl),
						js_id:menuId };
			
			//if(typeof(console) != 'undefined'){
				//check if this is a devel version
				if(urlPart[0]=='dev'){
					_obj.devDb = _obj.table+'_dev'; 
				}//end if
			//}
			
			jsMenu.storage[menuId] = null;
			jQuery.post(this.page,_obj,function(result){
				if(result != 'error_no_entry'){
					var arr = result.split(":");
					jsMenu.storage[arr[0]] = unescape(arr[1]);
					jsMenu.get_menu(obj, arr[0]);
				}//end if
			});
		
		}else{
			jsMenu.get_menu(obj, menuId);
		}//end else

		return false;
	},

	/**
	 * @param Object obj: the 'a' object
	 * @param int id: the id of the menu to retrieve
	 */
	get_menu : function (obj, id){
		jQuery('.ajaxMenu').hide();
		var data = jsMenu.storage[id];
		if(data != null){
			var tmp = jQuery(obj).next('.ajaxMenu');
			tmp.html(data);
			if(jsMenu.over){
				tmp.show();
			}
			//if(!jQuery.browser.msie){
			//	console.log( jsMenu.over );
			//}
			//jQuery(obj).next('.ajaxMenu').html(data).show();
		}//end if
	}
	
}//end jsMenu

/*
* this function is needed to prevent javascript error
* USED BY TYPO3
*/
function blurLink(obj){return false;}
