﻿/// <reference path="./Cineplex.js" />

Agility.RegisterNamespace("Cineplex.TheatresAndMoviesByFFEC");

(function(FFEC) {

	 $(function() {
		
		//bind the skinned dropdowns
		$("#lstShowtimeDates").CineplexCombo({
			onchange : function() {									
				_populateListings();
				return false;
			}
		});
		
		//add autocomplete to location field
		$("#movie-details-location").autocomplete(Agility.ResolveUrl("~/Services/LocationAutoComplete.ashx"));
		
		
		//location search
		var setLocation = function() {
			Cineplex.UserContext.SetCurrentLocation($("#movie-details-location").val(), _populateListings);
		};
		
		$("#movie-details-location-search-button").click(function() {
			setLocation();		
			return false;
		});
		
		$("#movie-details-location").keydown(function(e) {
			if (e.keyCode == 13) {
				setLocation();		
				return false;
			}
		});
		
		//check for location
        var location = Cineplex.UserContext.GetCurrentLocation();

        if (location == null) {
            //show the location and come back here when done...
            Cineplex.ShowLocationDialog(function(loc) {
                _populateListings(loc);
                
            });            
        } else {			
			_populateListings(location);						
        }
		
	 });

	var _isFirstLoad = true;


	var _currentDate = null;

	function _populateListings(location, pageIndex) {
		
		if (location == undefined) {
			location = $("#movie-details-location").val();			
		}
		
	
		if (pageIndex == undefined) {
			pageIndex = 0;
		}
		
		if (location == "") return;
						
		$("#movie-details-location").val(location);
		
		//get the current date
		var currentDate = new Date();
		
		var dateStr = $("#lstShowtimeDates li a.selected").attr("dateStr");
		if (dateStr != null && dateStr != "") {
			currentDate = Date.parse(dateStr);				
		} else {
			dateStr = currentDate.toString("yyyy-M-d");
		}		
				
		_currentDate = currentDate;
		
		
		//set the height so we don't "jump"
		if (! _isFirstLoad) {
			$("#pnlTheatreListing").css("minHeight", $("#pnlTheatreListing").height()).css("overflow", "hidden");	
			
		}
		
		//show progess....
		$("#pnlTheatreListing").html(Cineplex.AjaxSpinner());
		$("#pnlTheatreListing").show();
		
		//do the data access call...
		//build the query...
		var url = Agility.ResolveUrl("~/Services/AjaxServices.asmx/WebSelectMoviesAndTheatres_ByFFEC");				
		
		var pageSize = Cineplex_FFEC_PageSize;
		
		var arg = { 
			ffecType: Cineplex_Current_FFECType,
			listingType: Cineplex_Current_ListingType,
			dateStr: dateStr,
			location: location,
			pageIndex: pageIndex,
			pageSize: pageSize
			};
				
		
		$.ajax({
		  type: "POST",
		  contentType: "application/json; charset=utf-8",
		  url: url,
		  data: JSON.encode(arg),
		  dataType: "json",
		  success: function(result) {
			
			if (result == undefined || result.d == undefined) {
				 $("#pnlTheatreListing").html(Cineplex_errSearchNotCompleted);
			} else {
		
				var ary = result.d.ary;			
				var count = result.d.count;
											
				$("#pnlTheatreListing").setTemplateElement("txtTemplateOnFFECModule");
				$("#pnlTheatreListing").setParam("getTicketingLink", Cineplex.GetTicketingLink);	
				$("#pnlTheatreListing").setParam("renderExclusiveContent", _renderExclusiveContent);
				$("#pnlTheatreListing").setParam("renderCurrentDate", _getCurrentDate);					
				
				$("#pnlTheatreListing").processTemplate(ary);
				
				//init the pager
				Cineplex.InitializeAjaxPager($("#pnlTheatreListingPager"), pageSize, pageIndex, count, 
					function(newPageIndex) {
						_populateListings(location, newPageIndex)					
				});
				
				
				if (! _isFirstLoad) {				 				
					_scrollToShowTimes();
				}
								
			}
			
			_isFirstLoad = false;
		
		  },
		  error: function(error) {
			 $("#pnlTheatreListing").html(Cineplex_errSearchNotCompleted);	
			 _scrollToShowTimes();		 
		  }

		});
		
		
		
		
	}
	
	
	function _renderExclusiveContent(str) {
		if (str == null || str == "") return "";
		
		var div = $('<div></div>');
		div.html(str);
		
		return '<div class="ExclusiveListingContent">' + div.text() + "</div>";
	}
	
	function _getCurrentDate(fec) {		
		if (fec != undefined && fec != null) {
			
			var date = Date.parse(fec.ShowTimes[0].DateStr);
			return date.toString(Cineplex_DateFormat);
		}
		
		
		return _currentDate.toString(Cineplex_DateFormat) + "...";
	}
	
	function _scrollToShowTimes() {
		//set the height so we don't "jump"
		$(document).scrollTo($("#Showtimes"), {
			 duration:500,
			 easing: "swing",
			 onAfter : function() {
				$("#pnlTheatreListing").css("minHeight", null).css("overflow", "auto");	
			 }
		});
	}

})(Cineplex.TheatresAndMoviesByFFEC);