//klasa obslugi playera
var Player = new Class({
	
	Implements: [Events, Options],
	
	options: {
		player: 'player',
		tabs: null,
		content: null,
		delay: null,
		timer: false
	},
	
	//el.getElement('.cat').addEvent('mouseenter', this.show.bind(this, el));
	//el.getElement('.cat').addEvent('mouseleave', this.hide.bind(this));
	
	initialize: function(options){
		this.setOptions(options);
		this.options.switches = this.options.tabs+' a';
		this.linkTabs();
		this.slideshowStart();
		this.addPauseResumeBehaviour();
	},
	
	//dowiazuje zdarzenia onclick do pozycji playlisty
	linkTabs: function() {
		$$(this.options.switches).each(function(el) {
			el.addEvent('click', function(ev) { new Event(ev).stop(); })
			el.addEvent('click', this.goTo.bind(this, el));
		}.bind(this))
	},
	
	//przewija zawartosc do konkretnej pozycji lub nastepnej ('next')
	goTo: function(el) {
		if (el == 'next') {
			var active_tab = $$(this.options.tabs+'.active')[0];
			if (active_tab == $$(this.options.tabs).getLast()) var el = $$(this.options.tabs)[0].getFirst();
			else var el = $$(this.options.tabs+'.active')[0].getNext().getFirst();
		}
		$$(this.options.tabs).removeClass('active');
		el.getParent().set('class', 'active');			
		var href = el.get('href').substr(el.get('href').indexOf('#')+1);
		$(this.options.content).tween('opacity', 1, 0);
		(function() { new Fx.Scroll($(this.options.content), { duration: 0 }).toElement(href);}.bind(this)).delay(500);
		(function() { $(this.options.content).tween('opacity', 0, 1); }.bind(this)).delay(600);
	},

	slideshowStart: function() {
		if (!this.options.timer) this.options.timer = ( function() { /*console.log('timer goes, delay: '+this.options.delay);*/ this.goTo('next'); }.bind(this) ).periodical(this.options.delay);
	},
	
	slideshowStop: function() {
		if (this.options.timer) $clear(this.options.timer);
		this.options.timer = false;
	},
	
	addPauseResumeBehaviour: function() {
		$(this.options.player).addEvents({
			'mouseenter': this.slideshowStop.bind(this),
			'mouseleave': this.slideshowStart.bind(this)
		})
	}

}) //koniec klasy Player