/**
* JQuery Page curl Plugin
*/

(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);

        // We just won't show it for IE5.5 and IE6
        if (ie55 || ie6) { this.remove(); return true; }
        // We won't show it is the screen resolution is 1024x768 or less
        if (screen.width <= 1024) { this.remove(); return true; }

        // New - you don't have to specify options!
        options = options || {};

        // Default awesomeness
        var defaults = {
            directory: '.',
            side: 'left',
            turnImage: 'http://www.skydivinginnashville.com/images/fold.png',
            maxHeight: 400,
            starting_width: 80,
            starting_height: 80,
            autoCurl: true,
            url: 'index.php'
        };

        // Change turnImage if we're running the default image, and they've specified 'right'
        if (options.side == 'right' && !options.turnImage) defaults.turnImage = 'http://www.skydivinginnashville.com/images/fold-sw.png';

        // Merge options with the defaults
        var options = $.extend(defaults, options);

        // Set up the wrapper objects
        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) +'">');
        var img = $('<img id="turn_fold" src="' + (options.turnImage) + '" >');

        // Set starting width and height of our turn-o-ma-bob
        turn_object.css({
            width: options.starting_width,
            height: options.starting_height
        });

        img.click(function() {
            // Use the url passed in with the options object if available, otherwise use our default of RichContent.
            if (options.url)
                document.location.href = options.url;
            else
                document.location.href = defaults.url;
        }).hover(function() {
            document.body.style.cursor = 'pointer';
        }, function() {
            document.body.style.cursor = 'auto';
        });

        $(this).css("display", "block");

        // There are different CSS considerations for a top-right fold.
        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,
                ratio: true,
                border: false,
                dragHandle: false,
                knobHandles: true,
                handles: options.side == 'left' ? 'se' : 'sw'
            });
        } else {

            turn_object.hover(function() {
                turn_wrapper.css("width", options.maxHeight);
                turn_wrapper.css("height", options.maxHeight);
                turn_object.stop().animate({
                    width: options.maxHeight,
                    height: options.maxHeight
                }, 'normal');
            },
        function() {
            turn_object.stop().animate({
                width: options.starting_height,
                height: options.starting_height
            }, 'normal', function() {
                turn_wrapper.css("width", options.starting_width);
                turn_wrapper.css("height", options.starting_height);
            });
        }
      );
        }
    };
})(jQuery);
