/*
 * image gallery using jQuery 1.2.1 and hoverIntent - http://www.getintothis.com
 * 
 * note: this library depends on jQuery (http://www.jquery.com) and
 * hoverItent
 *
 * Copyright (c) 2006 Ramin Bozorgzadeh
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LECENSE.txt) linceses.
 * Modified by Minh Vu 12/03/2007
 */

$.fn.gallery = function(options) {
    var self = this;

    self.options = {
        imageHolderClass: 'image_holder',
		thumbHolderClass: 'thumbnail-container',
		navigation: false,
        thumbs: true,
        thumbsDimension: 48,
        thumbsBorderWidth: 4,
		thumbsSOpacity: 0.65,
        naviconWidth: 52,
        triggerHeight: 25
    };

    // make sure to check if options are given!
    if(options) {
        $.extend(self.options, options);
    }
    this.each(function() {
        var g = this;
        g.image_holder = $('#' + self.options.imageHolderClass)[0];
        g.swapping_image = false;
		
		if(self.options.navigation){
			$(g).append(
				'<div class="back_next">' +
					'<a href="" class="back"><span>&#171; back</span></a>' +
					'<a href="" class="next"><span>next &#187;</span></a>' +
				'</div>'
			);
			g.nav = $(g).find('div.back_next');
		}

        if(self.options.thumbs) {
            g.thumbs = $('#' + self.options.thumbHolderClass + ' img');
        }

        g.init_gallery = function() {
            g.imgs = $(g.thumbs);			
            g.image = $(g.image_holder).find('img')[0];
            g.image_index = 0;
            g.init_thumbs();
        } // g.init_gallery

        g.swap_image = function(new_img_src, callback) {
            var orig_img_width = g.image.width;
            var new_img = new Image();
			var description = $('#' +self.options.thumbHolderClass).find('a.active').children().attr('alt');
            g.swapping_image = true;
           
            $(new_img).addClass('swap').css("opacity",0);
            $(new_img).load(function() {
					$(g.image).animate({"opacity":0},1300,'linear',function(){
						$(this).remove();
						g.image = new_img;
						$(new_img).removeClass('swap');
						$(g.image_holder).append(new_img);
						var new_margin = ($(g.image_holder).height()-$(new_img).height())/2;
						$(new_img).css("margin-top",new_margin);
						g.swapping_image = false;
						$(new_img).animate({"opacity":1},800,'linear');
					});
            });
			new_img.src = new_img_src;
			new_img.alt = description;
            return;
        }
        
        g.init_thumbs = function() {
            if(!self.options.thumbs) return;
			$(g.thumbs).css("opacity",self.options.thumbsSOpacity).hoverIntent({
			   sensitivity: 1,
			   interval: 150,
			   over: function(){
					$(this).animate({"opacity":1},300, 'linear');
				},
			   timeout: 50,
			   out: function(){
					 $(this).animate({"opacity":self.options.thumbsSOpacity},50,'linear');
				}
			});
			$(g.thumbs).parent().click(function() {
				if(g.swapping_image) return false;
				$('#' +self.options.thumbHolderClass).find('a.active').removeClass('active');
				$(this).addClass('active');
                g.swap_image($(this).attr('href'));
				return false;
			});
        };
	});
	 if(this.size() == 1) {
        this[0].init_gallery();
    } else {
        this.init_gallery();
    }
};