var overStatus=false;

window.onload = function() {
   Windows.addObserver({ onResize: handleResize });
   Windows.addObserver({ onClose: handleClose });
   Windows.addObserver({ onMaximize: handleResize });
   Windows.addObserver({ onMinimize: handleMinimize });
   
   if(isDefined(window.onfocus) && isDefined(window.onblur) && isDefined(blinkerOn)) {
      window.onfocus = function() { blinkerOn(false); };
      window.onblur = function() { blinkerOn(true); };
   } else {
      document.onfocus = function() { blinkerOn(false); };
      document.onblur = function() { blinkerOn(true); };
   }
   
   login_dialog();
};

function getParent(src, tgName) {
     while (src.parentNode != null) {
          if (src.parentNode.tagName == tgName)
            return src.parentNode;
          src = src.parentNode;
     }
     return src;
}

function showHide(evt) {
     if (!evt) { evt = window.event; }
     if (document.all) { trgObj = evt.srcElement; }
     else { trgObj = evt.target; }
     if (!trgObj) { return; }
     if (!trgObj.id) {
        document.getElementById('statusList').style.display = 'none';
        document.getElementById('fontsList').style.display = 'none';
        return;
     }
     if (trgObj.tagName == 'DIV' && trgObj.id != 'statusList' && trgObj.id != 'fontsList')
     {
        var tObj=null;
        tObj = getParent(trgObj, 'DIV');
        if (tObj) { trgObj = tObj; }
     }
     if (trgObj.id != 'statusList' && trgObj.id != 'statusSettings' && trgObj.id != 'fontsList') {
     
     }
     else {
        trgObj.style.display = 'block';
     }
}

function handleResize(eventName, win) {
   if(win.getId() == 'bl') {
      sizeBuddyList();
   } else if(win.getId().indexOf('_im') != -1) {
      var mastername = win.getId().replace(/_im/, '');
      $(mastername + '_rcvd').style.height = (win.getSize()['height'] - 73) + 'px';
      $(mastername + '_rcvd').style.width = (win.getSize()['width'] - 10) + 'px';
      
      $(mastername + '_toolbar').style.top = (win.getSize()['height'] - 43) + 'px';
      $(mastername + '_toolbar').style.width = (win.getSize()['width'] - 10) + 'px';  
      
      $(mastername + '_setFont').style.top = (win.getSize()['height'] - 35) + 'px';
      
      $(mastername + '_sendBox').style.top = (win.getSize()['height'] - 15) + 'px';
      $(mastername + '_sendBox').style.width = (win.getSize()['width'] - 16) + 'px';
      
      var curIM = $(win.getId().replace(/_im/, '_rcvd'));
      curIM.scrollTop = curIM.scrollHeight - curIM.clientHeight + 6;
   }
}

function handleClose(eventName, win) {
   if(win.getId().indexOf('_im') == -1) return;
   
   var rcvdBox = $(win.getId().replace(/_im/, '') + '_rcvd');
   if(imHistory == true) {
      rcvdBox.innerHTML = '<span class="imHistory">' +
                          rcvdBox.innerHTML.replace(/\(Auto-Reply:\)/g, 'Auto-Reply:').replace(/<(?![Bb][Rr] ?\/?)([^>]+)>/ig, '') +
                          "</span>\n";
   } else {
      rcvdBox.innerHTML = '';
   }
}

function handleMinimize(eventName, win) {
   if(win.getId().indexOf('_im') == -1) return;
   
   var curIM = $(win.getId().replace(/_im/, '_rcvd'));
   curIM.scrollTop = curIM.scrollHeight - curIM.clientHeight + 6;
}

function sizeBuddyList() {
   $('blContainer').style.height = (buddyListWin.getSize()['height'] - 95) + 'px';
   $('blContainer').style.width = (buddyListWin.getSize()['width'] - 8) + 'px';
   $('blBottomToolbar').style.width = (buddyListWin.getSize()['width'] - 8) + 'px';
   $('blBottomToolbar').style.bottom = '3px';
}

