(function($){
  $.fn.pageSlide = function(options) {
    
    var settings = $.extend({
		    width:          "350px", // Accepts fixed widths
		    duration:       "normal", // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
		    direction:      "right", // default direction is left.
		    modal:          false, // if true, the only way to close the pageslide is to define an explicit close class. 
		    _identifier: $(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:'999'
		};
		
		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) {
          	    
	    // Callback events for window resizing
	    $(window).resize(function(){
        $("#slideWrap").width( $("body").width() );
      });
      
      // mark the anchor!
      $(anchor).attr("rel","pageslide");
      
	  };
	  
		function _openSlide(elm) {
		  if($("#exploreWrap").width() != 0) return false;
		  _showBlanket();
		  // decide on a direction
		  if (settings.direction == "right") {
		    direction = {right:"-"+settings.width};
		    $("#exploreWrap").css({left:0});
        _overflowFixAdd();
		  } 
		  else {
		    direction = {left:"-"+settings.width};
		    $("#exploreWrap").css({right:0});
		  }
    	$("#exploreWrap").animate({width: settings.width}, settings.duration);
		  $("#slideWrap").animate(direction, settings.duration);
  		  $("#exploreBar").css("width",settings.width);
		};
		
		function _closeSlide(event) {
		  if ($(event)[0].button != 2 && $("#exploreWrap").css('width') != "0px") { // if not right click.
        $.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) {
	      $("#pageslide-blanket").toggle().animate({opacity:'0.8'}, 'fast','linear');
	    }
	  };

	  // fixes an annoying horizontal scrollbar.
	  function _overflowFixAdd(){($.browser.msie) ? $("body, html").css({overflowX:'hidden'}) : $("body").css({overflowX:'hidden'});}
		
    // Initalize pageslide, if it hasn't already been done.
    _initialize(this);
    return this.each(function(){
      $(this).unbind("click").bind("click", function(){
    	  _openSlide(this);
    	  $("#exploreWrap").unbind('click').click(function(e){ if(e.target.tagName != "A") return false; });	  
    	  if (settings.modal != true) {
  	      $(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($){
  $.fn.pageSlideClose = function(options) {
    
    var settings = $.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: $(this)
		}, options);
		
		function _hideBlanket() { if(settings.modal == true && $("#pageslide-blanket").is(":visible")) {
      $("#pageslide-blanket").animate({opacity:'0.0'}, 'fast','linear',function(){$(this).hide();});
    }}
    
    function _overflowFixRemove(){($.browser.msie) ? $("body, html").css({overflowX:''}) : $("body").css({overflowX:''});}
		
    _hideBlanket();
    direction = ($("#exploreWrap").css("left") != "0px") ? {left: "0"} : {right: "0"};
	  $("#slideWrap").animate(direction, settings.duration);
    $("#exploreWrap").animate({width: "0"}, settings.duration, function() {
      // clear bug
      $("#exploreBar").css("width","0px");
      $('#slideWrap, #exploreWrap').css('left','');
      $('#slideWrap, #exploreWrap').css('right','');
      _overflowFixRemove();
    });
    
  }
})(jQuery);

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