
function fetchCinemas()
	{
		document.getElementById('cinemas').innerHTML = '<table width="100%" height="100%"><tr><td align="center" style="font-size:18px; color:#999999;"><img src="_images/frame/spinner.gif" height=16" width="16" style="margin-top:10px;"> Searching...</td></tr></table>';
		if(map) { map.clearOverlays(); }
		
		makeXMLRequest('_json/cinemas.aspx', 'pc=' + document.getElementById('locationFld').value, function(r) {
			cinemaData = JSON.parse(r).cinemaData;
			displayCinemas();		
		});
		pageTracker._trackEvent("Listings", "Search", document.getElementById('locationFld').value);
	}
	
function displayCinemas()
	{
		if (cinemaData.locationBase.Placemark) {
		
			//Set Map Centre
			map.setCenter(new GLatLng(cinemaData.locationBase.Placemark[0].Point.coordinates[1], cinemaData.locationBase.Placemark[0].Point.coordinates[0]), cinemaData.locationBase.Placemark[0].AddressDetails.Accuracy+9);
			var bounds = null;
				
			if (cinemaData.cinemas.length === 0) {
				document.getElementById('cinemas').innerHTML = '<table width="369" height="100%"><tr><td align="center" style="font-size:18px; color:#999999;">Sorry,<br/>no cinemas found<br/><div style="font-size:12px">The list of cinemas comes from Google,<br/>contact Google if your local cinemas<br/>can\'t be found <a href="http://www.google.com/movies">here</a></div></td></tr></table>';
			} else {
				w = '<form onSubmit="fetchFilms(); return false;">';
				w += '<table id="cinemaTable" width="100%" cellspacing="0" cellpadding="5" style="margin-top:5px;">';
				for (var i=0; i<cinemaData.cinemas.length; i++) {
					//Load Markers
					if (cinemaData.cinemas[i].geo.Placemark) {
						lng = cinemaData.cinemas[i].geo.Placemark[0].Point.coordinates[0];
						lat = cinemaData.cinemas[i].geo.Placemark[0].Point.coordinates[1];
						if (bounds === null) { bounds = new GLatLngBounds(new GLatLng(lat, lng), new GLatLng(lat, lng)); }
						cinemaData.cinemas[i].gMarker = new GMarker(new GLatLng(lat, lng));
						cinemaData.cinemas[i].gMarker.html = '<table cellpadding="0" cellspacing="0" style="margin-top:8px; margin-bottom:8px;"><tr><td valign="top"><div style="width:54px; height:39px; border:1px solid #999999; background:#FFFFFF url(\'_images/cinemas/' + ((cinemaData.cinemas[i].icon=='') ? '_default.gif' : cinemaData.cinemas[i].icon) + '\') no-repeat 2px 2px; margin-right:8px;"></div><td valign="top"><h3 style="margin:0;">' + cinemaData.cinemas[i].name + '<'+'/h3>' + cinemaData.cinemas[i].address + '<br/>' + cinemaData.cinemas[i].telephone + '</table><div style="margin-bottom:8px;"><a href="JavaScript:showTimesForCinema(' + i + ')">Show Movies</a> - <a href="JavaScript:showTimesForCinemaOnly(' + i + ')">Here Only</a> - <a href="JavaScript:starCinema(' + i + ')">Highlight</a><'+'/div>';
						bounds.extend(cinemaData.cinemas[i].gMarker.getPoint());
						
						GEvent.addListener(cinemaData.cinemas[i].gMarker, 'click', function() { showInfoWindow(this); });
						map.addOverlay(cinemaData.cinemas[i].gMarker);
						cinemaData.cinemas[i].gMarker.setImage('_images/mapMarkers/marker' + String.fromCharCode(i+65) + '.png');
					}
					
					//Load Names Table
					w += '	<tr>';
					w += '		<td id="cinemaRow' + i + 'x1" onClick="document.getElementById(cinemaData.cinemas[' + i + '].tid).click();" width="40" valign="center" style="cursor:pointer; border-bottom:1px solid #CCCCCC;"><div style="width:54px; height:39px; border:1px solid #999999; background:#FFFFFF url(\'_images/cinemas/' + ((cinemaData.cinemas[i].icon=='') ? '_default.gif' : cinemaData.cinemas[i].icon) + '\') no-repeat 2px 2px; margin-left:5px;"></div>';
					w += '		<td id="cinemaRow' + i + 'x2" onClick="document.getElementById(cinemaData.cinemas[' + i + '].tid).click();" valign="center" style="cursor:pointer; border-bottom:1px solid #CCCCCC; font-size:16px;">' + cinemaData.cinemas[i].name;
					w += '		<td id="cinemaRow' + i + 'x3" onClick="toggleStar(' + i + ')" valign="center" style="border-bottom:1px solid #CCCCCC;"><img src="_images/stars/star.png" height="11" width="11" id="' + cinemaData.cinemas[i].tid + 'Star" style="margin-top:3px;">';
					w += '		<td id="cinemaRow' + i + 'x4" width="15" style="border-bottom:1px solid #CCCCCC;"><input type="checkbox" id="' + cinemaData.cinemas[i].tid + '" onClick="checkCinema(' + i + ')">';
					w += '		<td id="cinemaRow' + i + 'x5" valign="middle" align="center" style="border-bottom:1px solid #CCCCCC;">' + ((cinemaData.cinemas[i].gMarker) ? '<img src="_images/mapMarkers/circle' + String.fromCharCode(i+65) + '.png" height="20" width="20" style="margin-right:5px; margin-top:2px; cursor:pointer;" onClick="showInfoWindowForCinema(' + i + ')" id="cinema' + i + 'MapMarker">' : '') + '<img src="_images/frame/spinner.gif" height=16" width="16" id="cinema' + i + 'Spinner" style="margin:2px 5px 0 0; display:none;">';
				}
				w += '</table>';
				w += '</form>';
				document.getElementById('cinemas').innerHTML = w;				
				fixHeight();
			}
			
			//Show Map
			if (bounds!==null) { map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); }
			scrollDisplay(449);
			
		} else {
			document.getElementById('cinemas').innerHTML = '<table width="100%" height="100%"><tr><td align="center" style="font-size:18px; color:#999999;">Sorry,<br/>location not found</td></tr></table>';
			scrollDisplay(0);
		}
	}
	
