jQuery(document).ready(function()
{
	if(jQuery.cookie('gallery') != null)
	{
		jQuery.address.value(jQuery.cookie('gallery'));
		jQuery.cookie('gallery', null);
	}
	
	if(jQuery("div[id='rotator'][class='auto']").length > 0)
	{
		Gallery.getInstance().run(0, true);
	}
	
	jQuery("a[class='slideshow-prev']").click(function()
	{
		Gallery.getInstance().prev();
	});
	
	jQuery("a[class='slideshow-next']").click(function()
	{
		Gallery.getInstance().next();
	});

	jQuery("a[class='image']").click(function(e)
	{
		Gallery.getInstance().next();
		e.preventDefault();
	});
	
	jQuery("a[class='pause']").click(function()
	{
		Gallery.getInstance().pause();
		jQuery("a[class='play']").show();
		jQuery("a[class='pause']").hide();
	});
	
	jQuery("a[class='play']").click(function()
	{
		Gallery.getInstance().run(jQuery.address.value());
		jQuery("a[class='pause']").show();
		jQuery("a[class='play']").hide();
	});
	
	jQuery("div[id='rotator'] a[class='image'] img").each(function()
	{
		jQuery(this).css("marginLeft", (900 - parseInt(jQuery(this).attr("width"))) / 2 + "px");
		jQuery(this).css("marginTop", (675 - parseInt(jQuery(this).attr("height"))) / 2 + "px");
	});
});

jQuery.address.strict(false);
jQuery.address.externalChange(function(event)
{
	
	if(jQuery("div[id='rotator']").find("input[type='password']").length > 0)
	{
		jQuery.cookie('gallery', event.value);
	} else
	{
		Gallery.getInstance().run(event.value);
	}
});

function Gallery()
{
	this.timer = undefined;
	this.timeout = 3000;
	this.silent = false;
	this.totalNumber = jQuery("div[id='rotator'] a[class='image']").length;
}

Gallery.__instance__ = null;
Gallery.prototype.timer;
Gallery.prototype.timeout;
Gallery.prototype.silent;
Gallery.prototype.totalNumber;

Gallery.getInstance = function()
{
    if(this.__instance__ == null)
	{
		this.__instance__ = new Gallery();
	}
	return this.__instance__;
}

Gallery.prototype.run = function(hash, silent)
{
	if(silent != undefined) this.silent = silent;
	if(!isNaN(parseInt(hash)))
	{	
		this.updateNumber(hash);
		this.timer = setTimeout("Gallery.getInstance().keepRunning(" + hash + ");", 0);
	}
}

Gallery.prototype.keepRunning = function(hash)
{
	hash = parseInt(hash);
	var timeout = this.timeout;
	jQuery("div[id='rotator'] a[class='image']").each(function(i)
	{
		if(hash != i)
		{
			jQuery(this).fadeOut(timeout / 3);
		}
	});
	var possibleElements = jQuery("div[id='rotator'] a[class='image']");
	if(hash == possibleElements.length) hash = 0;
	possibleElements.eq(hash).fadeIn(timeout / 3, function()
	{
		if(!Gallery.getInstance().silent)
		{
			jQuery.address.value(hash.toString());
			Gallery.getInstance().updateNumber(hash);
		}
		Gallery.getInstance().timer = setTimeout("Gallery.getInstance().keepRunning(" + (hash + 1) + ");", timeout);
	}).find("img").fadeIn(timeout / 3);
}

Gallery.prototype.updateNumber = function(number)
{
	number++;
	jQuery("span[id='slideshow-info265']").html(number.toString() + "/" + this.totalNumber);
}

Gallery.prototype.pause = function()
{
	this.timer = clearTimeout(this.timer);
}

Gallery.prototype.stop = function()
{
	jQuery("div[id='rotator'] a[class='image']").stop(true, true);
	this.pause();
}

Gallery.prototype.prev = function()
{
	var hash = parseInt(jQuery.address.value());
	hash--;
	if(hash < 0) hash = this.totalNumber - 1;
	this.switchPicture(hash);
}

Gallery.prototype.next = function()
{
	var hash = parseInt(jQuery.address.value());
	hash++;
	if(hash > this.totalNumber - 1) hash = 0;
	this.switchPicture(hash);
}

Gallery.prototype.switchPicture = function(hash)
{	
	var hash = hash;
	var restart = false;
	if(this.timer != undefined)
	{
		this.stop();
		restart = true;
	}
	jQuery("div[id='rotator'] a[class='image']").eq(hash).show().find("img").show();
	jQuery("div[id='rotator'] a[class='image']").each(function(i)
	{
		if(i != hash)
		{
			jQuery(this).hide();
		}
	});
	if(!this.silent)
	{
		jQuery.address.value(hash.toString());
		this.updateNumber(hash);
	}
	if(restart)
	{
		this.run(hash);
		this.timer = setTimeout("Gallery.getInstance().keepRunning(" + (hash + 1) + ");", timeout);
	}
}
