var TagHandleObj;

function TagHandle()
{
	// number of the icon set to display on mouse over of each tag
	var IconSetNum ;
	// these 3 vars are used for the icon set timer
	var MenuActive ;
	var TimeBeforeAutoHide ;
	var HideTimer ;
	// current tag id (triggered on mouse over)
	var CurTagId ;
	// previous tag id (triggered on mouse over)
	var CurTagIdAux ;
	// icon set name to display on tag rollover
	var RollOverIconSet ;
	
	this.IconSetNum = 1 ;
	this.MenuActive = true ;
	this.TimeBeforeAutoHide = 500 ;
	this.HideTimer = 0 ;
	this.CurTagId = '' ;
	this.CurTagIdAux = '' ;
	this.RollOverIconSet = 'IcoSet' ;
	
	TagHandleObj = this ;
}


TagHandle.prototype = {
	initTagHandle : function()
	{
		// 1: assigning onmouseover event
		var aTagCloud = $(TAB2).getElementsByClassName("TagCloud") ;
		
		var Tags ;
		// for each tag cloud
		for(elt = 0 ; elt < aTagCloud.length ; elt++)
		{
			Tags = aTagCloud[elt].getElementsByTagName('a') ;
			
			// for each tag in the tag cloud
			for(i = 0 ; i < Tags.length ; i++)
			{
				Tags[i].onmouseover = this.__displayIcons;
				Tags[i].onmouseout = this.__hideIcons;
				
				if(AgregatorObj.AgregateNames.indexOf(Tags[i].innerHTML) > -1)
				{
					Tags[i].addClassName('TagLit') ;
				}
					
				if(Tags[i].innerHTML == AgregatorObj.CurrentAgregName)
				{
					Tags[i].addClassName('TagSelected') ;
					AgregatorObj.LitTagsWithAtLeastOneKwInCommon(Tags[i].innerHTML) ;
				}
					
			}
		}
		
		// 2: create icon divs
		var DivIcoSet = this.GenerateIcoSet() ;
		$(TAB2).appendChild(DivIcoSet);
	}
	,
	GenerateIcoSet : function(pi_IcoSetNum)
	{
		var DivIcoSet = document.createElement('div');

		Element.extend(DivIcoSet);
		DivIcoSet.id = 'IcoSet' ; //+ pi_IcoSetNum;
		DivIcoSet.addClassName('IconList').hide();
		var s_Code = '' ;
		
		// ico to open an existing agregate
		s_Code += '<span id="'+DivIcoSet.id+'ReOpenAgregate"><a href="#" onclick="AgregatorObj.AddAgregate();$(TagHandleObj.RollOverIconSet).hide();return false" title="'+ txt_OpenAgregate +'">' ;
		s_Code += '<img src="img/common/icons/modify.gif" width="14" height="14" border="0" /></a></span>' ;
		
		// ico to generate a new agregate
		s_Code += '<span id="'+DivIcoSet.id+'GenerateAgregate"><a href="#" onclick="AgregatorObj.AddAgregate();$(TagHandleObj.RollOverIconSet).hide();return false" title="'+ txt_GenerateAgregate +'">' ;
		s_Code += '<img src="img/common/icons/add.gif" width="14" height="14" border="0" /></a></span>' ;
		
		// ico to add in an agregate
		s_Code += '<span id="'+DivIcoSet.id+'AddAgregate"><a href="#" onclick="AgregatorObj.AddAgregateItem();$(TagHandleObj.RollOverIconSet).hide();return false" title="'+ txt_AddToAgregate +'">' ;
		s_Code += '<img src="img/common/icons/add-green.gif" width="14" height="14" border="0" /></a></span>' ;
		
		// ico to close an agregate
		s_Code += '<span id="'+DivIcoSet.id+'CloseAgregate"><a href="#" onclick="AgregatorObj.CloseAgregate();$(TagHandleObj.RollOverIconSet).hide();return false" title="'+ txt_CloseAgregate +'">' ;
		s_Code += '<img src="img/common/icons/confirm.gif" width="14" height="14" border="0" /></a></span>' ;
		
		// ico to blacklist
		s_Code += '<span id="'+DivIcoSet.id+'Blacklist"><a href="#" onclick="AddToBlacklist();$(TagHandleObj.RollOverIconSet).hide();return false" title="'+ txt_AddToBlacklist +'">' ;
		s_Code += '<img src="img/common/icons/delete.gif" width="14" height="14" border="0" /></a></span>' ;
		
		// ico to delete agregate
		s_Code += '<span id="'+DivIcoSet.id+'DeleteAgregate"><a href="#" onclick="AgregatorObj.Delete();$(TagHandleObj.RollOverIconSet).hide();return false" title="'+ txt_DeleteAgregate +'">' ;
		s_Code += '<img src="img/common/icons/delete_gray.gif" width="14" height="14" border="0" /></a></span>' ;

		DivIcoSet.innerHTML = s_Code ;
		DivIcoSet.onmouseover = this.__displayIcons;
		DivIcoSet.onmouseout = this.__hideIcons;
		
		return DivIcoSet;
	}
	,
	getTopPos : function(inputObj)
	{		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
	  	if(inputObj.tagName!='HTML'){
	  		returnValue += inputObj.offsetTop;
	  		if(document.all)returnValue+=inputObj.clientTop;
	  	}
	  } 
	  return returnValue;
	}
	,
	getLeftPos : function(inputObj)
	{	  
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null){
	  	if(inputObj.tagName!='HTML'){
	  		returnValue += inputObj.offsetLeft;
	  		if(document.all)returnValue+=inputObj.clientLeft;
	  	}
	  }
	  return returnValue;
	}
	,
	timerAutoHide : function()
	{
		if(this.MenuActive)
		{
			this.HideTimer = 0;
			return;
		}
		
		if(this.HideTimer < this.TimeBeforeAutoHide)
		{
			this.HideTimer += 100;
			setTimeout('TagHandleObj.timerAutoHide()',99);
		}
		else
		{
			this.HideTimer = 0;
			// this test avoids a particular error, when the timer is not over WHILE user updates the tag cloud
			if($(this.RollOverIconSet) != null)
				$(this.RollOverIconSet).hide();
		}
	}
	,
	__displayIcons : function()
	{
		// alert(this) ;
		TagHandleObj.MenuActive = true ;
		
		if(this.id != 'IcoSet')
		{
			if($(TagHandleObj.RollOverIconSet) != null)
				$(TagHandleObj.RollOverIconSet).hide();
				
			TagHandleObj.CurTagId = this.id ;
			
			if(AgregatorObj.CurrentAgregName == '')
			{
				$(TagHandleObj.RollOverIconSet + 'GenerateAgregate').show();
				$(TagHandleObj.RollOverIconSet + 'AddAgregate').hide();
			}
			else
			{
				$(TagHandleObj.RollOverIconSet + 'GenerateAgregate').hide();
				$(TagHandleObj.RollOverIconSet + 'AddAgregate').show();
			}
			
			if(this.innerHTML == AgregatorObj.CurrentAgregName)
			{
				$(TagHandleObj.RollOverIconSet + 'CloseAgregate').show();
				$(TagHandleObj.RollOverIconSet + 'AddAgregate').hide();
			}
			else
				$(TagHandleObj.RollOverIconSet + 'CloseAgregate').hide();
				
			$(TagHandleObj.RollOverIconSet + 'Blacklist').show();
			for(var i = 0 ; i < AgregatorObj.AgregateNames.length ; i++)
			{
				if(AgregatorObj.AgregateNames[i] == null)	continue ;
				if(AgregatorObj.Agregates[ AgregatorObj.AgregateNames[i] ] == null)	continue ;
				
				if(AgregatorObj.Agregates[ AgregatorObj.AgregateNames[i] ].indexOf(this.innerHTML) > -1)
				{
					$(TagHandleObj.RollOverIconSet + 'Blacklist').hide();
					break ;
				}
			}
			
			$(TagHandleObj.RollOverIconSet + 'DeleteAgregate').hide();
			if(AgregatorObj.AgregateNames.indexOf(this.innerHTML) > -1)
				$(TagHandleObj.RollOverIconSet + 'DeleteAgregate').show();
				
			$(TagHandleObj.RollOverIconSet + 'ReOpenAgregate').hide();
			if((AgregatorObj.AgregateNames.indexOf(this.innerHTML) > -1) && (this.innerHTML != AgregatorObj.CurrentAgregName))
			{
				$(TagHandleObj.RollOverIconSet + 'ReOpenAgregate').show();
				$(TagHandleObj.RollOverIconSet + 'GenerateAgregate').hide();
			}
			
			// IE bugs : when a multi-keyword expression of the tagcloud is over 2 lines, the left pos of the icons is very high
			// that's why we have Math.min(920, ...).
			$(TagHandleObj.RollOverIconSet).style.left = Math.min(920, (TagHandleObj.getLeftPos(this) + this.offsetWidth - 5)) + 'px' ;
			$(TagHandleObj.RollOverIconSet).style.top = (TagHandleObj.getTopPos(this) - 5) + 'px' ;
				
			$(TagHandleObj.RollOverIconSet).show();
		}
	}
	,
	__hideIcons : function()
	{
		// alert(this.id) ;
		TagHandleObj.MenuActive = false ;
		
		TagHandleObj.timerAutoHide() ;
	}
}