var t;
var tMenu;
var listaMenus = new Array;
var listener;

var vMaxAltura = 0;
var vMaxBar = 194;
document.observe("dom:loaded", configMenu );

function dragged(event)
{
	vArg1 = $A(arguments)[1];
	vArg2 = $A(arguments)[2];

	vObj = $("scrollball");
	vDIV = $("area-principal");


	vPosition = parseInt( Event.pointerY(event) - vArg1 + vArg2 );

	if( vPosition <= 0 )
	{
		vPosition = 0;
		vDIV.scrollTop = 0;	
	}else if( vPosition >= vMaxBar )
	{
		vPosition = vMaxBar;
		vDIV.scrollTop = vMaxAltura;	
	}else
	{
		vScroll = ( vMaxAltura * vPosition )/vMaxBar;
		vDIV.scrollTop = Math.round( vScroll );
	}
	
	vObj.style.top = vPosition + "px"
}

function configScroll()
{
	vDIV = $("area-principal");
	
	vMaxAltura = vDIV.scrollHeight - 411;

	vtemp = vDIV.scrollTop;
	vDIV.scrollTop = vMaxAltura + 3000;
	

	vMaxAltura = vDIV.scrollTop;
	vDIV.scrollTop = vtemp;


	if( vMaxAltura  <= 0 )
		{$("scrolls").style.display = "none"}
	else {
		updateScroller();
		vObj = $("scrollball");
		vObj.observe( 'mousedown' , startdrag );
	}


}

function startdrag(event)
{
	vObj = $("scrollball");
	listener = dragged.bindAsEventListener(vObj , Event.pointerY(event) , parseInt(vObj.style.top) ) ;
	Event.observe( document , 'mousemove' , listener )
	//Event.observe( vObj , 'mousemove' , listener )
	document.observe( 'mouseup' , stopdrag );
}


function stopdrag()
{
	Event.stopObserving( document , 'mousemove' , listener )
	Event.stopObserving( document ,'mouseup' , stopdrag );
	//Event.stopObserving( vObj , 'mousemove' , listener )

}
function scrollUP() {

	vDIV = $("area-principal");
	vAlturaAtual = vDIV.scrollTop;


	if( vAlturaAtual >= vMaxAltura )
	{
		stopScroll();
	}else	{
		vDIV.scrollTop = vDIV.scrollTop + 1;
		updateScroller();
		t = setTimeout( "scrollUP()" , 2 );
	}

}

function updateScroller()
{
	vDIV = $("area-principal");
	vAlturaAtual = vDIV.scrollTop;

	if( vAlturaAtual == 0 )
		{$("scrollball").style.top = 0;}
	else {

		vPosition = parseInt((vAlturaAtual * vMaxBar) / vMaxAltura);
		if( vPosition < 0 )
			{vPosition = 0}
		else if( vPosition > vMaxBar )
			{vPosition = vMaxBar}
		$("scrollball").style.top = Math.round(vPosition) + "px";
	}
}

function scrollDOWN() {
	vDIV = $("area-principal");
	vAlturaAtual = vDIV.scrollTop;


	if( vAlturaAtual <= 0 )
	{
		stopScroll();
	}else	{
		vDIV.scrollTop = vDIV.scrollTop  - 1 ;
		updateScroller();
		t = setTimeout( "scrollDOWN()" , 2 );
	}
}


function stopScroll(){
	if(t)
	   	{clearTimeout(t);}
}



function hideMenu()
{
	listaMenus.forEach( function (element){
			clearTimeout( this.TimerAppear ) ;
			element.startFade();
		}
	);
}

function hideOtherMenu( vID )
{
	listaMenus.each( function (element){
		if( element.myID != vID )
		{
			clearTimeout( this.TimerAppear );
			element.startFade();
		} 
	});
	
}

function tryHideMenu(event) 
{
	(event.target)? vThis = event.target : vThis = event.srcElement
	vMenu = $(vThis).up("div.MenuPopUp");
	vObj = oMenus[vMenu.id];
	vObj.TimerFade = setTimeout( "listaMenus[" + vObj.myIndex + "].startFade()" , 500);
}

function tryHide(vObj) 
{
	vObj.TimerFade = setTimeout( "listaMenus[" + vObj.myIndex + "].startFade()" , 500);
}