function showInfoWindowForCinema(i)
	{
		showInfoWindow(cinemaData.cinemas[i].gMarker);
		scrollDisplay(449);
	}
	
function showInfoWindow(marker)
	{
		if(!marker) { marker=this; }
		currentMarker = marker;
		marker.openInfoWindowHtml('<div style="color:#000000;">' + marker.html + '</div>', {maxWidth:300});
	}
	
function showTimesForCinema(i)
	{
		if (!document.getElementById(cinemaData.cinemas[i].tid).checked) { document.getElementById(cinemaData.cinemas[i].tid).click(); }
		checkCinema(i);
	}
function showTimesForCinemaOnly(thisCinema)
	{
		for (var i=0; i<cinemaData.cinemas.length; i++) {
			if (thisCinema == i) {
				if (!document.getElementById(cinemaData.cinemas[i].tid).checked) { document.getElementById(cinemaData.cinemas[i].tid).click(); }
			} else {
				if (document.getElementById(cinemaData.cinemas[i].tid).checked) { document.getElementById(cinemaData.cinemas[i].tid).click(); }
			}
		}
		checkCinema(i);
	}
function starCinema(i)
	{
		if (!cinemaData.cinemas[i].star) { toggleStar(i); }
		showTimesForCinema(i);
	}
	
function checkCinema(i)
	{
		if (document.getElementById(cinemaData.cinemas[i].tid).checked && loadedForTIDs[currentDate].indexOf(cinemaData.cinemas[i].tid + '|')==-1) {
			if (document.getElementById('cinema' + i + 'MapMarker')) { document.getElementById('cinema' + i + 'MapMarker').style.display = 'none'; }
			document.getElementById('cinema' + i + 'Spinner').style.display = '';
		}
		if (currentCinema == -1) { fetchFilms(); } else { fetchFilmsAgain = true; }
	}

function highlightCinema(i)
	{
		checked = (document.getElementById(cinemaData.cinemas[i].tid).checked && (loadedForTIDs[currentDate].indexOf(cinemaData.cinemas[i].tid + '|')>=0));
		document.getElementById('cinemaRow' + i + 'x1').style.background = (checked) ? '#CCCCFF' : '#FFFFFF';
		document.getElementById('cinemaRow' + i + 'x2').style.background = (checked) ? '#CCCCFF' : '#FFFFFF';
		document.getElementById('cinemaRow' + i + 'x3').style.background = (checked) ? '#CCCCFF' : '#FFFFFF';
		document.getElementById('cinemaRow' + i + 'x4').style.background = (checked) ? '#CCCCFF' : '#FFFFFF';
		document.getElementById('cinemaRow' + i + 'x5').style.background = (checked) ? '#CCCCFF' : '#FFFFFF';
	}
	
function scrollDisplay(to)
	{
		if (to != document.getElementById('details').scrollLeft) {
			scrollStop = to;
			scrollDir = (to > document.getElementById('details').scrollLeft) ? 'left' : 'right';
			scrollDisplayGo();
		}
	}
	
function scrollDisplayGo()
	{
		document.getElementById('details').scrollLeft += (scrollDir=='left') ? 10 : -10;
		if ((document.getElementById('details').scrollLeft<scrollStop  && scrollDir=='left') || (document.getElementById('details').scrollLeft>scrollStop  && scrollDir=='right')) {
 			setTimeout(scrollDisplayGo, 10);
		} else {
			document.getElementById('details').scrollLeft = scrollStop;
		}
	}
	
var scrollStop;
var scrollDir;
	
