///////////////////////////////////
//         ajax im 3.41          //
//    AJAX Instant Messenger     //
//   Copyright (c) 2006-2008     //
//    http://www.ajaxim.com/     //
//   Do not remove this notice   //
///////////////////////////////////


/**
 * Handle all right-click menus for buddy list
 *
 * @author Benjamin Hutchins
 **/
var Context = {
   currentUser: null,   // current user that the menu is being shown for
   lastClicked: null,   // last user that was right-clicked
   
   /**
    * On window load, apply new observes
    *
    * @author Benjamin Hutchins
    **/
   loaded: function() {
      if (typeof document.oncontextmenu != 'undefined') {
         document.oncontextmenu = Context.oncontextmenu;
      } else {
         window.oncontextmenu = Context.oncontextmenu;
      }
      
   },
   /**
    * onClick of 'Get Info', open the users' profile.
    *
    * @author Benjamin Hutchins
    **/
   /*
   profile: function() {
      $('divContext').style.display = 'none';
      if(typeof(Profile.windows[ContextData.currentUser]) == 'undefined') {
         Profile.create(ContextData.currentUser, ContextData.currentUser);
      } else {
         if(!Profile.windows[ContextData.currentUser].isVisible()) {
            Profile.windows[ContextData.currentUser].show();
            Profile.windows[ContextData.currentUser].toFront();
         } else {
            Profile.windows[ContextData.currentUser].toFront();
         }
      }
   },
   */
   /**
    * onClick of 'IM', open the conversation window with the user.
    *
    * @author Benjamin Hutchins
    **/
    /*
   createIM: function() {
      $('divContext').style.display = 'none';
      if(typeof(IM.windows[ContextData.currentUser]) == 'undefined') {
         IM.create(ContextData.currentUser, ContextData.currentUser);
      } else {
         if(IM.windows[ContextData.currentUser].detached) {
            if(IM.windows[ContextData.currentUser].popup.closed) {
               IM.windows[ContextData.currentUser] = IM.windows[ContextData.currentUser].old;
               IM.windows[ContextData.currentUser].show();
            } else {
               IM.windows[ContextData.currentUser].popup.focus();
            }
         } else if(!IM.windows[ContextData.currentUser].isVisible()) {
            IM.windows[ContextData.currentUser].show();
            IM.windows[ContextData.currentUser].toFront();
            setTimeout("scrollToBottom('" + IM.windows[ContextData.currentUser].getId() + "_rcvd')", 125);
            setTimeout("$('" + IM.windows[ContextData.currentUser].getId() + "_sendBox').focus();", 250);
         } else {
            IM.windows[ContextData.currentUser].toFront();
            setTimeout("$('" + IM.windows[ContextData.currentUser].getId() + "_sendBox').focus();", 250);
         }
      }
   },
   
   showProfile: function(){
      
      profileOpen(ContextData.linkprofile);
      
   },
    */
   /**
    * onClick of 'Block' or 'Unblock', toggle the user's blocked status.
    *
    * @author Benjamin Hutchins
    **/
   /*
   blockBuddy: function() {
      
      $('divContext').style.display = 'none';
      
      Dialogs.blockBuddy(ContextData.currentUser);
      
   },
   */
   /**
    * onClick of 'Remove', remove the user from the friend's list.
    *
    * @author Benjamin Hutchins
    **/
   /*
   removeBuddy: function() {
      $('divContext').style.display = 'none';
      Dialogs.removeBuddy(ContextData.currentUser);
   },
   */
   /**
    * Global onContextMenu handler
    *
    * @author Benjamin Hutchins
    **/
   
   oncontextmenu: function (event) {
      
      if(!event) event = window.event;

      var insideMe = false;
      
      var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
      
      var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
      
      var eventX = event.clientX + scrollLeft;
      
      var eventY = event.clientY + scrollTop;
      
      //verifying where the event ocuured
      
      for(var i in Chatroom.windows){
         
         if(!isNull(Chatroom.windows[i].getId)){
            
            var winId = Chatroom.windows[i].getId();
            
            //verifying if the event came from user's list
            
            var pos = getPosition($(winId+'_ul'));
            
            var wSize = Chatroom.windows[i].getSize();
            
            var userListInitialX = pos['x'];
            
            var userListInitialY = pos['y'];
            
            var userListEndingX = userListInitialX + 150;
            
            var userListEndingY = userListInitialY + wSize['height'];
            
            if((eventX >= userListInitialX && eventX <= userListEndingX)
               && (eventX >= userListInitialX && eventX <= userListEndingX)){
               //event ocurred on chat window's userlist
               insideMe = true;
            }
            
         }
         
      }
      
      //buddy list
      
      var wSize = null;
      
      var pos = getPosition($('buddylist'));
      
      if(typeof(Buddylist.buddyListWin.getSize) != 'undefined'){
         
         wSize = Buddylist.buddyListWin.getSize();
         
      }else{
         
         wSize['height'] = Browser.height() - 60;
         
      }
      
      
      var userListInitialX = pos['x'];
      
      var userListInitialY = pos['y'];
      
      var userListEndingX = userListInitialX + 150;
      
      var userListEndingY = userListInitialY + wSize['height'];
      
      if((eventX >= userListInitialX && eventX <= userListEndingX)
         && (eventX >= userListInitialX && eventX <= userListEndingX)){
         //event ocurred on chat window's userlist
         insideMe = true;
      }
      
      if(insideMe){
         
         showDivContext(event);
         
      }else{
         
         hideDivContentHandler();
         
      }
      
      return false;
      
   }

   /**
    * Global onMouseDown handler, hide right-click menu,
    * as long as it wasn't a right click.
    *
    * @author Benjamin Hutchins
    **/
   
    /*
   onmousedown: function (event) {
      
      if (loggedIn) {
         event = event || window.event;
         if (event.button != 2 && event.button != 3) {
            setTimeout("$('divContext').style.display='none';", 100);
         }
      }
      
   }*/
    
};