function toggleStatusList() {
   if($('statusList').style.display == 'block') {
      $('statusList').style.display = 'none';
      if($('statusList').style.zIndex > Windows.maxZIndex) Windows.maxZIndex = $('statusList').style.zIndex;
   } else {
      $('statusList').style.left = parseInt(buddyListWin.getLocation()['left']) + $('statusSettings').offsetLeft + $('blTopToolbar').offsetLeft + 'px';
      $('statusList').style.top = parseInt(buddyListWin.getLocation()['top']) + $('statusSettings').offsetTop + $('blTopToolbar').offsetTop + $('statusSettings').offsetHeight + 'px';
      $('statusList').style.zIndex = Windows.maxZIndex + 20;
      $('statusList').style.display = 'block';
   }
}

function toggleFontList(win) {
   if($('fontsList').style.display == 'block') {
      $('fontsList').style.display = 'none';
      fontListToWin = '';
   } else {
      $('fontsList').style.left = (parseInt(IMWindows[win].getLocation()['left']) + parseInt($(win + '_setFont').offsetLeft)) + 'px';
      $('fontsList').style.top = (parseInt(IMWindows[win].getLocation()['top']) + parseInt(IMWindows[win].getSize()['height']) - 16) + 'px';
      $('fontsList').style.zIndex = Windows.maxZIndex + 20;
      $('fontsList').style.display = 'block';
      fontListToWin = win;
   }
}

function login_dialog() {
   Dialog.confirm('<p><span class="dialog_modal_title">Informe usuario e senha</span></p><div style="clear:both"></div><p><span id="login_error_msg" class="dialog_error" style="display:none">&nbsp;</span></p><div style="clear:both"></div><p><span class="dialog_label">Usuario:</span> <span class="dialog_input"><input type="text" id="username" onkeypress="loginHandler(event);" /></span></p><div style="clear:both"></div><p><span class="dialog_label">Senha:</span> <span class="dialog_input"><input type="password" id="password" onkeypress="loginHandler(event);" /></span></p><div style="clear:both"></div><p><span class="dialog_links"><a href="#" onclick="register_dialog();return false;">Cadastre-se</a> </div>',
                  {windowParameters: {className:alertCSS, width:alertWidth}, 
                                      okLabel: "Entrar", cancelLabel: "Cancelar",
                                      ok:function(win){
                                            login();
                                            return false;
                                         },
                                      cancel:function(win){
                                                login_dialog();
                                             }
                                     });
   setTimeout("$('username').focus();", 125);
}

function register_dialog() {
   Dialog.confirm('<p><span class="dialog_modal_title">Cadastre-se</span></p><div style="clear:both"></div><p><span id="register_error_msg" class="dialog_error" style="display:none">&nbsp;</span></p><div style="clear:both"></div><p><span class="dialog_label">usuario:</span> <span class="dialog_input"><input type="text" id="newusername" /></span></p><div style="clear:both"></div><p><span class="dialog_label">senha:</span> <span class="dialog_input"><input type="password" id="newpassword" /></span></p><div style="clear:both"></div><p><span class="dialog_label">confirma senha:</span> <span class="dialog_input"><input type="password" id="newpassword2" /></span></p><div style="clear:both"></div><input type="hidden" id="newemail" value="teste@teste.com" /><div style="clear:both"></div>',
                  {windowParameters: {className:alertCSS, width:alertWidth}, 
                                      okLabel: "Cadastrar", cancelLabel: "Cancelar",
                                      ok:function(win){
                                            register();
                                            return false;
                                         },
                                      cancel:function(win){
                                                login_dialog();
                                             }
                                     });
   setTimeout("$('newUsername').focus();", 125);
}