function toggleStar(i)
	{
		if (cinemaData.cinemas[i].star) {
			document.getElementById(cinemaData.cinemas[i].tid + 'Star').src = '_images/stars/star.png';
			cinemaData.cinemas[i].star = false;
		} else {
			document.getElementById(cinemaData.cinemas[i].tid + 'Star').src = '_images/stars/starGld.png';				
			cinemaData.cinemas[i].star = true;
		}
		updateTimes();
	}

function fetchFilms()
	{
		var checkCount = 0;
		for (var i=0; i<cinemaData.cinemas.length; i++) {
			if (document.getElementById(cinemaData.cinemas[i].tid).checked) { checkCount++; }
			highlightCinema(i);
		}
		if (checkCount === 0) {
			scrollDisplay(449);
		} else {
			currentCinema = -1;
			getNextCinemasFilms();
		}
	}
function getNextCinemasFilms()
	{
		if (currentCinema >= 0) {
			if (document.getElementById('cinema' + currentCinema + 'MapMarker')) { document.getElementById('cinema' + currentCinema + 'MapMarker').style.display = ''; }
			document.getElementById('cinema' + currentCinema + 'Spinner').style.display = 'none';

			var checkCount = 0;
			for (var i=0; i<cinemaData.cinemas.length; i++) {
				if (document.getElementById(cinemaData.cinemas[i].tid).checked && loadedForTIDs[currentDate].indexOf(cinemaData.cinemas[i].tid + '|') >= 0) { checkCount++; }
				highlightCinema(i);
			}
			scrollDisplay((checkCount === 0) ? 449 : 898);
		}
		currentCinema++;
		
		if (cinemaData.cinemas[currentCinema]) {
			if (document.getElementById(cinemaData.cinemas[currentCinema].tid).checked && loadedForTIDs[currentDate].indexOf(cinemaData.cinemas[currentCinema].tid + '|')==-1) {
				makeXMLRequest('_json/films.aspx', 'gDomain=' + cinemaData.gDomain + '&near=' + cinemaData.cinemas[currentCinema].near + '&tid=' + cinemaData.cinemas[currentCinema].tid + '&cinemaIndex=' + currentCinema + '&day=' + currentDate, function(r) {
					var films = JSON.parse(r).films;
					for (var i=0; i<films.length; i++) {
						var midAtIndex = -1;
						for (var j=0; j<movies.length; j++) {
							if (movies[j].mid == films[i].mid) { midAtIndex = j; }
						}
						if (midAtIndex >= 0) {
							movies[midAtIndex].times = movies[midAtIndex].times.concat(films[i].times);
						} else {
							//Create Movie Cell, and add to DOM
							var cellHTML = '<table width="100%" cellspacing="0" cellpadding="5">';
							cellHTML += '<tr>';
							cellHTML += '<td valign="top" align="right" style="cursor:pointer;" onClick="showFilmDetail(this);"><div class="posterDiv"><img src="_images/frame/defaultPoster.jpg" id="poster' + films[i].mid + '" border="0" style="width:50px;"></div><br/>';
							cellHTML += '<td valign="top" width="500" style="cursor:pointer;" onClick="showFilmDetail(this);"><span style="font-size:1.3em;">' + films[i].name + '<'+'/span><br/><div style="height:60px; overflow:auto;" id="movie' + films[i].mid + 'Times"></div>';
							cellHTML += '<td height="75" valign="top" align="center"><div style="position:relative; height:80px;"><div id="movie' + films[i].mid + 'Certificate" style="cursor:pointer;" onClick="showFilmDetail(this);">&nbsp;</div><div class="lnkBtn" style="position:absolute; bottom:0px; right:5px;" onMouseOver="lnkBtnOver(this);" onMouseOut="lnkBtnOut(this);" onMouseDown="lnkBtnDown(this); return false;" onMouseUp="lnkBtnUp(this, hideFilm);">Hide</div></div>';
							cellHTML += '<'+'/table>';
							films[i].cell = document.createElement('div');
							films[i].cell.id = films[i].mid + 'movieRow';
							films[i].cell.className = 'movieRow';
							films[i].cell.style.display = 'none';
							films[i].cell.style.height = '90px';
							films[i].cell.innerHTML = cellHTML;
							document.getElementById('filmRowHolder').appendChild(films[i].cell);

							movies.splice(movies.length, 0, films[i]);
							
							//Load IMBd Data
							makeXMLRequest('_json/imdb.aspx', 'url=' + films[i].gIMDb + '&mid=' + films[i].mid + '&name=' + films[i].name + '&country=' + ((cinemaData.locationBase.Placemark) ? cinemaData.locationBase.Placemark[0].AddressDetails.Country.CountryName : ''), function(r) {
								var imdb = JSON.parse(r);
								for (var j=0; j<movies.length; j++) {
									if (imdb.mid == movies[j].mid) {
										for (var property in imdb) { movies[j][property] = imdb[property]; }
										if (movies[j].releaseDate) { movies[j].releaseDate = new Date(movies[j].releaseDate); }
										if (movies[j].posterURL !== '') { document.getElementById('poster' + movies[j].mid).src = '_images/autoImgs/poster.aspx?src=' + movies[j].posterURL + '&mid=' + movies[j].mid; }
										if (movies[j].certificate) { document.getElementById('movie' + movies[j].mid + 'Certificate').innerHTML = '<nobr><img src="_images/certificateBadges/' + movies[j].certificateLocation + '/' + movies[j].certificate + '.gif" alt="' + movies[j].certificateLocation + ' - ' + movies[j].certificateName + '" title="' + movies[j].certificateLocation + ' - ' + movies[j].certificateName + '\n' + movies[j].certificateDescription + ((movies[j].certificateIsAverage) ? '\n\n*approximate certification based on an international average' : '') + '" align="top">' + ((movies[j].certificateIsAverage) ? '*' : '') + '</nobr>'; }
										updateInfoWindow(j);
									}
								}
							});
						}
					}
					loadedForTIDs[currentDate] += cinemaData.cinemas[currentCinema].tid + '|';
					fetchedFilms();
				});
				pageTracker._trackEvent("Listings", "Load Films", cinemaData.cinemas[currentCinema].tid);
			} else {
				getNextCinemasFilms();
			}
		} else {
			updateTimes();
			currentCinema = -1;
			if (fetchFilmsAgain) {
				fetchFilmsAgain = false;
				fetchFilms();
			}
		}
	}
