window.addEvent('domready',function(){


	//init watch movie button
	if($('movieplayer-playbutton')){
		$('movieplayer-playbutton').addEvent('click',function(ev){
			ev.stop();
			//$('movieplayer-player').setStyle('display','block');
			var movie_id = this.getProperty('data-movieid');
			$('movieplayer-player').load(rooturl+'/'+lang+"/movie_show/movie/"+movie_id);

		});
	}


	if($('watch-movie')){
		$('watch-movie').addEvent('click',function(event){
			event.stop();
			movie_id = this.getProperty('data-movieid');
			$('movieplayer-player').load(rooturl+'/'+lang+"/movie_show/movie/"+movie_id);
			var scroll = new Fx.Scroll(window).toElement('movieplayer-player');
		});
	}


	function addFavourite (movieId, sceneId) {

		var notimooManager = new Notimoo({
			locationVType: 'bottom',
			locationHType: 'left'
		});

		if (loggedin) {
			var json 	= new Request.JSON({
				url: '/en/movie/'+movieId+'/'+sceneId+'/favorite/title', 
				onFailure: function() {notimooManager.show({message: 'Favourite could not be added at this time, please try again!'});}, 
				onSuccess: function() {notimooManager.show({message: 'Favorite Added!'});} 
			}); 
			json.get(); 
		} else { 
			/* @todo : login box */
			window.location('login'); 
		}
	}


	if ($('fave-movie')) {
		console.log('in fave-movie')
		$$('.fave-movie').each(function(el) {
			console.log('looping fave-movie');
			el.addEvent('click', function(event) {
				console.log('binding');
				event.stop();
				el = event.target;
				movieId = el.getProperty('data-movieid');
				sceneId = (el.getProperty('data-sceneid')) ? el.getProperty('data-sceneid') : 0
				addFavourite(movieId, sceneId);
			})
		});
	}


	//init movie scenes
	/*if($$('#movie-page .movie-scenes')){
		//console.log('found scenes');
		$$('#movie-page .movie-scenes a.play-scene').each(function(el){
			//console.log('adding scene event');
			el.addEvent('click', function(ev){
				ev.stop();
				$('movieplayer-player').load(this.href); 
				var scroll = new Fx.Scroll(window).toElement('movieplayer-player');
			});
		});*/
		/*$$('#movie-page .movie-scenes a.fave-scene').each(function(el){
			//console.log('adding scene event');
			el.addEvent('click', function(ev){
				ev.stop();
				if(loggedin){
					MOOdalBox.open(el.href,{width:500, height:350});
				}else{
					MOOdalBox.open('/'+lang+'/login',{width:750, height:350});
				}
				//var timecode = el.rel;
				//window.addEvent('modalplayer_flashloaded',function(ev){ jumpto(timecode); });  
			}.bindWithEvent(el));
		});*/
	
		//highlight scene from location hash
	/*	if(window.location.hash && window.location.hash.match(/scene(id|no)\-([0-9]+)/g)){
			var anchor = $$('a[name="'+window.location.hash.replace('#','')+'"]')
			if(anchor){
				var scene = anchor.getNext('div.scene');
				if(scene){
					fx = new Fx.Morph(scene[0]);
					var morphFrom = { background: scene.getStyle('background-color'), border:scene.getStyle('border-color')};
					var morphTo = { background:'#fffca1', border:''}
					fx.start({'background-color':morphTo.background}).chain(function(){
						this.start( {'background-color':morphFrom.background})
					}).chain(function(){
						this.start( {'background-color':morphTo.background});
					}).chain(function(){
						this.start( {'background-color':morphFrom.background})
					});
				}
			}
		
		}
	}*/
	
	
	/* Set up the player scene menu*/
	if($$('#movieplayer .movie-scenes')){
		var scene_els = $$('#movieplayer .movie-scenes .movie-scene');
		
		scene_els.each(function(el){
			
			el.addEvent('mouseenter',function(ev){
				//this.getChildren('.scene-images')[0].setStyle('display','block');
				this.addClass('active');
			});
			el.addEvent('mouseleave',function(ev){
				//this.getChildren('.scene-images')[0].setStyle('display','none');
				this.removeClass('active');
			});
			
			el.addEvent('click',function(ev){
				ev.stop();
				var playbutton = this.getChildren('.scene-watch')[0].getChildren('.play-scene')[0];
				
				//is player already loaded?
				if($('flash-player-swf')){
					var time = playbutton.getProperty('data-scenetime');
					//send event
					jump(time);
				}else{
					//load player (button href points to scene)
					$('movieplayer-player').load(playbutton.href);
				}
				
					
			});
		});
		
	}
	
	
	$$('.viewcover').each(function(el){
		el.addEvent('click',function(ev){
			ev.stop();
			var url = ev.target.getProperty('href');
			SqueezeBox.open(url);
		})
	});
	

});