/**
 *
 *
 *
 * TODO: (fazer funcionar com multi Abas (atualmente nao funciona legal)
 *
 *
 */
var __oUI;
function ui(){
    //TabAtiva
	this.tabAtiva;
	//linkAtivo
	this.linkAtivo;
	//Cor da Borda que irá ser Realçada
	this.borderColor = '#817F96';
	//Tabs da Page
	this.Tabs;
	this.TabHorizontal = 1;
	this.TabVertical   = 2;
}
/**
 * Construtor (Inicializador das propiedades)
 *
 * @param String linkAtv
 * @param String tabAtv
 * @param String formName
 *
 */
ui.prototype.__construct = function(linkAtv, tabAtv, formName){
/*	for(formulario in document.forms){
		try{
			//if(formulario != 'length'){
				document.forms[formulario].onmouseover = this.resaltaCampo;
				document.forms[formulario].onmouseout  = this.resaltaCampo;
				//alert(document.forms[formulario].);
				for(var i=0; i<document.forms[formulario].elements.length; i++){
					document.forms[formulario].elements[i].selecionado = false;
					try{

						 document.forms[formulario].elements[i].formName = formulario;
						if(document.forms[formulario].elements[i].onblur != null){
							document.forms[formulario].elements[i].uOnBlur = document.forms[formulario].elements[i].onblur;
						}
						if(document.forms[formulario].elements[i].onfocus != null){
							document.forms[formulario].elements[i].uOnFocus = document.forms[formulario].elements[i].onfocus;
						}
					}catch(e){}
					document.forms[formulario].elements[i].onfocus = this.focaCampo;
					document.forms[formulario].elements[i].onblur  = this.blurCampo;
				}
			//}
		}catch(ee){}
	}*/
}

/**
 * Acao de quando sai do campo
 *
 */
ui.prototype.blurCampo = function(){
	var nodeName = this.nodeName;
	var name = this.name;
	var formulario = this.formName;
	if(nodeName == 'INPUT'){
		try{
			document.forms[formulario].elements[name].selecionado = false;
			try{
				document.forms[formulario].elements[name].uOnBlur();
			}catch(ee){}
			document.forms[formulario].elements[name].style.borderColor = '';
		}catch(e){}
	}
}

/**
 * Açao quando Foca o Campo (input)
 *
 */
ui.prototype.focaCampo = function(){
	var nodeName = this.nodeName;
	var name = this.name;
	var formulario = this.formName;
	if(nodeName == 'INPUT'){
		try{
			document.forms[formulario].elements[name].selecionado = true;
			document.forms[formulario].elements[name].style.borderColor = __oUI.borderColor;
		}catch(e){}
	}
}

/**
 * Resalta o Campo (input)
 *
 */
ui.prototype.resaltaCampo = function(e){
	var nodeName = '';
	var name = '';
	var formulario = '';
	try{
		nodeName   = event.srcElement.nodeName;
		name       = event.srcElement.name;
		formulario = event.srcElement.formName
	}catch(expt){
		nodeName   = e.target.nodeName;
		name       = e.target.name;
		formulario = e.target.formName;
	}
	if(nodeName == 'INPUT'){
		try{
			if(document.forms[formulario].elements[name].style.borderColor != ''){
				if(document.forms[formulario].elements[name].selecionado == false){
					document.forms[formulario].elements[name].style.borderColor = '';
				}
			}else{
				document.forms[formulario].elements[name].style.borderColor = __oUI.borderColor;
			}
		}catch(e){}
	}
}


/**
 * Troca de abas
 *
 */