var currentCinema = -1;
var fetchFilmsAgain = false;

function fetchedFilms()
	{
		sortFilms();
		getNextCinemasFilms();
	}
	
function sortFilms()
	{
		//Sort times
		for (var i=0; i<movies.length; i++) {
			movies[i].times.sort(function(a,b) {
				if (a.offsetTime < b.offsetTime) { return -1; }
				if (a.offsetTime > b.offsetTime) { return 1; }
				return 0;
			});
		}
		
		for (var i=0; i<movies.length; i++) {
			for (var j=movies[i].times.length-1; j>=0; j--) {
				movies[i].sortTime = (movies[i].times[j].offsetTime>=sliderTime && movies[i].times[j].day==currentDate) ? movies[i].times[j].offsetTime : 9999;
			}
		}
		
		//Sort films
		movies.sort(function(a,b) {
			if (sortBy == 'date') {
				if (a.releaseDate > b.releaseDate) { return -1; }
				if (a.releaseDate < b.releaseDate) { return 1; }
				return 0;
			} else {
				switch(sortBy) {
					case 'time':
						a = a.sortTime;
						b = b.sortTime;
						break;
					case 'genre':
						a = (a.genre) ? a.genre : '';
						b = (b.genre) ? b.genre : '';
						break;
					case 'certificate':
						a = (a.certificateSort) ? a.certificateSort : 0;
						b = (b.certificateSort) ? b.certificateSort : 0;
						break;
					default:
						a = a.name;
						b = b.name;
				}
				if (a < b) { return -1; }
				if (a > b) { return 1; }
				return 0;
			}
		});
		
		updateTimes();
	}
	
function updateTimes()
	{
		updateSliderTimeDisplay();

		var foundCurrentPoster = false;
		document.getElementById('filmRowHolder').style.height = '0px';
		var visibleCount = 0;
		
		for (var i=0; i<movies.length; i++) {
			var timeCount = 0;
			var timeStr = '';
			lastIsPM = null;
			for (var j=0; j<movies[i].times.length; j++) {
				if (document.getElementById(movies[i].times[j].tid)) {
					if (document.getElementById(movies[i].times[j].tid).checked && movies[i].times[j].offsetTime >= sliderTime && movies[i].times[j].day==currentDate && !movies[i].hide) {
						timeCount++;
						if (timeCount<=20) {
							if (use12Hr) { if (lastIsPM != null) { if (lastIsPM != movies[i].times[j].isPM) { timeStr += (lastIsPM) ? 'pm' : 'am'; } } }
							if (lastIsPM != null) { timeStr += ', '; }
							lastIsPM = movies[i].times[j].isPM;
							timeStr += '<span ' + ((cinemaData.cinemas[getCinemaWithId(movies[i].times[j].tid)].star) ? 'style="font-weight:bold;"' : '') + '>';
							timeStr += ((use12Hr) ? movies[i].times[j].time12Hr : movies[i].times[j].time24Hr) + '</span>';
						}
					}
				}
			}
			if (timeStr != '') {
				document.getElementById('movie' + movies[i].mid + 'Times').innerHTML = timeStr + ((use12Hr) ? ((lastIsPM) ? 'pm' : 'am') : '') + ((timeCount>20) ? ' more...' : '');
				movies[i].cell.style.top = document.getElementById('filmRowHolder').style.height = ((90*visibleCount)+((foundCurrentPoster) ? 60 : 0)) + 'px';
				movies[i].cell.index = i;
				movies[i].cell.style.display = '';
				if (movies[i].mid == currentPoster) { foundCurrentPoster = true; }
				visibleCount++;
			} else {
				movies[i].cell.style.display = 'none';
			}
			updateInfoWindowTimes(i);
		}

		var timeAsDate = new Date(); timeAsDate.setTime(((sliderTime-(sliderTime%100))*36000)+((sliderTime%100)*60000));
		document.getElementById('noFilmsError').innerHTML = (visibleCount === 0) ? 'Sorry,<br/>no films showing at<br/>the selected cinemas<br/><br/><span style="font-size:14px;">showing films starting after ' + ((!use12Hr && timeAsDate.getHours()<10) ? '0' : '') + ((use12Hr && timeAsDate.getHours()>12) ? timeAsDate.getHours()-12 : ((use12Hr && timeAsDate.getHours()==0) ? '12' : timeAsDate.getHours())) + ':' + ((timeAsDate.getMinutes()<10) ? '0' : '') + timeAsDate.getMinutes() + ((use12Hr) ? ((timeAsDate.getHours()>=12) ? 'pm' : 'am') : '') + '</span>' : '';
		
		if (!foundCurrentPoster && currentPoster != '0') {
			document.getElementById('poster' + currentPoster).style.width = '50px';
			movies[getMovieWithId(currentPoster)].cell.style.height = '90px';
			currentPoster = '0';
			shrinkPosterCell();
			closeDetails();
		}
	}
	

