var FilmReviewFriday = function(){


	FRF = {
		nextPage : 2,
		totalPages : 0
	};


	var init = function(){
	
		// Set the total number of pages so we can hide the More Reviews link later.
		FRF.totalPages = $('#total-pages').attr('value')*1;
		
		// Highlight text in search box on click - easier to rewrite or edit search criteria.
		$('#search-box').click(function(){ this.select(); });
	
		setMoreReviews();
		initTrailers();
		trailerEvents('');
		
	};
	
	
	var setMoreReviews = function(){
	
		var elMore = $('#more-reviews');

		if(elMore.length!==0){
			elMore.attr('href', '#'+elMore.attr('href')); // Add hash to beginning of URL for JS page loading.
		
			elMore.click(function(event){
				appendReviews(elMore.attr('href'));
			});
		}
	
	};
	
	
	var initTrailers = function(){
	
		// Add overlay and trailer container to DOM.
		$('body').append('<div id="overlay" style="display:none;"></div><div id="trailer-container" style="display:none;"></div>');
		$('#overlay').css('opacity',0.9);
		// Add events for closing overlay and trailer.
		$('#overlay').click(function(){
			$('#trailer-container').hide().empty();
			$('#overlay').hide();
		});
		$(document).keypress(function(event){
			if(event.keyCode == 27){ // Escape key
		        $('#trailer-container').hide().empty();
				$('#overlay').hide();
		    }
		});
	
	};
	
	
	var trailerEvents = function(links){

		if(links===''){ links = 'a.trailer'; }

		$(links).click(function(event){
	
			event.preventDefault();
			
			href = $(this).attr('href');
			tWidth = $.getQueryString({ ID:"width",URL:href });
			tHeight = $.getQueryString({ ID:"height",URL:href });
			href = href.substring(0,href.indexOf('?')); // Remove querystring otherwise trailer doesn't work in IE.
			scrollTop = 0;
			
			if($('#trailer-container').css('position')=='absolute'){
				scrollTop = $(document).scrollTop();
			}
			
			// The height may have changed (from loading more reviews) so set it to document height.
			$('#overlay').show().css('height',$(document).height());
	
			// Write embed code into Trailer Container and position in centre of screen.
			$('#trailer-container').html('<object width="'+tWidth+'" height="'+tHeight+'"><param name="movie" value="'+href+'"></param><param name="allowscriptaccess" value="always"><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><embed src="'+href+'" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" width="'+tWidth+'" height="'+tHeight+'" allowFullScreen="true"></embed></object>')
			.css({
				top:$(window).height()/2-(tHeight/2)+scrollTop,
				left:$(window).width()/2-(tWidth/2)
			}).show();
	
		});
	
	};
	
	
	var appendReviews = function(href){

		FRF.nextPage++;
	
		var loadHREF = href.substr(1),
			lenHREF = href.length,
			newHREF = href.substr(0, lenHREF-1) + FRF.nextPage;
		
		$('.new-content:last').load(loadHREF + ' .reviews-container', {nocache:'true'}, function(){
			$('#more-reviews').attr('href', newHREF);
			trailerEvents('#reviews div.new-content:last a.trailer'); // Add events to films just loaded.
			$('<div class="new-content"><div>').insertAfter('.new-content'); // Create another .new-content element to avoid nesting.		
	
			// If we've reached the last page, remove the More Reviews link.
			if(FRF.nextPage>FRF.totalPages){ $('article p.more-reviews').remove(); }
		});
	
	};
	
	
	return {
		init:init
	};


}();




$(document).ready(function(){
	var frf = FilmReviewFriday.init();
});