//a little container to ContextData

var ContextData = {
   
   menuActivity: false,
   currentUser: null,
   linkprofile: null,
   isOpen: false,
   oldActivityCounter: 0,
   activityCounter: 0,
   from: null
   
};

//closes the menu

function hideDivContentHandler(){
   
   $('divContext').style.display = 'none';
   
   //resets ContextData structure
   
   ContextData.isOpen = false;
   
   ContextData.currentUser = null;
   
   ContextData.linkprofile = null;
   
   ContextData.from = null;
   
   clearTimeout(menuTimer);

}

var menuTimer = null;

function hitActivity(){
   
   clearTimeout(menuTimer);
   
   menuTimer = setTimeout("hideDivContentHandler();",1000);
   
}

//sets data for the menu

function setData(username,profile_link,from){

   if(ContextData.isOpen == false){
      
      ContextData.currentUser = username;
      
      ContextData.from = from;
      
      ContextData.linkprofile = profile_link;
      
   }

}

//opens context menu

function showDivContext(event){
   
   event = event || window.event;
   
   //unabling menu system check's for a while
   
   $('divContext').style.display = 'none';

   if(ContextData.hitTimeoutHandler!=null){
      
      clearTimeout(ContextData.hitTimeoutHandler);
      
   }
   
   if(ContextData.currentUser!=null){
      
      //mark menu open
      ContextData.isOpen = true;
      
      $('divContext').style.display = 'none';
      
      $('divContextProfileLink').style.display = 'none';
      
      $('divContextNewBuddy').style.display = 'none';
      
      $('contextRemoveBuddy').style.display = 'none';
      
      $('contextBlock').style.display = 'none';
      
      if(ContextData.from == 'BuddyList'){
         
         //yeah, this came from buddy list
         
         if(typeof(Buddylist.listObjects[ContextData.currentUser].group)!='undefined'){
            
            var group = Buddylist.listObjects[ContextData.currentUser].group;
            
            $('contextBlock').innerHTML = (typeof Buddylist.list[group] != 'undefined' && Buddylist.list[group][ContextData.currentUser].blocked == true ? Languages.get('contextUnblock') : Languages.get('contextBlock'));
            
            $('contextBlock').style.display = 'block';
            
            $('contextRemoveBuddy').style.display = 'block';
            
         }else{
            
            $('contextBlock').style.display = 'none';
            
            $('contextRemoveBuddy').style.display = 'none';
            
         }
         
      }else{
         
         $('contextBlock').style.display = 'none';
         
         $('contextRemoveBuddy').style.display = 'none';
         
         if(isRegistered){
            
            $('divContextNewBuddy').style.display = 'block';
            
         }
         
      }
      
      
      if(!isUrl(ContextData.linkprofile)){
         
         $('divContextProfileLink').style.display = 'none';
         
      }else{
         
         $('divContextProfileLink').style.display = 'block';
         
         $('divContextProfileLink').innerHTML = Languages.get('contextProfile');
         
      }
      
      var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
      
      var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
      
      //make it visible
      
      Element.setStyle($('divContext'), {
        
        left: (event.clientX + scrollLeft - 15) + 'px',
        
        top: (event.clientY + scrollTop - 15) + 'px',
        
        zIndex: Windows.maxZIndex + 2000,
        
        display: 'block'
        
      });
      
      hitActivity();
      
   }else{
      
      hideDivContentHandler();
      
      clearTimeout(menuTimer);
      
   }
   
}

