/**
 * 	Date			: May 13, 2009
 *		Author		: Paul Warelis
 *		Copyright	: Dine.TO (www.dine.to)
 *		Description	: Theater tab
 */

var thList = {
	// The last action/argument is for sorting
	lastBrand	: null,
	lastRegion	: null,

	getTheaterList : function (brand, region, sortCol) {
//		if (callInProgress) return false;

		if (sortCol == null) sortCol = "";

		serverBusy("theaterCall", true);
		callInProgress = true;
		$.post(ajaxFile, {
				action		: "getTheaterList",
				region		: region,
				brand			: brand,
				postal		: postalCode,
				sortCol		: sortCol
			}, function (data) {
				serverBusy("theaterCall", false);
				if (errorDetected(data)) return;

				var tabTheaters = $("#tabTheaters");
				
				if (typeof data.postalError != "undefined") {
					// The invalid postal code code is a little retarded, tight schedule mate
					tabTheaters.find(".resultBox").removeClass("grayOutAllRows").html("");
					tabTheaters.find(".resultHeader").show().find("span").html(data.postalError).show();
					tabTheaters.find("#theaterMap").disableMap(data.postalError);
					tabTheaters.find("#theaterMapButton").hide();
					return;
				}
				
				// If postal is present, pass it to the map
				if (typeof data.postalGeo != "undefined") {
					$("#theaterMap").setPostalCode(data.postalGeo.split(","));
				} else {
					$("#theaterMap").setPostalCode(null);
				}

				tabTheaters.find(".resultBox").html(data.theaterList).removeClass("grayOutAllRows");
				tabTheaters.find(".resultHeader").show().find("span").html(data.heading).show();

				if (data.theaterList != "") {
					// Sort column controls
					tabTheaters.find(".thHeader span").bind("click", thList.sortMovies);

					var brList = tabTheaters.find(".brLink");
					if (brList.length > 0) {
						// Brand list
						tabTheaters.find(".brTheater").theaterTabRow();
						brList.click(thList.viewTheaterInfo);
						
						$(".brandDiv span").click(thList.displayBrand);
						$(".brandRegion").change(thList.listRegionBrand);
						
					} else {
						// Initialize all theater rows
						tabTheaters.find(".theaterRow").theaterTabRow(thList.viewMovieInfo);
					}

					// Attempt to map all of the theaters
					var theaterList = data.theaterIdList.split(",");
					
					$("#theaterMap").mapTheaters(theaterList).enableMap().get(0).regionList = theaterList;
				} else {
					$("#theaterMap").disableMap("No theaters found.");
				}
				tabTheaters.find(".backToList").hide();
				$("#theaterMapButton").show();
			},
		"json");
	},

	displayBrand : function () {
		var brandId = this.id.replace("brand", "");
		$("#theaterBrand").val(brandId);
		thList.getTheaterList(brandId, 0, null);
	},
	
	listRegionBrand : function () {
		var brand = this.id.replace("brand", "");
		var region = $(this).val();
		$("#theaterBrand").val(brand);
		$("#theaterRegion").val(region);
		thList.getTheaterList(brand, region, null);
	},
	
	/* Control event handlers */

	getTheatersByRegion : function() {
		if (this.selectedIndex == 0) return false;
		$("#theaterBrand").val(0);
		thList.getTheaterList(0, $("#theaterRegion :selected").val(), null);
	},

	getTheatersByBrand : function() {
		if (this.selectedIndex == 0) return false;
		$("#theaterRegion").val(0);
		thList.getTheaterList($("#theaterBrand :selected").val(), 0, null);
	},
	
	getTheatersByPostal : function() {
		if (postalCode == '') return false;
		// Reset the select boxes
		$("#theaterRegion, #theaterBrand").val(0);
		thList.getTheaterList(0, 0, null);
	},

	sortMovies : function () {
		thList.getTheaterList($("#theaterBrand :selected").val(), $("#theaterRegion :selected").val(), this.className);
	},
	
	backToListClick : function() {
		var tab = $(this).parents(".movieTabPage");
		var map = tab.find(".mapDiv");
		
		tab.find(".tempRow").remove();
		
		tab.find(".listHeading, .thHeader, .theaterDiv, .initTheaters").show();
		$(this).hide();

		map.mapTheaters(map.get(0).regionList);
		
		// Depending on the list state, modify the map		
		var selected = tab.find(".movieRowTheaterSelected");
		
		if (selected.length > 0) {
			// Show theater closeup
			map.zoomInOnTheater(selected.get(0).theaterId);
			tab.find(".resultBox").addClass("grayOutAllRows");
		} else {
			tab.find(".resultBox").removeClass("grayOutAllRows");
		}
	},
	
	viewMovieInfo : function(event) {
		if (callInProgress) return false;

		var movieRow = $(this).parents(".movieRow");
		if (movieRow.length == 0) {
			movieRow = $(this).parents(".theaterMovieRow");
		}
		var movieId = movieRow.get(0).movieId;
		callInProgress = true;
		serverBusy("theaterCall", true);
		$.post(ajaxFile, {
				action		: "getMovieInfo",
				movieId		: movieId
			}, function (data) {
				serverBusy("theaterCall", false);
				if (errorDetected(data)) return;

				var theaterList = data.theaterList.split(",");
				
				// Insert the html content into a temporary movie row
				var tabTheaters = $("#tabTheaters");
				tabTheaters.find(".tempRow").remove();
				
				tabTheaters.find(".thHeader, .theaterDiv").hide();
				tabTheaters.find(".listHeading").hide();
				tabTheaters.find(".backToList").show();
				
				tabTheaters.find(".resultBox").append(
				"<div class='movieDiv tempRow'><div id='mov"+data.movieId+"' class='movieRow movieRowSelected' style='display: none;'></div>\
				<div>"+data.htmlContent+"</div></div>");

				// Save movie and theater info inside the row element
				var tempRow = tabTheaters.find(".tempRow");
				var movieRow = tempRow.children();
				movieRow.get(0).movieId = data.movieId;
				movieRow.get(0).theaterList = theaterList;
				
				// Initialize all theater rows
				tempRow.find(".movLocation").theaterRow(data.movieId);
				tempRow.find(".movieRegionSelect select").change(movList.regionSelectChange);
				tempRow.find(".movLocation span.movieDetailLink").click(thList.viewTheaterInfo);

				// Attempt to map all of the theaters
				$("#theaterMap").enableMap().mapTheaters(theaterList);
				$("#theaterMapButton").show();
				callInProgress = false;
			},
		"json");
		event.stopPropagation();
		return false;
	},
	
	viewTheaterInfo : function(event) {
		if (callInProgress) return false;
		var $this = $(this);
		
		var currentTheater = $this.parents(".movLocation");
		if (currentTheater.length == 0) {
			currentTheater = $this.parents(".brTheater");
		}
		var theaterId = currentTheater.get(0).theaterId;

		callInProgress = true;
		$.post(ajaxFile, {
				action		: "getSingleTheater",
				theaterId	: theaterId
			}, function (data) {
				if (errorDetected(data)) return;

				var tab = $this.parents(".movieTabPage");
				var map = tab.find(".mapDiv");
				tab.find(".initTheaters, .listHeading").hide();
				tab.find(".tempRow").remove();
				tab.find(".resultHeader, .backToList").show();
				
				tab.find(".resultBox").append(data.theaterList);

				map.enableMap().mapTheaters(data.theaterIdList.split(","));
				$("#theaterMapButton").show();
				// Initialize all theater rows
				tab.find(".theaterDiv:last").addClass("tempRow")
					.find(".theaterRow").theaterTabRow(thList.viewMovieInfo).trigger("click");
				tab.find(".resultBox").removeClass("grayOutAllRows");
			},
		"json");
		event.stopPropagation();
		return false;
	}
	
}

