// Global variables.
var landscape = false;
var widgetMenu;
var loginScreen, updatesScreen, newgameScreen, inboxScreen, outboxScreen, settingsScreen, messageScreen, replyScreen;
var miniView;
var fbService;
var FBSERVICE_URL = "www.fatbookie.com";


var LOGIN_USER;
var LOGIN_SESSION_ID;

var INBOX_CURRENT_PAGE = 1;
var OUTBOX_CURRENT_PAGE = 1;

var MESSAGES_ITEMS_PER_PAGE = 5;
var FRIENDS_LIST_ITEMS_LIMIT = 10;
var UPDATES_LIST_ITEMS_LIMIT = 10;

//----------------------------------------------------------------------------------------------------------------------

function init() {

	// Load all strings.		
	loadStringTable();		

	// Set the correct stylesheet depending on whether we are running
	// on bigger or smaller screens. 
	Helper.applyCorrectStyles();

	// Disable cursor mode.	
	widget.setNavigationEnabled( false );

	// Create the FatBookie service.
	fbService = new FBService();

	// Create the screens.
	widgetMenu = new Menu();
	loginScreen = new LoginScreen();
	updatesScreen = new UpdatesScreen();
	inboxScreen = new InboxScreen();
	outboxScreen = new OutboxScreen();
	messageScreen = new MessageScreen();
	replyScreen = new ReplyScreen();
	newgameScreen = new NewgameScreen();
	gameOptionsScreen = new GameOptionsScreen();
	publishGameScreen = new PublishGameScreen();
	inviteFriendsScreen = new InviteFriendsScreen();
	settingsScreen = new SettingsScreen();
	miniView = new MiniView();
	
	// Set screen size.
	onOrientationChange();
	
	// Show first screen.
	widgetMenu.activate( Menu.LOGIN_SCREEN );
	loginScreen.loginIfPossible();
	
	// Known Issue: Work around a resize event not being triggered on
	//              older Nokia 5800 devices.
	//WrtHelper.startResizePoller();
}

//----------------------------------------------------------------------------------------------------------------------

function loadStringTable() {
	for ( var name in StringTable.HTML ) {
		var element = document.getElementById( name );
		element.innerHTML = StringTable.HTML[name];
	}
}

//----------------------------------------------------------------------------------------------------------------------

function setViewMode(){
	var isInMiniView = Helper.isMiniViewMode();
	
	if ( isInMiniView ) {
		// Hide main screen.
		Helper.show( "container", false );

		// ... and show MiniView.
		Helper.show( "mini_view", true );
		miniView.onActivated();
	} else {
		// Hide MiniView.
		Helper.show( "mini_view", false );
		miniView.onDeactivated();
		
		// ... and show main screen.
		Helper.show( "container", true );
	}
}

//----------------------------------------------------------------------------------------------------------------------

function onResize(){
	if ( !Helper.isMiniViewMode() ) {
		resizeNormalView();
	}
               
	setViewMode();          
}

//----------------------------------------------------------------------------------------------------------------------

function resizeNormalView() {
	// Get the height or width.
	var screenSize = Helper.getScreenSize();	
	
	// If we're on big screens and we're rotating, update the
	// bottom menu layout (bottom with portrait, right with
	// landscape). Also update the screen container to be smaller
	// for the amount of the menu to avoid overlapping.
	var menuStrip = document.getElementById( "menu_strip" );
	var topBar = document.getElementById( "top_bar" );
	var container = document.getElementById( "container" );
	var content = document.getElementById( "content" );
	var contentSlider = document.getElementById( "content-slider" );

	container.style.width = screenSize.width + "px";
	container.style.height = screenSize.height + "px";
		
	var largeScreen = Helper.isLargeScreen(); 
	if ( largeScreen ) {
		// Hide softkeys.
		menu.hideSoftkeys();
				
		// Show menu and resize content.
		var hidden = (menuStrip.className.indexOf( "hidden" ) != -1);		
		if ( Helper.isLandscape() ) {
			menuStrip.className = "buttons_bottom_landscape";

			var width = (screenSize.width - menuStrip.offsetWidth) + "px";
			var width_content = (screenSize.width - menuStrip.offsetWidth - 50) + "px"; // 50 for slider
			if ( widgetMenu.activeScreen == Menu.LOGIN_SCREEN ) {
				// If we're on login screen the menu isn't shown so update the width.
				width = screenSize.width + "px";
				width_content = (screenSize.width - 50) + "px"; // 50 for slider
			}
			topBar.style.width = width;
			content.style.width = width_content;
			content.style.height = (screenSize.height - topBar.offsetHeight) + "px";
			contentSlider.style.height = (screenSize.height - topBar.offsetHeight - 40) + "px"; // 40 for margins
		} else { //portrait
			menuStrip.className = "buttons_bottom";

			var width = screenSize.width + "px";
			var width_content = (screenSize.width - 50) + "px"; // 50 for slider
			topBar.style.width = width;
			content.style.width = width_content;
			content.style.height = (screenSize.height - menuStrip.offsetHeight - topBar.offsetHeight) + "px";
			contentSlider.style.height = (screenSize.height - menuStrip.offsetHeight - topBar.offsetHeight - 40) + "px"; // 40 for margins
		}
		// If the menu wasn't visible, hide it again.
		if ( hidden ) {
			menuStrip.className += " hidden";
		}
	} else {	
		// Hide menu strip and resize content.
		menuStrip.className = "hidden";
		content.style.width = screenSize.width + "px";
		content.style.height = (screenSize.height - topBar.offsetHeight) + "px";
	}

}

//----------------------------------------------------------------------------------------------------------------------

var ImagePreloader = {
	list: [],
	images: [],
	
	add: function( images ) {
		this.list = this.list.concat( images );
	},
	
	start: function() {
		for (var i = 0; i < this.list.length; i++) {
			var img = new Image();
			img.src = this.list[i];
			
			this.images.push( img );
		}		
	}
}

//----------------------------------------------------------------------------------------------------------------------

// Preload images before they are displayed to avoid flicker
// when loading them up for the first time.
function preloadImages() {
	if ( Helper.isLargeScreen() ) {
		ImagePreloader.add( [
			"images/flag.png",
			"images/colapse.gif",
			"images/button_bg.png",
			"images/small_button_bg.png",
			"images/small_button_bg_activated.png",
			"images/big_logo.png",
			"images/top_bg.png",
			"images/top_bg_landscape.png",
			"images/tr_bg.png",
			"images/checkbox.png",
			"images/checkbox_activated.png" ] );		
	} else {
		ImagePreloader.add( [
			"images/flag.png",
			"images/colapse.gif",
			"images/small_logo.png",
			"images/small_top_bg.png",
			"images/small_button_bg_activated.png",
			"images/small_button_bg.png" ] );				
	}

	ImagePreloader.start();
}

//----------------------------------------------------------------------------------------------------------------------

