﻿/**
 * redimensiona divs da janela
 */

function resizeWindow() {
    var newheight;
    var newWidth;
    
    var diff;
    
    var divBody = document.getElementById("divBody");
    var divBack = document.getElementById("divBack");

    if (divBack != null) {
        if (!document.all) {
            //firefox, others
            newheight = window.innerHeight;
            newWidth = window.innerWidth;
        } else {
            //IE
            //newheight = document.body.offsetHeight - 132;
            //newWidth = document.body.offsetWidth - diff;

            newheight = document.documentElement.clientHeight;
            newWidth = document.documentElement.clientWidth;

            //alert(document.documentElement.clientHeight);
        }
        document.getElementById("divBack").style.height = newheight;
        document.getElementById("divBack").style.width = newWidth;
    }
    
    if(divBody != null){
        if(divBody.style.left != "12px")
            diff = 250;
        else
            diff = 12;
        
        if (!document.all) {
            //firefox, others
            newheight = window.innerHeight - 132;
            newWidth =  window.innerWidth - diff;
        } else {
            //IE
            //newheight = document.body.offsetHeight - 132;
            //newWidth = document.body.offsetWidth - diff;
            
            newheight = document.documentElement.clientHeight - 132;
            newWidth = document.documentElement.clientWidth - diff;
            
            //alert(document.documentElement.clientHeight);
        }
        
        if(newWidth < 590){
            newWidth = 590;
        }
        if(newheight < 290){
           newheight = 290;
        }    
        
        if((newheight - 10) > 0){
            //document.getElementById("divMenu").style.height = (newheight - 13) + "px";
            //document.getElementById("divBody").style.height = (newheight - 10) + "px";
            document.getElementById("divMenu").style.height = (newheight) + "px";
            document.getElementById("divBody").style.height = (newheight) + "px";        
        }
        
        if(!document.all) {
            document.getElementById("divBody").style.textAlign =  "-moz-center";
        }
        
        if((newWidth - 10) > 0){
            document.getElementById("divBody").style.width = newWidth + "px";
        }
        
        resizeVisao(newWidth, newheight);
        
        //exibe a seta menus abaixo
        showMenuHide();
    }else{
        resizeVisao();
    }
}


/**
 * ajusta da janela de visoes
 */
function resizeVisao(newWidth, newheight){

    //alert(document.documentElement.clientHeight + "<>" + document.body.offsetHeight);

    if(newWidth == null){   
        newheight = document.documentElement.clientHeight;
        
        if (!document.all) {
            //firefox, others
            //newheight = window.innerHeight;
            newWidth =  window.innerWidth;
        } else {
            //IE
            //newheight = document.body.offsetHeight;
            
            //newWidth = document.body.offsetWidth;
            newWidth = document.documentElement.clientWidth;
        }
    
        if(newWidth < 500){
            newWidth = 500;
        }
        
        if(newheight < 200){
            newheight = 200;
        }     
    }
    
    if(document.getElementById("divGrdVisao") != null){
    
       document.getElementById("divGrdVisao").style.width = (newWidth - 26) + "px";
       document.getElementById("divGrdVisao").style.height = (newheight - 120) + "px";       
       document.getElementById("tbGrdVisao").style.width = (newWidth - 20) + "px";

    }    
 
}

function escondeMenuIE(e) 
{
     if (navigator.appName == "Microsoft Internet Explorer" && (event.button == "2" || event.button == "3"))
     {
          escondeMenu();
          return false;
     }
}

function escondeMenuNS(e) 
{
     if (document.layers || (document.getElementById && !document.all))
     {
          if (e.which == "2" || e.which == "3")
          {
                 escondeMenu();
                 return false;
          }
     }
}

function escondeMenu(){
    var divBody = document.getElementById("divBody");
    if(divBody != null){
    
        if(divBody.style.left != "12px")
            divBody.style.left = "12px";
        else
            divBody.style.left = "250px";

        resizeWindow();
    }
}

