window.addEvent('load', function() {
								 
	if($$('#showcaseContainer').length > 0) {
	
		var page = 0;
		var maxpage = $$('#showcasePager a').length-1;
		var turned = true;
		var rolled = false;
		var myFx = new Fx.Morph($('showcaseContainer'), {duration: 750, transition: Fx.Transitions.Quart.easeInOut}).set({'left' : page*682*(-1)});
		var myFx2 = new Fx.Morph($('showcaseContainer'), {duration: 1000, transition: Fx.Transitions.Quart.easeInOut}).set({'height' : 237});
		var myFx3 = new Fx.Morph($('showcaseViewPort'), {duration: 750, transition: Fx.Transitions.Quart.easeInOut}).set({'height' : 237});
		var myFx4 = new Fx.Morph($('showcase'), {duration: 750, transition: Fx.Transitions.Quart.easeInOut}).set({'height' : 318});
		
		$$('#showcaseContainer .desc').each(function(el, it){
			var myFx5 = new Fx.Morph(el, {duration: 1000, transition: Fx.Transitions.Quart.easeInOut}).set({'height' : 228});
			el.addEvent('rolldown', function(){
				myFx5.pause();
				myFx5.start({'height' : 492});
				turned = false;
			});
			el.addEvent('rollup', function(){
				myFx5.pause();
				myFx5.start({'height' : 228});
				turned = true;
			});
		});
		
		function checkPages(it) {
			if(it < 0 || it > maxpage)  {
				it = 0;
			}
			page = it;
		}
		
		function changeViewPort(it) {
			checkPages(it);
			myFx.pause();
			myFx.start({'left' : page*682*(-1)});
		}
		
		function autoChangeViewPort() {
			if(!turned || rolled) return false;
			page = page.toInt() + 1;
			checkPages(page);
			$('showcasePager-' + page).fireEvent('click');
		}
		
		autoChangeViewPort.periodical(6000);
	
		$$('#showcasePager a').each(function(el, it){
			el.addEvent('click', function(ev){
				$$('#showcasePager a').removeClass('on');
				this.addClass('on');
				var rel = this.get('rel').split('-')[1];
				changeViewPort(rel);
				if(ev) ev.stop();
			})
		});
		
		$('showcaseRolldown').addEvent('click', function(ev){
			if(ev) ev.stop();
			if(!rolled) {
				$('showcaseRolldownSpan').set('text', 'Zwiń');
				rolled = true;
				myFx3.start({'height' : 500});
				myFx2.start({'height' : 500});
				myFx4.start({'height' : 582});
				$$('#showcaseContainer .desc').each(function(el, it){
					el.fireEvent('rolldown');											 
				});
				this.addClass('up');
				if($$('#allegro').length > 0) { $('allegro').setStyle('display', 'none'); }
			} else {
				$('showcaseRolldownSpan').set('text', 'Rozwiń');
				rolled = false;
				myFx3.start({'height' : 237});
				myFx2.start({'height' : 237});
				myFx4.start({'height' : 318});
				$$('#showcaseContainer .desc').each(function(el, it){
					el.fireEvent('rollup');											 
				});
				this.removeClass('up');
				if($$('#allegro').length > 0) { $('allegro').setStyle('display', 'none'); }
			}
		});
		
		$('showcaseContainer').addEvent('mouseenter', function(){
			turned = false;
		});
		$('showcaseContainer').addEvent('mouseleave', function(){
			turned = true;
		});
		$('showcasePager').addEvent('mouseenter', function(){
			turned = false;
		});
		$('showcasePager').addEvent('mouseleave', function(){
			turned = true;
		});
		
		$('showcaseRolldown').addEvent('mouseenter', function(){
			turned = false;
		});
		$('showcaseRolldown').addEvent('mouseleave', function(){
			turned = true;
		});
		
	}


	if($$('#productLinker').length > 0) {
		$('productLinker').addEvent('click', function(ev){
			if(ev) ev.stop();
			$('productlisting').setStyle('display', 'block');
		});
	}
	if($$('#productlisting').length > 0) {
		$('productlisting').addEvent('mouseleave', function(ev){
			if(ev) ev.stop();
			$('productlisting').setStyle('display', 'none');
		});
	}
	if($$('#productlistingCloseButton').length > 0) {	
		$('productlistingCloseButton').addEvent('click', function(ev){
			if(ev) ev.stop();
			$('productlisting').setStyle('display', 'none');
		});
	}
	if($$('#allegroCloseButton').length > 0) {	
		$('allegroCloseButton').addEvent('click', function(ev){
			if(ev) ev.stop();
			$('allegro').setStyle('display', 'none');
		});	
	}
		
});
