function SaltGallery(element, options)
{
	// initialise the elements
	this.element			= $(element);
	var id					= this.element.attr('id');
	this.id					= id.split('-')[1];
	
	// initialise the variables
	this.running			= false;
	this.timer				= null;
	this.counter			= 0;
	this.length				= this.element.children().length;
	
	// initialise the settings
	this.animationtype		= options.animationtype;
	this.speed				= options.speed;
	this.autorun			= options.autorun;
	this.timeout			= options.timeout;
	this.containerheight	= options.containerheight;
	
	SaltGallery.objects[this.id] = this;
	
	this.init();
}
SaltGallery.objects = {};
SaltGallery.getGalleryByName = function(name)
{
	return SaltGallery.objects[name];
}
_$pr = SaltGallery.prototype;

_$pr.init = function()
{
	this.holder = $('div#gallery-'+this.id);
	var css = {
		'position'	: 'relative',
		'overflow'	: 'hidden'
	};
	this.holder.css(css);
	
	this.holder.wrapInner(this.createWrapper());
	this.imageItem = $('div.SaltGalItem', this.holder);
	if(this.holder.length < 1)
	{
		this.holder = $('<div class="item-gallery"></div>');
		this.element.before(this.holder);
	}
	
	this.element.click(function(e)
	{
		if(e.target.nodeName.toLowerCase() != 'a')
		{
			var a = $(e.target).parent('a');
		} else
		{
			var a = $(e.target);
		}
	
		if(a.length>0)
		{
			var gallery		= SaltGallery.getGalleryByName($(this).attr('id').split('-')[1]);
			
			gallery.goGalByAnchor(a);
		}
		
		return false;
	});
	
	// trigger the first gallery item
	this.goGalByNum(0);
}

_$pr.createWrapper = function()
{
	var $wrapper = $('<div class="SaltGalItem" id="SaltGalItem-'+this.id+'">');
	var css = {
		'position'	: 'absolute',
		'top'		: '0px',
		'z-index'	: 2
	}
	$wrapper.css(css);
	
	return $wrapper;
}

_$pr.goGalByNum = function(position)
{
	$($('a',this.element)[position]).trigger('click');
}

_$pr.goNextGalItem = function()
{
	this.goGalByNum(this.setCounter(this.getCounter()+1));
}

_$pr.setCounter = function(counter)
{
	if(counter < 0)
	{
		this.counter = this.length-1;
	} else if(counter >= this.length)
	{
		this.counter = 0;
	} else
	{
		this.counter = counter;
	}
	
	return this.counter;
}

_$pr.getCounter = function()
{
	return this.counter;
}

_$pr.goGalByAnchor = function(a)
{
	if(this.running)
	{
		return false;
	}
	
	clearTimeout(this.timer);
	
	this.running = true;
	
	// set up the image packet
	var p = this;
	var parentcontainer			= a.parent();
	this.imagepacket			= {};
	this.imagepacket.href		= a.attr('href');
	this.imagepacket.metafields	= [];
	
	// set the counter
	this.setCounter(parentcontainer.index());
	
	// add and remove the selected class
	$('.sel',this.element).removeClass('sel');
	parentcontainer.addClass('sel');
	
	$.each(parentcontainer.children(),function(key, value)
	{
		var op = $(value);
		if(op.attr('href') != a.attr('href'))
		{
			p.imagepacket.metafields.push($(value).clone());
		}
	});
	
	// test for animation type
	if(this.animationtype == 'crossfade')
	{
		this.stage2();
	} else
	{
		// fade the image down
		this.imageItem.fadeOut(this.speed, function()
		{
			var gallery		= SaltGallery.getGalleryByName($(this).attr('id').split('-')[1]);
			
			gallery.stage2();
		});
	}
}

_$pr.stage2 = function()
{
	var img = $('<img class="SaltGalImg-'+this.id+'" />').load(function()
	{
		var gallery		= SaltGallery.getGalleryByName($(this).attr('class').split('-')[1]);
		gallery.stage3();
	});

	this.oldImageItem = this.imageItem;
	this.oldImageItem.css('z-index',1);
//	this.imageItem.remove();
	
	this.imageItem = $(this.createWrapper()).appendTo(this.holder);
	this.imageItem.append(img);
	this.imageItem.css('opacity',0);
	
	for(var i=0 ;i < this.imagepacket.metafields.length ; i++)
	{
		this.imageItem.append(this.imagepacket.metafields[i]);
	}
	
	img.attr('src', this.imagepacket.href);
}

_$pr.stage3 = function()
{
	this.imageItem.animate({opacity:1},this.speed, function()
	{
		var gallery		= SaltGallery.getGalleryByName($(this).attr('id').split('-')[1]);
		gallery.cleanup();
	});
	this.holder.animate({height:$('img',this.imageItem).attr('height')},this.speed);
}

_$pr.cleanup = function()
{
	this.running = false;
	this.oldImageItem.remove();
	
	this.restarttimer();
}

_$pr.restarttimer = function()
{
	if(this.autorun)
	{
		var p = this;
		this.timer = setTimeout(function()
		{
			p.goNextGalItem();
		}, this.timeout);
	}
}

delete _$pr;


	$.fn.SaltGallery = function(options)
	{
		this.each(function()
		{
			var settings =
			{
				animationtype: 'crossfade',
				speed: 'normal',
				autorun: false,
				timeout: 2000,
				containerheight: 'auto'
			};
			
			if(options)
			{
				$.extend(settings, options);
			}
			
			var elements = $(this).children();
		
			if (elements.length > 1)
			{
				var gallery = new SaltGallery($(this),settings);
			}
		});
	};