function new_im_dialog() {
   Dialog.confirm('<p><span class="dialog_modal_title">Nova menssagem...</span></p><div style="clear:both"></div><p><span id="newim_error_msg" class="dialog_error" style="display:none">&nbsp;</span></p><div style="clear:both"></div><p><span class="dialog_label">nome do usuario: </span> <span class="dialog_input"><input type="text" id="sendto" /></span></p><div style="clear:both"></div>',
                  {windowParameters: {className:alertCSS, width:alertWidth}, 
                                      okLabel: "ok", cancelLabel: "cancela",
                                      ok:function(win){
                                            newIMWindow();
                                            return false;
                                         }
                                     });
   setTimeout("$('sendto').focus();", 125);
}

function new_buddy_dialog() {
   Dialog.confirm('<p><span class="dialog_modal_title">Addicionar Novo Membro</span></p><div style="clear:both"></div><p><span id="newbuddy_error_msg" class="dialog_error" style="display:none">&nbsp;</span></p><div style="clear:both"></div><p><span class="dialog_label">nome do usuario: </span> <span class="dialog_input"><input type="text" id="newBuddyUsername" /></span></p><div style="clear:both"></div><p><span class="dialog_label">Nome do grupo: </span> <span class="dialog_input"><input type="text" id="newBuddyGroup" /></span></p><div style="clear:both"></div>',
                  {windowParameters: {className:alertCSS, width:alertWidth}, 
                                      okLabel: "add", cancelLabel: "cancel",
                                      ok:function(win){
                                           addNewBuddyToList($('newBuddyUsername').value, $('newBuddyGroup').value);
                                           return false;
                                         }
                                     });
   setTimeout("$('newBuddyUsername').focus();", 125);
}

function remove_buddy_dialog() {
   Dialog.confirm('<p><span class="dialog_modal_title">Remover Membro</span></p><div style="clear:both"></div><p><span id="deletebuddy_error_msg" class="dialog_error" style="display:none">&nbsp;</span></p><div style="clear:both"></div><p><span class="dialog_label">nome do usuario: </span> <span class="dialog_input"><div id="usernameOptions"></div></span></p><div style="clear:both"></div>',
                  {windowParameters: {className:alertCSS, width:alertWidth}, 
                                      okLabel: "remover", cancelLabel: "cancelar",
                                      ok:function(win){
                                           deleteBuddyFromList($('oldBuddyUsername').value);
                                           $('usernameOptions').parentNode.removeChild($('usernameOptions'));
                                           return false;
                                         }
                                     });
   var selectBoxDiv = $('usernameOptions');
   selectBoxDiv.innerHTML = "<select id=\"oldBuddyUsername\">\n";
   selectBoxDiv.innerHTML += "</select>\n";
   var selectBox = $('oldBuddyUsername');
   for (var group in buddyList) {
      if(group != 'toJSONString') addOption(selectBox, group, group + '_group');
      for(var i=0; i<buddyList[group].length; i++) {
         addOption(selectBox, ' => ' + buddyList[group][i], buddyList[group][i] + '(' + group + ')[' + i + ']');
      }
   } 
   setTimeout("$('oldBuddyUsername').focus();", 125);
}



function change_pass_dialog() {
   Dialog.confirm('<p><span class="dialog_modal_title">Escolha uma Senha</span></p><div style="clear:both"></div><p><span id="changepass_error_msg" class="dialog_error" style="display:none">&nbsp;</span></p><div style="clear:both"></div><p><span class="dialog_label">Senha Atual: </span> <span class="dialog_input"><input type="password" id="currentpw" /></span></p><div style="clear:both"></div><p><span class="dialog_label">Nova Senha: </span> <span class="dialog_input"><input type="password" id="newpw" /></span></p><div style="clear:both"></div><p><span class="dialog_label">Confirme a nova Senha: </span> <span class="dialog_input"><input type="password" id="confirmpw" /></span></p><div style="clear:both"></div>',
                  {windowParameters: {className:alertCSS, width:alertWidth}, 
                                      okLabel: "ok", cancelLabel: "cancelar",
                                      ok:function(win){
                                           changePass();
                                           return false;
                                         }
                                     });
   setTimeout("$('currentpw').focus();", 125);
}
