// jQuery.bbrk.home.specGallery.js

(function ($) {
  
  if (typeof $.bbrk === "undefined") {
    $.bbrk = {};
  }
  
  if (typeof $.bbrk.home === "undefined") {
    $.bbrk.home = {};
  }
  
  $.bbrk.home.specGallery = function (data) {
    
    var $specGallery;
    var $background;
    
    var nameList = ["basic", "jellybean", "pattern", "flag", "sf1", "sf2", "animal", "cute1", "cute2", "hero", "artist1", "artist2"];
    var pageList = [];
    
    var events = $.bbrk.home.specGallery.events;
    
    var pageEvent = $.bbrk.home.specGalleryPage.events;
    
    var currentPage = -1;
    
    
    $specGallery = $("<div>", {
      "class": "specGallery"
    });
    
    $specGallery.css({
      width: "100%",
      height: "100%",
      display: "none"
    });
    
    $background = $("<div>", {
      "class": "background"
    });
    
    $background.css({
      opacity: 0
    });
    
    $background.html("<img src=\"./assets/img/icon/close.png\">");

    $specGallery.append($background);
    
    for (var i = 0; i < nameList.length; ++i) {

      var $page = $.bbrk.home.specGalleryPage(data[nameList[i]].detail);
      
      $page.addClass("page" + (i + 1));
      
      $specGallery.append($page);

      pageList.push($page);

      $page.hide(0);
    }
    
    function onPageHide(e) {
      
      if (currentPage >= 0) {
        pageList[currentPage].off(pageEvent.HIDE_COMPLETE, onPageHide);
      }
      
      $specGallery.css({
        display: "none"
      });
      
      currentPage = -1;
    }
    
    function onPageBgClick(e) {
      $specGallery.hide(400, "easeOutExpo");
    }
    
    $specGallery.hide = function (duration, easing) {
      
      $background.stop();
      
      $specGallery.trigger(events.HIDE);
      
      $background.animate({
        opacity: 0
      },{
        duration: duration
      });
      
      pageList[currentPage].on(pageEvent.HIDE_COMPLETE, onPageHide);
      pageList[currentPage].hide(duration, easing);
    };
    
    $specGallery.show = function (i, duration, easing) {
      
      if (i === currentPage) {
        return;
      }
      
      $specGallery.css({
        display: "block"
      });
      
      $background.stop();
      
      $background.animate({
        opacity: 1
      },{
        duration: duration
      });
      
      if (currentPage >= 0) {
        pageList[currentPage].off(pageEvent.HIDE_COMPLETE, onPageHide);
        pageList[currentPage].hide(duration, easing);
      }
      
      pageList[i].show(duration, easing);
      pageList[i].on(pageEvent.BG_CLICK, onPageBgClick);
      
      currentPage = i;
    };
    
    return $specGallery;
  };
  
  $.bbrk.home.specGallery.events = {
    HIDE: "hide"
  };
  
})(jQuery);

