(function($){
  $.fn.fold = function(options) {
    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
    
    if (ie55 || ie6) {this.remove(); return true;}

	options = options || {};
    
    var defaults = {
      directory: 'img',
      side: 'right',
      turnImage: 'fold.png',
      maxHeight: 450,
      startingWidth: 20,
      startingHeight: 20,
      autoCurl: false
    };

    var options = $.extend(defaults, options);
    
    var turn_hideme = $('<div id="turn_hideme">');
    var turn_wrapper = $('<div id="turn_wrapper">');
    var turn_object = $('<div id="turn_object">');
    var img = $('<img id="turn_fold" src="'+ (options.directory+'/'+options.turnImage) +'">');

    turn_object.css({
      width: options.startingWidth, 
      height: options.startingHeight
    });
  
    if (options.side == 'right') turn_wrapper.addClass('right');
  
    this.wrap(turn_wrapper).wrap(turn_object).after(img).wrap(turn_hideme);
    
    turn_wrapper = $('#turn_wrapper');
    turn_object = $('#turn_object');

    if (!options.autoCurl) {
      turn_object.resizable({ 
        maxHeight: options.maxHeight, 
        aspectRatio: true,
        handles: options.side == 'left' ? 'se' : 'sw'
      });
    } else {
      turn_wrapper.hover(
        function(){
          turn_object.stop().animate({
            width: options.maxHeight,
            height: options.maxHeight
          });
        },
        function(){
          turn_object.stop().animate({
            width: options.startingHeight,
            height: options.startingHeight
          });
        }
      );
    }
  };
})(jQuery);
