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{
			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){
		//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);
		
		
		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);
		$('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',
				'styles':{
					'opacity':(i==0?1:0),
					'visibility':(i==0?'visible':'hidden')
				}
			}).adopt(
				new Element('img',{
					'src':response[i].file
				}),
				new Element('div',{
					'class':'caption',
					'html':response[i].captionHTML
				})
			).inject('homepage-showlist-fader-items');
			
			if(i==0){
				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'));
					$('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/'+response[i].show_id;
							}
						}
					});
				}
				t.src = response[i].file;
			}else{
				new Image().src = response[i].file;
			}
			
			
		}
		
	};
	
	var showList_select_onChange = function(){
		tx_pishowlist_public.addEvent('change',showList_onChange);
	}
	
	tx_pishowlist_public.addEvent('change',showList_onChange);
	
	//CR: execute on every country change
	//so reattach the event to the select.onchange
	tx_pishowlist_public.scountry.addEvent('change',showList_select_onChange);
});