// Mac-style menus

var gScreenResolution;

/*
(function ()
{
	var prerequisites =
	[
		"../../js/foldable-headings.js",
		"js/corners.js",
		"js/menus.js",
		"js/window.js",
		"js/screen.js",
		"js/desktop.js"
	];
	
	if ( Bootstrap.ready( prerequisites ) )
	{
		windowLoader.defer( init );
	}
})();
*/

Bootstrap.importScript( "../../js/foldable-headings.js" );

Bootstrap.importScript( "js/corners.js"    );
Bootstrap.importScript( "js/Menus.js"      );
Bootstrap.importScript( "js/MacWindows.js" );
Bootstrap.importScript( "js/screen.js"     );
Bootstrap.importScript( "js/desktop.js"    );

windowLoader.defer( init );


// http://upload.wikimedia.org/wikipedia/en/c/c5/Happy_Mac.png


function xyFromString( string )
{
	var dimensions = string.split( /x/ );
	
	var x = dimensions[0];
	var y = dimensions[1];
	
	return { x: x, y: y };
}

function MacSimulation()
{
	var macSim = document.getElementById( 'maxim' );
	
	var screen = new Screen();
	var windowManager = new WindowManager();
	
	screen.structure.appendChild( windowManager.structure );
	
	var fakeMenuBar = document.createElement( 'div' );
	fakeMenuBar.className = 'visual menubar';
	
	fakeMenuBar.appendChild( (new ScreenCorners()).structure );
	
	windowManager.structure.appendChild( fakeMenuBar );
	
	
	windowManager.addLayer( new ScreenCorners() );
	
	
	var menuBar = new MenuBar();
	
	windowManager.structure.appendChild( menuBar.structure );
	
	
	var desktop = new Desktop();
	
	windowManager.structure.appendChild( desktop.structure );
	
	
	var screenSelect = document.getElementById( 'screen-select' );
	
	var that = this;
	
	screenSelect.onchange = function() { that.setResolution( xyFromString( this.value ) ) };
	
	
	var backgroundImageURLBox = document.getElementById( 'background-image-url' );
	
	backgroundImageURLBox.onchange = function() { windowManager.setBackgroundImage( this.value ) };
	
	
	var desktopPictureURLBox = document.getElementById( 'desktop-picture-url' );
	
	desktopPictureURLBox.onchange = function() { windowManager.setDesktopPicture( this.value ) };
	
	this.windowManager = windowManager;
	this.screen = screen;
	this.desktop = desktop;
	this.menubar = menuBar;
	
	this.structure = macSim;
	
	// Disable default action (i.e. selecting text in Web page)
	macSim.onmousedown = function()  { return false; };
	
	macSim.appendChild( screen.structure );
	
	screenSelect.onchange();
	
	desktopPictureURLBox.onchange();
	
	var console = document.getElementById( 'javascript-console' );
	
	console.onchange = function(){ eval( this.value ); };
}

MacSimulation.prototype.setSize = function( x, y )
{
	this.structure.style.width  = x + 'px';
	this.structure.style.height = y + 'px';
}

MacSimulation.prototype.setResolution = function( resolution )
{
	gScreenResolution = resolution;
	
	var x = resolution.x
	var y = resolution.y;
	
	this.setSize( x, y );
}

function makeFinderMenus()
{
	// Menus
	var apple = document.createElement( 'img' );
	
	apple.src = "images/apple-logo.gif";
	apple.alt = "Apple";
	
	var menus =
	[
		{ title: apple, items: [ 'About This Macintosh...', '-' ] },
		
		{ title: "File", items: [ [ 'New',          'N' ],
	                              [ 'Open',         'O' ],
	                              [ 'Close Window', 'W' ],
	                                '-',
	                              [ 'Quit',         'Q' ] ] },
		
		{ title: "Edit", items: [ [ 'Undo',       'Z' ],
	                                '-',
	                              [ 'Cut',        'X' ],
	                              [ 'Copy',       'C' ],
	                              [ 'Paste',      'V' ],
	                                'Clear',
	                              [ 'Select All', 'A' ],
	                                '-',
	                                'Show Clipboard'    ] },
		
		{ title: "View", items: [ 'by Small Icon',
	                              'by Icon',
	                              'by Name',
	                              'by Size',
	                              'by Kind',
	                              'by Label',
	                              'by Date' ] },
		
		{ title: "Label", items: [ [ 'None', null, "\u2713" ], '-' ] },
		
		{ title: "Special", items: [] },
	];
	
	return menus;
}

var Maxim;

function init()
{
	Maxim = new MacSimulation();
	
	Maxim.menubar.insertMenus( makeFinderMenus() );
	
	var one = NewWindow( null, { left: 100, top: 80,  right: 300, bottom: 220 }, "One" );
	var two = NewWindow( null, { left: 260, top: 200, right: 500, bottom: 300 }, "Two", true, 'proc', null );
	
	/*
	var checkedItem = new MenuItem( "Checked", "\u2713" );
	
	var bullet  = new MenuItem( "bullet",  "\u2022" );
	var lozenge = new MenuItem( "lozenge", "\u25ca" );
	
	var foo = new MenuItem( "Foo", [ 'Master', 'Fighters' ] );
	var bar = new MenuItem( "Bar", [ 'None', 'Nickels' ] );
	var baz = new MenuItem( "Baz", [ 'Words', 'Off' ] );
	
	var moreMarks = new MenuItem( "More marks", [ bullet, lozenge ] );
	var moreMenus = new MenuItem( "More menus", [ foo, bar, baz ] );
	
	gMenuBar.insertMenu( "Test", [ checkedItem, '-', moreMarks, moreMenus ] );
	*/
}

