/*
  Userpanel plugin
  (P) PSNet, 2008 - 2012
  http://psnet.lookformp3.net/
*/

var ls = ls || {};

ls.userpanel = (function ($) {

  iUpdateTimeProcId = null,
  bNewMsgNoticed = false,
  OriginalPanelHeight = 0,
  iLastTopicId = null,
  
  // ---

  this.GetNewRandomTopic = function () {
    $.ajax ({
      url: Userpanel_NewRandomTopic,
      data: {
        'security_ls_key': LIVESTREET_SECURITY_KEY
      },
      type: 'POST',
      dataType: 'json',
      success: function (data) {
        ls.userpanel.ShowNewTopicInStatusBar (data);
      }
    });
  }
  
  // ---
  
  this.DisplayNewMessages = function (iMsgsCount) {
    $ ('#UserPanel_NewMessagesContainer').removeClass ('NewMessage');
    $ ('#UserPanel_NewMessagesContainer').html (iMsgsCount);
    if (iMsgsCount != 0) {
      $ ('#UserPanel_NewMessagesContainer').addClass ('NewMessage');
      if ((!this.bNewMsgNoticed) && (Userpanel_Popup_Msg_When_New_PM)) {
        ls.msg.notice ('', Userpanel_New_Messages);
      }
      this.bNewMsgNoticed = true;
    } else {
      this.bNewMsgNoticed = false;
    }
  }
  
  // ---
  
  this.ShowNewTopicInStatusBar = function (data) {
    sHtmlText = data.sText;
    
    OldTopicString = $ ('#UserPanel_TopicContainer').children ();
    OldTopicString.each (function () {
      $ (this).fadeOut (Userpanel_Old_Topic_Fade_Time, function () {
        $ (this).remove ();
      });
    });
    
    $ ('#UserPanel_TopicContainer').append (sHtmlText);
    
    // get count of new messages
    this.DisplayNewMessages (data.iCountTalkNew);
    
    // assign last topic id
    this.iLastTopicId = data.iTopic_Id;
  }
  
  // ---
  
  this.PutTopicIntoCenterBar = function () {
    $ ('#UserPanel_CenterPanelPlace').html (
      '<iframe src="' + Userpanel_GetTopic + this.iLastTopicId + '" frameborder="0"></iframe>'
    );
  }
  
  // ---
  
  this.ClearCenterPlace = function () {
    $ ('#UserPanel_CenterPanelPlace').html ('');
  }
  
  // ---

	return this;
	
}).call (ls.userpanel || {}, jQuery);

// ---

jQuery (document).ready (function ($) {
  ls.userpanel.GetNewRandomTopic ();
  iUpdateTimeProcId = setInterval ('ls.userpanel.GetNewRandomTopic ()', Userpanel_New_Topics_Coming_Time);
  
  ls.userpanel.OriginalPanelHeight = $ ('div.UserPanelContainer').height ();
  
  // gottttta this
  
  $ ('#Userpanel_LogoPlace').toggle (function () {
    $ ('div.UserPanelContainer').animate ({
      height: $ (window).height () - 30
    });
    $ ('#UserPanel_CenterPanelPlace').toggleClass ('Closed');
    ls.userpanel.PutTopicIntoCenterBar ();
    
    return false;
  }, function () {
    $ ('div.UserPanelContainer').animate ({
      height: ls.userpanel.OriginalPanelHeight
    });
    $ ('#UserPanel_CenterPanelPlace').toggleClass ('Closed');
    ls.userpanel.ClearCenterPlace ();
  
    return false;
  });
});

