	$(document).ready(function() {
		// set the global array for lat & lon
		cinema_listings_lat_lon = '';
		
		// darken the images, disable the select box and disable the radio buttons
		if ($('#film_location') && $('#cinema_listings_users_lat').val() == '') {
			disable_cinema_listings();
			
			// add the Enter town or postcode string
			if ($('#film_location').val() == '') {
				$('#film_location').val('Enter town or postcode');
				// ghost it
				$('#film_location').css('color','#AFAFAF');
			}
		}
		
		// on focus of the film location, remove the default value
		$('#film_location').bind('focus click change', function(e){
			if ($('#film_location').val() == 'Enter town or postcode') {
				$('#film_location').val('');
				$('#film_location').css('color', '#505050');
			}
			
			// disable the submit so that when the user presses enter the geo lookup is done
			//$('div#cinema_listings_search_block .content input[type=submit]').attr('disabled', 'disabled');
		});
		
		/**
		 * check to see if the user has pressed enter when entering their location
		 * if yes, then do a ajax request to get the lat and lon
		 */
		$('#film_location').bind('keyup keypress',function(e) {
			if ($(this).val().length >=3 && $(this).val() != 'Enter town or postcode') {
				// check to see if the user pressed enter
				var code = (e.keyCode ? e.keyCode : e.which);

				if (code==13) {
					yql_ajax();
					return false;
				}
			}
		});
		
		
		/**
		 * check the film title selected.
		 * if it is the chart films or other films then disable form submission and produce an error message
		 */
//		$('#film_title').change(function(e){
//			if ($(this).val() == '') {
//				disable_cinema_listings(false);
//			} else {
//				enable_cinema_listings(false);
//			}
//			return false;
//		});
		
		/**
		 * check the cinema chains selected. we don't want user to search if they select no chains
		 */
		$('#cinema_listings_search input[type=checkbox]').click(function() {
			var selectedItems = new Array();
			$("input[@name='cinema_listings_venues[]']:checked").each(function(e) {
				selectedItems.push($(this).val());
			});

			if (selectedItems.length == 0) {
				// all the chains have been de selected, so darken and disable the select box
				disable_cinema_listings(false);				
			} else {
				// we have one or more chains selected to enable the search 
				enable_cinema_listings(false);
			}

		});

		$('#update_geo').click(function(e) {
			if ($('#film_location').val().length <= 0) {
				disable_cinema_listings();
			}

			// once the user enters a location, do a geocode look up for it
			if ($('#film_location').val().length >= 3 && $('#film_location').val() != 'Enter town or postcode') {				
				yql_ajax();
			} // end if			
		});

		$('div.cinema_multi_locations').delegate("p", "click", function() {
			// set the hidden vars lat and long
			$('#cinema_listings_users_lat').val(cinema_listings_lat_lon[$(this).html()]['lat']);
			$('#cinema_listings_users_lon').val(cinema_listings_lat_lon[$(this).html()]['lon']);

			// update the parent div with new location
			$('#film_location').val(cinema_listings_lat_lon[$(this).html()]['name_county']);

			// hide the multi location div
			$('.cinema_multi_locations').hide();
			
			// un darken the locations and show results 
			enable_cinema_listings();
		});
		
		
		/**
		 * multiple showing times for the same film,
		 * this shall switch between the showing times
		 */
		$('#bfcl_film_title').change(function() {
			// get the new value
			var film_e_id = $('#bfcl_film_title').val();
			
			// get all the children values and hide them
			$("#bfcl_film_title option").each(function() {
				$('#'+$(this).val()).hide();
			});
			
			// show the one that the user has selected
			$('#'+$('#bfcl_film_title').val()).show();			
		})
		
		function yql_ajax() {
			var userLocation = $('#film_location').val();
			//var geocode_url = 'http://test.3epp.com/navigator3e/standard/Content/getLocation.do';
			var geocode_url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'+userLocation+'" and country IN("United Kingdom", "Ireland") and placeTypeName != "Local Administrative Area"&format=json&callback=?';
	        $.ajax({
				url: geocode_url,
				type: 'GET',
				//data: {name: userLocation},
				dataType: 'jsonp',
				success: parseUserGeocode  
	        });			
		}
		
		function disable_cinema_listings(all) {

			if (all == false) {
				// we only want to disable the submit button
				$('div#cinema_listings_search_block .content .search_btn').addClass("darken_image");
				$('div#cinema_listings_search_block .content input[type=submit]').attr('disabled', 'disabled');
				return true;
			} else {
				$('div#cinema_listings_search_block .content .cinema_stack').hide();
				$('div#cinema_listings_search_block .content .search_btn').hide();
				$('div#cinema_listings_search_block .content #film_dates').hide();
				$('div#cinema_listings_search_block .content #bfcl_srch_when').hide();
				$('div#cinema_listings_search_block .content #bfcl_srch_which').hide();
				$('div#cinema_listings_search_block .content .cinema_stack .cinema_location :input').attr('disabled', true);
				$('div#cinema_listings_search_block .content #film_dates').attr('disabled', true);
				$('div#cinema_listings_search_block .content input[type=submit]').attr('disabled', 'disabled');
				$('div#cinema_listings_search_block .content input[type=submit]').attr('disabled', 'disabled');
			}
		}
		
		function enable_cinema_listings(all) {
			if (all == false) {
				// work out how many cinema chains have been selected
				var selectedItems = new Array();
				$("input[@name='cinema_listings_venues[]']:checked").each(function(e) {
					selectedItems.push($(this).val());
				});
				
				// we only want to enable the submit button
				if ($('#film_title').val() != '' && selectedItems != '') {
					$('div#cinema_listings_search_block .content .search_btn').removeClass("darken_image");
					$('div#cinema_listings_search_block .content #film_location').show();
					$('div#cinema_listings_search_block .content input[type=submit]').removeAttr('disabled', 'disabled');
				}
				return true;
			} else {
				$('div#cinema_listings_search_block .content .cinema_stack').show();
				$('div#cinema_listings_search_block .content .search_btn').show();
				$('div#cinema_listings_search_block .content #film_dates').show();
				$('div#cinema_listings_search_block .content #bfcl_srch_when').show();
				$('div#cinema_listings_search_block .content #bfcl_srch_which').show();
				$('div#cinema_listings_search_block .content .cinema_stack .cinema_location :input').removeAttr('disabled');
				$('div#cinema_listings_search_block .content #film_dates').removeAttr('disabled');
				
				if ($('#film_title').val() != '') {
					// only enable submit if the film title != ''
					$('div#cinema_listings_search_block .content input[type=submit]').removeAttr('disabled', 'disabled');
				}
			}
		}

		function parseUserGeocode(response) {
			var html = '';
	
			if (response.query.results == null) {
				bfcl_srch_word = $('#film_location').val();
				html = '<h5>Sorry but "'+bfcl_srch_word+'" was not found</h5>';
				
				// add the error message to the div
				$('.cinema_multi_locations').html(html);
				// show the multi locations div 
				$('.cinema_multi_locations').show();
				
				disable_cinema_listings();
				
				return false;
			}
			

			if (response.query.results.place.length > 0) {
				// multiple results returned, so hide the search function until a location is set
				disable_cinema_listings();		
				
				
				window.cinema_listings_lat_lon = new Array();
				
				$.each(response.query.results.place, function() {
					var geo_name		= this.name;
					var geo_town		= this.locality1.content;
					var geo_county		= this.admin2.content;
					var geo_country		= this.admin1.content;
					var geo_woeid		= this.woeid;
					var geo_latitude	= this.centroid.latitude;
					var geo_longitude	= this.centroid.longitude;
					var approxLatitude	= this.centroid.latitude;
					var approxLongitude	= this.centroid.longitude;

					html += '<p>' + geo_name + ', ' + geo_county + '</p>';

					// insert data into global lat lon array
					window.cinema_listings_lat_lon[geo_name+', '+geo_county] = new Array();
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['name_county'] 	= geo_name+', '+geo_county;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['name'] 			= geo_name;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['town'] 			= geo_town;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['county'] 			= geo_county;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['woeid'] 			= geo_woeid;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['lat'] 			= geo_latitude;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['lon'] 			= geo_longitude;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['alat'] 			= approxLatitude;
					window.cinema_listings_lat_lon[geo_name+', '+geo_county]['alon'] 			= approxLongitude;
					
				});

				// add the multi locations to the div
				$('.cinema_multi_locations').html(html);
				// show the multi locations div 
				$('.cinema_multi_locations').show();
				return true;
			} else {
				// its a single result, so make sure all vars are reset and the lat and lon is set
				$('.cinema_multi_locations').hide();
				$('#cinema_listings_users_lat').val(response.query.results.place.centroid.latitude);
				$('#cinema_listings_users_lon').val(response.query.results.place.centroid.longitude);
				
				// update the where input with the returned info
				html = response.query.results.place.name+', '+response.query.results.place.admin2.content;
				$('#film_location').val(html);
				
				enable_cinema_listings();
				return true;
			} // end if (response.query.results.place.length > 0)
			return true;
		} // end function		
	});
