EmbeddedGallery = function( el ) {
  this.__constructor( el );
}

$.extend(EmbeddedGallery.prototype, {
	
	gallery:			null,
	image:				null,
	anim1:				false,
	anim2:				false,
	preload:			null,
	
	__constructor: function( el ) {
		var __instance = this;
		__instance.gallery= el;
		__instance.image= el.find('.l_Img IMG');
		
		//preload
		__instance.preload = new Array();
		__instance.gallery.find('.r_Img .item A').each(function( i ) {
			__instance.preload[i] = new Image();
			__instance.preload[i].src = $(this).attr('href');
		});
		
		__instance.gallery.find('.r_Img .item A').click(function() {
			if( !$(this).parent().is('.on') ) {
				if( !__instance.anim1 && !__instance.anim2 ) {
					var clicked_a	= $(this);
					var zooming_image = clicked_a.find('IMG');
	
					var active		= __instance.gallery.find('.r_Img .item.on');
	
					var small_target_position = active.find('IMG').position();
					var small_target_width = active.find('IMG').width();
					var small_target_height = active.find('IMG').height();
	
					var big_target_position = __instance.image.position();
					var big_target_width = __instance.image.width();
					var big_target_height = __instance.image.height();
					
					__instance.image.css('opacity', 0);
					
					__instance.anim1 = true;
					
					__instance.image.next().html(clicked_a.attr('title'));
					
					__instance.image.clone().css('opacity', 1).insertBefore(__instance.image).css('position','absolute')
																			.css('left', __instance.image.position().left)
																			.css('top', __instance.image.position().top)
																			.animate({left:small_target_position.left, top:small_target_position.top, width:small_target_width, height:small_target_height}, 200, 'swing', function(){
						__instance.anim1 = false;
						$(this).remove();
						active.removeClass('on');
					});
					
					__instance.anim2 = true;
					zooming_image.css('opacity', 0);
					zooming_image.clone().css('width', 65)
										.css('height', 65)
										.css('opacity', 1)
										.css('left', zooming_image.position().left)
										.css('top', zooming_image.position().top)
										.css('position','absolute')
										.attr('src', clicked_a.attr('href')).insertBefore(zooming_image).animate({left:big_target_position.left, top:big_target_position.top, width:big_target_width, height:big_target_height}, 200, 'swing', function(){
						__instance.anim2 = false;
						$(this).remove();
						clicked_a.parent().addClass('on');
						zooming_image.css('opacity', 1);
						__instance.image.attr('src', clicked_a.attr('href')).css('opacity', 1);
					});
				}			
			}
			return false;
		});
		
	}
	
	
});

(function($) {
jQuery.fn.embeddedGallery = function() {
	$(this).each(function() {
		new EmbeddedGallery( $(this) );
	});
	return $(this);
};
})(jQuery);