ui.prototype.switchTab = function(GroupTabName, tabId){
    if(GroupTabName == ''){
        for(group in this.Tabs){
			if(group != 0){
				break;
			}
		}
		GroupTabName = group;
    }
    try{
	    var tab = this.Tabs[GroupTabName][tabId]['corpo'];
	    var Obj = this.Tabs[GroupTabName][tabId]['link'];
	    if(tab.style.visibility == 'hidden'){
		    try{
			    this.linkAtivo.className = 'tab';
 			    this.tabAtiva.style.visibility = 'hidden';
			    this.tabAtiva.style.position   = 'absolute';
			    this.tabAtiva.style.display   = 'none';
		    }catch(e){}

		    tab.style.visibility = 'visible';
		    tab.style.display = 'block';
		    Obj.className = 'tab_ativa';
		    tab.style.position   = 'static';
		    this.tabAtiva = tab;
		    this.linkAtivo = Obj;
		    for(formulario in document.forms){
		        try{
			        if(formulario != 'length'){
			            for(var i=0; i<document.forms[formulario].elements.length; i++){
			                 if(document.forms[formulario].elements[i].name == 'groupativo'){
			                    document.forms[formulario].elements[i].value = GroupTabName;
			                 }
			                if(document.forms[formulario].elements[i].name == 'tabAtiva'){
			                   document.forms[formulario].elements[i].value = tabId;
			                }
			            }
			        }
		        }catch(ee){}
	        }
	    }else{
	    }
	}catch(ee){}
}
/**
 * Limpa um Determinado Select q eh passado por parametro
 *
 */
ui.prototype.emptySelect = function(selectName){
    selectName.options.length = 0;
}


ui.prototype.startTabsEngine = function(ArrayGroupTabs, StyleType){
    this.Tabs = new Array(ArrayGroupTabs.lenght);
    for(key in ArrayGroupTabs){
		//Anda nos Filhos Buscando cada Tab na Lista
		this.Tabs[ArrayGroupTabs[key]] = new Array(document.getElementById(ArrayGroupTabs[key]).childNodes.length);
		for(keyChild in document.getElementById(ArrayGroupTabs[key]).childNodes){
			var tab = document.getElementById(ArrayGroupTabs[key]).childNodes[keyChild];
			if(tab.id != null){
				this.Tabs[ArrayGroupTabs[key]][tab.id] = new Array(2);
				if(tab.href != ''){
				    tab.href += "javascript:__oUI.switchTab('"+ArrayGroupTabs[key]+"','"+tab.id+"');";
				}
				this.Tabs[ArrayGroupTabs[key]][tab.id]['corpo'] = document.getElementById(tab.id+'_corpo');
				this.Tabs[ArrayGroupTabs[key]][tab.id]['link' ] = tab;
				try{
					this.Tabs[ArrayGroupTabs[key]][tab.id]['corpo'].style.visibility = 'hidden';
					this.Tabs[ArrayGroupTabs[key]][tab.id]['corpo'].style.position = 'absolute';
					this.Tabs[ArrayGroupTabs[key]][tab.id]['corpo'].style.display   = 'none';
				}catch(ee){}
			}
		}

		if(StyleType == this.TabVertical){
		    for(key in ArrayGroupTabs){
		        while(document.getElementById(ArrayGroupTabs[key]).childNodes.length > 0){
		            try{
                        document.getElementById(ArrayGroupTabs[key]).removeChild(document.getElementById(ArrayGroupTabs[key]).firstChild);
                    }catch(ee){}
		        }
		    }
		    var table = document.createElement('TABLE');
		    table.cellPadding = '0px';
		    table.cellSpacing = '0px';
		    table.width = '170px';
		    var tbody = document.createElement('TBODY');
		    table.appendChild(tbody);
		    for(group in this.Tabs){
			    if(group != 0){
				    for(tab in this.Tabs[group]){
					    if(tab != 0){
					        var tr = document.createElement('TR');
					        var td = document.createElement('TD');
					        td.align="left";
                            td.appendChild(this.Tabs[group][tab]['link']);
                            tr.appendChild(td);
                            table.tBodies[0].appendChild(tr);
					    }
				    }
				    document.getElementById(group).appendChild(table);
			    }
		    }
		}

		//Primeira Ocorrencia
		var tabAtiva = '';
		for(group in this.Tabs){
			if(group != 0){
				for(tab in this.Tabs[group]){
					if(tab != 0){
						this.setActiveTab(group,tab);
						tabAtiva = tab;
						break;
					}
				}
				break;
			}
		}

		for(formulario in document.forms){
	    try{
		    if(formulario != 'length'){
		     var input = document.createElement('INPUT');
		     input.type = 'hidden';
		     input.name = 'tabAtiva';
		     input.id = 'tabAtiva';
		     input.value = tabAtiva;
		     document.forms[formulario].elements.appendChild(input);

		     var input = document.createElement('INPUT');
		     input.type = 'hidden';
		     input.name = 'groupativo';
		     input.id = 'groupativo';
		     input.value = group;
		     document.forms[formulario].elements.appendChild(input);
		    }
	    }catch(ee){}
    }

    }
}