function updateInfoWindow(i)
	{
		if (currentPoster == movies[i].mid) {
			var w = '<table id="infoTitleTable" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top"><h2 style="margin-top:10px;">' + movies[i].name + '</h2>';
			w += '<td align="right" valign="top" style="padding-top:10px;">' + document.getElementById('movie' + movies[i].mid + 'Certificate').innerHTML;
			w += '</table>';
			if (movies[i].imdb) { if (movies[i].plot != '') { w += movies[i].plot + '<br/><br/>'; } }
			if (movies[i].rating) { if (movies[i].rating != '') {
				for (var j=0; j<5; j++) { w+= '<img src="_images/stars/star' + ((j<Math.round(movies[i].rating)) ? 'Gld' : '') + '.png" height="11" width="11" style="margin-top:3px;"> '; }
				w += '<a href="http://www.google.co.uk/movies?near=' + document.getElementById('locationFld').value + '&date=' + currentDate + '&mid=' + movies[i].mid + '" target="_blank">more...<'+'/a><br/><br/>'
			} }
			if (movies[i].imdb) { if (movies[i].genre != '') { w += movies[i].genre + '<br/><br/>'; } }
			if (movies[i].imdb) { if (movies[i].country != '') { w += movies[i].country + '<br/><br/>'; } }
			if (movies[i].imdb) { if (movies[i].imdb != '') { w += '<a href="' + movies[i].imdb + '" target="_blank">More at IMDb<'+'/a>'; } }
			if (movies[i].imdb && movies[i].gYouTube) { if (movies[i].imdb != '' && movies[i].gYouTube != '') { w += ' - '; } }
			if (movies[i].gYouTube) { if (movies[i].gYouTube != '') { w += '<a href="' + movies[i].gYouTube + '" target="_blank">Trailer<'+'/a>'; } }
			
			w += '<br/><br/><div id="infoWindowTimes"></div><br/>';
			document.getElementById('filmDetails').innerHTML = w;
			updateInfoWindowTimes(i);
		}
	}
function updateInfoWindowTimes(i)
	{
		if (currentPoster == movies[i].mid && document.getElementById('infoWindowTimes')) {
			var w = '';
			for (var j=0; j<movies[i].times.length; j++) {
				if (document.getElementById(movies[i].times[j].tid)) {
					if (document.getElementById(movies[i].times[j].tid).checked && movies[i].times[j].offsetTime >= sliderTime && movies[i].times[j].day==currentDate) {
						var cinemaIndex = getCinemaWithId(movies[i].times[j].tid);
						w += '<tr><td valign="top" align="right"><b>';
						if (movies[i].times[j].timeLink != '') w += '<a href="' + movies[i].times[j].timeLink + '" target="_blank">';
						w += (use12Hr) ? movies[i].times[j].time12Hr + ((movies[i].times[j].isPM) ? 'pm' : 'am') : movies[i].times[j].time24Hr
						if (movies[i].times[j].timeLink != '') w += '<'+'/a>';
						w += '<'+'/b><td valign="top" ' + ((cinemaData.cinemas[cinemaIndex].star) ? 'style="font-weight:bold;"' : '') + '>'
						w += '<img src="_images/cinemas/' + ((cinemaData.cinemas[cinemaIndex].icon=='') ? '_default.gif' : cinemaData.cinemas[cinemaIndex].icon) + '" height="11" style="margin-top:3px;"> ';
						w += cinemaData.cinemas[cinemaIndex].name + '<br/>';							
					}
				}
			}
			document.getElementById('infoWindowTimes').innerHTML = '<table cellpadding="2" cellspacing="0">' + w + '</table>';
		}
	}
	
function showFilmDetail(cell)
	{
		while (cell.className != 'movieRow') { cell = cell.parentNode; }
		lastPoster = currentPoster;
		shrinkPoster();
		
		if (currentPoster != movies[cell.index].mid) {
			currentPoster = movies[cell.index].mid;
			updateInfoWindow(cell.index);
			enlargePoster();
		} else {
			currentPoster = '0';
			closeDetails();
		}
		fixHeight();
		
		pageTracker._trackEvent("Listings", "Show Details", movies[cell.index].name);
	}
	
