var packshotSlider = {
	LIST_ITEM_WIDTH : 117,
	VISIBLE_LIST_WIDTH : 587,
	ANIMATION_TIME : 2500,
	
	width : null,
	current_position : 0,
	prev_position : 0,

	init: function() {
		if ($("#slider").size() > 0) {
			packshotSlider.width = ($('ul#packshot_list li').size() * packshotSlider.LIST_ITEM_WIDTH);
			$('ul#packshot_list').css({width : packshotSlider.width + 'px'});
			
			$("#slider").slider({
				animate: true,
				slide: function(event, ui) {
					$('ul#packshot_list').css({left : '-' + packshotSlider.getPosition(ui.value)  +'px'});
					// console.debug('slide');
				},
				change: function(event, ui) {
					$('ul#packshot_list').animate({left : '-' + packshotSlider.getPosition(ui.value)  +'px'}, packshotSlider.getDuration(ui.value), "swing");
					// console.debug('change');
				},
				stop: function(event, ui) {
					// console.debug('stop');
					packshotSlider.prev_position = packshotSlider.current_position;
					packshotSlider.current_position = ui.value;
				}
			});
	 	}
	},
	getPosition: function(pos) {
		var new_pos = parseInt(pos * (this.width - this.VISIBLE_LIST_WIDTH) * 0.01);
		// console.debug(new_post);
		return new_pos;
	},
	getDuration: function(pos) {
		return parseInt(Math.abs(this.prev_position - pos) * packshotSlider.ANIMATION_TIME * 0.01)
	}
}


function initYearSelect() {
	if ($("select#year_select").size()) {
		$("select#year_select").change(function() {
			window.location = '/news/?year=' + $(this).val();
		});
	}
}
function initMovieSelect() {
	if ($("select#movie_select").size()) {
		$("select#movie_select").change(function() {
			window.location = '/film/' + $(this).val();
		});
	}
}
function initLineupSelect() {
	if ($("select#lineup_select").size()) {
		$("select#lineup_select").change(function() {
			window.location = '/lineup/' + $(this).val();
		});
	}
}

function initResultToggle() {
	if ($("ul.search_result_list").size()) {
		var show_label = 'List all';
		var hide_label = 'Hide';
		$("ul.search_result_list").each(function(i) {
			if ($(this).find('li').size() > 5) {
				$(this).find('li:gt(4)').hide();
				$(this).after('<a href="#" class="list_all result_list_toggle" title="' + show_label +'">' + show_label + '</a>');
			}
			
		});
		$('a.result_list_toggle').click(function() {
			if ($(this).attr('title') == show_label) {
				$(this).parent().find('li:hidden').slideDown();
				$(this).text(hide_label);
				$(this).attr('title', hide_label);
			} else {
				$(this).parent().find('li:gt(4)').slideUp();
				$(this).text(show_label);
				$(this).attr('title', show_label);
			}
			$(this).toggleClass('list_all');
			$(this).toggleClass('hide');
			return false;
		});
	}
}

function initVideoplayer() {
	$("#videoplayer_holder").each(function (i){
		var width = 814;
		var height = 314;
		if ($(this).attr('class') == 'frontpage') {
			var xml_url = '/xml/trailer_player.xml';
			var app_mode = 'frontpage';
		} else if ($(this).attr('class') == 'frontpage_v2') {
			var xml_url = '/xml/trailer_player.xml';
			var app_mode = 'frontpage';
			var height = 314;		
		} else if ($(this).attr('class') == 'movie_v2') {
			var xml_url = '/xml/movie/' + $(this).attr('title') + '.xml';
			var app_mode = 'moviepage';
			var width = 792;			
			var height = 314;
		} else if ($(this).attr('class') == 'new_releases') {
			var xml_url = '/xml/new_releases.xml';
			var app_mode = 'subpage';
			var width = 792;
			var height = 314;
		} else if ($(this).attr('class') == 'series') {
			var xml_url = '/xml/series/' + $(this).attr('title') + '.xml';
			var app_mode = 'subpage';
			var width = 792;
			var height = 314;
		} else {
			var xml_url = '/xml/movie/' + $(this).attr('title') + '.xml';
			var app_mode = 'moviepage';
			var width = 792;
			var height = 314;
		}
		
		var flashvars = {
			color : 'FFFFFF',
			xmlUrl : xml_url,
			appMode : app_mode // "frontpage" eller "subpage"
		};
		var params = {
			bgcolor : '#000000',
			wmode : "transparent",
			allowFullScreen : "true"
		};
		var attributes = {
			'id' : "swf_videoplayer"
		};
		
		//if ($(this).attr('class') == 'frontpage_v2' || $(this).attr('class') == 'movie_v2') {
		if (true) {
			swfobject.embedSWF("/swf/videoplayer.swf?" + Math.random(), 'videoplayer_holder', width, height, "9.0.0", false, flashvars, params, attributes);	
		} else {
			swfobject.embedSWF("/swf/videoplayer.swf?" + Math.random(), 'videoplayer_holder', width, height, "9.0.0", false, flashvars, params, attributes);	
		}
		
	});
}


