/**
 * @author Thomas Sømoen
 */

(function($){
	
	$.movie = {
		flash:null,
		noFlash:'',
		init:function(){
			return $(this).each(function(){
				var elm = $(this);
				var rel = $(this).attr('rel');
				var flashplayer = $('#flashplayer').removeClass('hidden');
				var match = rel.match(/#([0-9]+)#([0-9]+)#|#([0-9]+)#/);
				
				if(flashplayer.hasClass('frontpage')){
					flashplayer.hide();
				}
				
				var settings = {
					movieID:-1,
					AdditionalID:-1,
					title:'',
					recipe:'',
					moviePath:'',
					imagePath:'',
					lookUp:false,
					showPlayer:false,
					pauseMovie:false,
					poster:1,
					rating:0,
					flash:null
				}
				
				$.extend(
					settings,
					{
						lookUp:/#([0-9]+)#/.test(rel),
						showPlayer:((rel.search(/(show)/) != -1) ? true : false),
						pauseMovie:((rel.search(/(paused)/) != -1) ? 1 : 0)
					}
				);
				
				if (settings.lookUp) {
		            var match = this.rel.match(/#([0-9]+)#([0-9]+)#|#([0-9]+)#/);
		
		            if (match) {
		                if (match[1]) {
		                    settings.movieID = parseInt(match[1]);
		                    settings.AdditionalID = match[2];
		                } else {
		                    settings.movieID = parseInt(match[3]);
		                }
		            }
		        }
				
				elm.attr('href','#playmovie');
				
				/* henter ut data som må sendes til server for oppslag */
		        if (this.lookUp) {
		            var match = rel.match(/#([0-9]+)#([0-9]+)#|#([0-9]+)#/);
		
		            if (match) {
		                if (match[1]) {
		                    movieID = parseInt(match[1]);
		                    AdditionalID = match[2];
		                } else {
		                    movieID = parseInt(match[3]);
		                }
		            }
		        }
		
		        if (this.movieID > 0) {
		            this.href = '#playmovie' + this.movieID;
		            if (this.AdditionalID > 0) {
		                this.href += '_' + this.AdditionalID;
		            }
		        }
				
				var insertFlash = function(){
					var obj;
					var playerSrc = $.settings.flash.mainPlayer+'?' + Math.random();
					var width = '547';
					var height = '308';
					var sendImage = false;
					var stopFlash = 1;
					
					sendImage = true;
					
					switch (true) {
						case $('#flashplayer').hasClass('fullimage'):
							width = '547';
							height = '333';
							sendImage = false;
						break;
						case $('#flashplayer').hasClass('tv'):
							width = '547';
							height = '333';
							stopFlash = 0;
						break;
						case $('#flashplayer').hasClass('frontpage'):
							width = '704';
							height = '397';
							sendImage = false;
						break;
					}
					
					var flashvars = {};
						flashvars.id = settings.movieID;
						flashvars.src = settings.moviePath;
						flashvars.paused = settings.pauseMovie;
						flashvars.poster = settings.poster;
					if (settings.imagePath.length > 0) {
						flashvars.image = settings.imagePath;
					}
						flashvars.link = settings.recipe;
						flashvars.linklabel = settings.linklabel;
						flashvars.rating = settings.rating;
						flashvars.fullscreen = settings.fullscreen;
						flashvars.fullscreenlabel = settings.fullscreenlabel;
						flashvars.close = stopFlash;
					
					var params = { allowfullscreen: 'true', allowScriptAccess: 'always', wmode:'window'};
					var attributes = {};
					$.movie.noFlash = flashplayer.html();
					swfobject.embedSWF(playerSrc, "flashmovie", '100%', '100%', "9.0.28", "js/swfobject/expressInstall.swf", flashvars, params, attributes);
					flashplayer.show();
					$.movie.flash = $('#flashmovie');
				}
				
				var play = function(settings){
					if(flashplayer.hasClass('frontpage')){
						$(flashplayer).parents('div.carousel:first').find('a').fadeOut();
						var desc = $(flashplayer).parents('div.carousel:first').find('div');
						desc.animate({
							bottom:(-1*desc.height())
						},'fast');
						
						$('ul#links').animate({top:23},'fast');
						insertFlash();
					}else{
						insertFlash();
					}
					
					
				}
				
				var getMovie = function(){
	        		window.location.hash = '#content'
					
					$.post(
						$.settings.actions.getMovie,
						{ id:settings.movieID, obj_id: settings.AdditionalID },
						function(response){
							$.extend(
								settings,
								response
							);
							play(settings);
						},
						"json"
					);
				}
				
				
				elm.click(function(e){
					e.preventDefault();
					getMovie();
				});
				
				
				if(settings.showPlayer){
					getMovie();
				}
			});
		},
		
		finished:function(){
			$('.carousel').data('hoverOn',true);
			$.movie.flash.parent().html($.movie.noFlash);
			if($('#flashplayer').hasClass('frontpage')){
				$('#flashplayer').hide();
			}
			if($.carousel != undefined){
				$.carousel.showNext(true);
			}
		},
		
		regRating: function(id, rating){
			$.getJSON(
				$.settings.actions.rateMovie,
				{id:id,rating:rating},
				function(response){
					$.movie.flash.get(0).showRatingResult(parseInt(response.result));
				}
			);
		}
		
	}
	
	$.fn.movie = $.movie.init;
	$(document).ready(function(){
		$('a[rel^="movie #"]').movie();
	});
})(jQuery);