function hideInfoLink(c)
	{
		lastPoster = currentPoster;
		shrinkPoster();
		currentPoster = '0';
		closeDetails();
	}

function enlargePoster()
	{
		var startIndex = getMovieWithId(currentPoster);
		var cssRule = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules[0] : document.styleSheets[0].rules[0];
		
		if (parseInt(document.getElementById('poster' + currentPoster).style.width) < 90 || parseInt(document.getElementById('filmDetailsHolder').style.left) > 1008 || (parseInt(movies[startIndex].cell.style.height) < 150)) {
			if (parseInt(document.getElementById('filmDetailsHolder').style.left) > 1008) { document.getElementById('filmDetailsHolder').style.left = (parseInt(document.getElementById('filmDetailsHolder').style.left) - 20) + 'px'; }
			if (parseInt(document.getElementById('poster' + currentPoster).style.width) < 90) {
				document.getElementById('poster' + currentPoster).style.width = (parseInt(document.getElementById('poster' + currentPoster).style.width) + 10) + 'px';
				cssRule.style.width = (parseInt(cssRule.style.width) + 10) + 'px';
			}
			if (parseInt(movies[startIndex].cell.style.height) < 150) {
				movies[startIndex].cell.style.height = (parseInt(movies[startIndex].cell.style.height) + 10) + 'px';
				for (var i=startIndex+1; i<movies.length; i++) {
					if (movies[i]) { movies[i].cell.style.top = (parseInt(movies[i].cell.style.top) + 10) + 'px'; }
				}
			}
			setTimeout(enlargePoster, 10);
		} else {
			cssRule.style.width = '100px';
			document.getElementById('filmDetailsHolder').style.left = '1008px';
			document.getElementById('poster' + currentPoster).style.width = '90px';
		}
	}
function shrinkPoster()
	{
		var cssRule = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules[0] : document.styleSheets[0].rules[0];

		if (document.getElementById('poster' + lastPoster)) {
			var startIndex = getMovieWithId(lastPoster);
			if (parseInt(document.getElementById('poster' + lastPoster).style.width) > 50 || parseInt(movies[startIndex].cell.style.height) > 90) {
				if (parseInt(document.getElementById('poster' + lastPoster).style.width) > 50) {
					document.getElementById('poster' + lastPoster).style.width = (parseInt(document.getElementById('poster' + lastPoster).style.width) - 10) + 'px';
					cssRule.style.width = (parseInt(cssRule.style.width) - 10) + 'px';
				}
				if (parseInt(movies[startIndex].cell.style.height) > 90) {
					movies[startIndex].cell.style.height = (parseInt(movies[startIndex].cell.style.height) - 10) + 'px';
					for (var i=startIndex+1; i<movies.length; i++) {
						movies[i].cell.style.top = (parseInt(movies[i].cell.style.top) - 10) + 'px';
					}
				}
				setTimeout(shrinkPoster, 10);
			}
		}
	}
function shrinkPosterCell()
	{
		var cssRule = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules[0] : document.styleSheets[0].rules[0];

		if (parseInt(cssRule.style.width) > 60) {
			cssRule.style.width = (parseInt(cssRule.style.width) - 10) + 'px';
			setTimeout(shrinkPosterCell, 10);
		} else {
			cssRule.style.width = '60px';
		}
	}
function closeDetails()
	{
		if (parseInt(document.getElementById('filmDetailsHolder').style.left) < 1347) {
			document.getElementById('filmDetailsHolder').style.left = (parseInt(document.getElementById('filmDetailsHolder').style.left) + 20) + 'px';
			setTimeout(closeDetails, 10);
		} else {
			document.getElementById('filmDetailsHolder').style.left = '1347px';
		}
	}
var currentPoster = '0';
var lastPoster = null;

	
function getCinemaWithId(tid)
	{
		for (var i=0; i<cinemaData.cinemas.length; i++) {
			if (cinemaData.cinemas[i].tid == tid)
				return i;
		}
		return '';
	}
function getMovieWithId(mid)
	{
		for (var i=0; i<movies.length; i++) {
			if (movies[i].mid == mid)
				return i;
		}
		return '';
	}
	
var loadedForTIDs = new Array('', '');


function openOptsMenu() { clearTimeout(closeOptsMenuTimeout); document.getElementById('optsMenu').style.display = ''; btnAnimateOver(document.getElementById('optsBtn')); }
function closeOptsMenu() { closeOptsMenuTimeout = setTimeout(closeOptsMenuNOW, 300); }
function closeOptsMenuNOW() { document.getElementById('optsMenu').style.display = 'none'; btnAnimateOut(document.getElementById('optsBtn')); }
var closeOptsMenuTimeout = null;
function toggle12Hr() {
	closeOptsMenuNOW();
	use12Hr = (use12Hr) ? false : true;
	document.getElementById('lbl12Hr').innerHTML = (use12Hr) ? 'Use 24 Hour Times' : 'Use 12 Hour Times'
	updateTimes();
	pageTracker._trackEvent("Listings", "Toggle 12 Hour");
}
function changeSort(menuItem) {
	closeOptsMenuNOW();
	document.getElementById(sortBy + 'SortMenuItem').className = 'menuItem';
	menuItem.className = 'menuItem ticked';
	sortBy = menuItem.id.slice(0, menuItem.id.length-12);
	sortFilms();
	pageTracker._trackEvent("Listings", "Change Sort", sortBy);
}