ui.prototype.starTabControl = function(fomulario){
     var input = document.createElement('INPUT');
     var input1 = document.createElement('INPUT');
     input.type = 'hidden';
     input.name = 'tabAtiva';
     input.id = 'tabAtiva';
     input1.type = 'hidden';
     input1.name = 'groupativo';
     input1.id = 'groupativo';
     document.forms[formulario].elements.appendChild(input);
     document.forms[formulario].elements.appendChild(input1);
}

ui.prototype.setActiveTab = function(GroupTName, TName){
    if(TName != '' && GroupTName != ''){
	    this.switchTab(GroupTName,TName);
	}
}




/* TODO */
/**
 * Lista de Links
 *  Ex.: < ... 5 6 7 8 9 10 11 12 13 14 15 ... >
 *
 */
ui.prototype.newLst = function(){}
ui.prototype.setLinkLst = function(){}
ui.prototype.setOnClickLst = function(){}
ui.prototype.setCssClassLst = function(){}
ui.prototype.setContentLst = function(){}

ui.prototype.backLst = function(){}
ui.prototype.nextLst = function(){}
ui.prototype.currentLst = function(){}



__oUI = new ui();



//Captura de eventos
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
	try{
	    if (document.layers) {
	        // When the page scrolls in Netscape, the event's mouse position
	        // reflects the absolute position on the screen. innerHight/Width
	        // is the position from the top/left of the screen that the user is
	        // looking at. pageX/YOffset is the amount that the user has
	        // scrolled into the page. So the values will be in relation to
	        // each other as the total offsets into the page, no matter if
	        // the user has scrolled or not.
	        xMousePos = e.pageX;
	        yMousePos = e.pageY;
	        xMousePosMax = window.innerWidth+window.pageXOffset;
	        yMousePosMax = window.innerHeight+window.pageYOffset;
	    } else if (document.all) {
	        // When the page scrolls in IE, the event's mouse position
	        // reflects the position from the top/left of the screen the
	        // user is looking at. scrollLeft/Top is the amount the user
	        // has scrolled into the page. clientWidth/Height is the height/
	        // width of the current page the user is looking at. So, to be
	        // consistent with Netscape (above), add the scroll offsets to
	        // both so we end up with an absolute value on the page, no
	        // matter if the user has scrolled or not.
	        xMousePos = window.event.x+document.body.scrollLeft;
	        yMousePos = window.event.y+document.body.scrollTop;
	        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
	        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
	    } else if (document.getElementById) {
	        // Netscape 6 behaves the same as Netscape 4 in this regard
	        xMousePos = e.pageX;
	        yMousePos = e.pageY;
	        xMousePosMax = window.innerWidth+window.pageXOffset;
	        yMousePosMax = window.innerHeight+window.pageYOffset;
	    }
    }catch(e){
    }
}



function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
}

var timer;

function bodyOverlay() {
	  var objBody = document.getElementsByTagName('body').item(0);
	  var sizesPage = getPageSize();
	  var bodyOverlay = document.createElement("div");
	  bodyOverlay.setAttribute('id','bodyOverlay');
	  bodyOverlay.style.height = arrayPageSize[1] + 'px'; // fundo com o tamanho total da página.
	  if (!document.getElementById('bodyOverlay')) {
		    objBody.insertBefore(bodyOverlay, objBody.firstChild);
	  }
}

function removerOverlay() {
	  var bodyOverlay = document.getElementById('bodyOverlay');
	  if (bodyOverlay) {
		    bodyOverlay.parentNode.removeChild(bodyOverlay);
	  }
}