/* * * * * * * * * * * * * * * *
 *                             *
 * @author Mike Cavaliere      *
 *                             *
 * http://cavaliere.org        *
 *                             *
 * * * * * * * * * * * * * * * */
$.fn.extend({
    
    /*
     * jQuery wrapper for Gallery constructor
     */
    gallery: function()
    {
        return this.each(function()
        {
            Cav.Gallery._instances.push( new Cav.Gallery( $(this) ) );
        });
    },

    /*
     * Toggle image's on/off states
     */        
    imageToggle: function()
    {
        var state = arguments[0];

        return this.each(function()
        {
           if (!$(this).is('img')) { return; }

           // Separate extension from rest of path
            var parts = $(this).attr('src').match(/(.+)\.(\w{3,4})$/),
                src;

           // Calculate on/off state
           if ( typeof state === 'undefined' )
           {
               if ( /-on$/.test(parts[1]) )
               {
                   state = false;
               }
               else {
                   state = true;
               }
           }
             
            if ( state === false )
            {   
                src = parts[1].replace(/-on$/, '-off') + '.' + parts[2];
            }
            else if ( state === true )
            {   
                src = parts[1].replace(/-off$/, '-on') + '.' + parts[2];
            }
            
            $(this).attr('src', src);
        });
    },
    
    /*
     * Add rollover effect to thumbnails
     * 
     * @param index {Integer} - index of element to skip
     */
    hoverToggle: function(index)
    {
        var skip; 
        
        if (typeof index != 'undefined')
        {
            skip = $(this).eq(index);
        }
        
        return this.each(function()
        {
            if (!!skip && skip[0] === this) { return; }
            
            $(this).hover(function()
            {
                $(this).imageToggle(true);
            }, function()
            {
                $(this).imageToggle(false);
            }); 
        });
    },
    
    /*
     * Set image to 'on' state when clicked, unsetting others in the group
     * 
     * @param index {Integer} - index of element to set to 'on' state
     */
    clickToggle: function(index)
    {
        var current, 
            defaults = {},
            options  = $.extend({}, defaults, arguments[0] || {});
            
            if (typeof index != 'undefined')
            {
                current = $(this).eq(index);
            }
 
            return $(this).click(function()
            { 
                // Turn off old one, remove hovers
                if ( !!current && current[0] !== this )
                {
                    current.imageToggle(false).hoverToggle();
                }
                
                current = $(this);
                
                // Turn on current one, add hovers
                current.unbind('mouseover').unbind('mouseout').imageToggle(true);                
            });
    }
});
