$(document).ready(function() {

	/// need to change the way this works, so that if 1 is the setting then clicking on another cancells the original
	var limit = 1;
	$('span').text(limit);
	
	/// select parking by default
	//$('dl').eq(0).addClass("on");
	//$('dl').eq(0).find('dt').find('input').attr("checked", "checked");
	populate('parking');
	$('dt').hide();
	
	// when someone clicks on the dl
	$('dl').bind('click', function() {
		
		/// if limit = 1 then run a little different
		if(limit == 1) {
		
			/// check to see if it has already been clicked
			var status = $(this).find('dt').find('input').attr("checked");
			if(status == true) {
			
				/// it has already been clicked, therefore empty it
				var id  = $(this).find('dt').find('input').attr("name");
				$(this).removeClass("on");
				$(this).find('dt').find('input').attr("checked", "");	
				clear();
			} else {
			
				/// it hasn't been clicked, so empty out all the data and then load this one
				clear();
				var id  = $(this).find('dt').find('input').attr("name");
				$(this).addClass("on");
				$(this).find('dt').find('input').attr("checked", "checked");
				populate('parking');
				populate(id);
			}
		} else {
		
			/// limit more than 1	
		}
	});

//

//
	
	/// function clear simply empties out all the data and removes any clicked states on the dl
	function clear() {
	
		$('#import').empty();
		$('#mapareas').empty();
		
		$('dl').removeClass("on");
		$('input').attr("checked", "");
	
	}
	
	/// function populate now takes the id and runs with it
	function populate(id) {
	
		/// first empty 'import' and the map areas
		$('#import').empty();
		$('#mapareas').empty();
		
		/// get the images first
		$.ajax({
			type : "GET",
			url  : "pages/" + id + "_image.htm",
			dataType : "html",
			success: function(html) {				
				$('#import').append(html);									
			}, error: function(){ 
				//alert('Page not found!'); 
			}
		
		});
		
		/// now get the areas
		$.ajax({
			type : "GET",
			url  : "pages/" + id + "_areas.htm",
			dataType : "html",
			success: function(html) {				
				$('#mapareas').append(html);									
			}, error: function(){ 
				// alert('Page not found!'); 
			}
		
		});
		
	}
	
	/// at this point we need to get the titles (if it isn't parking) so that there generated from the db
	$('area').live("hover", function() {
		
		var zone = $(this).attr('href');
	
		/// click on area and import xml from bid members
		/// needs to be re-coded for live
		
		if(zone != '' && zone != '#') {
			/// send an ajax request to get the data
			$.ajax({
				type : "GET",
				cache: false,
				url  : "../bid_members.asp",
				data : "zone=" + zone,
				dataType : "xml",
				success: function(xml) {
							
					//// for display box
					$(xml).find('name').each(function() { name = $(this).text(); });
						
					//alert(name);
						
					//// update the display box with the data
					$(this).find('p').text(name + "!");
																	
				}, error: function(){ alert('Failed for some reason'); }
			});
		}


	});
	
	/// click on area - get xml stuff
		$('area').live("click", function() {
			
			$('#displaybox').hide();
		
			var zone = $(this).attr('href');
		
			/// click on area and import xml from bid members
			/// needs to be re-coded for live
			
			if(zone != '' && zone != '#') {
				/// send an ajax request to get the data
				$.ajax({
					type : "GET",
					cache: false,
					url  : "../bid_members.asp",
					data : "zone=" + zone,
					dataType : "xml",
					success: function(xml) {
					
						$('#displaybox').show('slow');
		
						/// the amount of businesses returned, should only be 1
						var amount = $(xml).find('name').length;
						//alert(amount + " Businesses returned");
						
						if(amount != 1) {
						
							// alert('Error, either 0 businesses, or more than 1 business returned.');
						
						} else {
						
							//// for display box
							$(xml).find('name').each(function() { name = $(this).text(); });
							$(xml).find('email').each(function() { email = $(this).text(); });
							
							$(xml).find('address_1').each(function() { address_1 = $(this).text(); });
							$(xml).find('address_2').each(function() { address_2 = $(this).text(); });
							$(xml).find('address_3').each(function() { address_3 = $(this).text(); });
							$(xml).find('town').each(function() { town = $(this).text(); });
							$(xml).find('postcode').each(function() { postcode = $(this).text(); });
							$(xml).find('tel_day').each(function() { tel_day = $(this).text(); });
							$(xml).find('tel_evening').each(function() { tel_evening = $(this).text(); });
							$(xml).find('description').each(function() { description = $(this).text(); });
							$(xml).find('open_times').each(function() { open_times = $(this).text(); });
							$(xml).find('website').each(function() { website = $(this).text(); });
							$(xml).find('image_url').each(function() { img = $(this).text(); });
							
							open_times = open_times.replace(/\n/g, "<br />");
							
							// alert(open_times);
							// alert(img);
							
							if(img == '') {
								img = 'http://swindontowncentre.co.uk/map_sw/images/popup/img_cs.jpg';
							}
							
							var theimage = "<img src='" + img + "' alt='The Image' id='displaybox_img'/>";
							
							//// update the display box with the data
		
							$('#displaybox').find('h3').text(name);
							$('#displaybox').find('p').text(description);
							$('#displaybox').find('p').prepend(theimage);
							$('#displaybox').find('h4').eq(0).html('Address: <br />' + address_1 + ' ' + address_2 + ',<br /> ' + town + ',<br /> ' + postcode + '<br />Tel: ' + tel_day);
							$('#displaybox').find('h4').eq(1).html('Opening Times:<br /> ' + open_times);
							if(website != '') {
								$('#displaybox').find('h5').html("<a href='" + website + "' target='_blank'>> website</a>");
							} else {
								$('#displaybox').find('h5').remove();
							}
					
						}
																		
					}, error: function(){ alert('Failed for some reason'); }
				});
			} else {
				
				/// what is it (rel)
				var type = $(this).attr('rel');
				
				///information
				if(type == 'bus') {
					var theimage = "<img src='http://swindontowncentre.co.uk/map_sw/images/busstation.jpg' alt='The Image' id='displaybox_img'/>";
					$('#displaybox').show('slow');
					$('#displaybox').find('h3').text("Swindon Bus Station");
					$('#displaybox').find('p').html("Swindon's main bus station, just a minute's walk from The Parade Shopping Centre. <br /><br />Bus services are provided by primarily Thamesdown Transport and Stagecoach. <br /><br />Please click on the links below for relevant online bus information.");
					$('#displaybox').find('p').prepend(theimage);
					$('#displaybox').find('h4').hide();
					$('#displaybox').find('h5').html("<a href='http://www.thamesdown-transport.co.uk/' target='_blank'>http://thamesdown-transport.co.uk/</a> <a href='http://www.stagecoachbus.com' target='_blank'>http://www.stagecoachbus.com</a>");
				}
				
				if(type == 'rail') {
					var theimage = "<img src='http://swindontowncentre.co.uk/map_sw/images/trainstation.jpg' alt='The Image' id='displaybox_img'/>";
					$('#displaybox').show('slow');
					$('#displaybox').find('h3').text("Swindon Train Station");
					$('#displaybox').find('p').html("On the main line between London and Bristol.<br /><br />Just 2 minute's walk from Swindon town centre itself.");		
					$('#displaybox').find('p').prepend(theimage);
					$('#displaybox').find('h4').hide();
					$('#displaybox').find('h5').html("<a href='http://www.firstgreatwestern.co.uk/' target='_blank'>> Website</a>");
				}
				
				if(type == 'wyvern') {
					var theimage = "<img src='http://swindontowncentre.co.uk/map_sw/images/wyverntheatre.jpg' alt='The Image' id='displaybox_img'/>";
					$('#displaybox').show('slow');
					$('#displaybox').find('h3').text("Wyvern Theatre");
					$('#displaybox').find('p').html("Swindon's main theatre which includes the Circle Restaurant.");		
					$('#displaybox').find('p').prepend(theimage);
					$('#displaybox').find('h4').hide();
					$('#displaybox').find('h5').html("<a href='http://www.wyverntheatre.org.uk/' target='_blank'>> Website</a>");
				}
				if(type == 'hydro') {
					var theimage = "<img src='http://swindontowncentre.co.uk/map_sw/images/hydro.jpg' alt='The Image' id='displaybox_img'/>";
					$('#displaybox').show('slow');
					$('#displaybox').find('h3').text("Health Hydro");
					$('#displaybox').find('p').html("Swindon's first indoor swimming pool, opened in 1891. Also offering fitness facilities and a Turkish bath.");		
					$('#displaybox').find('p').prepend(theimage);
					$('#displaybox').find('h4').hide();
					$('#displaybox').find('h5').html("<a href='http://www.swindon.gov.uk/leisuresport/hydro.htm' target='_blank'>> Website</a>");
				}
				if(type == 'library') {
					var theimage = "<img src='http://swindontowncentre.co.uk/map_sw/images/library.jpg' alt='The Image' id='displaybox_img'/>";
					$('#displaybox').show('slow');
					$('#displaybox').find('h3').text("Town library");
					$('#displaybox').find('p').html("Swindon's Town Centre is home to one of the most modern libraries in the whole of the UK - and truly a library for the 21st century.");		
					$('#displaybox').find('p').prepend(theimage);
					$('#displaybox h4').eq(0).hide();
					$('#displaybox h4').eq(1).html("Opening Times:<br /><br />Monday: 9:30am-7:00pm<br />Tuesday: 9:30am-7:00pm<br />Wednesday: 9:30am-7:00pm<br />Thursday: 9:30am-7:00pm<br />Friday: 9:30am-7:00pm<br />Saturday: 9:30am-4:00pm<br />Sunday: 11:00am-3:00pm");
					$('#displaybox').find('h5').html("<a href='http://www.swindon.gov.uk/leisuresport/libraries/libraries-libraries/libraries-central.htm' target='_blank'>> Website</a>");
				}
				if(type == 'townhall') {
					var theimage = "<img src='http://swindontowncentre.co.uk/map_sw/images/townhall.jpg' alt='The Image' id='displaybox_img'/>";
					$('#displaybox').show('slow');
					$('#displaybox').find('h3').text("Swindon's Town Hall");
					$('#displaybox').find('p').html("Swindon's former Town Hall, now the home of Swindon Dance.");		
					$('#displaybox').find('p').prepend(theimage);
					$('#displaybox').find('h4').hide();
					$('#displaybox').find('h5').html("<a href='http://www.swindondance.org.uk/' target='_blank'>> Website</a>");
				}
				
				// 
				
			}
			
			return false;
		});
		
		/// click on bus station
		
		/// close button on popup
		$('#displaybox_close').bind('click', function() {
			$(this).parent('div').hide('slow');
		});

});