function escondeMenu2() {
    var divBody = document.getElementById("divMenu");
    if (divBody != null) {

        if (divBody.style.width != "12px")
            divBody.style.width = "12px";
        else
            divBody.style.width = "250px";

        resizeWindow();
    }
}

/**
 * exibe div modal
 */
function showModal(show) {
    if(document.getElementById("divModal") != null){
        if(show){
            document.getElementById("divModal").style.visibility = "visible";
        }else{
            document.getElementById("divModal").style.visibility = "hidden";
        }
    }
}

/**
 * expande todos os menus caso tenha espaço
 */ 
function verificaMenus(){
    var maior = verificaHeightMenus(true);
    var divMenus = document.getElementById("divMenus");
    
    if(maior < 1){
        var thisChild = null;  //divMenus.firstChild;
	    while ( thisChild != null )
	    {
		    if ( thisChild.nodeType == 1 )
		    {
			    showHideMenu(thisChild.id);
		    }
		    thisChild = thisChild.nextSibling;
	    }  
    }
}

/**
 * verifica se os heights dos menus expandido ou atual e maior que o da pagina
 */
function verificaHeightMenus(expandido){
    var divMenus = document.getElementById("divMenus");
    var thisChild = null; //divMenus.firstChild;
    var divHeight = null;  //divMenus.offsetHeight;
    var divItens;
    var pageheight;
    
    if(expandido == true){
    
        while ( thisChild != null )
        {
	        if ( thisChild.nodeType == 1 )
	        {
               divItens = document.getElementById("divItens" + thisChild.id);            
		       divHeight = divHeight + divItens.offsetHeight;
	        }
	        thisChild = thisChild.nextSibling;
        }   
    }
    
    if (!document.all) {
        //firefox, others
        pageheight = window.innerHeight - 132;
    } else {
        //IE
        //pageheight = document.body.offsetHeight - 132;
        
        pageheight = document.documentElement.clientHeight - 132;
    }
    
    if(divHeight > pageheight){
        //exibe alerta de menus escondidos
        return 1;
    }   

    return 0;        
}

/**
 * retrai ou expande o menu passado no parametro
 */
function showHideMenu(idDiv){
    var divMnu = document.getElementById(idDiv);    
    var divImg = document.getElementById("img" + divMnu.id);
    var divItens = document.getElementById("divItens" + divMnu.id);       
    
    if(divItens.style.visibility == "visible" ||
        divItens.style.visibility == "" ){

        divImg.src = divImg.src.replace("btn11.jpg", "btn22.jpg"); //"themes/blue/barra-btn22.jpg";
        divItens.style.visibility = "hidden"; 
        divItens.style.position = "absolute";
     }else{
        divItens.style.visibility = "visible";
        if (typeof document.body.style.maxHeight != "undefined") {
            //IE7, firefox, safari, opera
            divItens.style.position = "relative";
        }else{           
            divItens.style.position = "fixed";
        }
        
        divImg.src = divImg.src = divImg.src.replace("btn22.jpg", "btn11.jpg");//"themes/blue/barra-btn11.jpg";
    }   
    
    showMenuHide();
}

/**
 * exibe a seta menus abaixo
 */
function showMenuHide(){
    var maior = verificaHeightMenus(false);
    var divMenuHide = document.getElementById("divMenuHide");
    var divMenuHideUp = document.getElementById("divMenuHideUp");

    if(divMenuHide != null){
        if(maior > 0){
            divMenuHide.style.visibility = "visible";
        }else{
            divMenuHide.style.visibility = "hidden";
            divMenuHideUp.style.visibility = "hidden";
        }
    }
}

var t;

/**
 * scroll to bottom
 */
