var map;
var directions;
var dd_div;

window.addEvent('domready', function () {
	var inputs = $ES('input[type="text"], input[type="password"]');
	for (var i = 0; i < inputs.length; i++) {
		inputs[i].addEvent('focus', function () {
			this.style.borderColor = '#a1a1a1';
		});
		inputs[i].addEvent('blur', function () {
			this.style.borderColor = '#666';
		});
	}
	
	var buttons = $ES('.button');
	for (var i = 0; i < buttons.length; i++) {
		buttons[i].addEvent('mouseover', function () {
			$(this).addClass('light');
		});
		buttons[i].addEvent('mouseout', function () {
			$(this).removeClass('light');
		});
	}
	
	var rows = $ES('#guest_list table tbody tr');
	rows.extend($ES('#timetable table tbody tr'));
	for (var i = 0; i < rows.length; i++) {
		rows[i].addEvent('mouseover', function () {
			$(this).addClass('light');
		});
		rows[i].addEvent('mouseout', function () {
			$(this).removeClass('light');
		});
	}
	
	var gifts = $ES('.gift_table td.item')
	for (var i = 0; i < gifts.length; i++) {
		var button = $E('.more_details', gifts[i]);
		var details = $E('.gift_details', gifts[i]);
		button.slide = new Fx.Slide(details);
		button.slide.hide();
		
		
		button.addEvent('click', function (e) {
			var event = new Event(e);
			this.slide.toggle();
			if (this.slide.open) {
				this.firstChild.innerHTML = "More details";
			} else {
				this.firstChild.innerHTML = "Hide details";
			}
			event.stop();
		});
	}
	
	var countdown = new Countdown(new Date("May 24, 2008 13:30:00"));
	countdown.updateCount();
	
	var checkboxes = $ES('.checkbox_select input');
	for (var i = 0; i < checkboxes.length; i++) {
		checkboxes[i].addEvent('click', function () {
			if (this.checked) {
				$(this.parentNode.parentNode).addClass('selected');
			} else {
				$(this.parentNode.parentNode).removeClass('selected');
			}
		});
	}
	
	var map_div = $('map');
	if (map_div && GBrowserIsCompatible()) {
		hotel = new GLatLng(54.603531, -1.630440);
		map_div.style.width = "100%";
		map_div.style.height = "400px";
		map = new GMap2(map_div);
		map.setCenter(hotel, 10);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		var marker = new GMarker(hotel);
		map.addOverlay(marker);
		//marker.openInfoWindowHtml("Redworth Hall Hotel");
		
		var gdd = $('get_driving_directions');
		if (gdd) {
			dd_div = $('driving_directions');
			dd_div.innerHTML = '';
			directions = new GDirections(map, dd_div);
			
			var e = new Element('input', {'type': 'button', 'value': 'Clear', 'id': 'clear_button'});
			e.injectTop(gdd);
			e.disabled = true;
			e.addEvent('click', function () {
				directions.clear();
				map.setCenter(hotel, 10);
				this.disabled = true;
			});
			e.injectTop(gdd);
			
			var e = new Element('input', {'type': 'button', 'value': 'Get Directions'});
			e.injectTop(gdd);
			e.addEvent('click', function () {
				var from = $('directions_from').value;
				if (from) {
					var geocoder = new GClientGeocoder();
					geocoder.getLatLng(from, function(point) {
						if (!point) {
							alert(from + " not found");
							$('clear_button').disabled = true;
						} else {
							dd_div.innerHTML = '';
							directions.clear();
							directions.load(point.lat() + "," + point.lng() + " to " + hotel.lat() + "," + hotel.lng());
							map.setCenter(point, 10);
							$('clear_button').disabled = false;
						}
					});
				}
			});
			
			var e = new Element('input', {'type': 'text', 'id': 'directions_from'});
			if (postcode) {
				e.value = postcode;
			}
			e.injectTop(gdd);
		}
	}
});

var Countdown = new Class({
	
	initialize: function(eventDate) {
		this.eventDate = eventDate;
		this.count = 0;
		this.startTimer();
	},
	
	updateCount: function() {
		var now   = new Date();
		var count = Math.floor((this.eventDate.getTime()-now.getTime())/1000);
		var secs  = this.formatInt(count%60);
		count = Math.floor(count/60);
		var mins  = this.formatInt(count%60);
		count=Math.floor(count/60);
		var hours = this.formatInt(count%24);
		count=Math.floor(count/24);
		var days  = count;
		
		if(count<=0) {
			$('countdown').innerHTML = "Och dear...that went and happened...";
		} else {
			$('countdown').innerHTML = "<div class=\"countdown\">" + days + " Days, " +hours+" Hours, "+mins+" Minutes, "+secs+" Seconds until the big day!</div>";
		}
	},
	
	startTimer: function() {
		this.periodicalTimer = this.updateCount.periodical(500, this);
	},
	
	formatInt: function(n) {
		if(n<10) return "0"+n.toString();
		else     return n.toString();
	}
});
