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.katid = config.katid;
		this.pauza = config.pauza;
		this.linkwiecej = config.linkwiecej;
		this.play = config.play;
		this.boxobrazek = config.boxobrazek;
		this.boxobrazek_media = config.boxobrazek_media;
		this.boxlabel = config.label;
		this.skok_ile = config.skok_ile;
		this.imglink = config.imglink;
		this.styl = config.styl_aktywny;
		this.skalowanie = config.skalowanie;
		this.wskaznik = 1;
		this.offset = config.skok_ile*(config.szer_el+config.margin);
		this.pos = 0;
		this.zaznacz = 0;
		this.tryb_link = (config.tryb_link) ? config.tryb_link : 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('height',config.wys_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);
				
				
				othis.interval = 0;
				$clear(othis.startczas);
				//$clear(othis.startczas);
				//othis.startczas = othis.przelacz.periodical(othis.interval, othis); 
				
				return false;
			});			
			
			el.addEvent('mouseover',function() {
				if (othis.zaznacz !=  ids) {
					el.addClass(othis.styl);
				}
			});
			el.addEvent('mouseout',function() {
				if (othis.zaznacz !=  ids) {
					el.removeClass(othis.styl);
				}
			});
		});
		;
		
		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 spr = this.wskaznik;

		var elpop = this.zawartosc[this.zaznacz];
			elpop.removeClass(this.styl);
		
		var el = this.zawartosc[ids];
		var obrazek = $(this.obrazek);
		var boxobrazek = $(this.boxobrazek);
		var boxobrazek1 = $(this.boxobrazek_media);
		var boxlabel = $(this.boxlabel);
		var resizeo = $(this.resizeb);
		var link = el.getElement('a');
		var skalowanie = this.skalowanie;
		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('title')
		});
		var links = new Element('a', {
			'href': link.get('href'),
			'html': link.get('rel')
		});
		var wazne = new Element('h2');
		var tresc = $(this.tresc);

		if (this.katid) {
			var katid = $(this.katid);
			katid.set('html', link.get('rev'));
		}
		if (this.linkwiecej) {
			var linkwiecej = $(this.linkwiecej);
			linkwiecej.set('href', link.get('href'));
		}		
		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);
		
		el.addClass(this.styl);
		this.zaznacz = ids;
		
		othis = this;
		
		var zrodlo = (this.tryb_link) ? link.get('href') : img.get('longdesc');
		
		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': zrodlo,'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('longdesc'));
						largeImage.inject(linkobr);
						linkobr.inject(boxobrazek1);
						
						
						if (skalowanie) {
							var rozmiar_div = boxobrazek.getSize();
							var rozmiar_img = largeImage.getSize();					
							var tryb = (rozmiar_img.x > rozmiar_img.y) ? 1 : 0;
							
							var szer_n = rozmiar_img.x;
							var wys_n = rozmiar_img.y;
							
							if (tryb) {
								var procent = (rozmiar_div.x / rozmiar_img.x);							
								if (procent < 1) {
									var szer_n = procent * rozmiar_img.x;
									var wys_n = procent * rozmiar_img.y;
									largeImage.set('width',szer_n);
									largeImage.set('height',wys_n);
								}
							} else {
								var procent = (rozmiar_div.y / rozmiar_img.y);								
								if (procent < 1) {
									var wys_n = procent * rozmiar_img.y;
									var szer_n = procent * rozmiar_img.x;
									largeImage.set('height',wys_n);
									largeImage.set('width',szer_n);
								}
							}
							
							var padding = (rozmiar_div.x-szer_n)/2;						
							if (padding > 0) { largeImage.setStyle('padding-left',padding+'px'); }
							var padding = (rozmiar_div.y-wys_n)/2;
							if (padding > 0) { largeImage.setStyle('padding-top',padding+'px'); }						
						}		
						
						fx.start({'opacity': 1});
				});				

		});
	}
});

