(function(jQuery){
  jQuery.fn.pageSlide = function(options) {
    
    var settings = jQuery.extend({
		    width:          "300px", // Accepts fixed widths
		    duration:       "normal", // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
		    direction:      "left", // default direction is left.
		    modal:          false, // if true, the only way to close the pageslide is to define an explicit close class. 
		    _identifier: jQuery(this)
		}, options);
		
		// these are the minimum css requirements for the pageslide elements introduced in this plugin.
		
		var pageslide_slide_wrap_css = {
		  position: 'fixed',
		  width: '0',
		  top: '0',
		  height: '100%',
		  zIndex:'1'
		};
		
		var pageslide_body_wrap_css = {
		  position: 'relative',
		  zIndex: '0'
		};
		
		var pageslide_blanket_css = { 
	    position: 'absolute',
	    top: '0px',
	    left: '0px',
	    height: '100%',
	    width: '100%', 
	    opacity: '0.0',
	    backgroundColor: 'black',
	    zIndex: '1',
	    display: 'none'
	  };
		
		function _initialize(anchor) {
      
      // Create and prepare elements for pageSlide
      if (jQuery("#pageslide-body-wrap, #pageslide-content, #pageslide-slide-wrap").size() == 0) {
        
        var psBodyWrap = document.createElement("div");
        jQuery(psBodyWrap).css(pageslide_body_wrap_css);
        jQuery(psBodyWrap).attr("id","pageslide-body-wrap").width( jQuery("body").width() );
        jQuery("body").contents().wrapAll( psBodyWrap );
  	    
        var psSlideContent = document.createElement("div");
        jQuery(psSlideContent).attr("id","pageslide-content").width( settings.width );

        var psSlideWrap = document.createElement("div");
        jQuery(psSlideWrap).css(pageslide_slide_wrap_css);
        jQuery(psSlideWrap).attr("id","pageslide-slide-wrap").append( psSlideContent );
        jQuery("body").append( psSlideWrap );
  	    
      }
      
      // introduce the blanket if modal option is set to true.
      if (jQuery("#pageslide-blanket").size() == 0 && settings.modal == true) {
        var psSlideBlanket = document.createElement("div");
        jQuery(psSlideBlanket).css(pageslide_blanket_css);
        jQuery(psSlideBlanket).attr("id","pageslide-blanket");
        jQuery("body").append( psSlideBlanket );
  	    jQuery("#pageslide-blanket").click(function(){ return false; });
      }
          	    
	    // Callback events for window resizing
	    jQuery(window).resize(function(){
        jQuery("#pageslide-body-wrap").width( jQuery("body").width() );
      });
      
      // mark the anchor!
      jQuery(anchor).attr("rel","pageslide");
      
	  };
	  
		function _openSlide(elm) {
		  if(jQuery("#pageslide-slide-wrap").width() != 0) return false;
		  _showBlanket();
		  // decide on a direction
		  if (settings.direction == "right") {
		    direction = {right:"-"+settings.width};
		    jQuery("#pageslide-slide-wrap").css({left:0});
        _overflowFixAdd();
		  } 
		  else {
		    direction = {left:"-"+settings.width};
		    jQuery("#pageslide-slide-wrap").css({right:0});
		  }
    	jQuery("#pageslide-slide-wrap").animate({width: settings.width}, settings.duration);
		  jQuery("#pageslide-body-wrap").animate(direction, settings.duration, function() {
	      jQuery.ajax({
  		      type: "GET",
  		      url: jQuery(elm).attr("href"),
  		      success: function(data){
  		        jQuery("#pageslide-content").css("width",settings.width).html(data)
  		          .queue(function(){
  		            jQuery(this).dequeue();
  		            
  		            // add hook for a close button
  		            jQuery(this).find('.pageslide-close').unbind('click').click(function(elm){
  		              _closeSlide(elm);
  		              jQuery(this).find('pageslide-close').unbind('click');
  		            });
  		          });
  		      }
  		    });
		  });
		};
		
		function _closeSlide(event) {
		  if (jQuery(event)[0].button != 2 && jQuery("#pageslide-slide-wrap").css('width') != "0px") { // if not right click.
        jQuery.fn.pageSlideClose(settings);
      }
		};
		
		// this is used to activate the modal blanket, if the modal setting is defined as true.
		function _showBlanket() {
	    if(settings.modal == true) {
	      jQuery("#pageslide-blanket").toggle().animate({opacity:'0.8'}, 'fast','linear');
	    }
	  };

	  // fixes an annoying horizontal scrollbar.
	  function _overflowFixAdd(){(jQuery.browser.msie) ? jQuery("body, html").css({overflowX:'hidden'}) : jQuery("body").css({overflowX:'hidden'});}
		
    // Initalize pageslide, if it hasn't already been done.
    _initialize(this);
    return this.each(function(){
      jQuery(this).unbind("click").bind("click", function(){
    	  _openSlide(this);
    	  jQuery("#pageslide-slide-wrap").unbind('click').click(function(e){ if(e.target.tagName != "A") return false; });	  
    	  if (settings.modal != true) {
  	      jQuery(document).unbind('click').click(function(e) { if(e.target.tagName != "A"){ _closeSlide(e); return false } });
  	    }
    	  return false;
    	});	
    });
    
  };
})(jQuery);

// pageSlideClose allows the system to automatically close any pageslide that is currently open in the view.
(function(jQuery){
  jQuery.fn.pageSlideClose = function(options) {
    
    var settings = jQuery.extend({
		    width:          "300px", // Accepts fixed widths
		    duration:       "normal", // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
		    direction:      "left", // default direction is left.
		    modal:          false, // if true, the only way to close the pageslide is to define an explicit close class. 
		    _identifier: jQuery(this)
		}, options);
		
		function _hideBlanket() { if(settings.modal == true && jQuery("#pageslide-blanket").is(":visible")) {
      jQuery("#pageslide-blanket").animate({opacity:'0.0'}, 'fast','linear',function(){jQuery(this).hide();});
    }}
    
    function _overflowFixRemove(){(jQuery.browser.msie) ? jQuery("body, html").css({overflowX:''}) : jQuery("body").css({overflowX:''});}
		
    _hideBlanket();
    direction = (jQuery("#pageslide-slide-wrap").css("left") != "0px") ? {left: "0"} : {right: "0"};
	  jQuery("#pageslide-body-wrap").animate(direction, settings.duration);
    jQuery("#pageslide-slide-wrap").animate({width: "0"}, settings.duration, function() {
      // clear bug
      jQuery("#pageslide-content").css("width","0px").empty();
      jQuery('#pageslide-body-wrap, #pageslide-slide-wrap').css('left','');
      jQuery('#pageslide-body-wrap, #pageslide-slide-wrap').css('right','');
      _overflowFixRemove();
    });
    
  }
})(jQuery);

// this adds the ability to close pageSlide with the 'escape' key, if not modal.
(function(jQuery){
  jQuery(document).ready(function(){
    jQuery(document).keyup(function(event){
      if (!jQuery("#pageslide-blanket").is(":visible") && event.keyCode == 27) jQuery.fn.pageSlideClose();
    });
  });
})(jQuery);