
var bizmo = bizmo || { };

//var debug=true;
var defaultBizmoIdInputText = "";

function getDomElements() {
	if (debug) console.log("Canvas getDomElements");
	var elements = {
		loader: $("#loading"),
		bizmo_id_input: $("#bizmo_id"),
		settings: $("#dummy"),
		settings_links: $("#settings-links"),
		settings_toggle: $("#settings-toggle"),
		settings_save_button: $("#settings-save"),
		save_loading: $("#save-loading"),
		debug_container: $("#debug-container"),
		desc_container: $("#desc-container"),
		flash_container: $("#flash-container"),
		flash_container_inner: $("#flash-container-inner"),
		app_intro: $("#app-intro"),
		app_installed: $("#app-installed"),
		app_not_installed: $("#app-not-installed"),
		installed_bizmo_id: $("#installed-bizmo-id"),
		change_bizmo_id_link: $("#change-bizmo-id-link")
	};
	defaultBizmoIdInputText = elements.bizmo_id_input.val();

	elements.bizmo_id_input.focus(function() {
		if (debug) console.log("Bizmo id input focus, defaultBizmoIdInputText is " + defaultBizmoIdInputText);
		if ($(this).val() == defaultBizmoIdInputText) {
			$(this).val("");
		};
	});

	elements.bizmo_id_input.blur(function() {
		if (debug) console.log("Bizmo id input blur, defaultBizmoIdInputText is " + defaultBizmoIdInputText);
		if ($(this).val() == "") {
			$(this).val(defaultBizmoIdInputText);
		};
	});

	return elements;
};

function updateBizmoId(bizmo_id) {
	if (debug) console.log("Canvas updateBizmoId");

	/**
	 * Hide intro
	 */
	this.el.app_intro.hide();

	/**
	 * Update copy with new bizmo id
	 */
// 	this.el.installed_bizmo_id.text(bizmo_id);
	this.el.bizmo_id_input.val(bizmo_id);

	/**
	 * Show app installed text
	 */
	this.el.app_installed.show();

	/**
	 * Hide app NOT installed text
	 */
	this.el.app_not_installed.hide();

	/**
	 * Hide settings and sign-up form
	 */
	this.el.settings.hide();
	$('#sign-up-form').hide();


	/**
	 * Get the bizmo app object
	 */
// 	var app = null;
	/**
	 * This method gets called in 2 scopes
	 */
// 	if (this.bizmo) {
// 		app = this.bizmo;
// 	} else {
// 		app = this;
// 	}
// 	if (debug) console.debug(app);

	$('#desc-container').css('width', "320px");
	$('#flash-container').show();
	this.uiDrawMainPanel(bizmo_id);

};

function render() {
	if (debug) {
		console.log("Canvas render, owner_bizmo_id " + this.owner_bizmo_id + ", owner is ");
		console.debug(this.owner);
		this.showEnvironmentDebug();
	}

// 	console.debug(state);
// 	console.debug(this);
	this.el.loader.hide();

	if (this.owner == null) {

		/**
		* Show app NOT installed text
		*/
		this.el.app_not_installed.show();

		/**
		* Hide app installed text
		*/
		this.el.app_installed.hide();

		gadgets.window.adjustHeight();

		return;
	}

	this.el.app_not_installed.hide();
	this.el.app_installed.show();

	var app = this;
	if (this.owner.isViewer()) {
// 		this.el.change_bizmo_id_link.click(function() {
// 			if (debug) console.log("Change Bizmo ID Link clicked");
// 			app.el.settings.toggle();
// 		});
		if (debug) console.log("Owner is viewer");

		if (this.owner_bizmo_id) {

			/**
			* State 1:
			*   + App installed
			*   + Bizmo id set
			*/
			if (debug) console.log("Setting bizmo id to " + this.owner_bizmo_id);
			defaultBizmoIdInputText = this.owner_bizmo_id;
			this.updateBizmoId(this.owner_bizmo_id);

		} else {

			/**
			* State 2:
			*   + App installed
			*   + NO bizmo id set
			*/
// 			this.el.app_intro.show();

// 			this.el.settings.show();

// 			$('#sign-up-form').show();
		}
	}

	/**
	* Show settings links
	*/
// 	el.settings_links.show();

	/**
	* Listen for save button click
	*/
	this.el.settings_save_button.click(function() {
		if (debug) console.log("settings_save_button clicked");
		app.submitForm();
		return false;
	});

	/**
	* On toggle click, toggle settings visibility
	*/
// 	this.el.settings_toggle.click(function() {
// 		app.uiToggleSettings();
// 	});

	gadgets.window.adjustHeight();
};

// function test1() {
// 	console.info("Canvas TEST1, owner is ");
// 	console.debug(owner);
// }

gadgets.util.registerOnLoadHandler(function() {
	bizmo = new opensocial.Bizmo();
// 	console.log("TEST1 " + bizmo.test1);
// 	console.log("TEST2 " + bizmo.test2);
	bizmo.getDomElements = getDomElements;
	bizmo.updateBizmoId = updateBizmoId;
	bizmo.render = render;

// 	bizmo.test1 = test1;
	bizmo.init();
// 	bizmo.test1();
});
