/**
 * 	Date			: May 13, 2009
 *		Author		: Paul Warelis
 *		Copyright	: Dine.TO (www.dine.to)
 *		Description	: Movie row plugin
 */

(function($) {

	$.fn.movieRow = function() {
		return this.each(function() {
			// Save movie id
			this.movieId = this.id.replace("mov","");
			var $this = $(this);

			$this.click($.fn.movieRow.handleMouseClick);
		});
	};

	// Event handlers
	$.fn.movieRow.handleMouseClick = function() {
		if (callInProgress) return false;
		var $this = $(this);

		$this.addClass("movieRowSelected");
		
		var tabMovies = $("#tabMovies");
		// Hide all movie rows and any headers
		tabMovies.find(".movieRow, .thHeader, .listHeading").hide();
		tabMovies.find(".backToList").show();

		if (typeof(this.theaterList) == "undefined") {
			// Make an ajax call to get movie information
			$("#movieMap").disableMap("<img src='images/indicator.gif'>");
			serverBusy("movieCall", true);
			callInProgress = true;
			$.post(ajaxFile, {
					action	: "getMovieInfo",
					movieId	: this.movieId
				}, function (data) {
					serverBusy("movieCall", false);
					$("#movieMap").enableMap();
					if (errorDetected(data)) return;
					
					var theaterList = data.theaterList.split(",");
					// Save the list
					$this.get(0).theaterList = theaterList;
					
					// Insert the html content into the movie row
					$this.parent().append(data.htmlContent);
					
					// Loaded the movie datails along with a div holding all theaters.
					// Initialize all theater rows
					$this.next().find(".movLocation").theaterRow(data.movieId);
					$this.next().find(".movieRegionSelect select").change(movList.regionSelectChange);
					$this.next().find(".movLocation span.movieDetailLink").click(movList.viewTheaterInfo);

					// Attempt to map all of the theaters
					$("#movieMap").mapTheaters(theaterList);
				},
			"json");
			
		} else {
			// Movie info is there, show it and make a call to the map
			$this.next().show();
			$("#movieMap").mapTheaters(this.theaterList).enableMap();
		}
		
	};

})(jQuery);

