/**
 * 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
 **/

(function($){
    $.fn.pageSlide = function(options) {
        // Define default settings and override with options.
		settings = $.extend({
		    direction:       "top", 
		    height:          "80px", // Accepts fixed heights
		    width:          "300px", // Accepts fixed width
		    duration:       "normal", // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
		    start:        function(){},
		    stop:         function(){},
            loaded:       function(){},
			clickable:		true
		}, options);
		
		function _initialize() {
		    
		    var psSlideContent = document.createElement("div");
		    $(psSlideContent).attr("id", "pageslide-content")
			
			if ( settings.direction == "top" ) {
				//$(psSlideContent).height( settings.height )
				//$(psSlideContent).height( "0px" );
				//$(psSlideContent).width( "0px" );
			} else {
				//$(psSlideContent).height( "0px" );
				//$(psSlideContent).width( "0px" );

				}
		    
		    var psSlideWrap = document.createElement("div");
		    $(psSlideWrap).attr("id", "pageslide-slide-wrap").append( psSlideContent );
		    $(psSlideWrap).css("position", "fixed");
			$(psSlideWrap).css("z-index","999");
			$(psSlideWrap).css("top","0px");


		    var psSlideBox = document.createElement("div");
		    $(psSlideBox).attr("id", "pageslide-slide-box");
		   
		     $("body").prepend( psSlideWrap );
 		     $("body").prepend( psSlideBox );
			 
			
		    // If a user clicks the document, we should hide the pageslide
		    // and override that click functionality for the slide pane itself
		    $(document).click(function() {
		        _closeSlide();
		    });

			if ( settings.clickable == false ){
		    	$("#pageslide-slide-wrap").click(function(){ return false; });
				}


		};
		/**
		* 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($("#pageslide-body-wrap").length == 0) _initialize();
			
		    settings.start();


				
			if ( settings.direction == "top" ) {
				$("#pageslide-slide-wrap").height("0px");
				$("#pageslide-slide-wrap").width("100%");
				$("#pageslide-slide-wrap").css("left","0px")

				$("#pageslide-slide-box").height("0px");
				$("#pageslide-slide-box").width("100%");

				$("#pageslide-slide-wrap").animate({height: settings.height}, settings.duration);
				$("#pageslide-slide-box").animate({height: settings.height}, settings.duration);
			} else {
				
				$("#pageslide-slide-wrap").height("100%");
				$("#pageslide-slide-wrap").width("0px");

				if ( settings.direction == "left" ) {
					$("#pageslide-slide-wrap").css("left","0px")
					$("#pageslide-slide-wrap").animate({width: settings.width}, settings.duration);
					}			
			   
				if ( settings.direction == "right" ) {
					$("#pageslide-slide-wrap").css("right","0px")
					$("#pageslide-slide-wrap").animate({width: settings.width }, settings.duration);
					}			   
			}
			


			$("#pageslide-content").html("<p>loading...</p>");
		    $.ajax({
		        type: "GET",
		        url: $(el).attr("href"),
		        success: function(data) {
		            $("#pageslide-content").html(data)
		                                   .queue( function() {
		                                       settings.loaded();
		                                       $(this).dequeue();
		                                    });
		        }
		    });
		};
		
		function _closeSlide() {
		    settings.start();
			$("#pageslide-content").empty();
	
			if ( settings.direction == "top" ) {
				$("#pageslide-slide-box").animate({height: "0" }, settings.duration, function() {
					settings.stop();
				});
				
				$("#pageslide-slide-wrap").animate({height: "0"}, settings.duration, function() {
					settings.stop();
				});
			} else {
				$("#pageslide-slide-box").animate({width: "0" }, settings.duration, function() {
					settings.stop();
				});
				
				$("#pageslide-slide-wrap").animate({width: "0"}, settings.duration, function() {
					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").toggle( 
				function(){
					_openSlide(this);
					return false;
				}, 
				function(){
				    _closeSlide();
					return false;
				});
			
			
		});
		
    };

})(jQuery);

