var jGalleries = {
	
	photos : [],
	initialized : null,
	carousel : null,
	paused : null,
	index : 0,
	jGalleriesMainImg : null,
	
	init : function()
	{
		this.jGalleriesMainImg = document.getElementById('jGalleriesMainImg');
	},

	getAjaxObject : function ()
	{
		if (window.XMLHttpRequest)
			return new XMLHttpRequest();
		else if (window.ActiveXObject)
			return new ActiveXObject("Microsoft.XMLHTTP");
		else
			alert("Your browser does not support XMLHTTP!");
	},
		
	displayImage : function(path)
    {
		this.togglePlayPauseButton();
        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.paused)
				return;
			if (index >= this.photos.length)
			{
				index = 0;
				if (this.index > 0)
					this.carousel.scroll(1);
			}
			this.index = index;
			$(this.jGalleriesMainImg).fadeOut(2000, function(){	jGalleries.smoothStep(index);	});
		}
	},
	
	smoothStep : function(index)
	{
		if (index > 0)
			this.carousel.next();
		this.jGalleriesMainImg.src = "/app/helpers/img.php?w=550&h=250&constrain=1&img=" + jGalleries.photos[index].getAttribute('url').replace(/thumb/, "large");
		$(this.jGalleriesMainImg).fadeIn(3000, function()
			{
				jGalleries.playGallery(++index);
			});
	},
			
	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;
		
    	var ajaxObject = this.getAjaxObject();
		ajaxObject.onreadystatechange=function()
		{
			if (this.readyState==4)
			{
				jGalleries.photos = this.responseXML.documentElement.getElementsByTagName("photo");
				if (jGalleries.initialized)
				{
					jGalleries.writeGallery();
					jGalleries.playGallery(0);
				}
				else if (jGalleries.photos.length > 0) {
					jGalleries.initialized = true;
				}
			}
		};
		
		ajaxObject.open("GET","/app/modules/jGalleries/xml/" + gid + ".xml",true);
		ajaxObject.send();
    },
	
	writeGallery : function()
	{
		//this.jGalleriesMainImg.src = "/app/helpers/img.php?w=550&h=250&constrain=0&img=" + this.photos[0].getAttribute('url').replace(/thumb/, "large");
		
		this.carousel.reset();
		
		for (var i=0; i <= this.photos.length; i++)
		{
			
			var img = document.createElement('img');
			var thumbpath = "/app/helpers/img.php?w=100&h=85&&constrain=0&img=" + this.photos[i].getAttribute('url');
			img.src = thumbpath;
			img.width = 100;
			img.height = 85;
			var largepath = "/app/helpers/img.php?w=550&h=250&constrain=0&img=" + this.photos[i].getAttribute('url').replace(/thumb/, "large");
			img.onclick = function(largepath)
				{
					return function()
					{
						jGalleries.displayImage(largepath);
					};
				}(largepath);

			
			this.carousel.add(i, img);
		}
	}
		
}