$(document).ready(function() {


setTimeout("$('#invitation').fadeIn()",12000);
$('#closedialog').click(function(){$('#invitation').fadeOut();});

//FRASER -  wrap the new_inner in a helper div to allow spacing
$('.new_inner').wrap('<div class="helper" />');

// DAVID Applied to the .inner to get the bottom bat to the bottom.
$('.inner').wrap('<div class="helper" />');
//FRASER  -  fix css for bottom band - prevent the floated content from dropping down a level on small screens.  
//Conditional Horizontal Scroll is dealt with in doResize()
$('.bottomBand').css({'padding':'8px 8px 1px 22px','margin-left':'-1px','min-width':'1050px'})

	/* Fade the content in nicely */
	$("#maincontentWrapper .contentPane").fadeIn("slow");

	/* Logo hover over */
	$("#mainLogo img").hover(function() {
		$(this).attr('src', '/images/evocologo_hover.png');
	}, function() {
		$(this).attr('src', '/images/evocologo.png');
	});

	
	/* Prepend - to submenu items */
	$(".mainNavigationWrapper ul li ul li a").prepend(' - ');
	
	doResize();

	navigationAnimation();

	smoothScrolling();

	backgroundSlideshow();

	if (!mobile) {
		/**
		 * Bind the resize handler to the resize event.
		 */
		var resizeTimer = null;
		$(window).bind('resize', function() {
			if (resizeTimer) {
				clearTimeout(resizeTimer);
			}
			resizeTimer = setTimeout(function() {
				doResize();
			}, 100);
		});
	} else {

	}


	$('.gallery #thumbs').each( function() {
			gallery();
		}
	);

	
	paybackpredictor();
	
	
	if(typeof initializeMap == 'function') {
		initializeMap();
	} 
	
});

$(window).load(function() {
	doResize();
});

function doResize() {

	/*
	 * Position transparency elements so that sub elements do not inherit the
	 * transparency
	 */
	$(".transparentNoInherit > .transparent").each(function() {
		$(this).height($(this).siblings('.transparentOverlay').height());
		$(this).width($(this).siblings('.transparentOverlay').width());
	});

	
	/*
	 * If the window width is less than a certain width, or the window
	 * height is less than the required height of the sidebar, then change
	 * the format of the window so that it scrolls normally
	 */
	var requiredWidth = parseInt($('#maincontentWrapper').width()
			+ parseInt($('#maincontentWrapper').css('margin-left')));
	var sidebarRequiredHeight = parseInt($('#sidebarWrapper .transparentOverlay')
			.height()
			+ $('.mainNavigationWrapper').position().top );
	if ($(window).width() < requiredWidth
			|| $(window).height() < sidebarRequiredHeight) {
		$("#sidebarWrapper").css('position', 'absolute');
		var top;
		if (!$(".contentPane").position()) {
			top = 104;
		} else {
			top = $(".contentPane").position().top + 66;
		}
		var height = parseInt($(".contentPane").height() + top);
		height = Math.max(height, $(window).height(),sidebarRequiredHeight);
		$("#sidebarWrapper").height(height);
	} else {
		$("#sidebarWrapper").css('position', 'relative');
		$("#sidebarWrapper").height('476px');
	}


	$(".transparentNoInherit.fullSize > .transparent").each(function() {
		$(this).height($("#sidebarWrapper").height());
		$(this).width($(this).siblings('.transparentOverlay').width());
	});

//FRASER  -  OVERRIDE PROBLEM WITH NAV BAR GETTING LONGER ON NARROW PAGES
$('div.transparent.level70').css('height','400px');

//FRASER  -  DEAL WITH BIG SCREENS - MAKE SURE BOTTOM BAR STAYS AT BOTTOM

var requiredHelperHeight = ($(window).height()-$('.bottomBand').height()-42)+'px';
$('.helper').css('min-height',requiredHelperHeight);

//FRASER  -  DEAL WITH SMALL SCREENS - MAKE MENU SCROLLABLE ONLY WHEN  NECESSARY TO AVOID OVERLAP WITH BOTTOMBAR
var heightRequiredForFixedMenu = $('#sidebarWrapper').height()+$('.renewable').height()+$('.bottomBand').height()+14;
if($(window).height() < heightRequiredForFixedMenu){$('#sidebarWrapper').css('position','absolute');}else{$('#sidebarWrapper').css('position','fixed');}

//FRASER  -  DEAL WITH OVERFLOW OF THE BOTTOM BAR IF SCREEN IS TOO SMALL FOR ITS WIDTH
if($(window).width()<1100){$('body').css('overflow-x','scroll');$('#sidebarWrapper').css('position','absolute');}else{$('body').css('overflow-x','hidden');}
	
}

