var Finchley = {
	initialize: function() {
		$(function(){
			if ($('#home-slideshow').size()) Finchley.Slideshow.loadPhotos('#home-slideshow ul', '/data/slideshow.json.php');
			if ($('#locations-map').size()) Finchley.Locations.initialize();
			
			if ($('#contact-form').size()) {
				$('#contact-form').validate({
					rules: {
						sender_name: 'required',
						sender_email: {required: true, email: true},
						message: 'required'
					},
					messages: {
						sender_name: 'Please enter your name.',
						sender_email: {
							required: 'Please enter your email address.',
							email: 'Please enter a valid email address.'
						},
						message: 'Please enter your message.'
					},
					errorLabelContainer: '#contact-form-errors',
					errorElement: 'p',
					highlight: function(element, errorClass, validClass) {
						$(element).addClass(errorClass).removeClass(validClass);
						$(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
						$(element).closest('.field').addClass(errorClass).removeClass(validClass);
					},
					unhighlight: function(element, errorClass, validClass) {
						$(element).removeClass(errorClass).addClass(validClass);
						$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
						$(element).closest('.field').removeClass(errorClass).addClass(validClass);
					}
				});
			}
			
			if ($('#option-field').size()) {
				$('#option-field').change(function(){
					var price = $('#option-field option:selected').attr('data-price');
					$('#price-field').val(price);
				});
			}
		});
	},
	
	Slideshow: {
		$element: null,
		width: null,
		height: null,
		
		loadPhotos: function(element, url, options) {
			var s = Finchley.Slideshow;
			var options = $.extend(s.defaults, options);
			
			s.$element = $(element);
			s.width = s.$element.width();
			s.height = s.$element.height();

			$.getJSON(url, options.data, function(data){
				$.each(data, s.addPhoto);
				
				s.$element.cycle(options.cycle);
			});
		},
		
		addPhoto: function(index, data) {
			var s = Finchley.Slideshow,
				tag = '';
			
			tag += '<li>';
			if (data.link) tag += '<a href="'+data.link+'">'
			tag += '<img src="'+data.src+'" width="'+s.width+'" height="'+s.height+'" alt="" />';
			if (data.link) tag += '</a>';
			tag += '</li>';
			
			s.$element.append(tag);
		},
		
		defaults: {
			cycle: {
				fx: 'fade',
				speed: 1000,
				timeout: 4000
			},
			data: {}
		}
	},
	
	Locations: {
		map: null,
		center: null,
		options: {},
		
		locations: {},
		
		initialize: function() {
			var l = Finchley.Locations;
			
			l.center = new google.maps.LatLng(15,0);
			l.options = {
				zoom: 1,
				center: l.center,
				mapTypeId: google.maps.MapTypeId.SATELLITE
			}
			l.map = new google.maps.Map($('#locations-map')[0], l.options);
			
			$.getJSON('/data/locations.json.php', function(data, textStatus, xhr){
				$.each(data, function(index, data){
					var location = data;
					location.position = new google.maps.LatLng(location.lat, location.lng);
					location.marker = new google.maps.Marker({
						position: location.position,
						name: location.name,
						map: l.map
					});
					
					google.maps.event.addListener(location.marker, 'click', function() {
						l.map.setCenter(location.position);
						l.map.setZoom(16);
					});
					
					l.locations[location.id] = location;
				});
				
			});
		}
	}
};
Finchley.initialize();
