(function(){
	var Slider = new Class({
		Implements: [Events, Options],
		options: {
			'size': 0,
			'box': null,
			'items': null,
			'offset': 0,
			'interval': 1000,
			'autoPlay': false,
			'autoPlayDirection': 'next',
			'fxOpt': {}
		},
		
		initialize: function(opt){
			this.setOptions(opt);
			var klass = this;
			this.box = this.options.box.setStyle('width', (this.options.items.length * this.options.size) + 'px');
			this.box.setStyle('margin-left', this.options.offset);
			this.copyItem('prev');
			this.removeItem();
			
			this.slideFx = new Fx.Tween(this.box, $merge(this.options.fxOpt, {
				property: 'margin-left',
				onComplete: function(){
					klass.removeItem();
				}
			}));
			
			if ($type(this.options.btNext) == 'element') {
				this.options.btNext.addEvent('click', function(e){
					e.stop();
					klass.next(true, true);
				});
			}
			
			if ($type(this.options.btPrev) == 'element') {
				this.options.btPrev.addEvent('click', function(e){
					e.stop();
					klass.prev(true, true);
				});
			}
			
			if(this.options.autoPlay){
				this.play(this.options.interval, this.options.autoPlayDirection);
			}
		},
		
		slide: function(dir, fx, manual){
			fx = ($defined(fx)?fx: true);
			multi = (dir=='next'?-1:1);
			
			if(manual){
				this.stop();
			}
			
			if (!this.toRemove) {
				this.copyItem(dir);
			}
			
			if (fx) {
				this.dir = dir;
				this.slideFx.start(this.box.getStyle('margin-left').toInt() + (this.options.size * multi));
			}
		},
		
		copyItem: function(dir){
			if (dir == 'next') {
				this.toRemove = this.options.items[0];
				var newLast = $(this.toRemove).clone();
				var oldLast = $(this.options.items.getLast());
				this.options.items.push(newLast);
				this.box.setStyle('width', this.options.items.length * this.options.size);
				oldLast.grab(newLast, 'after');
			}
			else {
				this.toRemove = this.options.items.getLast();
				var newFirst = $(this.toRemove).clone();
				var oldFirst = $(this.options.items[0]);
				this.options.items.unshift(newFirst);
				this.box.setStyle('width', (this.options.items.length * this.options.size) + 'px');
				this.box.setStyle('margin-left', this.box.getStyle('margin-left').toInt() - this.options.size);
				oldFirst.grab(newFirst, 'before');
			}
		},
		
		removeItem: function(dir){
			if(this.toRemove){
				this.toRemove.dispose();
				this.options.items.erase(this.toRemove);
				this.box.setStyle('width', (this.options.items.length * this.options.size) + 'px');
				if (this.dir == 'next') {
					this.box.setStyle('margin-left', this.box.getStyle('margin-left').toInt() + this.options.size);
				}
				this.toRemove = null;
			}
		},
		
		next: function(fx, manual){
			this.slide('next', fx, manual);
		},
		prev: function(fx, manual){
			this.slide('prev', fx, manual);
		},

		play: function(interval,direction){
			this.stop();
			this._play = this[direction].periodical(interval,this);
		},

		stop: function(){
			$clear(this._play);
		}
	});
	
	var setSizeCss = function(file){
		var css_el = $('css_size');
		css_el.set('href', css_el.get('href').toURI().set('file', file).toString());
		Cufon.refresh.delay(200);
	};
	
	window.addEvent('load', function(){
		var text_size = Cookie.read('text-size');
		if(text_size){
			setSizeCss(text_size);
		}
	});
	
	window.addEvent('domready', function(){
			$$('a[target=thePicture]').each(function(el){
			el.addEvent('click', function(e){
				e.stop();
				return false;
			});
			el.removeProperty('onclick').removeProperty('target');
			var picUrl = el.get('href').match(/file=([^&]*)/i)[1];
			el.set({
				'rel': 'milkbox[Inhalt]',
				'href': decodeURIComponent(picUrl)
			});
		});
		milkbox.reloadGalleries();

		if (milkbox.overlay) {
			milkbox.overlay.setStyle('backgroundColor', '#FFF7BC');
			milkbox.changeOptions({
				'imageOfText': 'von'
			});
		}
		
		if (Browser.Engine.trident) {
			$('main_menu').getElements('li').addEvents({
				'mouseenter': function(){
					this.addClass('hover');
				},
				'mouseleave': function(){
					this.removeClass('hover');
				}
			});
		}
		
		$('size_menu').getElements('a').addEvent('click', function(e){
			e.stop();
			
			var file = '';
			
			switch(this.get('href')){
				case '#small':
					file = 'text.css';
					break;
				case '#big':
					file = 'text-big.css';
					break;
				case '#bigger':
					file = 'text-bigger.css';
					break;
			}
			setSizeCss(file);
			
			Cookie.write('text-size', file, {domain: 'msplhs24.bon.at'});
			
			return false;
		});
		
		if ($('gallery_items').getElements('li').length > 0) {
			var slider = new Slider({
				'autoPlay': true,
				'interval': 10000,
				'fxOpt': {duration: 1500},
				'size': 560,
				'offset': 210,
				'box': $('gallery_items'),
				'items': $('gallery_items').getElements('li'),
				'btNext': document.getElement('.gallery_button.right'),
				'btPrev': document.getElement('.gallery_button.left')
			});
		}
	});
})();



