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();
   //open_online_window('1', '12234324');
   //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 != 'countryList' && trgObj.id != 'specialityList') {
        if (document.getElementById('countryList')!=null){
		document.getElementById('countryList').style.display = 'none';
        document.getElementById('specialityList').style.display = 'none';
		}
        return;
     }
}

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.isMinimized()){
		$(win.getId()+'_minimize').removeClassName('dialog_minimize');
		$(win.getId()+'_minimize').addClassName('dialog_maximize'); 
	}else{
		$(win.getId()+'_minimize').removeClassName('dialog_maximize');
		$(win.getId()+'_minimize').addClassName('dialog_minimize'); 
	}
	if(win.getId().indexOf('_im') == -1) return;
   
   var curIM = $(win.getId().replace(/_im/, '_rcvd'));
   curIM.scrollTop = curIM.scrollHeight - curIM.clientHeight + 6;
}

function sizeBuddyList() {
	
   $('ouContainer').style.height = (buddyListWin.getSize()['height'] - 95) + 'px';
   $('ouContainer').style.width = (buddyListWin.getSize()['width'] - 8) + 'px';
   
   //$('blBottomToolbar').style.width = (buddyListWin.getSize()['width'] - 8) + 'px';
   //$('blBottomToolbar').style.bottom = '3px';
}

function toggleStatusCountry() {
   if($('countryList').style.display == 'block') {
      $('countryList').style.display = 'none';
      if($('countryList').style.zIndex > Windows.maxZIndex) Windows.maxZIndex = $('countryList').style.zIndex;
   } else {
      $('countryList').style.left = parseInt(buddyListWin.getLocation()['left']) + $('statusCountry').offsetLeft + $('blTopToolbar').offsetLeft + 'px';
      $('countryList').style.top = parseInt(buddyListWin.getLocation()['top']) + $('statusCountry').offsetTop + $('blTopToolbar').offsetTop + $('statusCountry').offsetHeight + 'px';
      $('countryList').style.zIndex = Windows.maxZIndex + 20;
      $('countryList').style.display = 'block';
   }
}
function toggleStatusSpeciality() {
   if($('specialityList').style.display == 'block') {
      $('specialityList').style.display = 'none';
      if($('specialityList').style.zIndex > Windows.maxZIndex) Windows.maxZIndex = $('specialityList').style.zIndex;
   } else {
      $('specialityList').style.left = parseInt(buddyListWin.getLocation()['left']) + $('statusSpeciality').offsetLeft + $('blTopToolbar').offsetLeft + 'px';
      $('specialityList').style.top = parseInt(buddyListWin.getLocation()['top']) + $('statusSpeciality').offsetTop + $('blTopToolbar').offsetTop + $('statusSpeciality').offsetHeight + 'px';
      $('specialityList').style.zIndex = Windows.maxZIndex + 20;
      $('specialityList').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;
   }
}