function hideFilm(cell)
	{
		if (cell.id == 'filmDetailsHideLnk') {
			cell = document.getElementById(currentPoster + 'movieRow')
		} else {
			while (cell.className != 'movieRow') { cell = cell.parentNode }
		}
		var poster = document.getElementById('poster' + movies[cell.index].mid);
		var table = document.getElementById('filmTable');
		var cssRule = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules[0] : document.styleSheets[0].rules[0];
		var flyingPoster = document.getElementById('flyingPoster')

		flyingPoster.src = poster.src
		flyingPoster.style.width = parseInt(poster.style.width) + 'px';
		flyingPoster.style.bottom = parseInt(table.style.height)-(((parseInt(cell.style.top)-table.scrollTop)+parseInt(cell.style.height))-33) + 'px';
		flyingPoster.style.right = 449-parseInt(cssRule.style.width)-5 + 'px';
			
		var animationSteps = (parseInt(flyingPoster.style.right)-animateHidePosterEnd.right)/animateHidePosterRate.right;
		animateHidePosterRate.bottom = (parseInt(flyingPoster.style.bottom)-animateHidePosterEnd.bottom)/animationSteps;
		animateHidePosterRate.width = (parseInt(flyingPoster.style.width)-animateHidePosterEnd.width)/animationSteps;

		flyingPoster.style.display = '';
		poster.style.display = 'none';
		movies[cell.index].hide = true;
		animateHidePoster();

		pageTracker._trackEvent("Listings", "Hide Film", movies[cell.index].name);
	}

function animateHidePoster()
	{
		var flyingPoster = document.getElementById('flyingPoster')
		if (parseInt(flyingPoster.style.bottom)>animateHidePosterEnd.bottom && parseInt(flyingPoster.style.right)>animateHidePosterEnd.right && parseInt(flyingPoster.style.width)>animateHidePosterEnd.width) {
			flyingPoster.style.width = (parseInt(flyingPoster.style.width)-animateHidePosterRate.width) + 'px';
			flyingPoster.style.bottom = (parseInt(flyingPoster.style.bottom)-animateHidePosterRate.bottom) + 'px';
			flyingPoster.style.right = (parseInt(flyingPoster.style.right)-animateHidePosterRate.right) + 'px';
			setTimeout(animateHidePoster, 10);
		} else {
			flyingPoster.style.width = animateHidePosterEnd.width + 'px';
			flyingPoster.style.bottom = animateHidePosterEnd.bottom + 'px';
			flyingPoster.style.right = animateHidePosterEnd.right + 'px';
			
			document.getElementById('unhideMenuBtn').src = flyingPoster.src;
			document.getElementById('unhideMenuBtn').style.display = '';
			flyingPoster.style.display = 'none';
			
			updateTimes();
		}
	}
	
var animateHidePosterRate = new Object({ bottom:0, right:30, width:0 });
var animateHidePosterEnd = new Object({ bottom:3, right:83, width:13 });


function openUnhideMenu() { clearTimeout(closeUnhideMenuTimeout); document.getElementById('unhideMenu').style.display = ''; btnAnimateOver(document.getElementById('unhideMenuBtn')); }
function closeUnhideMenu() { closeUnhideMenuTimeout = setTimeout(closeUnhideMenuNOW, 300); }
function closeUnhideMenuNOW() { document.getElementById('unhideMenu').style.display = 'none'; btnAnimateOut(document.getElementById('unhideMenuBtn')); document.getElementById('hiddenFilmList').style.width = ''; document.getElementById('unhideMenu').style.height = ''; }
var closeUnhideMenuTimeout = null;
	
function unhideFilm()
	{
		var filmsList = document.getElementById('hiddenFilmList');
		
		//Build Menu
		filmsList.innerHTML = '';
		for (var i=0; i<movies.length; i++) {
			if (movies[i].hide) {
				filmsList.innerHTML += '<div class="menuItem" style="" onClick="unhideFilmWithMid(\'' + movies[i].mid + '\');"><img src="' + document.getElementById('poster' + movies[i].mid).src + '" width="12" style="vertical-align:-3px;" /> ' +  movies[i].name + '</div>';
			}
		}
		openUnhideMenu();
		if (filmsList.offsetWidth > 250) { filmsList.style.width = '250px';	}
		if (document.getElementById('unhideMenu').offsetHeight > parseInt(document.getElementById('filmDetailsInnerHolder').style.height)) { document.getElementById('unhideMenu').style.height = document.getElementById('filmDetailsInnerHolder').style.height; }
	}
	
