/*
  Popupinfo plugin
  
*/

var ls = ls || {};

ls.popupinfo = (function ($) {

  storedEvent = null,
  hideProcId = null,
  showProcId = null,
  
  // ---

  this.GetMoreInfo = function (Param, URL) {
    $.ajax ({
      url: URL,
      data: {
        'param': Param,
        'security_ls_key': LIVESTREET_SECURITY_KEY
      },
      type: 'POST',
      dataType: 'json',
      success: function (data) {
        if (!data.bStateError) {
          ls.popupinfo.ShowMoreInfo (data.sText);
        } else {
          ls.msg.error (data.sMsgTitle, data.sMsg);
        }
      }
    });
  }
  
  // ---
  
  this.ShowMoreInfo = function (TextToShow) {
    // stop hiding process if that is planed
    this.StopHidingPanel ();
    
    // for html elements get correct places and borders of screen doesnt crashed them
    $ ('#Popupinfo_MoreInfoContainer').css ({
      'top': '0px',
      'left': '0px'
    });
    
    $ ('#Popupinfo_MoreInfoContainer').html (TextToShow);
    
    OffsetForMouseCursor = 16;                                              // for popup want be below cursor
    WindowScrollbarSize = 24;                                               // manually set the scrollbar size as default
    OffsetYForOverScreen = (ls.userpanel ? ls.userpanel.OriginalPanelHeight + 14 : WindowScrollbarSize);
    // if popup is over the screen - move window to this pixels from borders
    // also we check if user enabled user panel and get panel height + (2 * padding 5px + 2 * border 1px + 2px shadow offset) = 14px
    
    // get coordinates of mouse over the link
    XCoord = this.storedEvent.clientX + OffsetForMouseCursor;
    YCoord = this.storedEvent.clientY + OffsetForMouseCursor;
    
    // check if window is over of range of screen resolution
    WinW = $ ('#Popupinfo_MoreInfoContainer').width ();
    WinH = $ ('#Popupinfo_MoreInfoContainer').height ();
    PadW = parseInt ($ ('#Popupinfo_MoreInfoContainer').css ('paddingLeft').slice (0, -2)) * 2;   // padding: left + right
    PadH = parseInt ($ ('#Popupinfo_MoreInfoContainer').css ('paddingTop').slice (0, -2)) * 2;
    ScrW = $ (window).width ();
    ScrH = $ (window).height ();
    
    if (XCoord + WinW + PadW > ScrW - WindowScrollbarSize) {
      XCoord = parseInt (ScrW - WinW - PadW - WindowScrollbarSize);
    }
    if (YCoord + WinH + PadH > ScrH - OffsetYForOverScreen) {
      YCoord = parseInt (ScrH - WinH - PadH - OffsetYForOverScreen);
    }
    
    $ ('#Popupinfo_MoreInfoContainer').css ({
      'top': YCoord + 'px',
      'left': XCoord + 'px'
    });
    $ ('#Popupinfo_MoreInfoContainer').fadeIn (400);
  }
  
  // ---
  
  this._HideInfoPanel = function () {
    $ ('#Popupinfo_MoreInfoContainer').fadeOut (200);
  }
  
  // ---
  
  this.StartHidingPanel = function () {
    this.StopHidingPanel ();
    ls.popupinfo.hideProcId = setTimeout ('ls.popupinfo._HideInfoPanel ()', 700);
  }
  
  // ---
  
  this.StopHidingPanel = function () {
    if (ls.popupinfo.hideProcId != null) {
      clearTimeout (ls.popupinfo.hideProcId);
      ls.popupinfo.hideProcId = null;
    }
  }
  
  // ---
  
  this.StartShowingPanel = function (Param, URL) {
    this.StopShowingPanel ();
    ls.popupinfo.showProcId = setTimeout ('ls.popupinfo.GetMoreInfo ("' + Param + '", "' + URL + '");', Popupinfo_Panel_Showing_Delay);
  }
  
  // ---
  
  this.StopShowingPanel = function () {
    if (ls.popupinfo.showProcId != null) {
      clearTimeout (ls.popupinfo.showProcId);
      ls.popupinfo.showProcId = null;
    }
  }
  
  // ---

	return this;
	
}).call (ls.popupinfo || {}, jQuery);

// ---

jQuery (document).ready (function ($) {

  // for users login

  $('a[href^="' + aRouter['profile'] + '"]').each (function () {
    $ (this).bind ('mouseover', function (e) {
      CurLink = $ (this).attr ('href');
      CurLink = CurLink.replace (aRouter['profile'], '');
      LinkChains = CurLink.split ('/');
      
      // leave long links for profile alone
      if ((Popupinfo_Leave_Long_Links_Alone) && (LinkChains [1] != '')) return false;
      
      ls.popupinfo.storedEvent = e;
      ls.popupinfo.StartShowingPanel (LinkChains [0], Popupinfo_GetLoginMoreInfo);
    });
    $ (this).bind ('mouseout', function (e) {
      ls.popupinfo.StopShowingPanel ();
      ls.popupinfo.StartHidingPanel ();
    });
  });
  
  // for blogs

  $('a[href^="' + aRouter['blog'] + '"]').each (function () {
    $ (this).bind ('mouseover', function (e) {
      CurLink = $ (this).attr ('href');
      CurLink = CurLink.replace (aRouter['blog'], '');
      LinkChains = CurLink.split ('/');
      
      if (LinkChains [1] != '') return false;
      if (LinkChains [0] == 'add') return false;    // disable "add" event only,
                                                     // "edit" not needed to disable coz it has third parameter
                                                     // and will be disabled on previous check
      
      ls.popupinfo.storedEvent = e;
      ls.popupinfo.StartShowingPanel (LinkChains [0], Popupinfo_GetBlogMoreInfo);
    });
    $ (this).bind ('mouseout', function (e) {
      ls.popupinfo.StopShowingPanel ();
      ls.popupinfo.StartHidingPanel ();
    });
  });
  
  // for mouse moves over the wrapper
  
  $ ('#Popupinfo_MoreInfoContainer').bind ('mouseover', function () {
    ls.popupinfo.StopHidingPanel ();
  });
  
  $ ('#Popupinfo_MoreInfoContainer').bind ('mouseout', function () {
    ls.popupinfo.StartHidingPanel ();
  });

});

