/** Coded by Andrew Chalkley for the Film4 Mash-up contest 10-01-2008 **/
	/**Initalising**/
	$(function(){
		$(".footer p").hide();
		$("#menu li").click(function(){
			$('html,body').animate({scrollTop: $(this).offset().top}, 1000);
			var this_id = $(this).text().toLowerCase().replace(/ /g,'_').replace(/[^\w-]/g,'');
			if(this_id.substring(this_id.length-1,this_id.length)=="_") this_id=this_id.substr(0,this_id.length-1);
			$(this).addClass("selected").siblings().removeClass("selected");
			$("#"+this_id+":hidden").slideDown().siblings(".content_section:visible").slideUp();
		});
		$(".footer h2").click(function(){
			$(".footer p").slideToggle();
		});
		/**Loading TV Listings**/
		$.get("xml_proxy.php",
		{url: "http://www.channel4.com/apps26/syndication/film/todays_listings/rss.xml"},
		function(data){
				$("item",data).each(function()
				{
					$("#programs").append("<div class=\"program\">"+$("title",this).text().replace(/([0-9]{2}\:[0-9]{2})[\s](.*)/i,"<h3 class=\"title\"><span class=\"time\">$1</span> - $2</h3>")+"<div class=\"description\">"+$("description",this).text()+" <a href=\""+$("link",this).text()+"\">Film 4 Review</a></div></div>\n");
				});			 
				enableVisibility($("#programs"));
		});
		/**Loading Cinema Listings**/	
		$.get("xml_proxy.php",
		{url: "http://www.channel4.com/apps26/syndication/film/cinema_newreleases/rss.xml"}, 		
		function(data){
			$("item",data).each(function(){
				if($("title",this).text()!="Clips and trailers"){
					var movie_id = $("title",this).text().replace(/ /g,'_').replace(/[^\w-]/g,'');
					var movie = "<div class=\"movie_container\" id=\""+movie_id+"\">\n";
					movie = movie +  "<h3>"+$("title",this).text()+"</h3>\n";
					movie = movie + "<div class=\"description\">\n";						
					movie = movie + "<p class=\"film4_description\">"+$("description",this).text()+" <a href=\""+$("link",this).text()+"\">Film 4 Review</a></p>\n";
					movie = movie + "</div>\n";
					movie = movie + "</div>\n";
					$("#new_releases").append(movie);
				}
			});
			enableVisibility($("#new_releases"));
			$("#new_releases").slideDown();
		});
		/** Load SD Apple Trailers **/
		$.get("xml_proxy.php",
		{url: "http://www.apple.com/trailers/home/xml/current.xml"}, 		
		function(data){
			$("movieinfo",data).each(function(){
					var movie_id = $("info title",this).text().replace(/ /g,'_').replace(/[^\w-]/g,'');
					var movie = "<div class=\"movie_container\" id=\""+movie_id+"\">\n";
					movie = movie + "<h3>"+$("info title",this).text()+"</h3>\n";	
					movie = movie + "<div class=\"description\">";	
					movie = movie + "<div class=\"trailer_header\">\n";
					movie = movie + "<a href=\""+$("poster xlarge",this).text()+"\" title=\""+$("info title",this).text()+" Poster\" class=\"poster\"><img src=\""+$("poster location",this).text()+"\" alt=\""+$("info title",this).text()+" Poster\" /></a>\n";
					movie = movie + "<ul class=\"quicktime_trailers\">";
					movie = movie + "<li><a href=\"qtl.php?t="+$("preview large",this).text()+"&s=normal\">Large Trailer (Normal)</a></li>";
					movie = movie + "<li><a href=\"qtl.php?t="+$("preview large",this).text()+"&s=full\">Large Trailer (Full Screen)</a></li>";
					movie = movie + "</ul>\n";
					movie = movie + "<p class=\"runtime\">Runtime: "+$("info runtime",this).text()+"</p>\n";
					movie = movie + "<p class=\"director\">Director: "+$("info director",this).text()+"</p>\n";
					movie = movie + "<div class=\"cast\">\n";
					if($("genre name",this).length!=0)
					{	
					movie = movie + "<h4>Genre(s)</h4>\n";
					movie = movie + "<ul>\n";
					$("genre name",this).each(function(){
						movie = movie + "<li>"+$(this).text()+"</li>\n";
					})
					movie = movie + "</ul>\n";

					}

					if($("cast name",this).length!=0)
					{
					movie = movie + "<h4>Cast</h4>\n";
					movie = movie + "<ul>\n";
					$("cast name",this).each(function(){
						movie = movie + "<li>"+$(this).text()+"</li>\n";
					})
					movie = movie + "</ul>\n";
					}
					if($("soundtrack itms",this).length!=0)
					{	
					movie = movie + "<h4>Soundtrack</h4>\n";
					movie = movie + "<p><a href=\""+$("soundtrack itms",this).text()+"\" title=\"Buy "+$("soundtrack artist",this).text()+" Music on iTunes\"><img src=\""+$("soundtrack image",this).text()+"\" alt=\""+$("soundtrack artist",this).text()+" Album Cover\" /></a></p>";
					}
					movie = movie + "</div>\n";
					movie = movie + "<p class=\"apple_description\">"+$("info description",this).text()+"</p>\n";
					
					movie = movie + "</div>\n";
					movie = movie + "</div>\n";				
					movie = movie + "</div>\n";
					$("#trailers").append(movie);
			});
			enableVisibility($("#trailers"));
			enableTrailerFunctionality();			 
		});
	});
		
