var SlideShow = {
	
		//var pic 	= $('movie_pic');
		//var picfx = new Fx.Morph(pic, {duration: 500, transition: Fx.Transitions.linear});		
	
		init: function (options) {

		if ($('poster_wrapper')==null) return;

			// init default options
			
			this.options = Object.extend({
				scrollDuration: 200,
				selectDuration: 50
			}, options || {});

			this.leftEnabled=true;
			this.rightEnabled=true;
			this.padding = 2;
			this.pos = 0;
			this.offsetHorizontal = 0;
			this.offsetHorizontal = $('poster_wrapper').clientWidth;		// HOW MUCH TO MOVE
			this.offsetVertical = 0;
			this.offsetVertical = $('poster_wrapper').clientHeight;		// HOW MUCH TO MOVE
			
			//this.offset = 910;
			this.items = $('posters');
			this.slidesFx = new Fx.Morph(this.items, {duration: this.options.scrollDuration, transition: Fx.Transitions.linear});					
			this.scrollFx = new Fx.Scroll('poster_wrapper', {offset:{'x':0, 'y':0}, transition: Fx.Transitions.linear});	
		
			$$('.poster').each(function(item){
				menu = item.getElement('.poster_menu');
				menu.setStyle('display', 'none');
						
				item.addEvent('click', function(event) {				
					var id=item.id.split("_");
					this.show_movie_info(id[1],id[2]);
					return false;
				}.bind(this));
				
				item.addEvent('mouseover', function(event) {
					event = new Event(event).stop();
					var fx2 = new Fx.Morph(item, {duration: this.options.selectDuration, transition: Fx.Transitions.linear});
					fx2.start({
						'opacity': 0.8
					});
					menu = item.getElement('.poster_menu');
					menu.setStyle('display', '');
				}.bind(this));
				
				item.addEvent('mouseleave', function(event) {
					event = new Event(event).stop();
					var fx2 = new Fx.Morph(item, {duration: this.options.selectDuration, transition: Fx.Transitions.linear});
					fx2.start({
						'opacity': 1
					});
					menu = item.getElement('.poster_menu');
					menu.setStyle('display', 'none');				
				}.bind(this));
			}.bind(this));
			
			
			if ($('moveleft'))
			$('moveleft').addEvent('click', function(event) {			
				event = new Event(event).stop();
				this.scrollSlidesHorizontal(-1);
				return false;
			}.bind(this));		
			
			if ($('moveright'))		
			$('moveright').addEvent('click', function(event) { 			
				event = new Event(event).stop();
				this.scrollSlidesHorizontal(1);
				return false;
			}.bind(this));

			if ($('movedown'))
			$('movedown').addEvent('click', function(event) {			
				event = new Event(event).stop();
				this.scrollSlidesVertical(-1);
				return false;
			}.bind(this));		
					
			if ($('moveup'))
			$('moveup').addEvent('click', function(event) { 			
				event = new Event(event).stop();
				this.scrollSlidesVertical(1);
				return false;
			}.bind(this));
			
			$$('.scroll_up').each(function(item){
				item.set('opacity','1');
				item.addEvent('click', function(event) {			
					event = new Event(event).stop();
					this.scrollSlidesVertical(1);
					return false;
				}.bind(this));
			}.bind(this));		

			$$('.scroll_down').each(function(item){
				item.set('opacity','0.3');			
				item.addEvent('click', function(event) {
					event = new Event(event).stop();
					this.scrollSlidesVertical(-1);
					return false;
				}.bind(this));
			}.bind(this));		
			
			if ($('poster_wrapper'))
			$('poster_wrapper').addEvent('mousewheel', function(event){
				event = new Event(event).stop();
				if (event.wheel < 0)
					this.scrollSlidesVerticalByWheel(-1,10);
				else
					this.scrollSlidesVerticalByWheel(1,10);
				return false;
			}.bind(this));
	
			
			this.scrollFx.toLeft();
			this.scrollFx.toTop();
			this.slidesFx.start({'opacity': 1});			
		},
												
		unload: function () {

			this.slidesFx.start({'opacity': .3});
			$$('.poster').each(function(item){									
				item.removeEvent('click');				
				item.removeEvent('mouseover');				
				item.removeEvent('mouseleave');
			});												
			
			$('moveleft').removeEvent('click');							
			$('moveright').removeEvent('click');						
		},				
								
		scrollSlidesHorizontal: function (direction) {
			var maxpos = $('poster_wrapper').scrollWidth - $('poster_wrapper').clientWidth;
			maxpos = Math.floor(maxpos / $('poster_wrapper').clientWidth);
			maxpos = (maxpos) * $('poster_wrapper').clientWidth;
			
			if (((this.pos) >= (maxpos) && direction>=0) || (this.pos<=0 && direction<=0)) return;
			this.pos += direction * this.offsetHorizontal;			
						
			if (this.pos >= (maxpos)) {
				var img = $('moveright').getElement('img');
				this.pos = maxpos;
			}
			if (this.pos <= 0) {
				var img = $('moveleft').getElement('img');
				this.pos = 0;
			}
			this.slidesFx.start({ 
				'opacity': .3 
			}).chain(function(){
				this.slidesFx.start.delay(400, this.slidesFx, { 'opacity': 1 });
				this.scrollFx.start(this.pos,0);								
			}.bind(this)).chain(function(){					
					//scrollSlidesHorizontal(direction);
				}.bind(this));
		},

		scrollSlidesVertical: function (direction) {
			var maxpos = $('poster_wrapper').scrollHeight - $('poster_wrapper').clientHeight;
			maxpos = Math.floor(maxpos / ($('poster_wrapper').clientHeight));
			maxpos = (maxpos) * $('poster_wrapper').clientHeight;
			
			if (((this.pos) >= (maxpos) && direction>=0) || (this.pos<=0 && direction<=0)) return;
			this.pos += direction * this.offsetVertical;			
						
			if (this.pos >= (maxpos)) {
				$$('.scroll_up').each(function(item){
					item.set('opacity','0.3');
				});
				this.pos = maxpos;
			}
			else{
				$$('.scroll_up').each(function(item){
					item.set('opacity','1');
				});
			}
				
			if (this.pos <= 0) {
				$$('.scroll_down').each(function(item){
					item.set('opacity','0.3');
				});	
				this.pos = 0;
			}
			else
			{
				$$('.scroll_down').each(function(item){
					item.set('opacity','1');
				});	
			}
			this.slidesFx.start({ 
				'opacity': .3 
			}).chain(function(){
				this.slidesFx.start.delay(400, this.slidesFx, { 'opacity': 1 });
				this.scrollFx.start(0,this.pos);								
			}.bind(this)).chain(function(){					
					//scrollSlides(direction);
				}.bind(this));
		},
		
		scrollSlidesVerticalByWheel: function (direction, wheel_offset) {
			var maxpos = $('poster_wrapper').scrollHeight - $('poster_wrapper').clientHeight;
			maxpos = Math.floor(maxpos / ($('poster_wrapper').clientHeight));
			maxpos = (maxpos) * $('poster_wrapper').clientHeight;
			
			if (((this.pos) >= (maxpos) && direction>=0) || (this.pos<=0 && direction<=0)) return;
			this.pos += direction * wheel_offset;			
						
			if (this.pos >= (maxpos)) {
				var img = $('movedown').getElement('img');
				this.pos = maxpos;
			}
			if (this.pos <= 0) {
				var img = $('moveup').getElement('img');
				this.pos = 0;
			}
			$('poster_wrapper').scrollTo(0,this.pos);
			//this.scrollFx.start(0,this.pos);								
		},
		
		show_movie_info: function (movie_id, city_id){
			var request = new Request.HTML({				
					url :        	"movie/poster.php",
					method: 			"post", 
					data: 				"key="+_key+"&movie_id="+movie_id+"&city_id="+city_id,
          evalScripts : false, 
          evalResponse : false, 
          onFailure: function() {alert('Er is iets misgegaan bij de aanroep');},
					onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
						MovieMenu.unload();
          	$('detail_wrapper').set('html', responseHTML);          	
						MovieMenu.init();
						$exec(responseJavaScript);
					}
			}).send();
		}
}

// startup
window.addEvent('domready', SlideShow.init.bind(SlideShow));