/**
 * Date	: April 24, 2009
 * Author: Paul Warelis
 * Copyright (c) 2009 Dine.TO
 */

var movList = {
	lastAction			: "",
	lastArg				: null,
	
	getMovieList : function (action, argument, sortCol) {
		if (callInProgress) return false;
		if (action == null) {
			// Perform the last action
			action = this.lastAction;
			argument = this.lastArg;
		} else {
			// Save the action
			this.lastAction = action;
			this.lastArg = argument;
			sortCol = "";	// undefined defaults to ""
		}
		serverBusy("movieCall", true);
		callInProgress = true;
		$.post(ajaxFile, {
				action	: action,
				argument	: argument,
				sort		: sortCol
			}, function (data) {
				serverBusy("movieCall", false);
				if (errorDetected(data)) return;

				var tabMovies = $("#tabMovies");
				if (typeof data.postalError != "undefined") {
					// The invalid postal code code is a little retarded, tight schedule mate
					tabMovies.find(".resultBox").html("");
					tabMovies.find(".resultHeader").show().find("span").html(data.postalError).show();
					tabMovies.find(".backToList").hide();
					$("#movieMap").disableMap(data.postalError);
					$("#movieMapButton").hide();
					return;
				}
				
				// If postal is present, pass it to the map
				if (typeof data.postalGeo != "undefined") {
					$("#movieMap").setPostalCode(data.postalGeo.split(","));
				} else {
					$("#movieMap").setPostalCode(null);
				}
				
				tabMovies.find(".resultBox").html(data.movieList);

				$("#movieMap").disableMap("Please select a movie to show theater locations");
				
				if (data.movieList == "") $("#movieMapButton").hide();
				else $("#movieMapButton").show();
				
				tabMovies.find(".resultHeader").show().find("span").html(data.heading).show();
				tabMovies.find(".backToList").hide();

				// new releases links
				$(".nrLink").click(movList.viewMovieInfo).each(function () {
					var $movieRow = $(this).parent();
					$movieRow.get(0).movieId = $movieRow.get(0).id.replace("mov","");
				});
				
				// Sort column controls
				tabMovies.find(".thHeader span").bind("click", movList.sortMovies);
				tabMovies.find(".movieRow").movieRow();
				
			},
		"json");
		return true;
	},
	
	// This function is used by the auto complete control
	getMoviesByPhrase : function( textPhrase ) {
		var success = this.getMovieList("getMoviesByPhrase", textPhrase);
		if (!success) return;
		$("#movieRegion, #movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
	},

	getMoviesByPerson : function( personName ) {
		var success = this.getMovieList("getMoviesByPerson", personName);
		if (!success) return;
		$("#movieRegion, #movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
		movAutoComplete.phrase = "";
		movAutoComplete.textBox.value = "";
	},

	loadTheaterRegion : function ( theaterList, movieRow ) {
		var movieId = movieRow.get(0).movieId;
		callInProgress = true;
		serverBusy("theaterListCall", true);
		$.post(ajaxFile, {
				action		: "loadTheaterRegion",
				theaterList	: theaterList,
				movieId		: movieId
			}, function (data) {
				serverBusy("theaterListCall", false);
				if (errorDetected(data)) return;

				var movieInfoDiv = movieRow.parents(".movieTabPage").find("#mov"+data.movieId).next();
				movieInfoDiv.find(".movieTheaterWrapper").html(data.theaterListHtml);
				// Region select, if exists
				movieInfoDiv.find(".movLocation").theaterRow(data.movieId);
				
				callInProgress = false;
			},
		"json");
		return true;
	},

	/*		Events	[ Don't use "this" to refer to movList in any of these functions ] */

	regionSelectChange : function () {
		var movieRow = $(this).parents(".movieDiv").find(".movieRow");
		var theaterList = $(this).val();
		// Load new theaters
		movList.loadTheaterRegion(theaterList, movieRow);
		
		// Make an array and save it with the current movie row
		theaterList = theaterList.split(",");
		movieRow.get(0).theaterList = theaterList;
		
		// Map the region theaters
		movieRow.parents(".movieTabPage").find(".mapDiv").mapTheaters(theaterList);

		return true;
	},

	getMoviesNewReleases : function() {
		var success = movList.getMovieList("getMoviesNewReleases", this.id);
		if (!success) return;
		$("#movieRegion, #movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
		$(this).addClass("selectedIndex");
	},
	
	getMoviesComingSoon : function() {
		var success = movList.getMovieList("getMoviesComingSoon", this.id);
		if (!success) return;
		$("#movieRegion, #movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
		$(this).addClass("selectedIndex");
	},
	
	getMoviesByIndex : function() {
		var success = movList.getMovieList("getMoviesByIndex", this.id);
		if (!success) return;
		$("#movieRegion, #movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
		$(this).addClass("selectedIndex");
	},
	
	getMoviesByRating : function() {
		if (this.selectedIndex == 0) return;
		var success = movList.getMovieList("getMoviesByRating", $(this).val());
		if (!success) return;
		$("#movieRegion, #movieGenre").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
	},
	
	getMoviesByGenre : function() {
		if (this.selectedIndex == 0) return;
		var success = movList.getMovieList("getMoviesByGenre", $(this).val());
		if (!success) return;
		$("#movieRegion, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
	},

	getMoviesByRegion : function() {
		if (this.selectedIndex == 0) return;
		var success = movList.getMovieList("getMoviesByRegion", $(this).val());
		if (!success) return;
		$("#movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
	},

	getMoviesByPostal	: function() {
		if (this.selectedIndex == 0) return;
		var success = movList.getMovieList("getMoviesByPostal", postalCode);
		if (!success) return;
		$("#movieRegion, #movieGenre, #movieRating").val(0);
		$(".selectedIndex").removeClass("selectedIndex");
	},

	sortMovies : function() {
		movList.getMovieList(null, null, this.className);
	},
	
	// Show the movie list, if this fires that means a movie was selected
	backToListClick : function() {
		var tab = $("#tabMovies");
		// Remove the temp movie row if it exists
		var tempRow = tab.find(".tempRow").remove();
		
		var selected = tab.find(".movieRowSelected");
		// Show all movie rows and any headers
		selected.removeClass("movieRowSelected").next().hide();
		// Remove selected theater as well
		tab.find(".movieRowTheaterSelected").removeClass("movieRowTheaterSelected");
		tab.find(".newMovies, .movieRow, .movieDiv, .thHeader, .listHeading").show();

		tab.find(".resultBox").removeClass("grayOutAllRows");
		// Hide this button
		$(this).hide();
		$("#movieMap").disableMap("Please select a movie to show theater locations");
	},
	
	viewTheaterInfo : function(event) {
		var $this = $(this);
		var theaterId = $this.parents(".movLocation").get(0).theaterId;

		if (callInProgress) return false;
		serverBusy("movieCall", true);
		callInProgress = true;
		$.post(ajaxFile, {
				action		: "getSingleTheater",
				theaterId	: theaterId
			}, function (data) {
				serverBusy("movieCall", false);
				if (errorDetected(data)) return;

				var tab = $this.parents(".movieTabPage");
				var map = tab.find(".mapDiv");
				tab.find(".tempRow").remove();

				tab.find(".listHeading, .thHeader, .movieDiv").hide();
				tab.find(".backToList").show();
				
				tab.find(".resultBox").append(data.theaterList);

				map.enableMap().mapTheaters(data.theaterIdList.split(","));
				$("#movieMapButton").show();

				// Initialize all theater rows
				tab.find(".theaterDiv").addClass("tempRow")
					.find(".theaterRow").theaterTabRow(movList.viewMovieInfo).trigger("click");
			},
		"json");
		event.stopPropagation();
		return false;
	},
	
	viewMovieInfo : function(event) {
		var movieRow = $(this).parents(".movieRow");
		if (movieRow.length == 0) {
			movieRow = $(this).parents(".theaterMovieRow");
			if (movieRow.length == 0) {
				movieRow = $(this).parents(".nrMovie");
			}
		}
		var movieId = movieRow.get(0).movieId;
		
		if (callInProgress) return false;
		callInProgress = true;
		serverBusy("movieCall", true);
		$.post(ajaxFile, {
				action		: "getMovieInfo",
				movieId		: movieId
			}, function (data) {
				serverBusy("movieCall", false);
				if (errorDetected(data)) return;

				var theaterList = data.theaterList.split(",");
				
				// Insert the html content into a temporary movie row
				var tab = $("#tabMovies");
				tab.find(".newMovies, .listHeading").hide();
				tab.find(".tempRow").remove();
				tab.find(".resultHeader, .backToList").show();
				
				tab.find(".resultBox").append(
				"<div class='movieDiv tempRow'><div id='mov"+data.movieId+"' class='movieRow movieRowSelected' style='display: none;'></div>\
				<div>"+data.htmlContent+"</div></div>");

				// Save movie and theater info inside the row element
				var tempRow = tab.find(".tempRow");
				var movieRow = tempRow.children();
				movieRow.get(0).movieId = data.movieId;
				movieRow.get(0).theaterList = theaterList;
				
				// Initialize all theater rows
				tempRow.find(".movLocation").theaterRow(data.movieId);
				tempRow.find(".movieRegionSelect select").change(movList.regionSelectChange);
				tempRow.find(".movLocation span.movieDetailLink").click(movList.viewTheaterInfo);

				// Attempt to map all of the theaters
				$("#movieMap").enableMap().mapTheaters(theaterList);
				$("#movieMapButton").show();
				callInProgress = false;
			},
		"json");
		event.stopPropagation();
		return false;
	}	
}