function initPackshotView() {
	var href = $('#view_packshot a').attr('href');
	$('#view_packshot').append('<a href="'+ href +'" class="thickbox view_packshot"></a>');
	tb_init('a.view_packshot');
}

News = {
	init : function() {
		$('ul#news_list li a').click(function() {
			var href = $(this).attr('href');			
			if (!href.match(/http(s)?:\/\/.*/)) {
				$('ul#news_list li').removeClass('current');
				$('li#news_post_' + $(this).attr('rel')).addClass('current');
				$('.main').load(href);		
				return false;
			}
		});			
		
		$('#news_archive .pagination a').click(function() {
			$('#news_archive_holder').load($(this).attr('href'), function() {
				News.init();
			});		
			return false;
		});	
	}
}

Campaign = {
	TRAILER_ACTIVE : true,
	init : function() {
		Campaign.initVideo();
		
		$('.campaign_content:gt(0)').hide();
		$('ul#campiagn_menu li a:eq(0)').addClass('active');
		$('ul#campiagn_menu li a:last').addClass('last');
		$('ul#campiagn_menu li a').click(function() {
			var id = $(this).attr('href').split('#')[1];
			$('.campaign_content').hide();
			$('#content_' + id).show();
			$('ul#campiagn_menu li a').removeClass('active');
			$(this).addClass('active');
			if (!Campaign.TRAILER_ACTIVE) {
				Campaign.initVideo();
				Campaign.TRAILER_ACTIVE = true;
			}
			return false;
		});
		
		$('ul.stills_list li a').click(function() {
			var img = $(this).attr('href');
			$('#campaign_trailer').empty();
			$('#campaign_trailer').append('<img src="' + img +  '" />');
			Campaign.TRAILER_ACTIVE = false;
			return false;
		});
	},
	initVideo : function() {
		if($('#campaign_trailer').size() > 0) {
			
			$('#campaign_trailer').empty();
			$('#campaign_trailer').append('<div id="videoplayer_holder"></div>');
			
			var width = 514;
			var height = 290;
			if ($('#campaign_trailer').attr('class') == 'series') {
				var xml_url = '/xml/series/' + $('#campaign_trailer').attr('title') + '.xml';
			} else {
				var xml_url = '/xml/movie/' + $('#campaign_trailer').attr('title') + '.xml';
			}
			var app_mode = 'campaign';
			
			var flashvars = {
				color : 'FFFFFF',
				xmlUrl : xml_url,
				appMode : app_mode // "frontpage" eller "subpage"
			};
			var params = {
				bgcolor : '#000000',
				wmode : "transparent",
				allowFullScreen : "true"
			};
			var attributes = {
				'id' : "swf_videoplayer"
			};
			swfobject.embedSWF("/swf/videoplayer.swf?" + Math.random(), 'videoplayer_holder', width, height, "9.0.0", false, flashvars, params, attributes);
		}
	}
}


function init() {	
	News.init();	
	Campaign.init();	
	packshotSlider.init();	
	initYearSelect();
	initMovieSelect();
	initLineupSelect();
	initVideoplayer();
	initResultToggle();
	initPackshotView();
}


$(document).ready(init);


