var strOpt = '<table><tr><td><a href="[link]" onmouseover="AltBackTR(this, \'#afbae4\')" onmouseout="AltBackTR(this, \'#e8e8e8\')"><img src="[img]" border="0" width="16" height="16"></a></td>';
strOpt += '<td><a href="[link]" onmouseover="AltBackTR(this, \'#afbae4\')" onmouseout="AltBackTR(this, \'#e8e8e8\')" class="textOpt">[text]</a></td></tr></table>';
var arrOptions = new Array();
var staticTip = new TipObj('staticTip');

var domain = document.location.protocol +'//'+ document.location.hostname + '/';

inicializeMenu();

function expandContent(winId)
  {
  var state = (document.getElementById(winId).style.display=="none") ? "on" : "off";
  openContent(winId, state);
  }

function openContent(winId, state)
  {
  if (!document.getElementById(winId + "_imgExp"))
    return;

  var img = document.getElementById(winId + "_imgExp").src;
  var win = document.getElementById(winId);
  var display = (state == "off") ? "none" : "";
  var imgant = (state == "off") ? "on" : "off";

  document.getElementById(winId + "_imgExp").src = img.replace(imgant, state);
  win.style.display = display;
  }

var ultSelLineId;
function SelectedLineByChart(e)
  {
  var obj = e.data.Attributes['cell'];
  if (obj != undefined)
    {
    document.getElementById(obj).style.backgroundColor = '#FFFCDF';
    ultSelLineId = obj;
    }
  }

function UnselectedLineByChart(e)
  {
  document.getElementById(ultSelLineId).style.backgroundColor = '';
  }

function selectAba(obj, objId)
  {
  try { if (!obj.parentNode) return; }
  catch(e) { return; }

  var dad = getDad(obj);
  while (dad.tagName != "UL")
    {
    dad = getDad(dad);
    if (!dad) break;
    }

  var lis = dad.getElementsByTagName('li');
  for(var i = 0; i < lis.length; i++)
    lis[i].className = "";

  dad = getDad(obj);
  while (dad.tagName != "LI")
    {
    dad = getDad(dad);
    if (!dad) break;
    }

  dad.className = "abas_sel";
  openContent(objId, "on");
  }

function AltBackTR(obj, color)
  {
  if (!obj.parentNode) return;

  var dad = getDad(obj);
    while (dad.className != "opt_table_data")
      dad = getDad(dad);

    dad.style.backgroundColor = color;
    }

function getDad(obj)
  {
  return obj.parentNode;
    }

function DefineConfOption()
  {
    //staticTip.showDelay = 50;
    //staticTip.hideDelay = 200;
    //staticTip.doFades = false;
    //staticTip.minAlpha = 0;
    //staticTip.maxAlpha = 100;
    //staticTip.fadeInSpeed = 20;
    //staticTip.fadeOutSpeed = 20;
    staticTip.tipStick = 0;
    staticTip.IESelectBoxFix = true;

    staticTip.template = '<table width="%2%" border="0" cellspacing="0" cellpadding="0">' +
    '<tr>' +
    '<td width="5" height="5"><img src="'+domain+'/lib/interface/imagens/option_top_esq.gif" width="5" height="5"></td>' +
    '<td background="'+domain+'/lib/interface/imagens/option_top_bg.gif" style="background-repeat:repeat-x;"><img src="'+domain+'/lib/interface/imagens/transp.gif" width="1" height="5"></td>' +
    '<td width="6" height="5"><img src="'+domain+'/lib/interface/imagens/option_top_dir.gif" width="6" height="5"></td>' +
    '</tr>' +
    '%3%' +
    '<tr>' +
    '<td width="5" height="6"><img src="'+domain+'/lib/interface/imagens/option_bottom_esq.gif" width="5" height="6"></td>' +
    '<td background="'+domain+'/lib/interface/imagens/option_bottom_bg.gif" style="background-repeat:repeat-x;"><img src="'+domain+'/lib/interface/imagens/transp.gif" width="1" height="6"></td>' +
    '<td width="6" height="6"><img src="'+domain+'/lib/interface/imagens/option_bottom_dir.gif" width="6" height="6"></td>' +
    '</tr>' +
    '</table>';

  staticTip.tips.optMenu = new Array(5, 5, 211, '');
    }

function CleanOption()
  {
  arrOptions = new Array();
  }

function AddOption(lnk, img, txt)
  {
    var opt = strOpt;
    opt = opt.replace('[link]', lnk);
    opt = opt.replace('[img]', img);
    opt = opt.replace('[text]', txt);

    arrOptions[arrOptions.length] = opt;
    }

function DrawOption(width)
  {
  var W = 221;
  if(width)
    W = width;

  var html = '';
    for (var i = 0; i < arrOptions.length; i++)
      {
    html += '<tr class="opt_table_data" bgcolor="#e8e8e8">';
      html += '<td background="'+domain+'/lib/interface/imagens/option_esq_bg.gif" style="background-repeat:repeat-y;"><img src="'+domain+'/lib/interface/imagens/transp.gif" width="5" height="1"></td>';
    html += '<td>' + arrOptions[i] + '</td>';
    html += '<td background="'+domain+'/lib/interface/imagens/option_dir_bg.gif" style="background-repeat:repeat-y;"><img src="'+domain+'/lib/interface/imagens/transp.gif" width="6" height="1"></td>';
    html += '</tr>';
        }

  staticTip.tips.optMenu = new Array(0, 0, W, html);
  }

