// == one-time setup: modes and features ==

// ----------------------------------------------------------------------
// initUIDefines()
// ----------------------------------------------------------------------
function initUIDefines() {
	// ** object setup (sanity check)
	if ( beos.globals.pages == null )
		beos.globals.pages = new Object();
	if ( beos.globals.framenames == null)
		beos.globals.framenames = new Object();
	if ( beos.globals.geometry == null)
		beos.globals.geometry = new Object();
	
	// ** geometry configuration
	// if screen_resolution hasn't been forced on us, we set it here
	if (beos.globals.screen_resolution == null) {
		if ( window.innerWidth == 640 )
			beos.globals.screen_resolution = "640x480";
		else
			beos.globals.screen_resolution = "800x600";
	}

	switch ( beos.globals.screen_resolution ) {
	case "640x480": 
		beos.globals.geometry.window_h = 480;
		beos.globals.geometry.window_w = 640;
		beos.globals.use_fullscreen_bookmarks = true;
		break;
	case "800x600":
	default:
		beos.globals.geometry.window_h = 600;
		beos.globals.geometry.window_w = 800;
		beos.globals.use_fullscreen_bookmarks = false;
		break;
	}

	if (!beos.globals.use_fullscreen_bookmarks)
		beos.globals.geometry.bookmarks_h = 200;
	else
		beos.globals.geometry.bookmarks_h = beos.globals.geometry.window_h - 80;

	beos.globals.use_redirector = false;

	// ** softkeyboard geometry
	beos.globals.geometry.softkeyboard_h = 0;
	if ( beos.globals.enable_soft_keyboard )
		beos.globals.geometry.softkeyboard_h += 180;
	if ( beos.globals.enable_japanese_im ) 
		beos.globals.geometry.softkeyboard_h += 40;
		
	// ** html page locations
	beos.globals.pages.desktop = "file://$RESOURCES/Home/Home.html";
	
	if ( beos.globals.screen_resolution == "640x480" ) { 
		beos.globals.pages.webhome = "file://$RESOURCES/Home/SmallHome.html";
	} else {
		beos.globals.pages.webhome = "file://$RESOURCES/Home/Home.html";
	}
	beos.globals.pages.webtoolbar = "file://$RESOURCES/Toolbar/toolbar.html";
	beos.globals.pages.bookmarks = "file://$RESOURCES/Bookmarks/index.html";

	beos.globals.pages.settings = "file://$RESOURCES/Settings/Settings.html";

	beos.globals.pages.mediabar = "file://$RESOURCES/MediaBar/main.html";

	beos.globals.pages.softkeyboard = "file://$RESOURCES/SoftKeyboard/main.html";

	beos.globals.pages.updater = "file://$RESOURCES/Update/main.html";

	beos.globals.pages.v_divider = "file://$RESOURCES/PopUpDecor/PanebarVert.html";
	beos.globals.pages.h_divider = "file://$RESOURCES/PopUpDecor/PanebarHorz.html";
	
	beos.globals.pages.v_divider = "file://$RESOURCES/PopUpDecor/black.html";
	beos.globals.pages.h_divider = "file://$RESOURCES/PopUpDecor/black.html";
	beos.globals.pages.content_v_titleBar = "file://$RESOURCES/PopUpDecor/content_titlebar_v.html";
	beos.globals.pages.content_h_titleBar = "file://$RESOURCES/PopUpDecor/content_titlebar_h.html";
	beos.globals.pages.sub_v_titleBar = "file://$RESOURCES/PopUpDecor/sub_titlebar_v.html";
	beos.globals.pages.sub_h_titleBar = "file://$RESOURCES/PopUpDecor/sub_titlebar_h.html";
	
	// ** misc state variables
	beos.globals.preload_settings_panel = null;

	// ** modes and features: control UI behavior
	//    ASSUME:
	//    - Cluster: "application" with mode "web"
	//    - Feature: "web:subwindow"
	//    - Feature: "media:visible"
	//    - Feature: "softkeyboard:visible"
	if ( beos.globals.use_fullscreen_bookmarks ) 
		beos.globals.modes.AddMode("application", "bookmarks", null, null);
	else
		beos.globals.features.Add("web:bookmarks", null, null);

	// ---
	beos.globals.ui_inited = 1;
}

