;(function($){
	// if no console, ignore log msgs
	if (!window.console || document.all) { console = {log:function(){}, warn:function(){var i=0,out=[];for(; i<arguments.length; i++)out.push(arguments[i]);alert(out.join("\n"))}}; };

	window.TBox = {
		can_init: false,
		isDEV: /dev\.tbar/i.test(window.location.toString()),
		is_meebo: tbox_skin == "mb",
		is_meebo_acc: tbox_skin == "mb_acc",
		is_web: tbox_skin == "web",
		is_mobile: tbox_skin == "mobile",
		is_trace_on: false, // set to true to see debug msgs on bugzilla's console

// --- 1. Methods for Meebo Accelerator ONLY  -------------------------------
		init: function() {
			//ensure if all pre-necessary data is already setted
			if (!this.can_init) {
				window.setTimeout(function(){ TBox.init(); }, 750);
				return false;
			}
			//run the initializers
			this.UI.init();
			this.URL.init();
			this.Login.init();
			this.User.init();
			this.extUsers.init();
			this.Faq.init();
			this.Inbox.init();
			this.Settings.init();
			this.EmailCompose.init();

			//check if url hash, if empty redirect to login
			if ($.trim(this.URL.getHash()).length === 0)
				this.URL.changeURL('sessions/new')
		},

		// Actions called when a specific page is visited by the user. The hash keys should follow this structure:  <controller>:{<action1>,<actionN>}
		// No need to create keys for pages that don't need On Show actions
		// -- Used ONLY by Meebo Accelerator version --
		on_show: {
			super_contacts: {
				index: function() {TBox.Friends.On_Show()},
				share: function() {TBox.Share.On_Show()}
			},
			news_feed: {
				index: function() {TBox.Newsfeed.On_Show()}
			},
			inbox: {
				index: function() {TBox.Inbox.On_Show()}
			},
			email: {
				"new": function() {TBox.EmailCompose.On_Show()}
			},
			status_updates: {
				index: function() {TBox.Status.On_Show()}
			},
			settings: {
				index: function() {TBox.Settings.On_Show()}
			}
		},
		on_hide: {
			settings: {
				index: function() {TBox.Settings.On_Hide()}
			},
			inbox: {
				index: function() {TBox.UI.hideErrorMessage(true)}
			}
		},


// --- 2. PUBLIC METHODS for all skins ----------------------------------

		Common: {
			Template_HTML2JS: function(tpl) {
				if (!tpl) return "";

				var pos_if = 0,
					pos_endif = 0,
					ifs_html = [],
					timestart = new Date();

				// Step 1: create an array with containing each string block of IFs/ELSE/ENDIFs, replacing it with a reference: <@idx@>
				while (1) {
					pos_endif = tpl.indexOf("{/if}") + 5;
					if (pos_endif >= 5) {
						pos_if = tpl.lastIndexOf("{if", pos_endif); // finds the position for the endif's IF
						ifs_html.push(tpl.substring(pos_if, pos_endif).replace(/[\r\n]/g,"")); // save each string "{if ... /if}" into an array
						tpl = tpl.substring(0, pos_if) +"<@"+ (ifs_html.length-1) +"@>"+ tpl.slice(pos_endif); // replace the saved string from the parsed template with a reference
					}
					else break; if (new Date()-timestart>3000) {console.log('1: Taking too long. Infinite loop? We forced breaking out of WHILE!');break}
				}

				tpl = "'"+ tpl.replace(/'/g, "\\'").replace(/\r\n|\n/g, "'+\n'") +"'"; // Escapes evert single quote. Then add ' at the beggining and end of every line.

				// Step 2: replace each block of IF/ELSE/ENDIF with a JS string concatenation syntax
				for (var i in ifs_html)
					ifs_html[i] = this.Replace_statements(ifs_html[i]);

				tpl = this.Replace_statements(tpl);

				// Step 3: re-assemble the template
				while (tpl.match(/<@(\d+)@>/)) {
					var idx = RegExp.$1; // idx of IF block
					tpl = tpl.replace(new RegExp("<@"+ idx +"@>"), ifs_html[idx]);
					if (new Date()-timestart>3000) {console.log('2: Taking too long. Infinite loop? We forced breaking out of WHILE!');break}
				}

				// remove empty strings
				tpl = tpl
					.replace(/'([\s\t]{2,100})?'([\s\t]+)?\+/g, "")
					.replace(/\+([\s\t]+)?'([\s\t]{2,100})?'/g, "")
				return tpl;
			},

			Replace_statements: function(if_block) {
				if (!if_block.match(/{else}/)) if_block = if_block.replace(/{\/if}/, "{/elsif}");

				// replace IF/ELSE/ENDIF: {if cond}bla{else}xpto{/if} => (cond ? 'bla' : 'xpto')
				return if_block
					.replace(/{(\s+)?if (.+?)}/g, "'+ ($2 ? '") 		// replace {if xxx} with '+ (xxx ? '
					.replace(/{(\s+)?else(\s+)?}/g, "' : '") 			// replace {else} 	with ' : '
					.replace(/{(\s+)?\/elsif(\s+)?}/g, "' : '') +'") 	// replace {elsif} 	with ' : '') +'
					.replace(/{(\s+)?\/if(\s+)?}/g, "') +'") 			// replace {else} 	with ' : '

				// replace VARS: ${var} => '+ var +'
					.replace(/\${(.+?)}/g, "'+ ($1) +'")
					.replace(/\$%7B(.+?)%7D/g, "'+ ($1) +'")
			}

		}

	};

})(jQuery);

