window.addEvent('domready',function(){
	var _maxItems = 5;

	var getAllIDs = function(){
		var allIDs = [];
		if($$('#show-list1 li.show-list-item').length){
			var liEls = $('show-list1').getElements('li.show-list-item'),rel,umbs,count=0;
			for(var i=0,j=liEls.length;i<j && count<_maxItems;i++){
				rel = liEls[i].getProperty('rel');
				if(rel=='umbrella'){
					umbs = liEls[i].getElements('li.show-list2-item');
					for(var ii=0,jj=umbs.length;ii<jj;ii++){
						allIDs.push(umbs[ii].getProperty('rel').split(':')[0]);
						count++;
					}
				}else{
					allIDs.push(rel.split(':')[0]);
					count++;
				}
			}
		}else{
			//if($('show-list2')){
				var liEls = $('show-list2').getElements('li.show-list-item'),rel,count=0;
				console.log('2:',liEls);
				for(var i=0,j=liEls.length;i<j && count<_maxItems;i++){
					rel = liEls[i].getProperty('rel');
					allIDs.push(rel.split(':')[0]);
					count++;
				}
			//}
			
		}

		console.log('allIDs:',allIDs);
		return allIDs;
	}

	var showList_onChange = function(e){
		executed = true;
		//alert("n");
		//get the last x id
		var allIDs = getAllIDs();

		//we have got allIDs, remove event so this is executed only the 1st time
		//tx_pishowlist_public.removeEvent('change',showList_onChange);
		// do not so it change for every country change ;-)

		if(allIDs.length==0){
			$('homepage-showlist-fader').dispose();
			return;
		}

		//get images from ajax
		new Request.JSON({
			url:'index.php',
			method:'post',
			data:{
				eID: 'tx_ef_pishowlist_slideshow',
				showIDs: allIDs.join(','),
				dateFormat:tx_ef_pishowlist_dateFormat,
				dateFormat_noYear:tx_ef_pishowlist_dateFormat_noYear
			},
			onComplete:sL_slideshow_onRequestComplete
		}).send();
	};
	var sL_slideshow_onRequestComplete = function(response){
		console.log('response:',response);
		$('homepage-showlist-fader-items').setStyle('opacity',0);

		//clear .items elements
		$$('#homepage-showlist-fader-items .item').dispose();

		for(var i=0,j=response.length;i<j;i++){
			new Element('div',{
				'class':'item',
				'showID':response[i].show_id,
				'styles':{
					'opacity':(i==0?1:0),
					'visibility':(i==0?'visible':'hidden')
				}
			}).adopt(
				new Element('img',{
					'src':response[i].file,
					'alt':response[i].show_name
				}),
				new Element('div',{
					'class':'caption',
					'html':response[i].captionHTML
				})
			).inject('homepage-showlist-fader-items');

			if(i==(j-1)){
				var t =new Image();
				t.onload = function(){
					$('homepage-showlist-fader-items').setStyle('opacity',1);
					$('homepage-showlist-fader').removeClass('ajaxLoad-big');
					var sL_slideshow_fader = new efSlideShow($('homepage-showlist-fader'),{debug:false});
					//sL_slideshow_fader.enableDebug();
					$('homepage-showlist-fader').setStyle('cursor','pointer').addEvent('click',function(){
						var items = this.getElements('.item');
						found = false;
						for(var i=0,j=items.length;i<j && found==false;i++){
							if(items[i].getStyle('opacity')==1){
								found = true;
								window.location.href='/go-show/'+items[i].getAttribute('showID');
							}
						}
					});
				}
				t.src = response[i].file;
			}else{
				new Image().src = response[i].file;
			}


		}

	};


	var executed = false;
	tx_pishowlist_public.addEvent('change',showList_onChange);
	
	//force execution (IE cache issue)
	(function(){
		if(!executed) showList_onChange();
	}).delay(500);
	
	
});