function DrawText(id, text)
  {
  var html = '';

  //Define o tamanho do popup
  var width = 211;
  if(arguments[2])
    width = arguments[2];

  html += '<tr bgcolor="#f0f0f0">';
  html += '<td background="'+domain+'/lib/interface/imagens/option_esq_bg.gif" style="background-repeat:repeat-y;"><img src="'+domain+'/lib/interface/imagens/transp.gif" width="5" height="1"></td>';
  html += '<td>' + text + '</td>';
  html += '<td background="'+domain+'/lib/interface/imagens/option_dir_bg.gif" style="background-repeat:repeat-y;"><img src="'+domain+'/lib/interface/imagens/transp.gif" width="6" height="1"></td>';
  html += '</tr>';

  eval("staticTip.tips.optMenu"+id+" = new Array(5, 5, "+width+", html);");
  }

function printTable(obj)
  {
  qtdeprint = obj.style.zIndex;
  if (qtdeprint == 0 || qtdeprint == '' || qtdeprint == undefined)
    qtdeprint = 1;

  obj = obj.childNodes;
  for(var i in obj)
    {
    var obj2 = obj[i].childNodes;

    var count = 1;
    var type = Array("rel_table_data1", "rel_table_data2");
    var pos = 0;
    for(var j in obj2)
      {
      if (obj2[j].className == "rel_table_header") continue;
      if (obj2[j].className && obj2[j].className.indexOf("rel_table_data") != 0) continue;
      if (obj2[j].style && obj2[j].style.display == 'none') { continue; }

      if(obj2[j].style)
        {
        obj2[j].className = type[pos];

        if((count % qtdeprint) == 0) pos++;
        if (pos >= type.length) pos = 0;
        count++;
        }
      }
    }
  }

function inicializeInterface()
  {
  if (document.getElementById("divMenu"))
    {
    document.getElementById("divMenu").style.display = "";
    menuInit();
    }

  $(".textCaption").each(function()
    { 
    var td_width = $(this).width();

    $(this).find("div").css({width: td_width + 'px', display: '' });

    var texto_final = $(this).find("span").text() + '...';
    var objSpan = $(this).find("span");
    while ($(this).find("span").width() > td_width)
      {
      texto_final = texto_final.slice(0, -4) + '...';
      $(objSpan).text(texto_final);
      }
    }
  );

  var tabelas = document.getElementsByTagName('table');
  for(var i = 0; i < tabelas.length; i++)
    {
    var obj = tabelas[i];

    if (obj.className == "rel_table")
      printTable(obj);
    }

  DefineConfOption();
  }

function inicializeMenu()
    {
    jQuery().ready(function(){
        // second simple Accordion with special markup
        jQuery('#navigation').accordion({
            active: '.aberto',
            header: '.head',
            navigation: true,
            event: 'click',
            autoheight: false,
            animated: 'slide'
        });
    });
    }

function initAjax()
    {
    try
        {
        xmlhttp = new XMLHttpRequest();
        }
    catch(ee)
        {
        try
            {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
        catch(e)
            {
            try
                {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
            catch(E)
                {
                xmlhttp = false;
                }
            }
        }

    return xmlhttp;
    }

function autosuggest(processo, obj, params)
    {
    q = obj.value;
    field = obj.id;

    http = initAjax();
    http.open('get', domain + 'crm/painel/ajax/autosuggest.php?q=' + q + '&p=' + processo + '&nocache=' + Math.random() + '&' + params);
    http.onreadystatechange = autosuggestReply;
    http.send(null);
    }

function autosuggest_load()
    {
    $("#autosuggest").empty();
    $("#autosuggest").append("<div><img src=\""+domain+"/lib/interface/imagens/carregando.gif\"></div>");
    $("#autosuggest").css({ display:"block" });
    }

function autosuggestReply()
    {
    if(http.readyState == 4)
        {
        var objXml = http.responseXML;

        var registros = objXml.getElementsByTagName("sugestao");

        if(registros.length)
            {
            // Remove registros
            $("#autosuggest").empty();

            for(var i=0; i<registros.length; i++)
                {
                eval("suggest_text"+i+" = registros["+i+"].getElementsByTagName(\"text\")[0].firstChild.nodeValue;");
                eval("suggest_value"+i+" = registros["+i+"].getElementsByTagName(\"value\")[0].firstChild.nodeValue;");

                // Insere registros
                $("#autosuggest").append("<div id=\"sug"+i+"\">"+ eval("suggest_text"+i) +"</div>");
                $("#sug" + i).click( function(){
                    valor = eval("suggest_value"+this.id.replace("sug", "")+";");
                    $("#" + field).each( function(){
                        $(this).val(valor);
                        $("#autosuggest").css({display:"none"});
                    });
                });
                }
            $("#autosuggest").css({ display:"block" });
            }
        else
            {
            $("#autosuggest").css({ display:"none" });
            }
        }
    }


function error_messages_init()
    {
    // dá um fade quando os erros são mostrados ao
    colorFade("akna_ierror", "background", "efc0c0", "FFEFEF", 25, 35);

    $("#akna_ierror span a").click(function() {
        $("#" + ($(this).attr("rel"))).hide("fast");
    });
    }