var SlideShow = {
	cont: null,
	pics: [],
	pictures: [],
	count: 0,
	dir: '/images/',
	current: 1,
	paused: false,
	timeout: 0,
	
	idle_time: 5000,
	fade_time: 1800,

	Start: function(count) {
		this.cont = $('#top_gallery');

		this.pics[0] = $("<img src='" + this.pictures[0] +"'>").css({'z-index': 10}).appendTo(this.cont);
		
		this.pics[1] = $("<img src='" + this.pictures[1] +"'>").css({'z-index': 0}).appendTo(this.cont);
		
		var t = this;
		t.timeout = setTimeout(function(t){return function() {t.Slide();}}(this), t.idle_time);
	},
	
	Slide: function() {
		var t = this,
		first = (this.pics[0].css('z-index') == 10) ? 0 : 1, second = (first == 0) ? 1 : 0;
		
		if (!this.paused) {
		this.pics[first].animate({opacity: 0}, this.fade_time, 'linear', function() {
			var next_pic = t.GetNext(t.current);
			$(this).css({'z-index': 0, 'opacity': 1});
			$(this).attr('src', t.pictures[next_pic]);
			$(this).siblings('img').css({'z-index': 10, 'opacity': 1});
			
			t.current = next_pic;
			t.timeout = setTimeout(function(t){return function() {t.Slide();}}(t), t.idle_time);
		});
		} else {
			this.timeout = setTimeout(function(t){return function() {t.Slide();}}(t), t.idle_time);
		}
	},
	
	GetNext: function(i) {
		return (++i >= this.count) ? 0 : i;
	},
	
	SetPictures: function(pictures) {
		this.pictures = pictures;
		this.count = pictures.length;
	},
	
	PlayPause: function() {
		this.paused = !this.paused;
        document.getElementById('play-pause-button').src = this.paused ? '/images/play_button.gif' : '/images/pause.gif';
		return false;
	},
	
	Next: function() {
		clearTimeout(this.timeout);
		this.Slide();
		return false;
	},
	
	Previous: function() {
		clearTimeout(this.timeout);
		this.current = (this.current < 2) ? this.count-2+this.current-1 : this.current-2;
		this.Slide();
		return false;
	}
}
