// jQuery as.pageViewer.navContainer

(function () {

  if (typeof $.as === "undefined") {
    $.as = {};
  }

  if (typeof $.as.pageViewer === "undefined") {
    $.as.pageViewer = {};
  }

  $.as.pageViewer.navContainer = function () {

    var $window = $(window);
    var $document = $(document);
    
    var $navContainer = $("<div>", {
      id: "navContainer"
    });
    
    var $siteName = $("<div>", {
      "class": "siteName"
    });
    
    var $globalNavContainer = $("<div>", {
      "class": "globalNavContainer"
    });
    
    var $articleListContainer = $("<div>", {
      "class": "articleListContainer"
    });

    var $copyright = $("<div>", {
      "class": "copyright"
    });
    
    var $globalNavItems, $images;
    
    var mode = $.as.pageViewer.navContainer.mode;
    var currentMode = mode.relative;
    var isFixedNav = false;

    var originalHeight;
    
    var isTouchDevice = /(iPad|iPhone|Android)/.test(navigator.userAgent);

    
    // !初期化
    $siteName.css({
      opacity: 0
    });
    
    $globalNavContainer.css({
      opacity: 0
    });
    
    $articleListContainer.css({
      opacity: 0
    });
    
    $copyright.css({
      opacity: 0
    });
    
    if (isTouchDevice) {
      $navContainer.append($siteName, $globalNavContainer, $articleListContainer);
      $copyright.css({
        position: "absolute",
        bottom: 40,
        left: 40
      });
    }
    else {
      $navContainer.append($siteName, $globalNavContainer, $articleListContainer, $copyright);
    }
    
    
    // !イベントハンドラ
    
    function onImagesLoad(e) {
      $navContainer.trigger("complete");
    }
    
    function onMouseEnterNavItem(e) {
    
      var $this = $(this);
      
      if ($this.hasClass("current")) {
        return;
      }
      
      $this.stop().css({
        opacity: .15
      }).animate({
        opacity: 1
      },{
        duration: 600
      });
    }
    
    function getWindowScrollTop() {
      var top = $window.scrollTop();
      if (top === 0) {
        top = $html.scrollTop();
      }
      return top;
    }
    
    
    // !パブリックメソッド
    $navContainer.init = function (siteNameHTML, copyrightHTML, $globalNav, $articleList) {
    
      var currentLoad = 0;
      
      $html = $("html");

      $navContainer.bind("imagesLoad", onImagesLoad);
    
      if (isTouchDevice) {
        $navContainer.after($copyright);
      }
    
      $siteName.html(siteNameHTML);
      $globalNavContainer.html($globalNav);
      $articleListContainer.html($articleList);
      $copyright.html(copyrightHTML);
      
      originalHeight = $navContainer.height();

      $globalNavItems = $globalNavContainer.find("li");

      $globalNavItems.css({
        opacity : 0
      });
      
      $globalNavContainer.css({
        opacity: 1
      });
      
      $images = $navContainer.find("img");
      
      if (!$.support.opacity) {
        $images.each(function (i) {
          var src = $(this).attr("src");
          $(this).attr("src", src.replace(/\.png/g, "-8.png"));
        });
      }
  
      $images.each(function(i){
      
        var orgSrc = this.src;
        
        if (!$.browser.msie) {
          this.src = "";
        }
        
        $(this).bind("load", function(e) {
          ++currentLoad;
          if (currentLoad === $images.length) {
            $navContainer.trigger("imagesLoad");
          }
        });
        
        if (!$.browser.msie) {
          this.src = orgSrc;
        }
        
        else if (this.complete || this.complete === undefined) {
          this.src = orgSrc;
        }
      
      });
  
    };
    
    $navContainer.showSiteName = function (duration) {
      $siteName.animate({
        opacity: 1
      }, duration, function () {
        $siteName.trigger("complete");
      });
    };
    
    $navContainer.showGlobalNav = function (duration, randomise) {
      
      if (typeof randomise == "undefined" || !randomise) {
        $globalNavItems.animate({
          opacity: 1
        },{
          duration: duration,
          complete: function () {
            $(this).bind("mouseenter", onMouseEnterNavItem);
          }
        });
      }
      else {
      
        var timer;
        var itemList = [];
        
        for (var i = 0; i < $globalNavItems.length; ++i) {
          itemList.push($globalNavItems.eq(i));
        }
      
        (function showGlobalNavItem() {
          
          var i = Math.floor(Math.random() * itemList.length);
          
          itemList[i].animate({
            opacity: 1
          },{
            duration: duration,
            complete: function () {
              $(this).bind("mouseenter", onMouseEnterNavItem);
            }
          });
          
          itemList.splice(i,1);
          
          if (itemList.length > 0) {
            timer = setTimeout(showGlobalNavItem, 60);
          }
  
        })();

      }
      
    };
    
    $navContainer.showArticleList = function (duration) {
      $articleListContainer.animate({
        opacity: 1
      },{
        duration: duration,
        complete: function () {
          if (!$.support.opacity) {
            this.style.removeAttribute('filter');
          }
        }
      });
    };
    
    $navContainer.showCopyright = function (duration) {

      $copyright.animate({
        opacity: 1
      }, duration, function () {
        $copyright.trigger("complete");
      });
    };
    
    $navContainer.changeMode = function (modeStr, duration, easing) {
      
      if (modeStr === mode.fixed && !isTouchDevice) {
      
        var height = $window.height();
        
        if (currentMode === mode.fixed) {
          $navContainer.stop().animate({
            height: height
          }, {
            duration: duration,
            easing: easing
          });
        }
        else {
          $navContainer.stop().animate({
            top: getWindowScrollTop(),
            height: height
          }, {
            duration: duration,
            easing: easing,
            complete: function () {
              $(this).css({
                position: "fixed",
                top: 0
              });
            }
          });
        }
        
        currentMode = mode.fixed;
      }
      else if (modeStr === mode.relative && !isTouchDevice) {
        
        var height = $document.height();
        
        if (height < originalHeight) {
          height = originalHeight;
        }
      
        if (currentMode === mode.fixed) {
  
          $navContainer.css({
            position: "relative",
            top: getWindowScrollTop()
          });
          
          $navContainer.stop().animate({
            top: 0,
            height: height
          }, {
            duration: duration,
            easing: easing
          });

        }
        else {
          
          $navContainer.stop().animate({
            height: height
          }, {
            duration: duration,
            easing: easing
          });

        }

        currentMode = mode.relative;
      }
    }

    return $navContainer;
  };
  
  $.as.pageViewer.navContainer.mode = {
    fixed: "fixed",
    relative: "relative"
  };

})(jQuery);