// ----------------------------------------------------------------------
// initUILayout
//
// Attach a tree of LayoutObject()s (see layout.js) to beos.topLayout.
// ----------------------------------------------------------------------
function initUILayout() {
	//beos.dprintf('index.html: rebuilding topLayout structure');

	var obj;
	var config;
	var topLayout = beos.globals.topLayout 	
		= new LayoutBag(MakeObject(
				"name", "_be:topLayout",
				"orientation", LayoutBag_SPLIT_HORIZONTAL
			));

	{ // start top pane
		var applicationPane = new LayoutBag(MakeObject(
				"name", "_be:application",
				"orientation", LayoutBag_SPLIT_HORIZONTAL,
				"size", "*"
			)); 
		{ // start application pane
			/* Add the updater pane */
			applicationPane.AddChild(obj = new LayoutFrame(MakeObject(
				"name", beos.globals.framenames.updater,
				"location", beos.globals.pages.updater,
				"scrolling", false,
				"size", 50,
				"visible", false,
				"zeroPixelHiding", true
			)));

			// web toolbar
			obj = applicationPane.AddChild(new LayoutFrame(MakeObject(
					"name", beos.globals.framenames.navbar,
					"location", beos.globals.pages.webtoolbar, 
					"scrolling", false, 
					"size", "80", 
					"visible", true
				)));
				
			// bookmarks
			obj = applicationPane.AddChild(new LayoutFrame(MakeObject(
					"name", beos.globals.framenames.bookmarks,
					"location", beos.globals.pages.bookmarks, 
					"scrolling", false, 
					"size", (beos.globals.use_fullscreen_bookmarks
						? "*"
						: beos.globals.geometry.bookmarks_h),
					"visible", false,
					"zeroPixelHiding", true
				)));
			if ( beos.globals.use_fullscreen_bookmarks )  {
				CoupleLayoutObjectToMode(obj, "application", "bookmarks");
			} else {
				CoupleLayoutObjectToMode(obj, "application", "web");
				CoupleLayoutObjectToFeature(obj, "web:bookmarks", true);
			}

			// web content ==================================================
			// this is the main split area layoutBag
				
			var webSplit = applicationPane.AddChild(
				new LayoutBag(MakeObject(
				"name", "_be:application:websplit",
				"orientation", LayoutBag_SPLIT_VERTICAL,
				"size", "*",
				"zeroPixelHiding", true
			)));
			webSplit.RefreshHook = new Function('o','
			if ( beos.globals.subwin_height > beos.globals.subwin_width )
				o.m_orientation = LayoutBag_SPLIT_VERTICAL;
			else 
				o.m_orientation = LayoutBag_SPLIT_HORIZONTAL;
			');

			{				
				// this bag will hold the content frame and titlebar
				var contentBag = webSplit.AddChild(
					new LayoutBag(MakeObject(
					"name", "_be:application:websplit:contentBag",
					"orientation", LayoutBag_SPLIT_HORIZONTAL,
					"size", "*",
					"zeroPixelHiding", true
				)));
				contentBag.RefreshHook = new Function('o','
				if ( beos.globals.subwin_height > beos.globals.subwin_width )
					o.m_orientation = LayoutBag_SPLIT_HORIZONTAL;
				else 
					o.m_orientation = LayoutBag_SPLIT_HORIZONTAL;
					//o.m_orientation = LayoutBag_SPLIT_VERTICAL;
				');
	
				{
					// this is the content window titleBar
					// it only appears in the vertical layout
					obj = contentBag.AddChild(new LayoutFrame(MakeObject(
						"name", "_be:content:content_titleBar",
						"location", beos.globals.pages.content_v_titleBar,
						"scrolling", false,
						"size", "17",
						"visible", false,
						"zeroPixelHiding", false
					)));
					CoupleLayoutObjectToFeature(obj, "web:subwindow", true);
					/*obj.RefreshHook = new Function('o','
					if ( beos.globals.subwin_height <= beos.globals.subwin_width )
						o.SetVisible(false);
					');*/

					// this is the actual content frame
					obj = contentBag.AddChild(new LayoutFrame(MakeObject(
						"name", beos.globals.framenames.content,
						"location", "",
						"scrolling", true, 
						"size", "*", 
						"visible", true,
						"zeroPixelHiding", true
					)));
					obj.RefreshHook = new Function('o','
					if ( beos.globals.modes.Get("application") == "web" )
						o.SetLocation(beos.globals.mainwin_url);
					');
					
					// this is the content window titleBar
					// it only appears in the horizontal layout
					/*obj = contentBag.AddChild(new LayoutFrame(MakeObject(
						"name", "_be:content:titleBar",
						"location", beos.globals.pages.content_h_titleBar,
						"scrolling", false,
						"size", "20",
						"visible", false,
						"zeroPixelHiding", false
					)));
					CoupleLayoutObjectToFeature(obj, "web:subwindow", true);
					obj.RefreshHook = new Function('o','
					if ( beos.globals.subwin_height > beos.globals.subwin_width )
						o.SetVisible(false);
					');*/
				}

				// this is the divider between the 2 content bags
				obj = webSplit.AddChild(new LayoutFrame(MakeObject(
					"name", "_be:subwindow:divider",
					"location", "",
					"scrolling", false,
					"size", "3",
					"visible", false,
					"zeroPixelHiding", false
				)));
				CoupleLayoutObjectToFeature(obj, "web:subwindow", true);
				obj.RefreshHook = new Function('o','
				if ( beos.globals.subwin_height > beos.globals.subwin_width )
					o.m_location = beos.globals.pages.v_divider;
				else
					o.m_location = beos.globals.pages.h_divider;
				');

				// this bag will hold the subwindow content frame and titlebar
				var subwindowBag = webSplit.AddChild(
					new LayoutBag(MakeObject(
					"name", "_be:application:websplit:subwindowBag",
					"orientation", LayoutBag_SPLIT_HORIZONTAL,
					"size", "*",
					"zeroPixelHiding", false
				)));
				subwindowBag.RefreshHook = new Function('o','
				if ( beos.globals.subwin_height > beos.globals.subwin_width ) {
					o.SetSize(beos.globals.subwin_width);
				} else { 
					o.SetSize(beos.globals.subwin_height);
				}
				');
	
				{
					// this is the subwindow titleBar
					// it only appears in the horizontal layout
					obj = subwindowBag.AddChild(new LayoutFrame(MakeObject(
						"name", "_be:content:titleBar",
						"location", beos.globals.pages.sub_v_titleBar,
						"scrolling", false,
						"size", "17",
						"visible", false,
						"zeroPixelHiding", false
					)));
					CoupleLayoutObjectToFeature(obj, "web:subwindow", true);
					/*obj.RefreshHook = new Function('o','
					if ( beos.globals.subwin_height <= beos.globals.subwin_width )
						o.SetVisible(false);
					');*/

					// this is the subwindow frame
					obj = subwindowBag.AddChild(new LayoutFrame(MakeObject(
						"name", beos.globals.framenames.subwin,
						"location", "",
						"scrolling", true, 
						"size", "*", 
						"visible", false,
						"zeroPixelHiding", false 
					)));
					CoupleLayoutObjectToFeature(obj, "web:subwindow", true);
					obj.RefreshHook = new Function('o','
					if ( beos.globals.features.Get("web:subwindow"))
						o.m_name = beos.globals.framenames.content_prefix 
							+ beos.globals.subwin_name;
						o.SetLocation(beos.globals.subwin_url);
					');

					// this is the subwindow titleBar
					// it only appears in the vertical layout
					/*obj = subwindowBag.AddChild(new LayoutFrame(MakeObject(
						"name", "_be:content:titleBar",
						"location", beos.globals.pages.sub_h_titleBar,
						"scrolling", false,
						"size", "20",
						"visible", false,
						"zeroPixelHiding", false
					)));
					CoupleLayoutObjectToFeature(obj, "web:subwindow", true);
					obj.RefreshHook = new Function('o','
					if ( beos.globals.subwin_height > beos.globals.subwin_width )
						o.SetVisible(false);
					');*/
				}
				// do we need to couple it here?
				CoupleLayoutObjectToFeature(subwindowBag, "web:subwindow", true);
			}
			CoupleLayoutObjectToMode(webSplit, "application", "web");

		} // end application pane
		topLayout.AddChild(applicationPane);

		// ** Media Bar **
		topLayout.AddChild(obj = MakeLayoutFrame("_be:mediabar", 
			beos.globals.pages.mediabar, false, "25", true));
		CoupleLayoutObjectToFeature(obj, "media:visible", true);

		// if the soft keyboard wasn't built with the system,
		// we shouldn't even include it in the layout tree.  
		// It's impossible, absent either the enable_soft_keyboard
		// or enable_japanese_im variables, for the SK/JIM to be
		// shown during the entire lifetime of the browser.  So
		// we don't even pollute the layout tree with it.
		if ( beos.globals.enable_soft_keyboard 
			|| beos.globals.enable_japanese_im )
		{
			topLayout.AddChild(obj = new LayoutFrame(MakeObject(
				"name", beos.globals.framenames.softkeyboard,
				"location", beos.globals.pages.softkeyboard,
				"scrolling", false,
				"size", beos.globals.geometry.softkeyboard_h,
				"visible", false,
				"zeroPixelHiding", false
			)));
			CoupleLayoutObjectToFeature(obj, "softkeyboard:visible", true);
		}
	} // end top pane
}

// ----------------------------------------------------------------------
// initUIState
//
// Set all features and modes to start-up values.
// ----------------------------------------------------------------------
function initUIState() {
	// globals setup
	beos.globals.netron_url = beos.globals.pages.desktop;
	beos.globals.displayed_url = "";
	// set this to blank.  we lazy-load the webhome.
	beos.globals.old_content = ""; 

	var state = beos.globals.state = beos.binder.service.web.state;
	if (state["_launch_url"]()) {
		beos.globals.mainwin_url = state["_launch_url"]();
		beos.globals.displayed_url = beos.globals.mainwin_url;
	} else {
		beos.globals.mainwin_url = beos.globals.pages.webhome;
	}

	beos.globals.mainwin_name = "";
	beos.globals.mainwin_need_src = 1;
	beos.globals.subwin_url = "";
	beos.globals.subwin_name = "";
	beos.globals.subwin_need_src = 1;

	// ** initial mode/feature state **
	beos.globals.features.Close("media:visible");
	beos.globals.features.Close("softkeyboard:visible");
	if ( !beos.globals.use_fullscreen_bookmarks ) 
		beos.globals.features.Close("web:bookmarks");
	beos.globals.features.Close("web:subwindow");
	beos.globals.modes.Set("application", "web");

	beos.globals.topLayout.Refresh(); // apply changes to all layout objects
}

// ======================================================================
// Overridden behavior handlers.
//
// ui_custom.js can override any of the following functions 
// from ui_core.js:
/*
	function onload_func()
	function be_content()
	function be_refresh()
	function be_goto_home()
	function be_goto_search()
	function be_zoom()
	function be_goto_url(url)
	function be_goto_bookmarks()
	function be_goto_settings()
	function be_toggle_settings()
	function be_goto_browse()
	function be_stop()
	function be_reload()
	function be_forward()
	function be_back()
	function be_special_key(n)
	function be_set_status(s)
	function be_timer()
*/

var standard_special_key_handler = beos.globals.message_handlers['$KEY'];
beos.globals.message_handlers['$KEY'] = 
function fic_special_key_handler(msg) {
	switch (msg.keyname) {
		case "fic_email": {
			be_goto_url('http://mail.yahoo.com');
		} break;
		case "fic_news": {
			be_goto_url('http://news.yahoo.com');
		} break;
		case "fic_entertainment": {
			be_goto_url('http://www.moviego.com');
		} break;
		case "fic_shopping": {
			be_goto_url('http://shopping.yahoo.com');
		} break;
		case "fic_finance": {
			be_goto_url('http://finance.yahoo.com');
		} break;	
		case "fic_weather": {
			be_goto_url('http://www.weather.com');
		} break;
		case "fic_food": {
			be_goto_url('http://www.pizzahut.com');
		} break;
		case "fic_help": {
			be_goto_url('file://$RESOURCES/Tutorial/index.html');
		} break;
		case "reload": {
			be_reload();
		} break;
		case "addbookmark": {
			be_add_bookmark();
			be_goto_bookmarks();
		} break;
		case "print": {
			be_print_content();
		} break;
		default: {
			standard_special_key_handler(msg);
		} break;
	}
}


function be_goto_search() {
	var nav = be_navbar();
	var url = nav.GetURLText();
	var newurl;
	if ( url != '' && url.indexOf(':') == -1 ) {
		newurl = ("http://www.google.com/search?q=");
		for(var i=0; i<url.length; i++) {
			if (url.charAt(i) == ' ') 
				newurl += '%20';
			else if (url.charAt(i) == '&')
				newurl += '%26';
			else 
				newurl += url.charAt(i);
		}
		nav.SetURLText(newurl);
	} else {
		newurl = "http://www.google.com/";
	}
	be_goto_url(newurl);
}

function be_zoom(mode) {
	top.be_textzoom_change(mode); // now in ui_core.js
}

function be_goto_settings(optional_preload_panel)
{
	// if ( null != optional_preload_panel )
	var FORCE = true;
	beos.globals.preload_settings_panel = optional_preload_panel;
	top.be_goto_url(beos.globals.pages.settings, FORCE);
	if ( null != optional_preload_panel ) {
		// gotta fix the selector
		setTimeout('var selector = find_descendent_frame(be_content(), "nav"); selector.location.reload(true);', 250);
	}
}

// be_print_content()
// overrides version in core.js to provide the name of the frame to print.
function be_print_content() {
	var framename = beos.globals.framenames.content; 
	
	var pr = beos.binder.service.printing; 
	var cur = (pr!=null)?(pr.Selected):null; 
	if ( cur != null ) { 
		pr[cur].Print(framename); 
	} else { 
		beos.dprintf("be_print_content: error: could not get current printer from binder"); 
	}
}

// end of ui_custom.js
