/**
 * On page load functions.
 */
(function($) {
    $(document).ready(function(){
        // When hovering over menu, slide it down and fade out the page (and v.v.).
        $('#nav ul.menu1 > li.item1').hoverIntent(
            function(){
                if ($(this).children('ul.menu2').length > 0) {
                    if (jQuery.support.opacity) {
                        $('#fadeBox').fadeTo(600, 0.25);
                    }
                    $(this).children('ul.menu2').slideDown();
                }
                $(this).children('a').addClass('hover');
            },
            function(){
                if ($(this).children('ul.menu2').length > 0) {
                    $(this).children('ul.menu2').slideUp(function(){
                    });
                    if (jQuery.support.opacity) {
                        $('#fadeBox').fadeTo(300, 1);
                    }
                }
                $(this).children('a').removeClass('hover');
            }
        );

        // Add the fancybox plugin.
        $("a.fancybox").fancybox({
            'transitionIn' : 'elastic',
            'transitionOut' : 'elastic',
            'speedIn' : 600,
            'speedOut' : 200,
            'overlayShow' : true,
            'centerOnScroll' : true,
            'titlePosition' : 'over'
        });

        $('#preview-calendar-outer a').live('click', function(){
            if (jQuery.support.opacity) {
                $('#preview-calendar-outer').fadeTo(600, 0.25);
            }
            $('#preview-calendar-outer').load($(this).attr('href') + ' #preview-calendar-inner', function() {
                if (jQuery.support.opacity) {
                    $('#preview-calendar-outer').fadeTo(300, 1);
                }
            });
            return false;
        });
		
		$("#preview-calendar-inner").livequery(function(){
		    var index = $(".when0").parent().index();
			if (index < 0) index = 0;
			$(this).serialScroll({
				target:'#preview-calendar',
				items:'.widget',
				axis:'y',
				duration:250,
				prev:'#preview-calendar-up',
				next:'#preview-calendar-down',
				force:true,
				stop:true,
				lock:false,
				cycle:false,
				jump: true,
				start: index
			});
			
			$(this).hoverIntent(function() {
				$("#preview-calendar-up, #preview-calendar-down").fadeIn();
			}, function() {
				$("#preview-calendar-up, #preview-calendar-down").fadeOut();
			});
		});

		$(".blogHeadline").livequery(function() {
			var blogHeadline = $(this);
			blogHeadline.hoverIntent(function() {
				blogHeadline.css({background: '#efefff', cursor: 'pointer'});
			}, function() {
				blogHeadline.css({background: '#ffffff', cursor: 'pointer'});
			}).click(function() {
				window.location = blogHeadline.find("a").attr("href");
			});
		});

		$(".publication").livequery(function() {
			var publication = $(this);
			publication.hoverIntent(function() {
				publication.css({background: '#efefff', cursor: 'pointer'});
			}, function() {
				publication.css({background: '#ffffff', cursor: 'pointer'});
			}).click(function() {
				window.location = publication.find("a").attr("href");
			});
		});

        $.trackPage('UA-19103932-1');

        featuresLoop();
        //loadIdeaInformer();
        
        $('#map-canvas').click(function(){
		    $(this).unbind("click");
            loadGoogleMap();
        });
    });
})(jQuery);

function featuresLoop() {
    $('#featured').serialScroll({
        target:'#featured-img-window',
        items:'li',
        axis:'x',
        navigation:'#featured-menu li a',
        duration:400,
        force:true,
        event:'mouseover',
        stop:true,
        lock:false,
        cycle:true,
        jump:true,
        interval:30000,
		onAfter:function( e, elem, $pane, $items, pos ){
                        $(this).find('.textbox').slideDown();
        },
        onBefore:function( e, elem, $pane, $items, pos ){
                        $(this).find('.textbox').slideUp();
        }
    });
}

function loadGoogleMap() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=drawGoogleMap";
  document.body.appendChild(script);
}

function drawGoogleMap() {
    var mapDiv = document.getElementById('map-canvas');
    var map = new google.maps.Map(mapDiv, {
                center: new google.maps.LatLng(52.955567,-1.151762),
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP});

    var office = new google.maps.Marker({
          position: new google.maps.LatLng(52.952053,-1.14871),
          map: map, title: "Parish office"});
    var allsaints = new google.maps.Marker({
          position: new google.maps.LatLng(52.960159,-1.161246),
          map: map, title: "All Saints"});
    var stmary = new google.maps.Marker({
          position: new google.maps.LatLng(52.951146,-1.143286),
          map: map, title: "St Mary's"});
    var stpeter = new google.maps.Marker({
          position: new google.maps.LatLng(52.952226,-1.148554),
          map: map, title: "St Peter's"});
}

