var jGalleries = {
	
	photos : [],
	initialized : null,
	carousel : null,
	paused : null,
	index : 0,
	jGalleriesMainImg : null,
	
	init : function()
	{
		this.jGalleriesMainImg = document.getElementById('jGalleriesMainImg');
	},
		
	displayImage : function(path)
    {
		var jGalleriesPlayPauseButton = document.getElementById('jGalleriesPlayPauseButton');
		jGalleriesPlayPauseButton.src = '/app/modules/jGalleries/images/play.gif';
		jGalleriesPlayPauseButton.name = "play";
		this.paused = true;	
        this.jGalleriesMainImg.src = path;
	},
	
	playGallery : function(index)
	{
		//wait 5 seconds if the loadGallery hasn't completed yet and try again
		if (this.photos.length == 0)
			setTimeout(function(){	jGalleries.playGallery(0)}, 5000);
		else if (this.photos.length)
		{
			if (this.paused)
				return;
			if (index >= this.photos.length)
			{
				index = 0;
				if (this.index > 0)
					this.carousel.scroll(0);
			}
			this.index = index;
			$(this.jGalleriesMainImg).fadeOut(3000, function(){	jGalleries.smoothStep(index);	});
		}
	},
	
	smoothStep : function(index)
	{
		if (index > 0)
			this.carousel.next();
		$(this.jGalleriesMainImg).attr("src", "/app/helpers/img.php?w=560&h=400&constrain=1&img=" + jGalleries.photos[index].getAttribute('url').replace(/thumb/, "large"));
		this.jGalleriesMainImg.onload = function(){	
			$(jGalleries.jGalleriesMainImg).fadeIn(3000, function()
				{
					jGalleries.playGallery(jGalleries.index + 1);
				});
		};
	},
			
	togglePlayPauseButton : function()
	{
		var jGalleriesPlayPauseButton = document.getElementById('jGalleriesPlayPauseButton');
		if (jGalleriesPlayPauseButton.name == 'pause')
		{
			jGalleriesPlayPauseButton.src = '/app/modules/jGalleries/images/play.gif';
			jGalleriesPlayPauseButton.name = "play";
			this.paused = true;	
		}
		else if (jGalleriesPlayPauseButton.name == 'play')
		{
			jGalleriesPlayPauseButton.src = '/app/modules/jGalleries/images/pause.gif';
			jGalleriesPlayPauseButton.name = "pause";
			this.paused = false;
			this.playGallery(this.index);
		}
		return false;
	},
    
    loadGallery : function(gid)
    {
		if (this.paused === false)
			this.paused = true;
		$(this.jGalleriesMainImg).attr("src", "/app/modules/jGalleries/images/loading.gif");
		if (this.index > 0)
		{
			this.index = 0;
			this.carousel.scroll(0);
		}
		
		$.get("/app/modules/jGalleries/xml/" + gid + ".xml", 
				function(data)
				{
					jGalleries.photos = data.documentElement.getElementsByTagName("photo");
					if (jGalleries.initialized)
					{
						jGalleries.writeGallery();
					}
					else if (jGalleries.photos.length > 0) 
					{
						jGalleries.initialized = true;
					}
				}, "xml"
			);
    },
	
	writeGallery : function()
	{
		this.carousel.reset();
		if (this.photos.length){
			for (var i=0; i <= this.photos.length; i++)
			{
				var img = document.createElement('img');
				var thumbpath = "/app/helpers/img.php?w=100&h=70&constrain=1&img=" + this.photos[i].getAttribute('url');
				img.src = thumbpath;
				var largepath = "/app/helpers/img.php?w=560&h=400&constrain=1&img=" + this.photos[i].getAttribute('url').replace(/thumb/, "large");
				img.onclick = function(largepath)
					{
						return function()
						{
							jGalleries.displayImage(largepath);
						};
					}(largepath);
				
				this.carousel.add(i, img);
			}
		}
		this.playGallery(0);
	}
		
}
