// Mac-style menus

var gScreenResolution;

(function ()
{
	var prerequisites =
	[
		"../../js/more-dom.js",
		
		"js/Menus.js",
		"js/MacWindows.js",
		"js/screen.js",
		"js/desktop.js"
	];
	
	var loader = new Loader( prerequisites );
	
	// Not vital
	loader.require( "../../js/foldable-headings.js" );
	
	loader.defer( init );
}
)();

window.Mac = { apps: {} };

// 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 keyPress( event )
{
	if ( event.metaKey )
	{
		var menuitem = MenuKey( event );
		
		if ( menuitem )
		{
			menuitem.run();
			
			return false;
		}
	}
}

function MacSimulation()
{
	var macSim = document.getElementById( 'maxim' );
	var screen = document.getElementById( 'screen' );
	
	// Disable default action (i.e. selecting text in Web page)
	macSim.onmousedown = function()  { return false; };
	
	var fakeMenuBar = { node: 'div.visual.menubar' };
	
	var menuBar = new MenuBar();
	var desktop = new makeDesktop();
	
	var windowManagerChildren = [ fakeMenuBar, menuBar.structure, desktop ];
	
	var windowManagerModel = { node: 'div#window-manager', children: windowManagerChildren };
	
	var dsat = document.getElementById( 'dsat' );
	
	if ( dsat )
	{
		dsat.parentNode.removeChild( dsat );
	}
	
	screen.appendChild( MoreDOM.instantiateModel( windowManagerModel ) );
	
	var windowManager = document.getElementById( 'window-manager' );
	
	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() { setBackgroundImage( windowManager, this.value ) };
	
	
	var desktopPictureURLBox = document.getElementById( 'desktop-picture-url' );
	
	desktopPictureURLBox.onchange = function() { setDesktopPicture( windowManager, this.value ) };
	
	this.windowManager = windowManager;
	
	this.menubar = menuBar;
	
	this.structure = macSim;
	
	screenSelect.onchange();
	
	desktopPictureURLBox.onchange();
	
	var editor = document.getElementById( 'script-editor' );
	
	var runScript = document.getElementById( 'run-script' );
	
	runScript.onclick = function() { eval( editor.value ); };
	
	insertModalShield();
}

function insertModalShield()
{
	var body = document.getElementsByTagName( 'body' )[0];
	
	body.insertBefore( MoreDOM.instantiateModel( { node: 'div#modal-shield' } ), body.firstChild );
	
	document.onkeypress = keyPress;
}

function removeModalShield()
{
	document.onkeypress = undefined;
	
	var shield = document.getElementById( 'modal-shield' );
	
	if ( shield )
	{
		shield.parentNode.removeChild( shield );
	}
}

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 abou = function()
	{
		invoke( "apps/about.js" );
	}
	
	var msg = function()
	{
		invoke( "apps/message-box.js" );
	}
	
	var www = function()
	{
		invoke( "apps/web-browser.js" );
	}
	
	var clos = function()
	{
		var window = FrontWindow();
		
		if ( window )
		{
			window.goAway();
		}
	}
	
	var full = function()
	{
		var mark = this.structure.childNodes[0].firstChild;
		
		var marked = mark.data;
		
		var body = document.getElementsByTagName( 'body' )[0];
		
		if ( marked )
		{
			document.getElementById( 'screen-select' ).onchange();
		}
		else
		{
			var macSim = document.getElementById( 'maxim' );
			
			macSim.style.width  = '100%';
			macSim.style.height = '100%';
			
			gScreenResolution.x = window.innerWidth;
			gScreenResolution.y = window.innerHeight;
		}
		
		var adjustClass = marked ? removeClass : addClass;
		
		adjustClass( body, 'exclusive' );
		
		mark.data = marked ? "" : "\u2713";
	}
	
	var slep = function()
	{
		removeModalShield();
		
		var macSim = document.getElementById( 'maxim' );
		
		addClass( macSim, 'sleeping' );
		
		document.getElementById( 'lid' ).onclick = function()
		{
			removeClass( macSim, 'sleeping' );
			
			insertModalShield();
			
			this.onclick = undefined;
		}
	}
	
	var shut = function()
	{
		var screen = document.getElementById( 'screen' );
		
		var macSim = screen.parentNode;
		
		var dsatModel = { node:     'div#dsat',
		                  children: { node:     'div',
		                              children: { node: 'p',
		                                          children: "It is now safe to turn off your computer." } } };
		
		var dsat = MoreDOM.instantiateModel( dsatModel );
		
		macSim.replaceChild( dsat, screen );
		
		removeModalShield();
	}
	
	var menus =
	[
		{ title: apple, items: [ [ 'About This Macintosh...', null, abou ],
		                         '-',
		                         [ 'Message Box', 'M', msg ],
		                         [ 'World-Wide Web', null, www ] ] },
		
		{ title: "File", items: [ [ 'New',          'N' ],
	                              [ 'Open',         'O' ],
	                              [ 'Close Window', 'W', clos ],
	                                '-',
	                              [ '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: [ [ 'Full Screen', null, full ],
		                               '-',
		                             [ 'Sleep',     null, slep ],
		                               'Restart',
		                             [ 'Shut Down', null, shut ] ] },
	];
	
	return menus;
}

var Maxim;

function init()
{
	Maxim = new MacSimulation();
	
	Maxim.menubar.insertMenus( makeFinderMenus() );
	
	invoke( "apps/about.js" );
	
	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 ] );
	*/
}


