/**
 * 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
 * @author Ian Lewis - http://www.ianlewis.org/
 * @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
 **/

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

    var _initialized = false;

    // Define default settings and override with options.
    if ($(this).length) {
      $(this).data("settings", $.extend({
        width:          "300px", // Accepts fixed widths
        duration:       "normal", // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
        start:        function(){},
        stop:         function(){},
        loaded:       function(){}
      }, options));
    }

    function _getSettings(el) {
      return $(el).data("settings");
    }
    
    /**
    * 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) {
      if (! $.page_slide_settings) {
        var settings = _getSettings(el);
        settings.start();
        $("#pageslide-content").width( settings.width );
        $("#pageslide-slide-wrap").animate({width: settings.width}, settings.duration);
        $("#pageslide-body-wrap").animate({left: "-" + settings.width}, settings.duration, function() {
          settings.stop();
        });
        $.ajax({
          type: "GET",
          url: $(el).attr("href"),
          success: function(data) {
            $("#pageslide-content").html(data)
            .queue( function() {
                settings.loaded();
                $(this).dequeue();
            });
          }
        });
        $.closeSlide = function() { _closeSlide(); };
        $.page_slide_settings = settings;
      }
    };
		
    function _closeSlide() {
      var settings = $.page_slide_settings;
      settings.start();
      $("#pageslide-body-wrap").animate({left: "0" }, settings.duration);
      $("#pageslide-slide-wrap").animate({width: "0"}, settings.duration, function() {
          $("#pageslide-content").empty();
          settings.stop();
      });
      $.closeSlide = function() {};
      $.page_slide_settings = null;
    }
        
    return this.each(function(){
      $(this).unbind("click").bind("click", function(){
        _openSlide(this);
        return false;
      });
    });
    
  };
  
  $.closeSlide = function() {};
  $.page_slide_settings = null;

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

})(jQuery);

