/**
    Galleria jQuery Plugin
    ======================
    Author: Francesco Paggin ( cescopag@gmail.com )
    Version: 1.0 
    jQuery version: 1.3 or later
    
    params
    ------
    thumbSel: selector for thumb links
    zoomSel: selector for zoomed image

*/
(function($) {

    $.fn.galleria = function(params) {
        
        var defaults = {
            thumbSel:    '.thumb',
            zoomSel:     '.zoom'
        };
        
        var options = $.extend(defaults, params);
        
        //RUN LOOP:
        return this.each(init);
        
        /** Initialization function */
        function init() {
            
            //Caching main elements...
            var gallery  = $(this);
            var thumbs   = $(options.thumbSel, gallery);
            var bigImage = $(options.zoomSel,  gallery);
                       
            //Events:
            thumbs.bind('click', function() {
            	var newImg = $(this).attr('href');
            	var newImgZoom = $(this).attr('rel');
                swapImage( bigImage, newImg, newImgZoom );
                return false;
                
            });
        }
        
        /**
        Swap image function
        
        Params
        ------
        target: image to swap
        url:    new image URL
        */
        function swapImage(target, newImg, newImgZoom) {
        
            //find current image height:
            currentHeight = target.height();
            
            target.parent().height(currentHeight);
            
            //on load modify height to fit new image...
            target.unbind('load').bind('load', function() {
                
                newHeight = target.height();              
                target.parent().animate({height: newHeight}, 'fast');
                
                //now show new image...
                $(this).fadeIn('fast');
                
            });
            
            target.fadeOut('fast', function() {
                $(this).attr('src', newImg);
                $(this).parent("a").attr('href', newImgZoom);
            });
            
        }

    }

})(jQuery);
