var JSK_Galeria_rotacja = new Class ({
	initialize: function(config) {	
		this.elementy = $(config.listaid).getElements('li');
		this.ile = this.elementy.length;		
		this.interval = config.interval;
		this.resizeb = config.resize;
		this.obrazek = config.imgid;
		this.highlights = config.highlights;
		this.tresc = config.textid;
		this.pauza = config.pauza;
		this.play = config.play;
		this.boxobrazek = config.boxobrazek;
		this.boxlabel = config.label;
		this.skok_ile = config.skok_ile;
		this.imglink = config.imglink;
		this.wskaznik = 1;
		this.offset = config.skok_ile*(config.szer_el+config.margin);		
		this.pos = 0;
		this.wskaznik_slide = 1;
		this.startczas = this.przelacz.periodical(this.interval, this); 

		var othis = this;
		var slides = Math.round((this.ile/config.skok_ile));		
		var zawartosc = new Array();
		
		this.fx = new Fx.Morph($(config.listaid), {duration: 500, transition: Fx.Transitions.linear});
		this.scroll = new Fx.Scroll(config.wrapper, {
			offset:{'x':0, 'y':0}, 
			transition: Fx.Transitions.Cubic.easeOut
		}).toLeft();

		$(config.pauza).addEvent('click', function(event) {
			event = new Event(event).stop();
			othis.interval = 0;
			$clear(othis.startczas);
		});
		
		$(config.play).addEvent('click', function(event) {
			event = new Event(event).stop();
			if (othis.interval == 0) {
				othis.interval = config.interval;
				othis.startczas = othis.przelacz.periodical(othis.interval, othis); 
			}
		});		

		$(config.previous).addEvent('click', function(event) { 
			event = new Event(event).stop();
			othis.wskaznik = othis.wskaznik - 2;
			if (othis.wskaznik < 0) {
				othis.wskaznik = 0;
			}
			var l1 = config.skok_ile*othis.wskaznik_slide;
			var l2 = l1 - config.skok_ile;

			othis.przelacz();
			$clear(othis.startczas);
			startczas = othis.przelacz.periodical(othis.interval, othis); 
			if (othis.wskaznik <= l2) {
				othis.wskaznik_slide--;			
				othis.pos += -(othis.offset);
				othis.fx.start({ 
					'opacity': .3 
				}).chain(function(){
					this.start.delay(800, this, { 'opacity': 1 });
					othis.scroll.start(othis.pos);
				});	
			}				
		});	
		
		$(config.next).addEvent('click', function(event) { 
			event = new Event(event).stop();			
			othis.przelacz();
			$clear(othis.startczas);
			othis.startczas = othis.przelacz.periodical(othis.interval, othis); 			
		});			
		
		$(config.lewo).addEvent('click', function(event) {
			event = new Event(event).stop();			
			
			if(othis.wskaznik_slide == 1) return;
			othis.wskaznik_slide--;
			var ruch = (config.skok_ile*othis.wskaznik_slide)-config.skok_ile;
			othis.wskaznik = ruch+1;
			othis.ustawbox(ruch);
			othis.pos += -(othis.offset);
			
			$clear(othis.startczas);
			othis.startczas = othis.przelacz.periodical(othis.interval, othis); 
			
			othis.fx.start({ 
				'opacity': .3 
			}).chain(function(){
				this.start.delay(800, this, { 'opacity': 1 });
				othis.scroll.start(othis.pos);
			});			
		});		
		
		$(config.prawo).addEvent('click', function(event) { 
			event = new Event(event).stop();		
			
			var ruch = config.skok_ile*othis.wskaznik_slide;			
			if(othis.wskaznik_slide >= slides) return;
			
			othis.wskaznik = ruch+1;
			othis.ustawbox(ruch);
			othis.wskaznik_slide++;
			othis.pos += othis.offset;
			
			//$clear(othis.startczas);
			//startczas = othis.przelacz.periodical(othis.interval, othis);	
			
			othis.fx.start({ 
				'opacity': .3 
			}).chain(function(){
				this.start.delay(800, this, { 'opacity': 1 });
				othis.scroll.start(othis.pos);
			});			
		});	
		
		tresc = $(config.textid);
		obrazek = $(config.imgid);	
		
		this.elementy.each( function(el,ids){										
			zawartosc[ids] = el;
			el.setStyle('width',config.szer_el);
			el.setStyle('margin-left',config.margin);
			var link = el.getElement('a');
			link.addEvent('click', function(e){
				e = new Event(e).stop();
				othis.wskaznik = ids+1;
				othis.ustawbox(ids);
				$clear(othis.startczas);
				othis.startczas = othis.przelacz.periodical(othis.interval, othis); 
											
				return false;
			});
			link.addEvent('mouseover',function() {
				var img = el.getElement('img');
				img.highlight(othis.highlights);
			});				
		});
		;
		
		this.startczas
		this.zawartosc = zawartosc;
		this.ustawbox(0);
	},
	
	przelacz: function() {
		var wskaznik = this.wskaznik;

		if (wskaznik >= this.ile) {
			wskaznik = 0;
		}
		this.ustawbox(wskaznik);
		wskaznik++;		
		this.wskaznik = wskaznik;
		
		var ruch = wskaznik/(this.skok_ile*this.wskaznik_slide);
		
		if ((ruch > 1) || (wskaznik == 1)) {
			if (wskaznik == 1) {
				this.wskaznik_slide = 1;
				this.pos = 0;
			} else {	
				this.wskaznik_slide++;
				this.pos += this.offset;
			}
			
			var othis = this;
			this.fx.start({ 
				'opacity': .3 
			}).chain(function(){
				this.start.delay(800, this, { 'opacity': 1 });
				othis.scroll.start(othis.pos);
			});
		
		
		}
	},
	
	ustawbox: function(ids) {

		var el = this.zawartosc[ids];
		var obrazek = $(this.obrazek);
		var boxobrazek = $(this.boxobrazek);
		var boxobrazek1 = $('media_obrazek');
		var boxlabel = $(this.boxlabel);
		var resizeo = $(this.resizeb);
		var link = el.getElement('a');
		var boxlabel = $(this.boxlabel);
		var img = el.getElement('img');
		var nextb = $(this.next);
		var linkt = new Element('a', {
			'href': link.get('href'),
			'html': link.get('rel')
		});
		var links = new Element('a', {
			'href': link.get('href'),
			'html': link.get('title')
		});
		var wazne = new Element('h2');		
		var tresc = $(this.tresc);
		var fx = new Fx.Morph(boxobrazek1, {duration: 300, transition: Fx.Transitions.Sine.easeOut});
 		var fx2 = new Fx.Morph(boxobrazek1, {duration: 200, transition: Fx.Transitions.Sine.easeOut});
			
		tresc.empty();
		
		if (!link.get('title')) {
			boxlabel.setStyle('display','none');
		} else {
			boxlabel.setStyle('display','block');	
		}
		
		wazne.adopt(linkt);
		tresc.adopt(wazne);
		tresc.adopt(links);
		img.highlight(this.highlights);
		othis = this;
		
		fx2.start({ 
			'opacity' : 0													
		}).chain(function(){					
				boxobrazek1.empty();
				var loadimg = '/img/ajax/loading7.gif';
			//	var load = new Element('img', { 'src': loadimg, 'class': 'loading' }).inject(boxobrazek1); 
				boxobrazek1.addClass('loader');
				fx2.start({ 'opacity' : 1 });
				var largeImage = new Element('img', { 'src': link.get('href'),'border': 0 });
				var linkobr = new Element('a', {
								'href': '#',								
								'events': {
									'click': function(event){
										event = new Event(event).stop();			
										othis.przelacz();
										$clear(othis.startczas);
										othis.startczas = othis.przelacz.periodical(othis.interval, othis); 
									}
								}
				});						
				fx.start({ 
						'opacity' : 0													
				}).chain(function(){
						boxobrazek1.removeClass('loader');
						boxobrazek1.empty();
						resizeo.set('href', img.get('rel'));											
						largeImage.inject(linkobr);
						linkobr.inject(boxobrazek1);
						fx.start({'opacity': 1});	
				});	
					
		});	
	}	
});
		
		
									