function unhideFilmWithMid(mid)
	{
		closeUnhideMenuNOW();
		for (var i=0; i<movies.length; i++) {
			if (movies[i].mid == mid) {
				movies[i].hide = false;
				if (document.getElementById('hiddenFilmList').childNodes.length == 1) {
					document.getElementById('unhideMenuBtn').style.display = 'none';
				} else {
					if (document.getElementById('unhideMenuBtn').src == document.getElementById('poster' + movies[i].mid).src) {
						for (var j=0; j<movies.length; j++) {
							if (movies[j].hide) { document.getElementById('unhideMenuBtn').src = document.getElementById('poster' + movies[j].mid).src; }
						}
					}
				}
				document.getElementById('poster' + movies[i].mid).style.display = '';
				updateTimes();
				pageTracker._trackEvent("Listings", "Unhide Film", movies[i].name);
			}
		}
	}
	
function unhideAllFilms()
	{
		closeUnhideMenuNOW();
		for (var i=0; i<movies.length; i++) {
			if (movies[i].hide) {
				movies[i].hide = false;
				document.getElementById('poster' + movies[i].mid).style.display = '';
			}
		}
		document.getElementById('unhideMenuBtn').style.display = 'none';
		updateTimes();
		pageTracker._trackEvent("Listings", "Unhide All Films");
	}




//XML
function newXMLRequest() {
	var xmlRequest = false;
	if(window.XMLHttpRequest) {
		try {
			xmlRequest= new XMLHttpRequest();
		} catch(e) {
			xmlRequest= false;
		}
	} else if(window.ActiveXObject) {
		try {
			xmlRequest= new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlRequest= new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlRequest= false;
			}
		}
	}
	return xmlRequest;
}

function makeXMLRequest(address, params, callback) {
	var xmlRequest = newXMLRequest();
	if(xmlRequest) {
		xmlRequest.onreadystatechange = function() {
			if (xmlRequest.readyState == 4) {
				if (xmlRequest.status == 200) {
					callback(xmlRequest.responseText);
				}
			}
		}
		xmlRequest.open('POST', address, true);
		xmlRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlRequest.send(params + ((params!='') ? '&' : '') + 't=' + new Date().valueOf());
	}
}



//Global Data
var cinemaData;
var movies = new Array();



//*** Buttons ***//
function goBtnDown() { goBtnAnimateDown(); downOverBtn = true; }
function goBtnOver() { if (downOverBtn) { goBtnAnimateDown(); } }
function goBtnOut() { goBtnAnimateUp(); }
function goBtnUp() { if (downOverBtn) { goBtnAnimateUp(); fetchCinemas(); } }
function goBtnAnimateDown() { document.getElementById('goBtn').style.backgroundImage = "url('_images/frame/goBtnDown.png')"; }
function goBtnAnimateUp() { document.getElementById('goBtn').style.backgroundImage = ''; }
document.onmouseup = function() { downOverBtn = false; }
document.onmousemove = function() { if (downOverBtn) { return false; } }

function btnOver(btn) { if (!downOverBtn) { btnAnimateOver(btn); } else { btnDown(btn); } }
function btnOut(btn) { btnAnimateOut(btn); }
function btnDown(btn) { btnAnimateClick(btn); downOverBtn = true; }
function btnUp(btn, action) { if (downOverBtn) { btnAnimateOver(btn); action(); } }
function btnAnimateOut(btn) { btn.style.border = ''; btn.style.padding = ''; }
function btnAnimateOver(btn) { btn.style.borderTop = '1px solid #999999'; btn.style.borderLeft = '1px solid #999999'; btn.style.borderRight = '1px solid #000000'; btn.style.borderBottom = '1px solid #000000'; btn.style.padding = '2px 3px'; }
function btnAnimateClick(btn) { btn.style.borderTop = '1px solid #000000'; btn.style.borderLeft = '1px solid #000000'; btn.style.borderRight = '1px solid #999999'; btn.style.borderBottom = '1px solid #999999'; btn.style.padding = '3px 2px 1px 4px'; }

function lnkBtnOver(lnk) { if (!downOverBtn) { lnkBtnAnimateOver(lnk); } else { lnkBtnDown(lnk); } }
function lnkBtnOut(lnk) { lnkBtnAnimateOut(lnk); }
function lnkBtnDown(lnk) { lnkBtnAnimateClick(lnk); downOverBtn = true; }
function lnkBtnUp(lnk, action) { if (downOverBtn) { lnkBtnAnimateOver(lnk); action(lnk); } }
function lnkBtnAnimateOut(lnk) { lnk.style.color = ''; lnk.style.textDecoration = ''; }
function lnkBtnAnimateOver(lnk) { lnk.style.color = '#0000FF'; lnk.style.textDecoration = 'underline'; }
function lnkBtnAnimateClick(lnk) { lnk.style.color = '#FF0000'; lnk.style.textDecoration = 'underline'; }

var downOverBtn = false;