var gs = {
	currentImagePos	: 0,
	imageids		: [],
	imageholderid	: 'fotoholder',
	thumbholderid	: 'thumbnailholder',
	scrollerUpId	: 'arrowup',
	scrollerDownId	: 'arrowdown',
	currentScrollerOffset : 0,
	currentImageOffset : 0,
	scrollerdown: function() {
		this.showThumb(gs.getThumbId('down'));
	},
	
	scrollerup 	: function() {
		this.showThumb(gs.getThumbId('up'));
	},
	
	getThumbId	: function (dir) {
		var index = 0;
		if(dir == 'down') {
			if(this.currentScrollerOffset + 2 >= this.imageids.length) {
				index = this.imageids.length - 2;
			} else {
				index = this.currentScrollerOffset + 1;
			}
		} else {
			if(this.currentScrollerOffset - 1 >= 0) {
				index = this.currentScrollerOffset - 1;
			} else {
				index = 0;
			}
		}
		
		this.currentScrollerOffset = index;
		return this.imageids[index];
		
	},
	
	getImageId	: function (dir) {
		var index = 0;
		if(dir == 'down') {
			if(this.currentImageOffset + 1 >= this.imageids.length) {
				index = this.imageids.length - 1;
			} else {
				index = this.currentImageOffset + 1;
			}
		} else {
			if(this.currentImageOffset - 1 >= 0) {
				index = this.currentImageOffset - 1;
			} else {
				index = 0;
			}
		}
		
		this.currentImageOffset = index;
		if(this.currentImageOffset == 0) {
			$$('.button_left').set('opacity',0.3);
		} else {
			$$('.button_left').set('opacity',0.8);
		}
		if(this.currentImageOffset >= this.imageids.length - 1) {
			$$('.button_right').set('opacity',0.3);
		} else {
			$$('.button_right').set('opacity',0.8);
		}
		return this.imageids[index];
		
	},
	
	gotoNextImage : function () {
		this.showImage(this.getImageId('down'));
		return false;
	},
	gotoPreviousImage : function () {
		this.showImage(this.getImageId('up'));
		return false;
	},
	
	setCurrentScrollerOffset : function(id) {
		$each(gs.imageids,function(imageid,key) {
			if(imageid == id) {
				if(key == gs.imageids.length-1) {
					gs.currentScrollerOffset = previouskey;
				} else {
					gs.currentScrollerOffset = key;
				}
			}
			previouskey = key;
		});
	},
	
	setCurrentImageOffset : function(id) {
		$each(gs.imageids,function(imageid,key) {
			if(imageid == id) {
					gs.currentImageOffset = key;
			}
			previouskey = key;
		});
	},
	
	showThumb	: function(id) {
		var myFx = new Fx.Scroll($(gs.thumbholderid),{wheelStops : false}).toElement('thumb' + id);
		this.setCurrentScrollerOffset(id);
	},
	
	showImage	: function(id) {
		$(gs.imageholderid).fade(0.3,{'link' : 'chain'})
		
		var myFx = new Fx.Scroll($(gs.imageholderid),{wheelStops : false}).toElement('image' + id);
		var myFx = new Fx.Scroll($(gs.thumbholderid),{wheelStops : false}).toElement('thumb' + id);
		
		$(gs.imageholderid).fade(1,{'link' : 'chain'});
		
		
		this.setCurrentScrollerOffset(id);
		this.setCurrentImageOffset(id);
		if(this.currentImageOffset == 0) {
			$$('.button_left').set('opacity',0.3);
		} else {
			$$('.button_left').set('opacity',0.8);
		}
		if(this.currentImageOffset == this.imageids.length - 1) {
			$$('.button_right').set('opacity',0.3);
		} else {
			$$('.button_right').set('opacity',0.8);
		}
		$('piccount').set('html',this.currentImageOffset + 1)
	},

	addImage	: function(id) {
		$('image' + id).set('style','background:url(/image.php?ent=image&id=' + id + '&h=445&w=720&res=1) no-repeat center center');
		this.imageids[this.imageids.length] = id;
	},
	
	init		: function(){
	
		$(gs.scrollerUpId).addEvent('click', function() {
			gs.scrollerup();
		});
		$(gs.scrollerDownId).addEvent('click', function() {
			gs.scrollerdown();
		});

		$$('.button_left').set('opacity',0.8);
		$$('.button_right').set('opacity',0.8);
		this.showImage(this.imageids[0]);
	},
	setDefaultImage : function(id) {
		$('image').set('style','background:url(/image.php?ent=msprojectsettings&id=' + id + '&h=445&w=720&res=1) no-repeat center center');
		var myFx = new Fx.Scroll($(gs.imageholderid),{wheelStops : false}).toElement('image');
		this.init = function() { return true }
	}
}
window.addEvent('domready',function() {
	$('navigationbar').set('opacity',0.5);
});

window.addEvent('load', function() {
	gs.init();
});
