/*
	Author: Bruno Amaral
	Date: 26/10/2007
	Company: StepValue
	Description: 
		Functions of shortcut MECI from projecto MECI
		Supports IE & Firefox
*/

function selectedSC( id, selected )
{
	if ( id==undefined || id=="" ) return false;
	
	var obj = document.getElementById(id);
	
	if ( obj ==null || obj == undefined ) return false;
	
	if ( selected ) obj.className = 'linkboxsel';
	else obj.className = 'linkbox';
}

function showSCList( id, show )
{
	if ( id==undefined || id=="" ) return false;
	var obj = document.getElementById(id);
	if ( obj==null || obj== undefined ) return false;
	
	if ( show ) slide(id,true,3);
	else slide(id,false,3);
}

function overSC( source, id_sc, id_list )
{
	if (source.contais!=null && source.contains(event.fromElement))
		return false;
	
	selectedSC(id_sc,true);
	showSCList(id_list,true);
}

function outSC( source, id_sc, id_list )
{
	// Firefox don't have the contains method. However is event processing archicteture is different from IE and works fine
	if (source.contais!=null && source.contains(event.toElement))
		return false;
	
	selectedSC(id_sc,false);
	showSCList(id_list,false);
}


var sliders = Array();
function getSlider(id,time)
{
	var obj = null;
	if ( sliders.length > 0 )
		for( i=0; i < sliders.length; ++i )
			if ( sliders[i][0] == id )
				return sliders[i][1];
	
	if ( obj == null )
	{
		var len = sliders.push(Array(id,new slider(id,time)));
		return sliders[len-1][1];
	}
}

function slide( id, down, time )
{
	var obj = getSlider(id,time);
	
	if ( down ) obj.down();
	else obj.up();
}

function slideTick( id, down, time )
{
	var obj = getSlider(id,time);
	
	if ( down ) obj.downTick();
	else obj.upTick();
}

function slider(id,time)
{
	this.time = time;
	this.timer = "";
	this.id = id;
	this.obj = "";
	this.height = 0;
	this.start = false;
	this.down_on = false;
	this.up_on = false;
	
	this.down = function()
	{
		this.obj = document.getElementById(this.id);
		this.obj.style.display = "block";
		
		if ( !this.start )
		{
			if ( this.obj.height != undefined ) this.height = this.obj.height;
			else this.height = this.obj.offsetHeight;
			// It must have metrics to work in Firefox
			this.obj.style.height = "0px";
			this.start = true;
		}
		
		this.down_on = true;
		this.up_on = false;
		this.timer = setTimeout("slideTick('"+this.id+"',true,"+this.time+")",this.time);
	}
	
	this.up = function()
	{
		this.down_on = false;
		this.up_on = true;
		this.timer = setTimeout("slideTick('"+this.id+"',false,"+this.time+")",this.time);
	}
	
	this.downTick = function()
	{
		if ( !this.down_on ) return false;
		
		if ( this.height <= parseInt(this.obj.style.height) )
		{
			this.obj.style.height = this.height+"px";
			if ( this.timer != "" )
			{
				clearTimeout(this.timer);
				this.timer = "";
			}
			this.down_on = false;
			return true;
		}
		
		this.obj.style.height = (parseInt(this.obj.style.height) + 1) + "px";
		this.timer = setTimeout("slideTick('"+this.id+"',true,"+this.time+")",this.time);
	}
	
	this.upTick = function()
	{
		if ( !this.up_on ) return false;
		
		if ( parseInt(this.obj.style.height) <= 0 )
		{
			this.obj.style.height = "0px";
			this.obj.style.display = "none";
			clearTimeout(this.timer);
			this.timer = "";
			this.up_on = false;
			return true;
		}
		
		this.timer = setTimeout("slideTick('"+this.id+"',false,"+this.time+")",this.time);
		this.obj.style.height = (parseInt(this.obj.style.height) - 1)+"px";
	}
}



/*
 * Add event to elements
 */
var wload = window.onload;
window.onload = function()
{
	var tmp = document.getElementsByTagName("div");
	for(i=0;i<tmp.length;i++)
	{
		if (tmp[i].className=="linkbox")
		{
			attach_event(tmp[i],"mouseover",event_handler,false);
			attach_event(tmp[i],"mouseout",event_handler,false);
		}
	}
}

//Add an event to an element
function attach_event(obj,evt,fnc,useCapture)
{
	if (!useCapture) useCapture=false;
	if (obj.addEventListener)
	{
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	}
	else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else
		eval("obj.on"+evt+" = function() { "+fnc+"(evt); };");
} 

function event_handler(e)
{
	// Cross browser
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	var relTarg = e.relatedTarget || e.fromElement;
	
	var climb = tg;
	for(i=0;i<5;i++)
	{
		if (climb.tagName!="DIV" || !climb.id)
			climb = climb.parentNode;
		else break;
	}
	
	switch(e.type)
	{
		case "mouseover":
			overSC(climb,climb.id,"list_"+climb.id);
			break;
		case "mouseout":
			outSC(climb,climb.id,"list_"+climb.id);
			break;
	}
}
