function animate() {

	//animate header and voting button
	$('header').setStyle( 'visibility: visible' ).morph( 'top: -110px;', { duration: 2.2, transition: Effect.Transitions.spring } );
	$('page-loading').setStyle( 'visibility: visible; opacity: 1' ).fade( { duration: .5 } );
	$('intro-text').setStyle( 'visibility: visible; opacity: 0' ).morph( 'opacity: 1', { duration: .5, delay: .5 } );
	if( isMSIE ) setTimeout( function() { $('winner-flash').setStyle( 'visibility: visible; top: 50px; right: 190px; opacity: 1' ) }, 800 );
	else $('winner-flash').setStyle( 'visibility: visible; opacity: 0' ).morph( 'top: 50px; right: 190px; opacity: 1', { duration: 2.2, transition: Effect.Transitions.spring, delay: .5 } );
	
}

var gallery = {
	
	current_id: 0,
	
	//add handlers and initialise actions when page is loaded
	init: function() {
		
		//add handlers to close, vote left and right buttons
		$('popup-close').observe( 'click', function(e) {
			
			gallery.hide();
			popup.hide();
			
			//reset the size of the popup	
			$('popup-wrapper').morph( 'top: 300px; height: 100px', { duration: 0, delay: .5 } );
			$('popup').morph( 'width: 100px', { duration: 0, delay: .5 } );
			$('popup-loading').morph('left: 30px; top: 32px', { duration: 0, delay: .5 } );
			
			e.stop();
		
		} );
		$('arrow-left').observe( 'click', function(e) { gallery.prev(); e.stop() } );
		$('arrow-right').observe( 'click', function(e) { gallery.next(); e.stop() } );

	},

	//displays the previous entry
	prev: function() {

		gallery.hide();
		gallery.show( gallery.current_id > 1 ? gallery.current_id - 1 : 31, false );
		
	},
	
	//displays the next entry
	next: function() {

		gallery.hide();
		gallery.show( gallery.current_id < 31 ? gallery.current_id + 1 : 1, false );
		
	},
	
	//shows an entry
	show: function( id, fade ) {

		//set default for fade argument
		if( fade == null ) fade = true;

		//update current id
		this.current_id = id;
	
		//hide details and show loading screen
		if( fade ) popup.show();
		$('popup-content').setStyle('opacity: 0');
		$('popup-loading').morph('opacity: 1').show();

		//get data from ajax
		new Ajax.Request( 'get-image.php?id=' + gallery.current_id, { 
			onSuccess: function( t ) {
				$('gallery-container').update( t.responseText );
				gallery.load_entry();
			}
		});

	
	},
	
	load_entry: function() {

		//remove loading screen
		$('popup-loading').setStyle('opacity: 0').hide();
		
		//resize popup to current size
		var popup_w = 600;
		var popup_h = 700;
		$('popup-wrapper').morph( 'top: 40px; height: ' + popup_h + 'px', { duration: .5 } );
		$('popup').morph( 'width: ' + popup_w + 'px', { duration: .5 } );
		
		//reset loading position
		$('popup-loading').setStyle('left: ' + ( ( popup_w / 2 ) - 20 ) + 'px; top: ' + ( ( popup_h / 2 ) - 16 ) + 'px');
		 
		//show entry
		setTimeout( function() { $('popup-content').morph('opacity: 1', { duration: .5 } ) }, 600 );
		
	},

	//hides an entry
	hide: function() {

		$('popup-content').morph('opacity: 0', { duration: 0.3 });
	
	}

}

Event.observe( window, 'load', function() {

	gallery.init();
	animate();
	
} );
