//function getChromeWindow() {
//	return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
//				 .getInterface(Components.interfaces.nsIWebNavigation)
//				 .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
//				 .rootTreeItem
//				 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
//				 .getInterface(Components.interfaces.nsIDOMWindow);
//}
//
//function getTabBrowser() {
//	return getChromeWindow().gBrowser.getBrowserForDocument(document);		
//}

/******* functions for localization ***/
var stringBundle = contentAPI.stringBundle;
var _ = contentAPI._;

function _onmouseover(event) {
	if (event.target.title || !event.target.getAttribute('_title'))
		return;
	
	event.target.title = _(event.target.getAttribute('_title'));
}

document.addEventListener('mouseover', _onmouseover, false);

window.addEventListener('unload', function(event) {
	document.removeEventListener("mouseover", _onmouseover, false);
}, false);
/*************    end    **************/

/******* functions for tab views ******/
function switchView(tabId, changeHistory) {
	var tab = _$(tabId);
	if (!tab)
		return;
	
	function getOffset (node) {
	    if (node.parentNode == document.documentElement) {
	        return {
				top: node.offsetTop,
				left: node.offsetLeft
			};
	    }
	    
	    var offsetPos = {
	        top: node.offsetTop,
	        left: node.offsetLeft
	    };
	
	    var parentOffset = getOffset(node.parentNode);
	    offsetPos.top += parentOffset.top;
	    offsetPos.left += parentOffset.left;
	
	    return offsetPos;
	}
		
	var content_id = CSS.getRel(tab);
	
	var contents = $('div.content-box > .content');
	for (var i = 0; i < contents.length; i++) {
		var child = contents[i];
		
		if (child.id == content_id) {
			child.style.display = '';
		} else {
			child.style.display = 'none';
		}
	}
	
	var tabs = $('div.tab-box > div.tab');
	for (var i = 0; i < tabs.length; i++) {
	    if (tabs[i] == tab) {
			CSS.add(tabs[i], 'selected');
			var offset = getOffset(tabs[i]);
			_$('tab-mask').style.left = offset.left + 'px';
			_$('tab-mask').style.top  = offset.top  + 'px';
	    } else {
			CSS.del(tabs[i], 'selected');
	    }
	}

	contentAPI.setPref('tabview', tabId);
	
	if (false !== changeHistory) {
		ViewHistory.muteChangeHistory('switchview-' + tabId);
	}
}

var viewSwitcher = {
	switchTo: function(id, changeHistory) {
		switchView(id, changeHistory);
		switch (id) {
			case "tab-local":
				appManager.init();
				break;
			case "tab-history":
				appHistory.init();
				break;
			case "tab-install":
				if (contentAPI.getLocation(_$('web-apps-frame')) == "about:blank") {
					contentAPI.setLocation(_$('web-apps-frame'), appManager.appcenterDomain);
				}
				break;
		}
		_$t.track(_$t.view.type, _$t.view.actions.SWITCH_VIEW, id);
	}
};

function _historyEventListener(event) {
	viewSwitcher.switchTo(event.data);
}
ViewHistory.addHistoryEventListener('switchview', _historyEventListener, false);

var _appPrefObserver = {
	onShowAppBookmarks: function(uid) {
		viewSwitcher.switchTo("tab-local");
	}
};

contentAPI.appCenter.appPref.addObserver(_appPrefObserver);

window.addEventListener("unload", function() {
	ViewHistory.removeHistoryEventListener('switchview', _historyEventListener, false);
	contentAPI.appCenter.appPref.removeObserver(_appPrefObserver);
}, false);

var _initFuncs = [];
function appCenterInit() {
	_initFuncs.forEach(function(func) {
		try {
			func();
		} catch (e) {
			return;
		}
	});
}

function registerInitListener(func) {
	_initFuncs.push(func);
}

registerInitListener(function(event) {
	// Initial history
	var tabview = contentAPI.getPref('tabview');
	if (tabview != 'tab-local' && tabview != 'tab-history' && tabview != 'tab-install') {
		tabview = 'tab-local';
	}
	viewSwitcher.switchTo(tabview);	
	
	// add event listeners
	$("#btn_do_search").click(function() {	
		switchView("tab-install");
		appManager.searchApp($("#search_input_install").val());
	});
	$("#search_input_install").bind("keypress", function(evt) {
		if (evt.keyCode == 13) {
			switchView("tab-install");
			appManager.searchApp(this.value);
		}
	});
	$("#search_input_install").bind("change", function() {
		switchView("tab-install");
		appManager.searchApp(this.value);
	});
	$("#search_input_install").bind("mouseover", function() {
		this.focus();
	});
	$("#search_input_install").bind("focus", function() {
		this.select();
	});
	$("#tab-local").click(function() {
		viewSwitcher.switchTo(this.id);
	});
	$("#tab-history").click(function() {
		viewSwitcher.switchTo(this.id);
	});
	$("#tab-install").click(function() {
		viewSwitcher.switchTo(this.id);
	});
	$("#apps-history-box button.opt-btn-clear").click(function() {
		appHistory.clearHistory();
	});
});
/**************** end ******************/