/**
 * 	Date			: May 13, 2009
 *		Author		: Paul Warelis
 *		Copyright	: Dine.TO (www.dine.to)
 *		Description	: Showtime theater row plugin
 */

(function($) {

	$.fn.showtimeTheaterRow = function(movieId) {
		return this.each(function() {
			var $this = $(this);

			var theaterId = this.id.replace("mth","");
			this.theaterId = theaterId;

			// Load the show times based on the movie/theater combo for today (0)
			var showtimeTheaterRow = $this.find(".movTheaterShowtime");
			showtimeTheaterRow.timeSlide(true);

			// Hover actions
			$this.hover($.fn.showtimeTheaterRow.handleMouseHover);
			$this.mouseleave($.fn.showtimeTheaterRow.handleMouseLeave);
			// Select/unselect theaters
			$this.click($.fn.showtimeTheaterRow.handleMouseClick);
		});
	};

	// Private functions
	$.fn.showtimeTheaterRow.selectTheaterRow = function(theaterId) {
		var $this = $("#tabShowtimes .movieRowSelected").next().find("#mth"+theaterId);

		if ($this.hasClass("movieRowTheaterSelected")) {
	  		$.fn.showtimeTheaterRow.unselectTheaterRow();
		} else {
			$.fn.showtimeTheaterRow.unselectTheaterRow();
			$this.addClass("movieRowTheaterSelected");
			$("#showtimeMap").zoomInOnTheater( $this.get(0).theaterId );
		}
   }
	
	$.fn.showtimeTheaterRow.unselectTheaterRow = function(){
		var $this = $("#tabShowtimes .movieRowTheaterSelected");

		// Unselect the theater, unconditionally
		if ($this.removeClass("movieRowTheaterSelected").length == 1) {
			// If there was one selected, zoom out the map
			$("#showtimeMap").zoomOut();
		}

   }

	// Event handlers
	$.fn.showtimeTheaterRow.handleMouseHover = function() {
		var $this = $(this);
		$this.addClass("movLocationHover");
		
		// If the map is visible, simulate a hover on a theater icon
		if ($("#showtimeMap").get(0).isVisible) {
			var marker = $("#showtimeMap").getTheaterMarker($this.get(0).theaterId);
			if (marker == null) return;
			marker.tooltip.show();
			marker.tooltip.redraw(true);
		}
	};

	$.fn.showtimeTheaterRow.handleMouseLeave = function() {
		var $this = $(this);
		$this.removeClass("movLocationHover");
		
		// Simulate a mouseleave on a theater icon
		if ($("#showtimeMap").get(0).isVisible) {
			var marker = $("#showtimeMap").getTheaterMarker($this.get(0).theaterId);
			if (marker != null) marker.tooltip.hide();
		}
	};
	
	$.fn.showtimeTheaterRow.handleMouseClick = function() {
		// This selects the theater and does things to the map
		$.fn.showtimeTheaterRow.selectTheaterRow(this.theaterId);
	};

})(jQuery);