function stopHide(event){
	(event.target)? vThis = event.target : vThis = event.srcElement
	vMenu = $(vThis).up("div.MenuPopUp");
	vObj = oMenus[vMenu.id];
	clearTimeout( vObj.TimerFade );
	vObj.startAppear();
}


var PMenu = Class.create();
PMenu.prototype = {
	initialize:function (vID)
	{ 
		this.myID = vID;
		vOBJ = $(vID);
		this.disabled = true;
		this.opacity = 0;
		this.TimerFade = null;
		this.TimerAppear = null;
		this.action = "";

		vIndex = listaMenus.length;
		listaMenus.length = vIndex + 1;
		listaMenus[vIndex] = this;
		this.myIndex = vIndex;

		PopMenu =  $(this.myID); 
		vLinks = PopMenu.select( "a" );
		vLinks.each( function(s) 
		{ 

			s.observe( 'mouseout' , tryHideMenu.bindAsEventListener(s) );
			s.observe( 'mouseover' , stopHide.bindAsEventListener(s) );
		});

	},
	showMenu: function ( vOBJ )
	{
		this.disabled = false;
		clearTimeout( this.TimerFade );

		vSize = $(vOBJ).getDimensions();
		vPosition = $(vOBJ).cumulativeOffset();
		PopMenu =  $(this.myID); 

		PopMenu.style.top = (vSize.height + vPosition.top + 1) + "px";
		PopMenu.style.left = vPosition.left + "px";

		this.startAppear()

	},

	startFade:function()
	{
		this.disabled = true;
		clearTimeout( this.TimerAppear );		
		this.fade();
	},
	fade:function()
	{
		if( this.disabled )
		{	
			this.opacity = this.opacity - 0.1
		
			if( this.opacity <= 0 )
				{this.opacity = 0;}


			vOBJ = $(this.myID);
			vOBJ.style.MozOpacity = this.opacity;
			vOBJ.style.opacity = this.opacity;
			vOBJ.style.filter= ("alpha(opacity="+ parseInt(this.opacity*100)+")")
		
			if( this.opacity <= 0 )
			{
				vOBJ.style.display = "none";
				this.action = "";
			}else {
				this.TimerFade = setTimeout( "listaMenus[" + this.myIndex + "].fade()" , 0.5);
			}
		} else {
			clearTimeout( this.TimerFade );
			this.startAppear();
		}
	},
	startAppear:function()
	{
		this.disabled = false;
		vOBJ.style.display = "block";
		hideOtherMenu( this.myID );
		this.action = "appear";
		clearTimeout( this.TimerFade ) ;
		this.appear();
	},
	appear:function(pe)
	{
		if( this.disabled == false)
		{		
			this.opacity = this.opacity + 0.05

			if( this.opacity >= 1 )
				{this.opacity = 1;}
		
			vOBJ = $(this.myID);
			vOBJ.style.display = "block";
			vOBJ.style.MozOpacity = this.opacity;
			vOBJ.style.opacity = this.opacity;
			vOBJ.style.filter= ("alpha(opacity="+ parseInt(this.opacity*100)+")")

			if( this.opacity >= 1 )
				{this.action = "";}			
			else {
				this.TimerAppear = setTimeout( "listaMenus[" + this.myIndex + "].appear()" , 0.5);
			}
		} else {
			clearTimeout( this.TimerAppear ) ;
			this.startFade()
		} 
	}
}


function getSourceObj(evento){
	var ie_var = "srcElement";
	var moz_var = "target";
	
	evento[moz_var] ? e_out = evento[moz_var]: e_out = evento[ie_var];
	return e_out;
}

function menu_mouseover( el , vStyle )
{
	el = Event.element(el);
	while( !el.hasClassName("item-menu" ) && !el.hasClassName("item-menu-ativo" ) )
	{
		el = el.up();
	}
	el.setStyle( vStyle  );
}

MyColorBlue = {color:'#0000FF'}
MyNoColor = {color:'#B9C5CD'}

function configMenu() {
	$$( ".item-menu" ).each( function( s ) {
		s.observe( "mouseover" , menu_mouseover.bindAsEventListener( s , MyColorBlue ) );
		s.observe( "mouseout" , menu_mouseover.bindAsEventListener( s , MyNoColor ) );
	})
}