window.addEvent('domready', function() {		
	// animacja logotypów w nagłówku
	
	/* settings */
	var showDuration = 4000;
	var container = $('headerSponsors');
	if (container != null) {
		var items = container.getElements('li');
	}
	
	if (items != null && items.length > 1) {
		var currentIndex = 0;
		var interval;
		/* opacity and fade */
		items.each(function(t,i){ 
			if(i > 0) {
				t.set('opacity', 0);
			}
		});
		
		/* worker */
		var show = function() {
			items[currentIndex].fade('out');
			items[currentIndex = currentIndex < items.length - 1 ? currentIndex+1 : 0].fade('in');
		};
		/* start once the page is finished loading */
		window.addEvent('load',function(){
			interval = show.periodical(showDuration);
		});
	}
	
	// czyszczenie pola z mailem na newsletter, animacja logotypów w stopce
	Element.implement({ 
		clearFocusResetBlur: function(attr){ 
			var valueString = this.get(attr); 
			this.addEvents({ 
				'focus': function(){ 
					if( this.get('value') == valueString ) this.set('value',''); 
				}, 
				'blur': function(){ 
					if( this.get('value') == "" ) this.set('value',valueString); 
				} 
			}); 
		},
		
		switchBWtoC: function(){
			var BWsrc = this.get('src');
			var Csrc = this.get('rel');
			//var Csrc = this.getStyle('background-image').replace('url(', '').replace(')', '');
			//Csrc = Csrc.replace('\'', '');
			//Csrc = Csrc.replace('&quot;', '');
			
			
			this.addEvents({ 
				'mouseover': function(){ 
					this.set('src', Csrc); 
				}, 
				'mouseleave': function() {
					this.set('src', BWsrc);
				}
				 
			});
		}
	}); 
	
	
	$('email').clearFocusResetBlur('alt');
	
	$$('.logotypes img').switchBWtoC();
	
	$$('h2.toggler').setStyles({
		cursor: 'pointer'
	});
	
	$$('h4.toggler').setStyles({
		cursor: 'pointer',
		color: '#f2c800'
	});
	
	// ustal numer elementu do rozwinięcia w mainMenu
	var number = 0;
	var itemFounded = false;
	
	var itemHref = '';
	var lastHrefLength = 0;
	
	var requestURL = window.location.href;
	
	$$('#mainMenu ul.element').each(function(el) {
		var items = el.getElements('li a');
		items.each(function(el) {
			itemHref = el.getProperty('href');
			
			if (itemHref == requestURL.substr(0,itemHref.length)) {
				if (itemHref.length > lastHrefLength) {
					el.getParent().addClass('selected');
					itemFounded = true;
					lastHrefLength = itemHref.length;
				}
			}
		});
		if (itemFounded) return;
		else number++;
	});
	
	if (itemFounded) {
		var myAccordion = new Accordion($('mainMenu'), 'h2.toggler', 'ul.element', {
			opacity: false,
			initialDisplayFx: true,
			alwaysHide: true,
			show: number
		});
	} else {
		var myAccordion = new Accordion($('mainMenu'), 'h2.toggler', 'ul.element', {
			opacity: false,
			initialDisplayFx: false,
			alwaysHide: true,
			display: -1
		});
	}
	
	var timeTablesAccordion = new Accordion(
		$('timeTablesAccordion'), 'h4.toggler', 'div.element', {
			opacity: false,
			initialDisplayFx: false,
			alwaysHide: true,
			show: -1
		}
	)
	
	var myShow = new Slideshow('sbGallery', null , { captions: true, controller: false, delay: 4000, height: 300, width: 250 });
	
	
	// pobieram boxy gameScroll
	$$('.gameScroll .displayArea').each(function(el) {
		var games = el.getElements('.game');
		if (games.length > 1) {			
			var displayAreaHeight = 0;
			games.each(function(gameBox) {
				var size = gameBox.getSize();
				if ( size.y > displayAreaHeight)
					displayAreaHeight = size.y;
			});
			el.setStyles({
				height: displayAreaHeight
			});	

			var displayAreaWidth = el.getSize().x;
			el.setStyles({
				width: displayAreaWidth
			});
			
			// zamknij wszystkie boxy .game w nowym divie
			
			var wrapper = new Element('div', {'class' : 'wrapper'});
			wrapper.setStyles({
				width: displayAreaWidth*games.length,
				//height: displayAreaHeight,
				position: 'absolute',
				top: 0,
				right: 0
			});
			wrapper.adopt(games);
			wrapper.inject(el);

			var rightOffset = 0;
			games.each(function(gameBox) {
				gameBox.setStyles({
					float: 'right',
					display: 'inline',
					clear: 'none',
					margin: 0,
					width: displayAreaWidth
				});
				rightOffset += displayAreaWidth;
			});
			
			
			// dodaj buttony nawigacyjne
			
			var prevBox = new Element('a', {'id': 'prevBox', 'class': 'navButton alpha'});
			var nextBox = new Element('a', {'id': 'nextBox', 'class': 'navButton alpha'});
			
			// Declaring increment vars 
			var totIncrement = 0;
			var increment = displayAreaWidth;
			var maxRightIncrement = -increment*(games.length-1);
			// set tweens
			wrapper.set('tween', {
				duration: 500,
				transition: Fx.Transitions.Back.easeInOut,
				wait: true
			});
			
			prevBox.addEvent('click', function() {
				totIncrement -= increment;
				if (totIncrement < maxRightIncrement)
					totIncrement += increment;
				wrapper.tween('right', totIncrement);
			});
			
			nextBox.addEvent('click', function() {
				totIncrement += increment;
				if (totIncrement > 0)
					totIncrement -= increment;
				wrapper.tween('right', totIncrement);
			});
			
			prevBox.inject(el, 'before');
			nextBox.inject(el, 'before');
			
		}
		
	});
});
