$(document).ready(function() {

	// External links
	$("a[rel=external]").attr({
	    target: "_blank", 
	    title: "Opens in a new browser window"
	  });
			
	// Image cycle		
	$('.carousel').cycle({
	    fx:      'scrollHorz',
	    next:   '.carousel',
	    timeout:  0,
			speed: 300,
			after: onAfter
	});
	
	// Setup action for buttons underneath image
	$('.counter').children().click(function() { 
		var selectedbutton = $(this).attr("id").substring(6,7)-1;
		$('.carousel').cycle(selectedbutton);
		return false; 
	});
	
	// Update text, caption and buttons when the image changes
	function onAfter(curr,next,opts) {
		var thisSlide = (opts.currSlide+1)
		var caption = thisSlide + '/' + opts.slideCount;
		$('.slidenav').html(caption);
		var i=0;
		for (i=0;i<=opts.slideCount;i++) {
			$('#button'+i).removeClass('lit');
		}
		$('#button'+thisSlide).addClass('lit');
		// Homepage only - update the text
		if (document.title == "~ The Old Vicarage ~ Isleham ~") {
			for (i=0;i<=opts.slideCount;i++) {
				$('#intro'+i).hide();
			}
			$('#intro'+thisSlide).fadeIn('fast');
		}
	}
		
	// Image cycle	
	$('.carousel2').cycle({
	    fx:      'scrollHorz',
	    next:   '.carousel2',
	    timeout:  0,
			speed: 300,
			after: onAfter2,
			timeout:0,
			pause:   1
	});

	// Setup action for buttons underneath image
	$('.counter2').children().click(function() { 
		var selectedbutton2 = $(this).attr("id").substring(7,8)-1;
		$('.carousel2').cycle(selectedbutton2);
		return false; 
	});

	// Update text, caption and buttons when the image changes
	function onAfter2(curr,next,opts) {
		var thisSlide2 = (opts.currSlide+1)
		var caption2 = thisSlide2 + '/' + opts.slideCount;
		$('.slidenav2').html(caption2);
		var i=0;
		for (i=0;i<=opts.slideCount;i++) {
			$('#buttonb'+i).removeClass('lit');
		}
		$('#buttonb'+thisSlide2).addClass('lit');
	}
	
	// Image cycle	
	$('.carousel3').cycle({
	    fx:      'scrollHorz',
	    next:   '.carousel3',
	    timeout:  0,
			speed: 300,
			after: onAfter3,
			timeout:0,
			pause:   1
	});

	// Setup action for buttons underneath image
	$('.counter3').children().click(function() { 
		var selectedbutton3 = $(this).attr("id").substring(7,8)-1;
		$('.carousel3').cycle(selectedbutton3);
		return false; 
	});

	// Update text, caption and buttons when the image changes
	function onAfter3(curr,next,opts) {
		var thisSlide3 = (opts.currSlide+1)
		var caption3 = thisSlide3 + '/' + opts.slideCount;
		$('.slidenav3').html(caption3);
		var i=0;
		for (i=0;i<=opts.slideCount;i++) {
			$('#buttonc'+i).removeClass('lit');
		}
		$('#buttonc'+thisSlide3).addClass('lit');
	}
	
	// More info toggle
	$('#extrainfo').hide();
	$('#bottomdrawer, .column1, .column2').click(function() {
			if ($('#moreinfo').css('background-image').indexOf("bg_more_info.gif")!=-1) {
				$('#moreinfo').addClass('expanded')
			} else {
				$('#moreinfo').removeClass('expanded')
			}
			$('#extrainfo').toggle(300);
	    return false;
	  });

	// Google map
	function initialize() {
	    var myLatlng = new google.maps.LatLng(52.342871,0.412081);
	    var myOptions = {
	    	zoom: 15,
	    	center: myLatlng,
	    	mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false	
	    }
	
	    var map = new google.maps.Map(document.getElementById("map"), myOptions);

	    var marker = new google.maps.Marker({
	        position: myLatlng, 
	        map: map,
	        title:"The Old Vicarage"
	
	    });  
	
		google.maps.event.addListener(marker, 'click', function() {
		    				window.open('http://www.google.co.uk/maps?hl=en&ie=UTF8&view=map&f=d&daddr=7+Church+Street,+Isleham,+Ely,+Cambridgeshire+CB7+5RX&geocode=CU_3ifp3K9LFFZCwHgMdvUkGACEngv2aDrX0Hw&ved=0CHsQ_wY&ei=PzGLTInhKIOTjAfixvw-&z=16');
		  });
	  }
	
	// Location page only - run Google maps
	if (document.title == "~ The Old Vicarage ~ Isleham ~ Location ~") {
		initialize();
	}
	
});


