﻿/* Bottom menu API */
var Menu = {
    // Constructor
    init : function () {
      this._load_state ();
      
      this.state.pinned ? this.pin () : this.unpin ();
      this.state.collapsed ? this.collapse () : this.uncollapse ();
      this.locked = false;
      
      var menu = this;

      $("#footer .toggler").click (function () { menu.collapse_toggle (true); return false; });
      $("#footer .link").click (function () { menu.pin_toggle (true); return false; });
    }
    
    // Collapse menu
  , collapse : function (save_state) {
      $("#menu").animate ({ marginTop : "-9px", height : "18px" }, 250);

      $("#footer")
        .delay (130)
        .animate ({ height : "63px" }, 250)
        .find (".panel")
          .animate ({ height : "53px" }, 250)
          .find (".center")
            .animate ({ top : "-9px" }, 250);
 
      $("#menu .top").css ("background-position", "0 1000px");
      $("#menu .top DIV").css ("background-position", "50% -20px");
      
      $("#footer .toggler").removeClass ("open");
      
      if (save_state) {
        this.state.collapsed = true;
        this._save_state ();
      }
    }
    
    // Un-collapse menu
  , uncollapse : function (save_state) {
      $("#footer .panel .center").animate ({ top : "-69px" }, 250);
      $("#footer").animate ({ height : "83px" }, 250);
      $("#footer .panel").css ({ height : "28px" }); 
      $("#menu").delay (130).animate ({ marginTop : "-60px", height : "129px" }, 250);

      $("#menu .top").css ("background-position", "0 -1px");
      $("#menu .top DIV").css ("background-position", "50% 0px");
      
      $("#footer .toggler").addClass ("open");
      
      if (save_state) {
        this.state.collapsed = false; 
        this._save_state ();
      }
    }
    
    // Pin menu to screen bottom border
  , pin : function (save_state) {
      if (!this.locked) {
        $("#footer").addClass ("fixed")
      }
      
      $("#footer")
        .find (".link")
          .text ("отключить привязку к краю экрана");

      if (save_state) {
        this.state.pinned = true;
        this._save_state ();
      }
    }
    
    // Un-pin menu from screen bottom border
  , unpin : function (save_state) {
      if (!this.locked) {
        $("#footer").removeClass ("fixed");
      }
      
      $("#footer")
        .find (".link")
          .text ("включить привязку к краю экрана");
          
      if (save_state) {
        this.state.pinned = false;
        this._save_state ();
      }
    }
    
    // Toggle pinned / unpinned state
  , pin_toggle : function (save_state) {
      this.state.pinned ? this.unpin (save_state) : this.pin (save_state);
    }
    
    // Toggle collapsed / uncollapsed state
  , collapse_toggle : function (save_state) {
      this.state.collapsed ? this.uncollapse (save_state) : this.collapse (save_state);
    }
    
    // Lock pin state
  , lock : function () {
      this.locked = true;
    }
    
    // Unlock pin state
  , unlock : function () {
      this.locked = false;
      // Reset to saved state
      this.state.pinned ? this.pin () : this.unpin ();
    }
    
    // Load state from cookies
  , _load_state : function () {
      // Default settings: menu pinned and un-collapsed
      var state = { pinned : true, collapsed : false };
      var pinned, collapsed;

      if ((pinned = $.cookie.get ("menu-pinned")) != null) state.pinned = pinned == "true";
      if ((collapsed = $.cookie.get ("menu-collapsed")) != null) state.collapsed = collapsed == "true";

      this.state = state;
    }
    
    // Save state to cookies
  , _save_state : function () {
      $.cookie.set ("menu-pinned", this.state.pinned ? "true" : "false");
      $.cookie.set ("menu-collapsed", this.state.collapsed ? "true" : "false");
    }
};
