var Teaser = new Class({

	// current position of teaser
	position: new Number,

	// navigation links collection
	navigation: new Array,

	// teaser animation
	fx: new Object,

	// delay of shifting
	delay: 10000,

	// delay of animation start after click
	delayClick: 25000,

	// shifting timer
	timer: new Object,

	// method poses teaser to an aimed state
	pose: function(position){
		this.position = position;
		this.navigation.removeClass('this');
		this.navigation[position-1].addClass('this');
		this.fx.start('left', -(this.position -1)*1000);
	},

	// method shifts teaser to next position
	shift: function(){
		var next = (this.position >= this.navigation.length) ? 1 : this.position+1;
		this.pose(next);
	},

	// method starts shifting
	start: function(){
		this.timer = this.shift.periodical(this.delay, this);
	},
	// method stops shifting
	stop: function(){
		$clear(this.timer);
	},

	// constructor
	initialize: function(elem){

		// construct props
		this.position = 1;
		this.navigation = elem.getElements('a');
		this.fx = new Fx.Tween(elem.getElement('img').get('id'), {
			fps: 60,
			unit: 'px',
			duration: 300,
			link: 'chain',
			transition: Fx.Transitions.Circ.easeOut
		});
		this.start();

		// initialize events
		var _this_ = this;
		this.navigation.addEvent('click', function(){
			_this_.pose(+this.get('text'));
			_this_.stop();
			_this_.start.delay(_this_.delayClick, _this_);
			return false;
		});
	}
});


var Swing = new Class({

	// current position of swing
	position: new Number,

	// Able to rotate
	rotatable: new Boolean,

	// elements collection
	elems: new Number,
	// elements with images count
	elemsCount: new Number,
	// element's width
	elemsWidth: 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.elemsCount-this.elemsOnPage+1;
		} else if (position > this.elemsCount-this.elemsOnPage+1) {
			this.position = 1;
		} else {
			this.position = position;
		}

		this.fx.start('left', -(this.position -1)*this.elemsWidth);
	},

	// 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.elemsCount = obj.getElements('[id|="wrapper"] div img').length;
		
		this.elemsWidth = 250; 
		this.rotatable = (this.elemsWidth*this.elemsCount > this.obj.getStyle('width').toInt()) ? true:false;
		this.elemsOnPage = 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();


	// milkbox init
	try {
		new Milkbox({
			overlayOpacity: 0.8
		});
	} catch(e) {}


	// release lang switcher
	try {
		$$('#langs a').addEvent('click', function(){
			this.getParent('form').set('action', this.get('href')).submit();
			return false;
		});
	} catch(e) {}


	// release calendars
	try {
		new DatePicker('input.sys-calendar', {
			positionOffset: { x: 0, y: 5 },
			pickerClass: 'sys-datepicker',
			days: [_['ui_cal_d_short_sun'], _['ui_cal_d_short_mon'], _['ui_cal_d_short_tue'], _['ui_cal_d_short_wed'], _['ui_cal_d_short_thu'], _['ui_cal_d_short_fri'], _['ui_cal_d_short_sat']],
			months: [_['ui_cal_m_jan'], _['ui_cal_m_feb'], _['ui_cal_m_mar'], _['ui_cal_m_apr'], _['ui_cal_m_may'], _['ui_cal_m_jun'], _['ui_cal_m_jul'], _['ui_cal_m_aug'], _['ui_cal_m_sep'], _['ui_cal_m_oct'], _['ui_cal_m_nov'], _['ui_cal_m_dec']],
			startDay: 1,
			format: 'd.m.Y',
			inputOutputFormat: 'd.m.Y',
			allowEmpty: true
		});
	} catch(e) {}


	// release Swing for banners on home
	try {
		new Swing($('banners'));
	} catch(e) {}


	// release rotator on home
	try {
		new Teaser($('teaser'));
	} catch(e) {}


	// release rotator on home
	try {
		var Rotator = new Hash({
			images: $$('#rotator img'),
			navlinks: $$('#rotator-nav a'),
			timer: null,
			position: null,
			delay: 10000,
			delayAfterClick: 25000,

			// method poses teaser to an aimed state
			goto: function(to){
				this.position = to;
				this.images.fade('hide');
				this.images[to-1].fade('in');
				this.navlinks.removeClass('this');
				this.navlinks[to-1].addClass('this');
				return this;
			},
			// method shifts teaser to next position
			step: function(){
				var next = (this.position >= this.navlinks.length) ? 1 : this.position+1;
				this.goto(next);
			},
			// method starts shifting
			start: function(){
				this.timer = this.step.periodical(this.delay, this);
			},
			// method stops shifting
			stop: function(){
				$clear(this.timer);
			},
			init: function() {
				var _this_ = this;
				this.images
					.fade('hide')
					.removeClass('x-hidden');
				this.navlinks.addEvent('click', function(){
					_this_.goto(+this.get('href').substr(1));
					_this_.stop();
					_this_.start.delay(_this_.delayAfterClick, _this_);
					return false;
				});
				this.goto(1);
				return this.start();
			}
		});
		Rotator.init();

	} catch(e) {}


	// accordeons
	try {
		$$('dl.accordeon dt').addEvent('click', function(){
			this.getParent('dl').getElements('dd').removeClass('this');
			this.getNext('dd').toggleClass('this');
			return false;
		});
	} catch(e) {}


	// accordion for thirdmenu
	try {
		$$('#thirdmenu a.accordion').addEvent('click', function(){
			this.getParent().getElements('.i').removeClass('open');
			this.getNext('.i').addClass('open');
			return false;
		});
	} catch(e) {}


	// Swap banners on HOME
	try {
		$$('.banner a.togg').addEvent('click', function(){
			this.getParent('.x-1').getElements('.banner').toggleClass('x-hidden');
			return false;
		});
		$$('.banner a.togg2').addEvent('click', function(){
			this.getParent('.x-1').getElements('.banner').toggleClass('x-hidden');
			return false;
		});
	} catch(e) {}


	// brands rotator
	try {
		if ($('brands')) {
			var quantity = 19-8;
			$('brands-next').addEvent('click', function(){
				var offset = $('brands-wrapper').getStyle('left').toInt() - 125;
				$('brands-wrapper').tween('left', (offset < -125*quantity)?(offset == -125*quantity-125)?0:-125*quantity:offset);
				return false;
			});
			$('brands-prev').addEvent('click', function(){
				var offset = $('brands-wrapper').getStyle('left').toInt() + 125;
				$('brands-wrapper').tween('left', (offset>0)?(offset==125)?-125*quantity:0:offset);
				return false;
			});
		}
	} catch(e) {}


	return;
});
