var productsAccordion;
var elem;
var t;

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);

		var theHeight = 0;
		
		/*$$('.toggled').each(function(el) {
			theCompareHeight = 45 * Math.round(el.getChildren('li').length/2);
			if (theCompareHeight > theHeight) {
				theHeight = theCompareHeight;
			}
		});*/
		
		productsAccordion = new Accordion($$('.toggler'), $$('.toggled'), {
			typeOfEvent : 'mouseover',
			display : 100,
			alwaysHide : false
		});
		
		$$('.toggled').each(function(el) { el.getParent().addEvent('mouseleave', function() { productsAccordion.display(100);}); });
		
		/*if($('chunk-products').getFirst('ul') != null)
			$('chunk-products').getFirst('ul').setStyle('height' , $('chunk-products').getFirst('ul').getSize().y + theHeight);*/
	
		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'));	
		
		new iFishEye({
			useAxis:"both",
			/*dimThumb: {width:43, height:60},
			dimFocus: {width:65, height:90},*/
			dimThumb: {width:85, height:117},
			dimFocus: {width:130, height:180},
			eyeRadius: 150,
			pupilRadius: 70,
			container: $('domains-container'),
			'onPupilOver' : function(obj) {
			}
		});
		
		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');
			}	
		});
		

	}
};

function hideMenu(element, direction)
{
	if(direction && element == null)
	{
		productsAccordion.display(elem);
		elem = null
	}
	else
	{
		if(elem == null && !direction)
		{
			elem = element
			t = setTimeout('hideMenu(null, true)', 1000);
		}
		else
		{
			clearTimeout(t);
			t = setTimeout('hideMenu(null, true)', 1000);
		}
	}
}
window.addEvent("domready", Page.initialize);