$(document).ready(function() {
    $('.slideshow1')
	.before('<div id="nav1">') 
	.cycle({ 
		fx:     'uncover',
		delay: 0,
		timeout: 4000,
		pager:  '#nav1' 
	});
	$('.slideshow2')
	.before('<div id="nav2">') 
	.cycle({ 
		fx:     'uncover',
		delay: 500,
		timeout: 4000,
		pager:  '#nav2' 
	});
    $('.slideshow3')
	.before('<div id="nav3">') 
	.cycle({ 
		fx:     'uncover',
		delay: 1000,
		timeout: 4000,
		pager:  '#nav3' 
	});
    $('.slideshow4')
	.before('<div id="nav4">') 
	.cycle({ 
		fx:     'uncover',
		delay: 1500,
		timeout: 4000,
		pager:  '#nav4' 
	});
});


function adjust() {
	// function to centre the page contents depending on the user's window size.
	// probably won't work in IE or Opera but doesn't matter much - it'll just put the content down the side of the screen in that case.	
	var baseMargin = (window.outerWidth-896)/2;
	if(baseMargin < 0) {
		baseMargin = 0;
	}
	
	document.getElementById('title_container').style.left=baseMargin+"px";
	document.getElementById('content_container').style.left=baseMargin+"px";	
}

// smooth scrolling functions

function currentYPosition() {
	// Gets the current Y coordinates of the top left corner of the visible screen
	// Has to be adjusted for different browsers

    // Firefox, Chrome, Opera, Safari
    if (self.pageYOffset) return self.pageYOffset;
    // Internet Explorer 6 - standards mode
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    // Internet Explorer 6, 7 and 8
    if (document.body.scrollTop) return document.body.scrollTop;
    return 0;
}

function elementYPosition(element_id) {
    var e = document.getElementById(element_id);
    var y = e.offsetTop;
	
	// traverse up the nodes until the true Y coordinate is computed
    var node = e;
    while (node.offsetParent && (node.offsetParent != document.body)) {
        node = node.offsetParent;
        y += node.offsetTop;
    }
	return y;
}

function smoothScroll(element_id) {
    var start = currentYPosition();
    var stop = elementYPosition(element_id) - (window.outerHeight-800)/2;

	// Calculate how far we need to scroll (up or down)
    var distance = 0;
	if(stop > start) {
		distance = stop - start;
	}
	else {
		distance = start - stop;
	}

	// Jump to the target position if within 100 pixels of it
    if (distance < 100) {
        scrollTo(0, stop);
		return;
    }

	// Otherwise, calculate how fast we need to scroll
    var speed = Math.round(distance / 100); // how often the page scrolls..
    var step = Math.round(distance / 25); // ..by how many pixels

    var leap = 0; // Y coordinate of the next postion to scroll to
	if(stop > start) {
		leap = start + step;
	}
	else {
		leap = start - step;
	}

	//Finally, scroll the page (Up or Down) until the destination is reached
    var timer = 0;
    if (stop > start) {
        for ( var i = start; i < stop; i += step ) {
            setTimeout("window.scrollTo(0, "+leap+")", timer * speed); // scroll to the next leap
            leap += step; // set the next leap coordinate
			if (leap > stop) {
				leap = stop; // don't overshoot
			}
			timer++; // incrementing the timer variable sets the timeout function to happen again in 'speed' millisecond's time
        }
		return;
    }
	else {
        for ( var i = start; i > stop; i -= step ) {
            setTimeout("window.scrollTo(0, "+leap+")", timer * speed); // scroll to the next leap
            leap -= step; // set the next leap coordinate
			if (leap < stop) {
				leap = stop; // don't overshoot
			}
			timer++;
        }
		return;
    }
}
