$(function() {
	$('body').removeClass('no-js');

	$('#main_nav .active').data('open', true);
	$('#main_nav > li').hoverIntent({
		sensitivity : 5,
		timeout : 250,
		over : function() {
			var my_link = $('a:first', $(this));
			var target = $('ul', $(this));
			if (target.length == 1) {
				my_link.addClass('active');
				target
					.data('animating', true)
					.slideDown('normal');
			}
		},
		out: function() {
			var my_link = $('a', $(this));
			var target = $('ul', $(this))
			target
				.slideUp('normal', function() {
					if (!!!my_link.data('open')) {
						my_link.removeClass('active');
					}
				});
		}
	});
	
	var $ml = $('#mailing_list');
	$('#join_mailing').click(function() {
		var main = $('#top');

		if (!!!$ml.data('animating')) {
			$ml.data('animating', true);
			if (!!$ml.data('open')) {
				$ml.animate({top : -100}, 500);
				main.animate({paddingTop : 0}, 500, function() {$ml.data('animating', false)});
				$ml.data('open', false);
			} else {
				$ml.animate({top : 0}, 500);
				main.animate({paddingTop : 100}, 500, function() {$ml.data('animating', false)});
				$ml.data('open', true);
			}
		}
		return false;
	});
	
		
	$('input[type=text]', $ml)
	.each(function() {$(this).data('original_text', $(this).val());})
	.focus(function() {
	if ($(this).val() == $(this).data('original_text')) { 
	$(this).val('');
	}
	})
	.blur(function() {
	if ($(this).val() == '') {
	$(this).val($(this).data('original_text'));
	}
	});


	var midline = $('#midline');
	if (midline.length == 1) {
		$('#mid_history .frame').cycle({
			timeout : 5000
		});
		$('#mid_shopping .frame').cycle({
			timeout : 5000,
			delay : 1000
		});
		$('#mid_leisure .frame').cycle({
			timeout : 5000,
			delay : 2000
		});
	}
});

function get_coords(coords) {
	coords = coords.split(',');

	var lat, lng;

	lat = coords[0];
	lng = coords[1];
	lng = (lng.charAt(0) == ' ') ? lng.slice(1) : lng;

	lat = parseFloat(lat);
	lng = parseFloat(lng);
	return [ lat, lng ];
}

function init() {
	$(function() {
		var $map_canvas = $('#map_canvas');
		if ($map_canvas.length == 1) {
			var map_json = eval('(' + $map_canvas.text() + ')');
			var lat, lng, latLng;

			$map_canvas.empty().removeClass('ir');

			if (!!map_json.map_centre) {
				var myOptions, map, img, coords, marker;
				latLng = get_coords(map_json.map_centre[0]);

				lat = latLng[0];
				lng = latLng[1];
				
				latLng = new google.maps.LatLng(lat, lng);
				myOptions = {
					center : latLng,
					zoom : 16,
					mapTypeId : google.maps.MapTypeId.ROADMAP
				};
				map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
				
				if (!!map_json.map_quarter && map_json.map_quarter.length == 1) {
					img = new google.maps.MarkerImage(	'/wp-content/themes/minsterquarter/img/mq_marker.png',
																					new google.maps.Size(70, 58),
																					new google.maps.Point(0,0),
																					new google.maps.Point(35, 27));
				} else {
					img = new google.maps.MarkerImage(	'/wp-content/themes/minsterquarter/img/map_marker.png',
																					new google.maps.Size(20, 35),
																					new google.maps.Point(0,0),
																					new google.maps.Point(10, 35));
				}

				$.each(map_json.map_point, function(k,v) {
					coords = get_coords(v);
					marker = new google.maps.Marker({
						map : map,
						icon : img,
						draggable : false,
						animation : google.maps.Animation.DROP,
						position : new google.maps.LatLng(coords[0], coords[1])
					});
				});
			}
		}
	});
}
