/**
 * Date	: April 24, 2009
 * Author: Paul Warelis
 * Copyright (c) 2009 Dine.TO
 */

var ajaxFile = "ajax/movieSupport.ajax.php";
var callInProgress = false;
var postalCode = "";

var theaterInfo = {};
var estInfo = {};
var parkInfo = {};

$(document).ready(function () {

	$("#cinemaSearchPanel").tabs({ selected : 0 });
	$("#movieTabs a").click(function() { return false; });

	movAutoComplete.init("movieName");
	$(".searchRegion").val(0);

	$(".resultHeader .mapButton").click(function() {
		var $this = $(this);
		if ($this.hasClass("mapButtonHide")) {
			$this.removeClass("mapButtonHide");
		} else {
			$this.addClass("mapButtonHide");
		}
		return true;
	});

	// Theater controls
	$("#tabTheaters .backToList").click(thList.backToListClick);
	$("#theaterRegion").change(thList.getTheatersByRegion);
	$("#theaterBrand").change(thList.getTheatersByBrand);
	$("#theaterPostal").click(thList.getTheatersByPostal);
	$("#theaterMap").theaterMap().enableMap();
	$("#theaterMapButton").click(function() { $("#theaterMap").toggleMapVisibility(); });


	// Movie controls
	$("#tabMovies .backToList").click(movList.backToListClick);
	$("#movieIndex div.button").not("#miNew, #miSoon").click(movList.getMoviesByIndex);
	$("#miNew").click(movList.getMoviesNewReleases);
	$("#miSoon").click(movList.getMoviesComingSoon);
	$("#movieRating").change(movList.getMoviesByRating);
	$("#movieGenre").change(movList.getMoviesByGenre);
	$("#movieRegion").change(movList.getMoviesByRegion);
	$("#moviePostal").click(movList.getMoviesByPostal);
	$("#searchMovies").click(function() { movAutoComplete.enterPressed(); });
	$("#movieMap").theaterMap();
	$("#movieMapButton").click(function() { $("#movieMap").toggleMapVisibility(); });


	// Showtime controls
	$("#tabShowtimes .backToList").click(stList.backToListClick);
	$("#showtimeRegion").change(stList.getShowtimesByRegion);
	$("#showDay").change(stList.getShowtimesByDay);
	$("#showTime").change(stList.getShowtimesByTime);
	$("#showtimePostal").click(stList.getTheatersByPostal);
	$("#showtimeMap").theaterMap();
	$("#showtimeMapButton").click(function() { $("#showtimeMap").toggleMapVisibility(); });
	// Update the time options according to the day selected
	stList.updateTimeOptions();
	
	// Postal code code
	$(".postalCode").focus(function() {
		if (postalCode == "") {
			$(".postalCode").val("").css("color","black");
		}
	});
	$(".postalCode").blur(function() {
		postalCode = this.value;
		if (this.value == "") {
			$(".postalCode").val("Postal code").css("color","gray");
		} else {
			$(".postalCode").val(postalCode).css("color","black");
		}
	});
	$(".postalCode").keyup(function(event) {
		// On enter, trigger a click on the neighbouring button
		switch (event.keyCode) {
			case 13:		// enter
				postalCode = this.value;
				$(this).next().trigger("click");
				break;
		}
		return true;
	});

	$("#movieCall, #theaterCall, #showtimeCall").fadeTo(0, 0.8);

	movList.getMoviesNewReleases();
	thList.getTheaterList(1000, 0, null);
	stList.getFancyShowtimes();

});

function serverBusy(htmlId, inProgress) {
	var $this = $("#"+htmlId);
	var map = $this.prevAll(".mapDiv");
	if (inProgress && map.length > 0 && !map.get(0).isVisible) {
		$this.fadeIn("normal");
		// Calculate new height and show cover
		var height = $("#page_bottom_footer").offset();
		var coverPos = $this.offset();
		height = height.top - coverPos.top;
		$this.height(height);
	} else {
		$this.hide();
	}
}


