/**
 * jQuery PageSlide
 *
 * This jQuery plugin was inspired by the UI designs of Aza Raskin (http://www.azarask.in/),
 * in his Firefox mobile and Ubiquity mouse gesture prototypes, adapted for use as a jQuery lightBox-esque plugin.
 *
 * @name jquery-pageslide-0.2.js
 * @author Scott Robbin - http://srobbin.com
 * @version 0.2
 * @date January 7, 2009
 * @category jQuery plugin
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 **/

/**
 * Changes by Joseph Pecoraro
 * Friday January 16, 2009
 **/

(function($){
  $.fn.pageSlide = function(options) {

    // Define default settings and override with options.
    var open = false;
    var settings = $.extend({
        width:     300,      // Accepts fixed widths
        duration:  "normal", // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
        start:     function(){},
        stop:      function(){},
        loaded:    function(){},
        html:      null,
        closers:   null
    }, options);


    // Append the pageslider
    function _initialize() {

      // Create and prepare elements for pageSlide
      var psBodyWrap = document.createElement("div");
      $(psBodyWrap).attr("id", "pageslide-body-wrap").width( $("body").width() );

      var psSlideContent = document.createElement("div");
      $(psSlideContent).attr("id", "pageslide-content").width( settings.width );

      var psSlideWrap = document.createElement("div");
      $(psSlideWrap).attr("id", "pageslide-slide-wrap").append( psSlideContent );

      // Wrap and append so that we have the slide containers
      $("body").contents().wrapAll( psBodyWrap );
      $("body").append( psSlideWrap );

      // Append the content now (it may have ids that are needed)
      $("#pageslide-content").html(settings.html)
        .queue(function() {
          settings.loaded();
          $(this).dequeue();
        });

      // If a user clicks the document, we should hide the pageslide
      $(document).click(function() {
        if (open) { _closeSlide(); }
      });

      // Make it so if the user clicks in the pageslide, it won't close
      // Exception for given closers that may be in the pageslide
      $("#pageslide-slide-wrap").click(function(e){
        if ( $('a, ' + settings.closers).index( $(e.target) ) == -1 ) {
          return false;
        }
      });

      // Callback events for window resizing
      $(window).resize(function(){
        $("#pageslide-body-wrap").width( $("body").width() );
      });

    };

    var BOUNCE_TIME = "fast";

    /**
    * Start the jQuery pageslide plugin
    *
    * Wraps the body's children inside of a DIV, so that it can slide upon start action
    */
    function _openSlide(el) {
      settings.start();
      $("#pageslide-slide-wrap")
        .animate({width: settings.width*1.2}, settings.duration)
        .animate({width: settings.width }, BOUNCE_TIME);
      $("#pageslide-body-wrap")
        .animate({left: "-" + settings.width*1.2}, settings.duration)
        .animate({left: "-" + settings.width }, BOUNCE_TIME, function() {
        open = true;
        settings.stop();
      });
    };

    function _closeSlide() {
      settings.start();
      $("#pageslide-body-wrap")
        .animate({left: "-" + settings.width*1.2}, BOUNCE_TIME)
        .animate({left: "0" }, settings.duration);
      $("#pageslide-slide-wrap")
        .animate({width: settings.width*1.2}, BOUNCE_TIME)
        .animate({width: "0"}, settings.duration, function() {
        open = false;
        settings.stop();
      });
    }

    // Initalize pageslide, if it hasn't already been done.
    if( $("#pageslide-body-wrap").length == 0) _initialize();
    return this.each(function() {
      $(this).unbind("click").bind("click", function() {
        _openSlide(this);
        return false;
      });
    });

  };
})(jQuery);
