var roomGallery = {
	
	photos : [],
	initialized : null,
	carousel : null,
	paused : null,
	index : null,
	roomGalleryMainImg : null,
	
	init : function()
	{
		//this.roomGalleryMainImg = $('#roomGalleryMainImg');
	},
	
	setRoomGalleryMainImg : function(eyeD)
	{
		this.roomGalleryMainImg = document.getElementById("roomGalleryMainImg"+eyeD);
	},

	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, eyeD)
    {
		this.pausePlayButton(eyeD);
        this.roomGalleryMainImg.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(){	roomGallery.playGallery(0)}, 5000);
		else 
		{
			if (this.paused)
				return;
			if (this.index === null)
			{
				$(this.roomGalleryMainImg).fadeOut(2000, function(){	
					$(roomGallery.roomGalleryMainImg).addClass("roomGalleryMainImg");	
					});
			}
			if (index >= this.photos.length)
			{
				index = 0;
				if (this.index > 0)
					this.carousel.scroll(1);
			}
			this.index = index;
			$(this.roomGalleryMainImg).fadeOut(2000, function(){	roomGallery.smoothStep(index);	});
		}
	},
	
	smoothStep : function(index)
	{
		if (index > 0)
			this.carousel.next();
		this.roomGalleryMainImg.src = "/app/helpers/img.php?w=480&h=360&constrain=1&img=" + roomGallery.photos[index].getAttribute('url');
		$(roomGallery.roomGalleryMainImg).fadeIn(3000, function()
			{
				setTimeout(function(){	roomGallery.playGallery(roomGallery.index + 1)}, 3000);
			});
	},
			
	togglePlayPauseButton : function(eyeD)
	{
		var roomGalleryPlayPauseButton = document.getElementById('roomGalleryPlayPauseButton'+eyeD);
		if (roomGalleryPlayPauseButton.name == 'pause')
		{
			roomGalleryPlayPauseButton.src = '/app/modules/rooms/images/play.gif';
			roomGalleryPlayPauseButton.name = "play";
			this.paused = true;	
		}
		else if (roomGalleryPlayPauseButton.name == 'play')
		{
			roomGalleryPlayPauseButton.src = '/app/modules/rooms/images/pause.gif';
			roomGalleryPlayPauseButton.name = "pause";
			this.paused = false;
			this.playGallery(this.index);
		}
		return false;
	},

	pausePlayButton : function(eyeD)
	{
		var roomGalleryPlayPauseButton = document.getElementById('roomGalleryPlayPauseButton'+eyeD);
		roomGalleryPlayPauseButton.src = '/app/modules/rooms/images/play.gif';
		roomGalleryPlayPauseButton.name = "play";
		this.paused = true;	
	},

    loadGallery : function(eyeD)
    {
		if (this.paused === false)
			this.paused = true;
    	var ajaxObject = this.getAjaxObject();
		ajaxObject.onreadystatechange=function()
		{
			if (this.readyState==4)
			{
				roomGallery.photos = this.responseXML.documentElement.getElementsByTagName("image");
				if (roomGallery.initialized)
					roomGallery.writeGallery();
				else if (roomGallery.photos.length > 0) {
					roomGallery.initialized = true;
				}
			}
		};
		
		ajaxObject.open("GET","/app/modules/rooms/xml/" + eyeD + ".xml",true);
		ajaxObject.send();
    },
	
	writeGallery : function()
	{
		
		this.carousel.reset();
		
		for (var i=0; i <= this.photos.length; i++)
		{
			
			var img = document.createElement('img');
			var thumbpath = "/app/helpers/img.php?w=94&h=80&&constrain=0&img=" + this.photos[i].getAttribute('url');
			img.src = thumbpath;
			img.width = 94;
			img.height = 80;
			var largepath = "/app/helpers/img.php?w=480&h=360&constrain=0&img=" + this.photos[i].getAttribute('url').replace(/thumb/, "large");
			img.onclick = function(largepath)
				{
					return function()
					{
						roomGallery.displayImage(largepath);
					};
				}(largepath);

			
			this.carousel.add(i, img);
		}
	}
		
}