var alv_id = 0;
var activeMenu = false;
var activeThumb = false;
var globalHTML = null;
var object = null;
var timelimit = 0;
var timelimitAjax = 400;
var globalTimerAjax = null;
var globalTimer = null;

$(document).ready(function(){
	$('<link href="alv/actions/css/css_actions.css" rel="stylesheet" type="text/css" />').appendTo('head');
		alv_init();
		
})

function removeMenu(){
	if (activeMenu == false) $('#menu').remove();
	if (activeThumb == false && $('#menu').length == 0) 
	{	
		clearTimeout(globalTimerAjax);
		$('#alv_actions').remove();
	}	
}

function alv_init_actions(){
	$('#alv_actions').hover(function(){
		if ($('#menu').length == 0)	
		{
			object = this;
			globalTimerAjax = setTimeout("alv_getContent()", timelimitAjax);
		}	
	},function(){
		//removeMenu();
		globalTimer = setTimeout("removeMenu()", timelimit);
	});
}

function getScrollingPosition()
{
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined')
	{
		position = [window.pageXOffset,window.pageYOffset];
	}
	else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0){
		position = [document.documentElement.scrollLeft,document.documentElement.scrollTop];
	}
	else if (typeof document.body.scrollTop != 'undefined')
	{
		position = [document.body.scrollLeft,document.body.scrollTop];
	}
	return position;
}

function clear(){
	clearTimeout(globalTimer);
	clearTimeout(globalTimerAjax);
	globalTimerAjax = null;
	$('#alv_actions').remove();
	$('#menu').remove();
	activeMenu = false;
}

function alv_init(){
	$('div.alv_handle').hover(function(){
		try{
			if (parseInt($(this).attr('id')) == parseInt(alv_global.ownerID)) return;
			clear();
			activeThumb = true;
			$(this).append('<div id = "alv_actions" class="action_div"><a href="javascript:void(0);">' + alv_global.actions + '</a></div>');
			alv_init_actions();
		  }catch(e)
		  {
			alert('ALV action modification was not install properly!');
		  }
		
	},function(){
		activeThumb = false;
		
		if ($('#menu').length == 0)
		{
			clearTimeout(globalTimerAjax);
			$('#alv_actions').remove();
			$('#menu').remove();
			
		}
    });
}

function alv_showTMPDIV(){
	return '<div id = "alv_tmp" class="alv_loading"><img src="alv/actions/icon/ajax-loader.gif" /></div>';
}

function IsThisBrowserIE6() {
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}


function alv_showItemsDIV(e){
		var coverLeft = 0;
		var coverTop = 0;
		var element = e;
		var coverLeft = 0;
		var coverTop = 0;
	
	
	while (element.offsetParent) {
		coverLeft += element.offsetLeft;
		coverTop += element.offsetTop;
		element = element.offsetParent;
	}

		coverTop += parseInt($(e).height());
				
		if ($.browser.msie) coverLeft += 1;
		else
		coverLeft += 2;
		
		var alv_width = '180px';
		var alv_height = '280';
		
		if ($.browser.msie) alv_height = '380';
		
		$('<div id="menu" class="alv_popup_window">' + globalHTML + '</div>').appendTo('body');
		
		var scrollpos = getScrollingPosition();
		
		var scrollDiff = parseInt($(window).height()) + parseInt(scrollpos[1]);
		var coorDiff = coverTop + parseInt(alv_height);
		
		if ((scrollDiff - coorDiff) < 0) coverTop -= coorDiff - scrollDiff;
		
		$('#menu').css('left', coverLeft);
		$('#menu').css('top', coverTop );
		$('#menu').css('visibility','visible');
		
		//alert(scrollDiff - coorDiff);
		alv_height += 'px';
		
		if (IsThisBrowserIE6()) 
			{	
				alv_height = '100%';	
				alv_width = '180px';
			}	
		
		$('#menu').animate({ 
	        width: alv_width,
			height: alv_height,
	        opacity: 1,
			borderWidth: "5px"
	      }, 500).hover(function() {
				
				activeMenu = true;
		  
		  }, function(){
			 
			 activeMenu = false;
			 if (activeThumb == false)	
				{
					clearTimeout(globalTimerAjax);
					$('#alv_actions').remove();	
					$(this).remove();					
				}	
	
		 });
return false;	
}

function alv_getContent(){
	
	var var_id = parseInt($(object).parent().attr('id'));
	
	if (alv_id == var_id){
		alv_showItemsDIV(object);
		return false;
	}
	
	alv_id = var_id;
	
	globalHTML = alv_showTMPDIV();
	
	alv_showItemsDIV(object);
	
	$.ajax({
			url: "alv/actions/exec/getActions.php?id="  + var_id,
			async: true,
			cache: false,
			success: function(html){
					$('#alv_tmp').remove();	
					if ($.trim(html).length == 0) 
					{	
						$('#menu').remove();
						return false;	
					}	
					
					$('#menu').html('');					
					globalHTML = html;
					$('#menu').append(globalHTML);
					
				}	
		});
		
	
	return false;
}	

