function fixWidth( ) {
	var img = new Image;
	img.src = $('photo').getAttribute( 'src' );

	var size = getPageDimensions();
	
	if ( size[3] <= 800 ) {
		var w = img.width;
		var h = img.height;
		
		while ( h >= 450 ) {
			w = w * 0.95;
			h = h * 0.95;		
		}
	} else {
		var w = img.width;
		var h = img.height;	
	}
	
	if ( $('photoinfo') ) {
		var nwidth = ( w + 26 ) + 'px';
		$('photoinfo').setStyle({ 'width' : nwidth });
	}

	if ( $('desc') ) {
		var nwidth  = w  / 2.2 + 'px';
		var nheight = h  - 20 + 'px';

		$('desc').setStyle({ 'width' : nwidth });
		$('desc').setStyle({ 'height' : nheight });
	}		
}

function createMenuHover( ) {
	$$('a.menuitem').each( function( e ) {
		e.observe( 'mouseover', function() {
			if ( !$( e.identify() + "-active" ) ) {
				var img = "url('../images/menu_" + e.identify() + ".png')";
				$('items').addClassName( e.identify() );			
			}
		} );
		
		e.observe( 'mouseout', function() {
			if ( !$( e.identify() + "-active" ) ) {
				$('items').removeClassName( e.identify() );			
			}			
		} );
	} );
}
/*
function createMenuClick( ) {
	Event.observe('projects', 'click', function( event ) {
		$('submenu-projects').toggle();
		
		if ( !$('stylesheet-red') ) {
			stylesheet = new Element( 'link', { 'id' : 'stylesheet-red', 'type' : 'text/css', 'href' : '/stylesheets/menu-red.css', 'rel' : 'stylesheet' } );
			$$('head').first().insert( { bottom : stylesheet } );		
		}
		
		$('menu').down('div').writeAttribute( {'id' : 'projects-active' } );
		$('items').removeClassName('projects');
		$('projects').stopObserving('mouseover');
		
		event.stop();	
	} );
	
	Event.observe('things', 'click', function( event ) {
		$('submenu-things').toggle();
		
		if ( !$('stylesheet-red') ) {
			stylesheet = new Element( 'link', { 'id' : 'stylesheet-red', 'type' : 'text/css', 'href' : '/stylesheets/menu-red.css', 'rel' : 'stylesheet' } );
			$$('head').first().insert( { bottom : stylesheet } );		
		}
		
		$('menu').down('div').writeAttribute( {'id' : 'things-active' } );
		$('items').removeClassName('things');
		$('things').stopObserving('mouseover');
		
		event.stop();	
	} );
}
*/

function createDescHover( ) {
	if ( $('desc') ) {
		Event.observe('photo', 'mouseover', function() {
			$('desc').setStyle({ 'visibility' : 'visible' });
		} );

		Event.observe('photo', 'mouseout', function() {
			$('desc').setStyle({ 'visibility' : 'hidden' });
		} );

		Event.observe('desc', 'mouseover', function() {
			$('desc').setStyle({ 'visibility' : 'visible' });
		} );

		Event.observe('desc', 'mouseout', function() {
			$('desc').setStyle({ 'visibility' : 'hidden' });
		} );
	}
}

function createArrowClicks( ) {
	if ( $('arrows') ) {
		Event.observe('arrow-previous', 'click', function( event ) {
			Gallery.previousInline( $('photodiv') );
			event.stop();
		} );

		Event.observe('arrow-next', 'click', function( event ) {
			Gallery.nextInline( $('photodiv') );
			event.stop();
		} );
	}
}

function preloadImages( ) {
	var images = [  'menu_bg_blue.png',
					'menu_bg_green.png',
					'menu_bg_red.png',
					'menu_contact_a_blue.jpg',
					'menu_contact_a_green.jpg',
					'menu_contact_a_red.jpg',
					'menu_contact_blue.jpg',
					'menu_contact_green.jpg',
					'menu_contact_red.jpg',
					'menu_info_a_blue.jpg',
					'menu_info_a_green.jpg',
					'menu_info_a_red.jpg',
					'menu_info_blue.jpg',
					'menu_info_green.jpg',
					'menu_info_red.jpg',
					'menu_portfolio_a_blue.jpg',
					'menu_portfolio_a_green.jpg',
					'menu_portfolio_a_red.jpg',
					'menu_portfolio_blue.jpg',
					'menu_portfolio_green.jpg',
					'menu_portfolio_red.jpg',
					'menu_projects_a_blue.jpg',
					'menu_projects_a_green.jpg',
					'menu_projects_a_red.jpg',
					'menu_projects_blue.jpg',
					'menu_projects_green.jpg',
					'menu_projects_red.jpg',
					'menu_things_a_blue.jpg',
					'menu_things_a_green.jpg',
					'menu_things_a_red.jpg',
					'menu_things_blue.jpg',
					'menu_things_green.jpg',
					'menu_things_red.jpg'
				 ];
	
	var image = $H();
	
	images.each(function(s) {
 		image[images.indexOf(s)] = new Image;
 		image[images.indexOf(s)].src = '/images/' + s;
	});
}

Event.observe( window, 'load', function() {
	if ( $('jswarning') ) {
		$('jswarning').hide();	
	}
	
	createMenuHover();
	//createMenuClick();
	createDescHover();
	createArrowClicks();
	fixWidth();
	preloadImages();
});