/*
Replace candidate for Context.createIM method
I try to isolate the bug by unabling this class for a while
*/

function ContextCreateIM(){
   
   username = ContextData.currentUser;
   
   if(username!=null){
      
      fetchHistory = true;
      
      if(typeof(IM.windows[username]) == 'undefined') {
         
         IM.create(username, username);
         
         WinTools.posMe(false,IM.windows[username]);
         
      } else {
         
         if(IM.windows[username].detached) {
            
            if(IM.windows[username].popup.closed) {
               
               IM.windows[username] = IM.windows[username].old;
               
               $(IM.windows[username].getId() + '_rcvd').innerHTML = '';
               
               IM.windows[username].show();
               
               WinTools.posMe(false,IM.windows[username]);
               
            } else {
               
               fetchHistory = false;
               
               IM.windows[username].popup.focus();
               
            }
            
         } else if(!IM.windows[username].isVisible()) {
            
            $(IM.windows[username].getId() + '_rcvd').innerHTML = '';
            
            IM.windows[username].show();
            
            IM.windows[username].toFront();
            
            setTimeout("scrollToBottom('" + IM.windows[username].getId() + "_rcvd')", 125);
            
            setTimeout("$('" + IM.windows[username].getId() + "_sendBox').focus();", 250);
            
            WinTools.posMe(false,IM.windows[username]);
            
         } else {
            
            fetchHistory = false;
            
            IM.windows[username].toFront();
            
            setTimeout("$('" + IM.windows[username].getId() + "_sendBox').focus();", 250);
            
         }
         
      }
      
      if(fetchHistory==true){
         
         makeHistoryCall(0,username);
         
      }
      
   }else{
      
      alert('username is null');
      
   }
   
   $('divContext').style.display = 'none';
   
}

function ContextShowProfile(){
   
   linkprofile = ContextData.linkprofile;
   
   if(linkprofile!=null && linkprofile != 'null' && linkprofile !='' ){
      
      profileOpen(linkprofile);
      
   }else{
      
      alert('profile link is null');
      
   }
   
   $('divContext').style.display = 'none';
   
}

function ContextBlockBuddy() {
   
   username = ContextData.currentUser;
   
   if(username!=null){
      
      Dialogs.blockBuddy(username);
      
   }else{
      
      alert('username is null');
      
   }
   
   $('divContext').style.display = 'none';
   
}

function ContextRemoveBuddy() {
   
   username = ContextData.currentUser;
   
   if(username!=null){
      
      Dialogs.removeBuddy(username);
      
   }else{
      
      alert('username is null');
      
   }
   
   $('divContext').style.display = 'none';
   
}

function ContextNewBuddy(username){
   
   username = ContextData.currentUser;
   
   if(username!=null){
      
      Dialogs.newBuddy();
      
      $('newBuddyUsername').value=username;
      
   }else{
      
      alert('username is null');
      
   }
   
   $('divContext').style.display = 'none';
   
}

function getPosition(el){
   
   x=0; y=0; var temp;
   
   if(!isNull(el)){
      
      if(el.offsetParent){
         
        temp = el;
        
        while(temp.offsetParent){
         
          temp=temp.offsetParent;
          
          x+=temp.offsetLeft
          
          y+=temp.offsetTop;
          
        }
        
      }
      
   }
   
   x+=el.offsetLeft;
   
   y+=el.offsetTop;
   
   return {x: x, y: y};

}

function isUrl(s) {
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}


//init the menu engine

//setTimeout("hitCounter()",100);