	// Variables
	var cols = 3;
	var current_year = null;
	
	function loadYear (year)
	{
		var layer_year = document.getElementById ("year" + year);
		var layer_rankings_year = document.getElementById ("rankings" + year);
		var i;
		
		for (i = 0; i < years.length; ++i)
			unloadYear (years[i]);
		unloadYear ("all");
		layer_year.className = "year special_color";
		current_year = year;
		loadMovies (year);
		
		if (!layer_rankings_year)
			return;
		layer_rankings_year.style.display = "block";
	}
	
	function unloadYear (year)
	{
		var layer_year = document.getElementById ("year" + year);
		var layer_rankings_year = document.getElementById ("rankings" + year);
		
		if (layer_year)
			layer_year.className = "year";
		if (layer_rankings_year)
			layer_rankings_year.style.display = "none";
	}
	
	function Movie (id, title, release, seen, rating, director, year, year_out, genre)
	{
		this.id = id;
		this.title = title;
		this.release = release;
		this.seen = seen;
		this.rating = rating;
		this.director = director;
		this.year = year;
		this.year_out = year_out;
		this.genre = genre;
		this.filter = function (word)
		{
			if (this.title.toLowerCase ().indexOf (word.toLowerCase ()) >= 0)
				return true;
			if (this.rating.toLowerCase ().indexOf (word.toLowerCase ()) >= 0)	
				return true;
			if (this.director.toLowerCase ().indexOf (word.toLowerCase ()) >= 0)	
				return true;
			return false;
		}
		this.toHTML_picture = function (current)
		{
			var res = "";
			
			if (current % cols == 0)
			{
				if (current > 0)
					res += "</tr>";
				res += "<tr>";
			}
			res += "<td width=\"" + Math.round (100 / cols) + "%\" valign=\"top\">";
			res += "	<table cellpadding=\"4\" cellspacing=\"0\" width=\"100%\" class=\"movie\" onmouseover=\"this.className='movie over';\" onmouseout=\"this.className='movie';\">";
			res += "		<tr>";
			res += "			<td valign=\"top\" width=\"130\">";
			res += "				<img src=\"images/posters/" + this.id + ".jpg\" border=\"0\">";
			res += "			</td>";
			res += "			<td valign=\"top\">";
			res += "				<span class=\"title dark_grey\">" + this.title + "</span><br>";
			res += "				<span class=\"light_grey\">" + this.director + " (" + this.year + ")</span><br>";
			res += "				<div id=\"rating\" class=\"right\"><div id=\"rating_bg\"><div id=\"rating_bar_container\"><div id=\"rating_bar\" class=\"special_color_bg\" style=\"width: " + (this.rating * 10) + "px; -moz-opacity: " + (this.rating / 10) + "; filter:alpha(opacity=" + (this.rating * 10) + ");\"></div></div></div></div>";			
			res += "				<span class=\"grade dark_grey\"><b>" + this.rating + "</b><small class=\"light_grey\">/10</small></span><br>";
			res += "				<br>";
			res += "				<span class=\"light_grey\">Genre : </span><span class=\"dark_grey\">" + this.genre + "</span><br>";
			res += "				<span class=\"light_grey\">Sorti le </span><span class=\"dark_grey\">" + this.release + "</span><br>";
			res += "				<span class=\"light_grey\">Vu le </span><span class=\"dark_grey\">" + this.seen + "</span><br>";
			res += "				<a href=\"http://www.imdb.com/find?s=tt&q=" + this.title.replace (/ /g, "+") + "\" target=\"_blank\"><img src=\"images/icon_imdb.gif\" border=\"0\"></a>";
			res += "				<a href=\"http://www.allocine.fr/recherche/?motcle=" + this.title.replace (/ /g, "+") + "\" target=\"_blank\"><img src=\"images/icon_allocine.gif\" border=\"0\"></a>";
			res += "			</td>";
			res += "		</tr>";
			res += "	</table>";
			res += "</td>";
			return res;
		}
	}
	
	function searchMovies (words, info)
	{
		var layer_content = document.getElementById ("content");
		var res = "";
		var k = 0;
		var i;
		
		if (info == true)
		{
			unloadYear (current_year);
			current_year = 0;
		}
		for (i = 0; i < movies.length; ++i)
		{
			if (movies[i].filter (words))
			{
				if (current_year > 0)
				{
					if (movies[i].year_out == current_year)
						res += movies[i].toHTML_picture (k++);
				}
				else
					res += movies[i].toHTML_picture (k++);
			}
		}
		while (k % cols)
		{
			res += "<td width='" + Math.round (100 / cols) + "%'></td>";
			++k;
		}
		layer_content.innerHTML = "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">" + res + "</table>";
	}
	
	function loadAllMovies ()
	{
		var layer_content = document.getElementById ("content");
		var layer_content_stats = document.getElementById ("content_stats");
		var layer_rankings_year = document.getElementById ("rankingsall");
		var res = "";
		var i;
		
		unloadYear (current_year);
		current_year = 0;
		for (i = 0; i < movies.length; ++i)
			res += movies[i].toHTML_picture (i);
		if (i % cols > 0)
		{	
			while (i % cols)
			{
				res += "<td width='" + Math.round (100 / cols) + "%'></td>";
				++i;
			}
			res += "</tr>";
		}
		layer_content_stats.innerHTML = "<div id=\"rubrique\" class=\"normal\"><div id=\"title\" class=\"special_color\">Statistiques globales</div></div>";
		layer_content_stats.innerHTML += "<center><img src=\"stats/barplot_grades.php?year=all\"> <img src=\"stats/lineplot_seen_movies_month.php?year=all\"> <img src=\"stats/lineplot_seen_movies_year.php\"></center>";
		layer_content_stats.innerHTML += "<div id=\"rubrique\" class=\"normal\"><div id=\"title\" class=\"special_color\">Tous les films</div></div>";
		layer_content.innerHTML = "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">" + res + "</table>";
		
		if (layer_rankings_year)
			layer_rankings_year.style.display = "block";
	}
	
	function loadMovies (year)
	{
		var layer_content = document.getElementById ("content");
		var layer_content_stats = document.getElementById ("content_stats");
		var res = "";
		var i;
		var j = 0;
		
		for (i = 0; i < movies.length; ++i)
		{
			if (movies[i].year_out == year)
				res += movies[i].toHTML_picture (j++);
		}
		if (j % cols > 0)
		{	
			while (j % cols)
			{
				res += "<td width='" + Math.round (100 / cols) + "%'></td>";
				++j;
			}
			res += "</tr>";
		}
		layer_content_stats.innerHTML = "<div id=\"rubrique\" class=\"normal\"><div id=\"title\" class=\"special_color\">Statistiques " + year + "</div></div>";
		layer_content_stats.innerHTML += "<center><img src=\"stats/barplot_grades.php?year=" + year + "\"> <img src=\"stats/lineplot_seen_movies_month.php?year=" + year + "\"> <img src=\"stats/lineplot_seen_movies_year.php\"></center>";
		layer_content_stats.innerHTML += "<div id=\"rubrique\" class=\"normal\"><div id=\"title\" class=\"special_color\">Films " + year + "</div></div>";
		layer_content.innerHTML = "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">" + res + "</table>";
	}
	
	function setCookie (variable, value)
	{
		var expire = new Date();
		var year = expire.getTime() + (365 * 24 * 60 * 60 * 1000);
		
		expire.setTime (year);
		document.cookie = variable + "=" + value + "; expires=" + expire.toGMTString() + "; path=/movies";
	}
	
	function colorToggle (color)
  	{
		var layer_main = document.getElementById ("main");
		
		layer_main.className = "main_" + color;
		setCookie ("color", color);
 	}
	
	function homeFocus ()
	{
		var layer_content = document.getElementById ("content");
		
		unloadYear (current_year);
		current_year = 0;
		layer_content.innerHTML = focus_html;
	}