var Swing = new Class({

	// current position of swing
	position: new Number,

	// Able to rotate
	rotatable: new Boolean,

	// elements collection
	elems: new Number,
	// element's width
	elemsWidth: new Number,
	// element's margin-right
	elemsMargin: new Number,
	// How many elements shown on page
	elemsOnPage: new Number,

	// navigation
	nextButton: new Object,
	prevButton: new Object,

	// The Object and his wrapper
	obj: new Object,
	wrapper: new Object,

	// teaser animation
	fx: new Object,

	// method poses teaser to an aimed state
	pose: function(position){
		if (position == 0) {
			this.position = this.elems.length-this.elemsOnPage+1;
		} else if (position > this.elems.length-this.elemsOnPage+1) {
			this.position = 1;
		} else {
			this.position = position;
		}

		this.fx.start('left', -(this.position -1)*(this.elemsWidth+this.elemsMargin));
	},

	// constructor
	initialize: function(obj){
		this.obj = obj;
		this.wrapper = obj.getElements('[id|="wrapper"]')[0];
		this.nextButton = obj.getElements('[id|="next"]')[0];
		this.prevButton = obj.getElements('[id|="prev"]')[0];
		this.elems = obj.getElements('[id|="wrapper"] div');
		this.elemsWidth = this.elems[0].getStyle('width').toInt();
		this.elemsMargin = this.elems[0].getStyle('margin-right').toInt();
		this.rotatable = (this.elemsWidth*this.elems.length > this.obj.getStyle('width').toInt()) ? true:false;
		this.elemsOnPage = Math.floor(this.obj.getStyle('width').toInt() / this.elemsWidth);
		if (!this.rotatable) {
				this.nextButton.setStyle('visibility', 'hidden');
				this.prevButton.setStyle('visibility', 'hidden');
			}
		this.fx = new Fx.Tween(this.wrapper, {
			fps: 60,
			unit: 'px',
			duration: 300,
			link: 'chain',
			transition: Fx.Transitions.Circ.easeOut
		});
		var _this_ = this;

		// initialize events
		this.nextButton.addEvent('click', function(){
			if (_this_.rotatable) _this_.pose(_this_.position+1);
			return false;
		});

		// initialize events
		this.prevButton.addEvent('click', function(){
			if (_this_.rotatable) _this_.pose(_this_.position-1);
			return false;
		});

		return this.pose(1);
	}
});




window.addEvent('domready', function(){


	// run markup hacks
	new Exm('input');


	// milkbox init
	try {
		window.milkbox = new Milkbox({
			overlayOpacity: 0.8
		});
	} catch(e){}


	// vacancy module
	try {
		$$('#vacancy form select').addEvent('change', function(){
			this.getParent('form').submit();
		});
	} catch(e){}
	
	
	// image (swop the IMG src after mouseover event on A)
	try {
		if ( $('image').match('.image-dynamic') && ( $('links') || $('fields') ) ) {
			$$('#links a[rel], #fields a[rel]').addEvent('mouseover', function(){
				var path_small = '/design/img/image.' + this.get('rel') + '_small.jpg';
				var path_big = '/design/img/image.' + this.get('rel') + '_big.jpg';
				$$('#image img').set('src', path_small);
				$$('#image a').set('href', path_big);
				window.milkbox.reloadGalleries();
			});
		}
	} catch(e){}
	
	
	// release Swing for banners on home
	try {
		new Swing($('banners'));
	} catch(e) {}


	// Teaser init (for Partners)
	try {
		if ($('partners-ribbon')) {
			var Teaser = new Class({initialize:function(d,a,c){this.items=3;this.step=280;this.cursor=1;this.fx=new Fx.Tween(d,{fps:60,unit:"px",duration:300,link:"chain",transition:Fx.Transitions.Circ.easeOut});try{a.addEvent("click",this.prev.bind(this));c.addEvent("click",this.next.bind(this))}catch(b){return this}return this},prev:function(){if(this.cursor>1){this.cursor--;this.fx.start("margin-left",-(this.cursor-1)*this.step)}return this},next:function(){if(this.cursor<(this.items)){this.cursor++;this.fx.start("margin-left",-(this.cursor-1)*this.step)}return this}});
			new Teaser($('partners-ribbon'), $('partners-previous'), $('partners-next'));
		}
	} catch(e){}


	// News switcher
	try {
		$$('#publications-nav a').addEvent('click', function(){
			this.getParent('div').getElements('a').removeClass('this');
			this.addClass('this');
			this.getParent('div').set('class', this.get('rel'));
			this.getParent('div').getAllNext().addClass('x-none');
			$(this.get('rel')).removeClass('x-none');
			return false;
		});
	} catch(e){}


});
