/*!
 * Side Bar v1.0.1
 * http://sideroad.secret.jp/
 *
 * Copyright (c) 2009 sideroad
 *
 * Dual licensed under the MIT licenses.
 * Date: 2009-09-01
 */
//Side Bar Plugin
(function($) {

 $.fn.sidebar = function(options){
   return this.each(function(){
     options = options || {};

     //default setting
     options = $.extend({
      position: "left",
      width: 100,
      height: 200,
      injectWidth: 50,
      injectHeight: 50,
      liMouseOver: {
        marginLeft: "5px"
      },
      liMouseOut: {
        marginLeft: "0px"
      },
      open : "mouseenter",
      close : "mouseleave",
      elementId : new Date().getTime()
    }, options);
    var m;
    var icss;
    var ccss = {
      height: options.height,
      width: options.width
    };
    if(options.position == "left" || options.position == "right") {
      m = options.width - options.injectWidth;
      icss = {
        //height: options.height,//
        height: options.injectHeight,//
        width: options.injectWidth
      };
      // not work on opera
      //ccss.top = ($(window).height()/2) - (options.height/2) + "px";
      ccss.top = "150px";
      ccss.height = options.injectHeight + "px";//
    }
    else {
      //m = options.height - options.injectWidth;//
      m = options.height - options.injectHeight;//
      icss = {
        //height: options.injectWidth,//
        height: options.injectHeight,//
        //width: options.width//
        width: options.injectWidth//
      };
      // not work on opera
      //ccss.left = ($(window).width()/2) - (options.width/2) + "px";
      ccss.left = "150px";
      ccss.width = options.injectWidth + "px";//
    }
    var bcss = {
      height: options.height - options.injectHeight,
      width: options.width - options.injectWidth
    };
    var e = {};
    var l = {};

    if(options.position == "left" || options.position == "right") {
      ccss[options.position] = "-" + m + "px";
      icss[options.position] = m + "px";
      e[options.position] = 0;
      e["height"] = options.height;
      l[options.position] = "-" + m;
      l["height"] = options.injectHeight;
      bcss["width"] -= 40;
    }
    else {
      ccss[options.position] = "-" + m + "px";
      icss[options.position] = m + "px";
      e[options.position] = 0;
      e["width"] = options.width;
      l[options.position] = "-" + m;
      l["width"] = options.injectWidth;
    }

    //container
    var c = $("<div><div/>").attr("id", "jquerySideBar" + options.elementId).addClass("sidebar-container-" + options.position).css(ccss);

    //inject
    var i = $("<div><div/>").addClass("sidebar-inject-" + options.position).css(icss);

    //body
    var b = $("<div><div/>").addClass("sidebar-body").css(bcss).hide();

    //menu events
    var isEnter;
    // changed: for EM
    //$(this).addClass("sidebar-menu").find("li,li *").mouseenter(function(){
    $(this).addClass("sidebar-menu").find("li,li *").mouseover(function(){
      if (!isEnter) return;
      $(this).animate(options.liMouseOver, 250);
      // changed: for EM
      //}).mouseleave(function(){
    }).mouseout(function(){
      $(this).animate(options.liMouseOut, 250);
    });

    //container events
    var isProcessing;
    c.bind(options.open,function(){
      if (isEnter) return;
      if (isProcessing) return;
      isEnter = true;
      isProcessing = true;
      c.animate(e, {
        duration: 200,
        complete: function(){
          i.fadeOut(200, function(){
            b.show("clip", 200,function(){
              isProcessing = false;
            });
          });
        }
      });
    }).bind(options.close,function(){
      if(!isEnter) return;
      if(isProcessing) return;
      isProcessing = true;
      c.animate(l, {
        duration: 200,
        complete: function(){
          b.hide("clip", 200, function(){
            i.fadeIn(200, function(){
              isEnter = false;
              isProcessing = false;
            });
          });
        }
      });
    });

    //append to body
    b.append(this);
    c.append(b);
    c.append(i);
    $(document.body).append(c);
    $(window).resize(function(){
      if(options.position == "left" || options.position == "right") {
        // not work on opera
        //newTop = Math.max(0, ($(this).height()/2) - (options.height/2));
        newTop = "150";
        c.css({top:newTop + "px"});
      }
      else {
        newLeft = Math.max(0, ($(this).width()/2) - (options.width/2));
        c.css({left:newLeft + "px"});
      }
    });
  });
 }
})(jQuery);

