/**
 * 	Date			: May 13, 2009
 *		Author		: Paul Warelis
 *		Copyright	: Dine.TO (www.dine.to)
 *		Description	: Theater row plugin
 */

(function($) {

	$.fn.theaterRow = 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 theaterShowtimes = $this.find(".movTheaterShowtime");
			theaterShowtimes.timeSlide();

			var today = $this.find(".movTheaterDays select").val();
			// if today is empty, there's no select: passing a -1 will get the earliest date
			if (typeof today == "undefined") today = -1;
			theaterShowtimes.loadShowtimes(theaterId, movieId, today);

			// Hover actions
			$this.hover($.fn.theaterRow.handleMouseHover);
			$this.mouseleave($.fn.theaterRow.handleMouseLeave);
			// Select/unselect theaters
			$this.click($.fn.theaterRow.handleMouseClick);
			// Day select/change
			$this.find(".movieDaySelect")
				.click($.fn.theaterRow.handleDayClick)
				.change($.fn.theaterRow.handleDayChange);
		});
	};

	$.fn.theaterRow.handleDayChange = function() {
		var $this = $(this);
		
		// Update the plain text day name
		var selectedDay = $this.find("option:selected").html();
 		$this.next().html(selectedDay);

		// Go up to the theater row div
		$this = $this.parents(".movLocation");

		var theaterId = $this.get(0).theaterId;
		var movieId = $this.parents(".movieDiv").children().get(0).movieId;
		
		$this.find(".movTheaterShowtime").loadShowtimes(theaterId, movieId, $(this).val());
	}

	$.fn.theaterRow.handleDayClick = function(event) {
		var $this = $(this);
		var selectedTheater = $this.parents(".movieTheaterWrapper").find(".movieRowTheaterSelected").get(0);
		var thisTheater = $this.parents(".movLocation").get(0);
		// If and only if this is the selected row, stop propagation
		
		if ( (typeof selectedTheater != "undefined") && (selectedTheater == thisTheater) ) {
			event.stopPropagation();
		}
	}

	// Private functions
	$.fn.theaterRow.selectTheaterRow = function(row) {
		var theaterId = row.theaterId;
		var tab = $(row).parents(".movieTabPage");
		var $this = tab.find("#mth"+theaterId+":visible");

		if ($this.hasClass("movieRowTheaterSelected")) {
	  		$.fn.theaterRow.unselectTheaterRow(tab);
		} else {
			$.fn.theaterRow.unselectTheaterRow(tab);
			$this.addClass("movieRowTheaterSelected");
			tab.find(".mapDiv").zoomInOnTheater( $this.get(0).theaterId );
			
			var daySelect = $this.find(".movTheaterDays :first");
		
			if ( daySelect.length > 0) {
				daySelect.show().next().hide();
			}
		}
   }
	
	$.fn.theaterRow.unselectTheaterRow = function(tab) {
		var $this = tab.find(".movieRowTheaterSelected:visible");
	
		// Unselect the theater, unconditionally
		if ($this.removeClass("movieRowTheaterSelected").length == 1) {
			// If there was one selected, zoom out the map
			tab.find(".mapDiv").zoomOut();
		}

		// If hover is on, leave the day select control
		if ($this.hasClass("movLocationHover")) return;
		
		$this.find(".movTheaterDays select").hide().next().show();
   }

	// Event handlers
	$.fn.theaterRow.handleMouseHover = function() {
		var $this = $(this);

		var daySelect = $this.addClass("movLocationHover").find(".movTheaterDays :first");
		var selectedTheater = $this.parents(".movieTheaterWrapper").find(".movieRowTheaterSelected");
		
		if ( daySelect.length > 0 && selectedTheater.length == 0) {
			// Display the select control and hide text
			daySelect.show().next().hide();
		}

		var mapDiv = $this.parents(".movieTabPage").find(".mapDiv");
		// If the map is visible, simulate a hover on a theater icon
		if (mapDiv.get(0).isVisible) {
			var marker = mapDiv.getTheaterMarker($this.get(0).theaterId);
			if (marker == null) return;
			marker.tooltip.show();
			marker.tooltip.redraw(true);
		}
	};

	$.fn.theaterRow.handleMouseLeave = function() {
		var $this = $(this);
		
		var daySelect = $this.find(".movTheaterDays select");
		var selectedTheater = $this.parents(".movieTheaterWrapper").find(".movieRowTheaterSelected");
		
		if ( (daySelect.length > 0) ) {
			if (selectedTheater.length == 0 || this.id != selectedTheater.attr("id")) {
			// Display the text and hide the select control
		 		daySelect.hide().next().show();
			}
		}
		$this.removeClass("movLocationHover");
		
		var mapDiv = $this.parents(".movieTabPage").find(".mapDiv");
		// Simulate a mouseleave on a theater icon
		if (mapDiv.get(0).isVisible) {
			var marker = mapDiv.getTheaterMarker($this.get(0).theaterId);
			if (marker != null) marker.tooltip.hide();
		}
	};
	
	$.fn.theaterRow.handleMouseClick = function() {
		// This selects the theater and does things to the map
		$.fn.theaterRow.selectTheaterRow(this);
	};

})(jQuery);

