/**
 * 	Date			: May 13, 2009
 *		Author		: Paul Warelis
 *		Copyright	: Dine.TO (www.dine.to)
 *		Description	: Theater tab movie plugin
 */

(function($) {

	$.fn.theaterMovie = function(theaterId) {
		return this.each(function() {
			var $this = $(this);

			this.movieId = this.id.replace("mov","");

			// Load the show times based on the movie/theater combo for today (0)
			var movieShowtimes = $this.find(".thMovieShowtime");
			movieShowtimes.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;
			movieShowtimes.loadShowtimes(theaterId, this.movieId, today);

			// Hover actions
			$this.hover($.fn.theaterMovie.handleMouseHover);
			$this.mouseleave($.fn.theaterMovie.handleMouseLeave);
			// Day select/change
			$this.find(".movieDaySelect")
				.change($.fn.theaterMovie.handleDayChange)
				.blur($.fn.theaterMovie.handleDayBlur)
				.click($.fn.theaterMovie.handleDayClick);
		});
	};

	$.fn.theaterMovie.handleDayChange = function() {
		var $this = $(this);

		var movieRow = $this.parents(".theaterMovieRow");
		var movieId = movieRow.get(0).movieId;
		
		// Update the plain text day name
		var selectedDay = $this.find("option:selected").html();
 		$this.next().html(selectedDay);

		// Go up to the theater row div
		var theaterId = $this.parents(".theaterDiv").find(".movieRowTheaterSelected").get(0).theaterId;

		movieRow.find(".thMovieShowtime").loadShowtimes(theaterId, movieId, $(this).val());
	};

	$.fn.theaterMovie.handleDayBlur = function(event) {
		var $this = $(this);
		var currentHoverRow = $this.parents(".theaterMovieRow").removeClass("theaterMovieRowSelected");
	 	$this.hide().next().show();
		
		// if a different row is hovered over, make sure to update it with day select
		rowActivateHover(currentHoverRow.parents(".theaterDiv").find(".theaterMovieHover"));
	}

	$.fn.theaterMovie.handleDayClick = function(event) {
		var $this = $(this);
		$this.parents(".theaterMovieRow").addClass("theaterMovieRowSelected");
	}

	$.fn.theaterMovie.handleMouseHover = function() {
		var $this = $(this);
		$this.addClass("theaterMovieHover");
		rowActivateHover($this);
	};

	function rowActivateHover($this) {
		if ($this.parents(".theaterDiv").find(".theaterMovieRowSelected").length > 0) return;
		
		var daySelect = $this.find(".thMovieDays :first");
		if ( daySelect.length > 0) {
			// Display the select control and hide text
			daySelect.show().next().hide();
		}
	}

	$.fn.theaterMovie.handleMouseLeave = function() {
		var $this = $(this);
		$this.removeClass("theaterMovieHover");
		
		if ($this.hasClass("theaterMovieRowSelected")) return;
		
		var daySelect = $this.find(".thMovieDays select");
		
		if ( (daySelect.length > 0) ) {
			// Display the text and hide the select control
	 		daySelect.hide().next().show();
		}
	};

})(jQuery);

