var Page = {
	initialize: function() {

		var searchFormFocus = function() {
			if (this.value == 'search') {
				this.value = '';
			}
		}
	
		var searchFormBlur = function() {
			if (this.value == '') {
				this.value = 'search';
			}
		}

		$('search').addEvent('focus', searchFormFocus);
		$('search').addEvent('blur', searchFormBlur);

		theBanner = 'main';
		theFade = [];
		theFirstSlide = [];
		theSecondSlide = [];
		
		$('banners').setStyle('height', '240px');
		$('banners').getChildren('div').setStyle('position', 'static');
		$('banners').getChildren('div').setStyle('display', 'inline');
		$('banners').getChildren('div').setStyle('height', '0px');

		$(document.body).getElement('.container').setStyle('display', 'block');
		
		new iFishEye({
			useAxis:"both",
			dimThumb: {width:85, height:117},
			dimFocus: {width:130, height:180},
			eyeRadius: 100,
			pupilRadius: 70,	
			'onPupilOver' : function(obj) {
				theBannerOld = theBanner;
				theBanner = obj.id.substring(14);
				if (theBannerOld != theBanner) {
					bannerOver();
				}	
			}
		});
		
		newsBox = $('news').getFirst('ul');
		eventsBox = $('events').getFirst('ul');
		
		newsBox.setStyle('overflow', 'hidden');
		eventsBox.setStyle('overflow', 'hidden');
		
		theNumber = newsBox.getChildren('li').length;
		if (theNumber % 2) {
			newsBox.getLast('li').setStyle('height', '100px');
		}
		
		theNumber = eventsBox.getChildren('li').length;
		if (theNumber % 2) {
			eventsBox.getLast('li').setStyle('height', '100px');
		}
		
		var myNewsScroll = new Fx.Scroll(newsBox, {
			duration: 1000	
		});		
		var myEventScroll = new Fx.Scroll(eventsBox, {
			duration: 1000	
		});
		
		var moreNews  = new Element('p', {
			'id' : 'morenews',
			'class': 'button-down'
		});
		moreNews.inject($('news'));
		
		var moreEvents  = new Element('p', {
			'id' : 'moreevents',
			'class' : 'button-down'
		});
		moreEvents.inject($('events'));	
		
		moreNews.addEvent('click', function() {
			if (newsBox.getScroll().y + 100 < newsBox.getScrollSize().y) {
				toScrollY = newsBox.getScroll().y + 100;
				myNewsScroll.start(0, toScrollY);

				if (toScrollY + 200 > newsBox.getScrollSize().y) {
					moreNews.addClass('button-up');	
				}				
			} else {
				myNewsScroll.toTop();
				moreNews.removeClass('button-up');
			}	
		});
		
		
		moreEvents.addEvent('click', function() {		
			if (eventsBox.getScroll().y + 100 < eventsBox.getScrollSize().y) {
				toScrollY = eventsBox.getScroll().y + 100;
				myEventScroll.start(0, toScrollY);
			
				if (toScrollY + 200 > eventsBox.getScrollSize().y) {
					moreEvents.addClass('button-up');	
				}
			} else {
				myEventScroll.toTop();
				moreEvents.removeClass('button-up');
			}	
		});
		
		bannerOver();

	},

	reloadContainer : function() {

		//$$('span.container-bot').setStyle('display', 'none');

	}
};

function bannerOver() {
	$('banners').getChildren('div').each(function(el){
		el.getChildren().setStyle('display', 'none');
	});
	
	$('section_banner_'+theBanner).getChildren().setStyle('display', 'block');
	
	if (!theFirstSlide[theBanner]) {
		theh3 = $('section_banner_'+theBanner).getFirst('h3').getFirst('span');
		theFirstSlide[theBanner] = new Fx.Slide(theh3, {mode: 'horizontal'}).hide();
	}
	
	if (!theSecondSlide[theBanner]) {
		thep = $('section_banner_'+theBanner).getFirst('p').getFirst('span');
		theSecondSlide[theBanner] = new Fx.Slide(thep, {mode: 'horizontal'}).hide();
	}
	
	if (!theFade[theBanner]) {
		theimg = $('section_banner_'+theBanner).getFirst('img');
		theFade[theBanner] = new Fx.Tween(theimg, {'duration': 1000})
	}
	

	theFirstSlide[theBanner].hide();
	theSecondSlide[theBanner].hide();
	
	theFade[theBanner].set('opacity','0').start('opacity', '1').chain(function(){
    	theFirstSlide[theBanner].slideIn().chain(function(){
    		theSecondSlide[theBanner].slideIn();
		});
	});
}
window.addEvent("domready", Page.initialize);
window.addEvent("domready", Page.reloadContainer);