
function mainmenu(){

var descendant = new Array();
var orientation = 1;
var ie6 = $.browser.msie && $.browser.version == "6.0";


$("#nav li").find('img').attr("src",new Arrow($('#nav a').css("background-color").hex()).src);
$("#nav ul ").css({display: "none"}); // Opera Fix

$(" #nav li").hover(function(){
	var display = false;	
	
	$(this).find('ul:first > li').each(function(){
		if($(this).css("display") != "none"){
			display = true;				
		}
	})
	
	if (display){
		
		//$("body").css({overflow:'hidden'});
		var ul = $(this).find('ul:first');
		
		
		$(this).find(':first img').attr("src",new Arrow($(this).find('a').css("background-color").hex()).src);
		
		var left = $(this).width() -1;
		var top = -1;
		
		//first level
		if($(this).parent().get(0).id == "nav") {
			left = -1;
			orientation = 1;
			top = 23;
		}
		
		index = ul.css("z-index");
		
		if (descendant.length > 0){
			if (index > descendant[0]){
				orientation = 1;
			}
		descendant.length = 0;
		}
		descendant.push(index);
		
		ul.css({visibility: "visible",display: "none",left: orientation * left + "px", top:top}).show(0);
		
		if(xPos(this) > screen.width + 4){
			orientation = -1;
			ul.animate({ left: orientation * left},0);
		}else if(xPos(this) < 0){
			orientation = 1;
			ul.animate({ left: orientation * left},0);
		}
		
	}
	
	
	//scroller($(this).parent());

		
		
	},function(){
		$(this).find('ul:first')
		.css({visibility: "hidden",display: "none"});
		$(this).find(':first img').attr("src",new Arrow($('#nav a').css("background-color").hex()).src);
	});
}

function scroller(element){
	var ul  = $(element);
	//se a altura for maior q 0 a altura disponivel será menor
	//se chegar a metade do screen disponivel começo a jogar para cima
	//trace($('body').scrollTop())
	var stage = 581 - ul.parent().offset().top
	if(ul.height() > stage){
	var originalHeight = ul.height();	
	ul.bind("mousemove", function(e){
	var y = ((e.pageY - $(this).offset().top) * ((stage-10)-originalHeight) / originalHeight) +5;
		if (y <= 0 && y >= -stage){						
			$(this)
			.css('clip','rect('+-y+'px '+screen.width+'px '+originalHeight+'px '+-screen.width+'px)')						
			.animate({top: y},0);
		}
	})
	ul.bind("mouseleave", function(e){
		$(this).css('clip','rect(auto auto auto auto)');
	})
	
	}else{
		ul.parent().parent().unbind("mousemove");
	}
}


/*
function scroller(element){
	var ul = $(element);
	if(ul.height() > 400){				
		//clip area
		$('body').css({overflow:'hidden'});
		var originalHeight = ul.height();
		ul.mouseenter(function(){
			$(this).bind("mousemove", function(e){
				var y = ((e.pageY - $(this).offset().top) * (400-originalHeight) / originalHeight) + 10;
				if (y <= 0 && y >= -450){						
					$(this)
					.css('clip','rect('+-y+'px '+screen.width+'px '+originalHeight+'px '+-screen.width+'px)')						
					.animate({ top: y},0);
				}
				// Se a tua posição for menor que left ou maior que left + width siginifica que saiu
				if(e.pageX < $(this).offset().left || e.pageX > ($(this).offset().left + $(this).width())){
					//$(this).unbind("mousemove");
				}
			})
		})
	
		ul.mouseleave(function(){
			$(this)
			unbind("mousemove")
			.css('clip','rect(auto auto auto auto)');
			$('body').css({overflow:'auto'});
		})
		
	}
}
*/

function trace(ex){	

	if($("#trace")[0]){
		$("#trace").html(ex);
	}else{
		$("body").before('<span id="trace"></span>');
		$("#trace").html(ex);
	}
}

function xPos(element){
	var x = $(element).find('ul:first').offset().left + $(element).width() ;
	return x;
} 

function Arrow(color){

	if (dark(color)){
		this.src = "admin/images/right_white.gif";
	}else{
		this.src = "admin/images/right_black.gif";
	}
}

String.prototype.hex = function()
{ var b="", x=this;
  if(x.substr(0,1)!="r") { return x; }
  for (a in x=x.substr(4,x.length-5).split(", "))
  { b+=((x[a]<16)?"0":"")+(x[a]&0xff).toString(16); }
  return "#"+b;
}

function dark(color){
	
	vermelho = color.substring(1,3)
	verde = color.substring(3,5)
	azul = color.substring(5,7)
	
	//vou converter a inteiros os string, que tenho em hexadecimal
	intvermelho = parseInt(vermelho,16)
	intverde = parseInt(verde,16)
	intazul = parseInt(azul,16)
	
	//agora somo os valores
	obscuridade = intvermelho + intverde + intazul
	
	//se o valor obscuridade for menor que ((255 + 255 + 255) / 2) é que é uma cor mais escura
	if (obscuridade<(255+255+255)/2){
		return true;
	}else{
		return false
	}
}

$(document).ready(function(){
      if( $('#navigation').length > 0 ){
		  mainmenu();
      }	
});