function navigationAnimation() {
	$(".mainNavigationWrapper ul li a").css( {
		backgroundPosition : "-300px 0"
	}).mouseover(function() {
		$(this).css( {
			backgroundPosition : "-270px 0"
		}).stop().animate( {
			backgroundPosition : "0px 0px"
		}, "fast");
	}).mouseout(function() {
		$(this).dequeue().stop().animate( {
			backgroundPosition : "-300px 0px"
		}, "fast");
	});
}

function backgroundSlideshow() {

	/*
	 * Initialize Backgound Stretcher Todo: make the images dynamic from the
	 * content pages
	 */
	$(document).bgStretcher( {
		images : backgroundImages,
		imageWidth : 1500,
		imageHeight : 1000,
		nextSlideDelay : 10000,
		slideShowSpeed : "slow"
	});
	
	$('#bgstretcher *').css('z-index','-1000');

}

function smoothScrolling() {

	$('.contentPane').localScroll();

}



function gallery() {

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});
	
	var gallery = $('#thumbs').galleriffic({
        delay:                     3000, // in milliseconds
        numThumbs:                 10, // The number of thumbnails to show page
        preloadAhead:              40, // Set to -1 to preload all images
        enableTopPager:            false,
        enableBottomPager:         true,
        maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager
        imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
        controlsContainerSel:      '', // The CSS selector for the element within which the slideshow controls should be rendered
        captionContainerSel:       '', // The CSS selector for the element within which the captions should be rendered
        loadingContainerSel:       '', // The CSS selector for the element within which should be shown when an image is loading
        renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
        renderNavControls:         false, // Specifies whether the slideshow's Next and Previous links should be rendered
        playLinkText:              'Play',
        pauseLinkText:             'Pause',
        prevLinkText:              'Previous',
        nextLinkText:              'Next',
        nextPageLinkText:          'Next &rsaquo;',
        prevPageLinkText:          '&lsaquo; Prev',
        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
        defaultTransitionDuration: 500, // If using the default transitions, specifies the duration of the transitions
        onSlideChange:             undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
        onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
        onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
        onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
        onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
        onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
        onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
    });
}



function paybackpredictor() {

	// When a link is clicked
	$("#pp a.tab").click(function () {
		
		// switch all tabs off
		$(".active").removeClass("active");
		
		// switch this tab on
		$(this).addClass("active");
		
		// slide all content up
		$(".tab_content").hide();
		
		// slide this content up
		var content_show = $(this).attr("title");
		$("#"+content_show).show();
		
		doResize();
	  
	});

	$('#pp .tab_content').hide();
	
	$('#pp a.tab.active').each(function() {
		var content_show = $(this).attr("title");
		$("#"+content_show).show();
	});

}


//Dump the object contents to the log
function var_dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }
//    log(out);
    return out;
}//end function var_dump


//Output the log to the screen (useful for debugging)
function log(message) {
	$('#jslog pre').append('<p>' + message + "</p>");
	$('#reportOptions pre').empty();
	$('#reportOptions pre').append(var_dump(ppReportOptions));
}