function enableVisibility(jqObj)
{
	$("h3",jqObj).mouseover(function(){
		$(this).addClass("selected");
	}).mouseout(function(){
		$(this).removeClass("selected");
	});
	$("h3",jqObj).click(function(){
		
		if($(this).parent().hasClass("movie_container"))
		{
		var movie_container = $(this).parent(".movie_container");
		var this_id = movie_container.attr("id").replace(/[\_]/g,"+");
		
		/** Check if blog reaction has been already checked **/
		if($(".blog_reaction",movie_container).length==0)
		{
			$(".description",movie_container).append("<div class='blog_reaction'><h4>Reaction in the Blogosphere</h4><div class='blog_posts'></div></div>");
				$.get("xml_proxy.php",{url: 'http://feeds.technorati.com/search/%22'+this_id+'%22?authority=a7&language=en'}, 		
				function(data){
					if($("item",data).length==0) {
						$(".blog_posts",movie_container).height("20px").append("<p>No reaction!</p>");	
					}
					$("item",data).each(function(){
						$(".blog_posts",movie_container).append("<div class='blog_post'><p><a href='"+$("link",this).text()+"' class='title'>"+((!$("title",this).text())? "No Blog Post Title" : $("title",this).text() )+"</a> <a href='"+$("comments",this).text()+"' class='comments'>Comments</a></p><p class='url'>"+$("link",this).text()+"</p></div>");
					});
				});
			
		}
		}
		/** Load HD trailers **/
		if(jqObj.attr("id")=="trailers")
		{
			/** Checks how many trailers there are present **/
			var trailerjqObj = $(this).parent();
			if($(".quicktime_trailers li", trailerjqObj).length==2)
			{
				
				var hd_movies =  $(".quicktime_trailers", trailerjqObj);
				$.get("xml_proxy.php",{url: "http://www.apple.com/trailers/home/xml/current_480p.xml"}, 					function(data){
					$("movieinfo",data).each(function(){
							if(trailerjqObj.attr("id")== $("info title",this).text().replace(/ /g,'_').replace(/[^\w-]/g,''))
							{
								hd_movies.append( "<li><a href=\"qtl.php?t="+$("preview large",this).text()+"&s=normal\">HD 480p Trailer (Normal)</a></li><li><a href=\"qtl.php?t="+$("preview large",this).text()+"&s=full\">HD 480p Trailer (Full Screen)</a></li>");
							}
				});
				
			});
			
			$.get("xml_proxy.php",
			{url: "http://www.apple.com/trailers/home/xml/current_720p.xml"},
			function(data){
				$("movieinfo",data).each(function(){
						if(trailerjqObj.attr("id")== $("info title",this).text().replace(/ /g,'_').replace(/[^\w-]/g,''))
						{
							hd_movies.append("<li><a href=\"qtl.php?t="+$("preview large",this).text()+"&s=normal\">HD 720p Trailer (Normal)</a></li><li><a href=\"qtl.php?t="+$("preview large",this).text()+"&s=full\">HD 720p Trailer (Full Screen)</a></li>");
						}
			});
		});
		 
		 
			}
		}
		$(".description:visible",jqObj).slideUp(500);
		$(this).next(".description:hidden").slideDown(500,function(){
			var targetOffset = $(this).parent().offset().top;
			if(jqObj.attr("id")=="programs")
			{
				$('#programs').animate({scrollTop: targetOffset-28}, 500);
				$('html,body').animate({scrollTop: jqObj.offset().top}, 500);
			} else $('html,body').animate({scrollTop: targetOffset}, 500);
			
		});
	});
}
function enableTrailerFunctionality()
{
	tb_init("a.poster");
}