/**
 * 	Date			: May 13, 2009
 *		Author		: Paul Warelis
 *		Copyright	: Dine.TO (www.dine.to)
 *		Description	: Theater tab row plugin
 */

(function($) {

	$.fn.theaterTabRow = function(viewMovieCallback) {
		return this.each(function() {
			// Save movie id
			this.theaterId = this.id.replace("mth","");
			var $this = $(this);

			// Callback function for when the view info link is clicked on a movie entry
			this.viewMovieCallback = viewMovieCallback;
			
			$this.click($.fn.theaterTabRow.handleMouseClick);
			$this.hover($.fn.theaterTabRow.handleMouseHover);
			$this.mouseleave($.fn.theaterTabRow.handleMouseLeave);
		});
	};

	// Event handlers
	$.fn.theaterTabRow.handleMouseClick = function(event) {
		if (callInProgress) return false;
		var $this = $(this);
		if ($this.hasClass("brTheater")) {
//			$this.find(".brLink").trigger("click");
			event.stopPropagation();
			return false;
		}

		var tab = $this.parents(".movieTabPage");
		var mapDiv = tab.find(".mapDiv");

		var selectedMovie = $this.parents(".resultBox").find(".movieRowTheaterSelected:visible");
		if (selectedMovie.length > 0) {
			var bail = $this.hasClass("movieRowTheaterSelected");
			selectedMovie.removeClass("movieRowTheaterSelected").next().hide();
			// Unselect active theater
			$.fn.showtimeTheaterRow.unselectTheaterRow();

			if (bail) {
				tab.find(".resultBox").removeClass("grayOutAllRows");
				mapDiv.zoomOut();
				return;
			}
		}
		
		$this.addClass("movieRowTheaterSelected");
		mapDiv.zoomInOnTheater( $this.get(0).theaterId );

		if ($this.next().show().length == 0) {
			mapDiv.disableMap("<img src='images/indicator.gif'>");
			callInProgress = true;
			serverBusy("theaterCall", true);
			$.post(ajaxFile, {
					action		: "getMoviesByTheater",
					theaterId	: $this.get(0).theaterId
				}, function (data) {
					serverBusy("theaterCall", false);
					mapDiv.enableMap();
					if (errorDetected(data)) return;
					
					// Insert the html content into the theater row
					$this.parent().append(data.htmlContent);
					$this.parent().find(".theaterMovieRow").theaterMovie($this.get(0).theaterId);

					$this.parent().find(".theaterMovieRow span.movieDetailLink").click($this.get(0).viewMovieCallback);
				},
			"json");
		}
		tab.find(".resultBox").addClass("grayOutAllRows");
	};

	$.fn.theaterTabRow.handleMouseHover = function() {
		var $this = $(this);
		if ($this.hasClass("brTheater") == false) $this.addClass("movLocationHover");

		// If the map is visible, simulate a hover on a theater icon
		if ($("#theaterMap").get(0).isVisible) {
			var marker = $("#theaterMap").getTheaterMarker($this.get(0).theaterId);
			if (marker == null) return;
			marker.tooltip.show();
			marker.tooltip.redraw(true);
		}
	};

	$.fn.theaterTabRow.handleMouseLeave = function() {
		var $this = $(this);
		if ($this.hasClass("brTheater") == false) $this.removeClass("movLocationHover");
		
		// Simulate a mouseleave on a theater icon
		if ($("#theaterMap").get(0).isVisible) {
			var marker = $("#theaterMap").getTheaterMarker($this.get(0).theaterId);
			if (marker != null) marker.tooltip.hide();
		}
	};

})(jQuery);

