var MediaGallery = Class.create({

	initialize: function(element)
	{
		nest = element;
		text = nest.innerHTML.split(":")[1];
		nest.update();
		
		ajax_submitPost('action=loadGallery&itemid=' + text, function(response){
			nest.update(response.galleryContent);
			nest.addClassName('gallery');
			
			this.infoBox = $('galleryInfoBox');
			this.leftArrow = $('galleryLeftArrow');
			this.rightArrow = $('galleryRightArrow');
			this.leftImage = $('galleryLeftImage');
			this.leftImageTitle = $('galleryLeftImageTitle');
			this.rightImage = $('galleryRightImage');
			this.rightImageTitle = $('galleryRightImageTitle');
			this.centerImage = $('galleryCenterImage');
			this.galleryCenterImageTitle = $('galleryCenterImageTitle');
			this.galleryCenterImageInfo = $('galleryCenterImageInfo');

			this.gdata = response.gallery;

			this.rightArrow.down().observe('click', function(){
				this.stepForward();
			}.bindAsEventListener(this));

			this.rightImage.down().observe('click', function(){
				this.stepForward();
			}.bindAsEventListener(this));
			
			this.leftArrow.down().observe('click', function(){
				this.stepBack();
			}.bindAsEventListener(this));

			this.leftImage.down().observe('click', function(){
				this.stepBack();
			}.bindAsEventListener(this));

			this.previous = null;
			this.current = 0;
			if (this.gdata.length > 1)
				this.next = 1;

			this.redraw(false);

		}.bind(this));
	},
	
	stepForward: function()
	{
		if (this.current >= this.gdata.length-1)
			return false;
		this.previous = this.current++;
		if (this.current >= this.gdata.length-1)
			this.next = null;
		else
			this.next = this.current + 1;
		this.redraw(true);
	},

	stepBack: function()
	{
		if (this.current < 1)
			return false;
		this.next = this.current--;
		if (this.current <= 0)
			this.previous = null;
		else
			this.previous = this.current - 1;
		this.redraw(true);
	},
	
	redraw: function(autostart)
	{
		if (this.next == null)
		{
			this.rightImage.fade({duration: 0.3});
			this.rightArrow.fade({duration: 0.3});
		}
		else
		{
			this.rightImage.down(1).src = this.gdata[this.next].thumb;
			this.rightImage.appear({duration: 0.3});
			this.rightArrow.appear({duration: 0.3});
		}

		if (this.previous == null)
		{
			this.leftImage.fade({duration: 0.3});
			this.leftArrow.fade({duration: 0.3});
		}
		else
		{
			this.leftImage.down(1).src = this.gdata[this.previous].thumb;
			this.leftImage.appear({duration: 0.3});
			this.leftArrow.appear({duration: 0.3});
		}



		var s1 = new SWFObject("/img/mediaplayer.swf", "mediaplayer", "220", "185", "9");
		s1.addParam("allowfullscreen","true");
		s1.addVariable("width","220");
		s1.addVariable("height","185");
		s1.addVariable("file",this.gdata[this.current].mediafile);
		s1.addVariable("image",this.gdata[this.current].thumb);
//		s1.addVariable("autostart", autostart);
		s1.write(this.centerImage);

		var currentTitle = this.gdata[this.current].name;
		this.galleryCenterImageTitle.fade({duration: 0.2, afterFinish: function(){
			this.galleryCenterImageTitle.update(currentTitle);
			this.galleryCenterImageTitle.appear({duration: 0.2});
		}.bind(this)});

		var currentInfo = this.gdata[this.current].description;
		this.galleryCenterImageInfo.fade({duration: 0.2, afterFinish: function(){
			this.galleryCenterImageInfo.update(currentInfo);
			this.galleryCenterImageInfo.appear({duration: 0.2});
		}.bind(this)});

		var previousTitle = (this.previous == null) ? '' : this.gdata[this.previous].name;
		this.leftImageTitle.fade({duration: 0.2, afterFinish: function(){
			this.leftImageTitle.update(previousTitle);
			this.leftImageTitle.appear({duration: 0.2});
		}.bind(this)});

		var nextTitle = (this.next == null) ? '' : this.gdata[this.next].name;
		this.rightImageTitle.fade({duration: 0.2, afterFinish: function(){
			this.rightImageTitle.update(nextTitle);
			this.rightImageTitle.appear({duration: 0.2});
		}.bind(this)});
	}

});


Event.observe(window, 'load', function(){
	var galleries = $$('div.gallery');
	
	if (galleries.length)
		var gallery = new MediaGallery(galleries[0]);
});