function scrollBottom(pos){
    var divMenuHide = document.getElementById("divMenuHide");
    var divMenuHideUp = document.getElementById("divMenuHideUp");
    var divMenu = document.getElementById("divMenu");
    var pageheight; 
    var scrollMax;
        
    if (!document.all) {
        //firefox, others
        pageheight = window.innerHeight - 132;
        scrollMax = divMenu.scrollHeight - pageheight + 17;
    } else {
        //IE
        //pageheight = document.body.offsetHeight - 132;
        pageheight = document.documentElement.clientHeight - 132;
        
        scrollMax = divMenu.scrollHeight - pageheight + 25;
    }    
    
    if(pos == null){
        pos = divMenu.scrollTop;
    }
        
    if(pos > scrollMax){
        clearTimeout(t);
        divMenuHide.style.visibility = "hidden";
        divMenuHideUp.style.visibility = "visible";    
    }else{
        pos = pos + 10;
        divMenu.scrollTop = pos;
        t=setTimeout('scrollBottom(' + pos + ')', 40);    
    }
}

/**
 * scrool to top
 */
function scrollToTop(pos){
    var divMenuHide = document.getElementById("divMenuHide");
    var divMenuHideUp = document.getElementById("divMenuHideUp");
    var divMenu = document.getElementById("divMenu");
    var pageheight; 
        
    if (!document.all) {
        //firefox, others
        pageheight = window.innerHeight - 132;
    } else {
        //IE
        //pageheight = document.body.offsetHeight - 132;
        pageheight = document.documentElement.clientHeight - 132;
    }    
    
    if(pos == null){
        pos = divMenu.scrollTop;
    }
    
    pos = pos - 10;
        
    if(pos <= 0){
        divMenu.scrollTop = 0;
        clearTimeout(t);
        divMenuHide.style.visibility = "visible";
        divMenuHideUp.style.visibility = "hidden";    
    }else{
        divMenu.scrollTop = pos;
        t=setTimeout('scrollToTop(' + pos + ')', 40);    
    }
}

/**
 * retorna a posicao X do objeto na tela, utilizado pelo visoes.js
 */
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

/**
 * retorna a posicao Y do objeto na tela
 */
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

/**
 * insere a nova linha na linha abaixo
 */
function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}  

/**
 * recupera a linha em que o objeto se encontra
 */
function getParentRow(obj) {
    var tmp = obj;
    while (tmp = tmp.parentNode)
         if (tmp.nodeName.toUpperCase() == "TR")
              return tmp;
}  

/**
 * recupera o nome da tabela
 */
function getParentTable(obj) {
    var tmp = obj;
    while (tmp = tmp.parentNode)
         if (tmp.nodeName.toUpperCase() == "TABLE")
              return tmp;
}  

/**
* Desabilita botão de voltar a página utilizando backspace
*/
function disableBackButton(evt)
{
    evt = evt || window.event;

    if(evt.keyCode == 8 || evt.keyCode == 13)
    {
        var ref=(evt.target) ? evt.target : evt.srcElement;
        var t = ref.type;
        
        if(t != "text" && t != "textarea" && t != "file")
        {
            return false;
        }
    }
    
    return true;
}

function scrollItemGrid(divGrid) //, objToScroll
{
    var objDiv = document.getElementById(divGrid);
    //var objToScroll = document.getElementById(objToScroll);
    
    //var diff = findPosY(objToScroll) - findPosY(objDiv);
    
    //objDiv.scrollTop = diff;
    objDiv.scrollTop = scrollPos;
}

function scrollPageTop() {
    window.scrollTo(0, 0);
}

function scrollDiv(objDiv)
{
    scrollPos = objDiv.scrollTop;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if (document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
}

function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

    //make image transparent
    changeOpac(0, imageid);

    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for (i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + i + ",'" + imageid + "')", (timer * speed));
        timer++;
    }
}

function currentOpac(id, opacEnd, millisec) {
    //standard opacity is 100
    var currentOpac = 100;

    //if the element has an opacity set, get it
    if (document.getElementById(id).style.opacity < 100) {
        currentOpac = document.getElementById(id).style.opacity * 100;
    }

    //call for the function that changes the opacity
    opacity(id, currentOpac, opacEnd, millisec)
}

//right click instrucoes
//document.onmousedown=escondeMenuIE;
//document.onmouseup=escondeMenuNS;
document.oncontextmenu=new Function("return false");

var scrollPos = 0;