(function() {
	// Check if this is app page.
	var appInfo = null;
	
	function $$(str) {
		return document.querySelectorAll(str);
	}
	
	if (document.getElementById('runtime')) {
		appInfo = getIframeAppInfo();
	} else if (document.getElementById('app_flash')) {
		appInfo = getFlashAppInfo();
	}
	
	if (!appInfo) {
		return;
	}
	
	function injectOpenButton() {
		var menuBox = $$('#AppRun > div.fullscreen-showcase-menu > ul.right');
		if (menuBox.length == 0) {
			return false;
		} else {
			menuBox = menuBox[0];
		}
		
		var button = document.createElement('li');
		button.innerHTML = '<a href="javascript:;" class="coll" title="作为火狐应用打开">打开</a>';
		menuBox.insertBefore(button, menuBox.childNodes[0]);
		
		return button.childNodes[0];
	}
	
	function getAppUID() {
		var tmp = /http:\/\/app.baidu.com\/widget\?appid=(\d+)/i.exec(document.location.href);
		if (tmp && tmp.length == 2) {
			return 'bapp_id_' + tmp[1];
		}
		
		// get appid from iframe src
		if (document.getElementById('runtime')) {
			var tmp = /http:\/\/app.baidu.com\/(\d+)/i.exec(document.getElementById('runtime').src);
			if (tmp && tmp.length == 2) {
				return 'bapp_id_' + tmp[1];
			}
		}
		
		var appName = document.location.pathname.split('/')[1];
		return 'bapp_' + appName;
	}
	
	function getBAppId() {
		var tmp = /bapp_id_(\d+)/i.exec(getAppUID());
		return tmp && tmp.length == 2 ? parseInt(tmp[1]) : null;
	}
	
	function getAppName() {
		return $$('body > div.wrapper > div.content > div.app-content div#AppRun > div.app-detail-title > h2')[0].textContent;
	}
	
	function getBAppIdFromHref(href) {
		var tmp = /appid=(\d+)/i.exec(href);
		return tmp && tmp.length == 2 ? tmp[1] : null;
	}
	
	function getIconUrl() {
		if (document.getElementById('DeveloperInfo')) {
			var id = getBAppId();
			id = id ? id : getBAppIdFromHref(document.location.href);
			
			var infoLinks = $$('#DeveloperInfo dl.app-single-item > dt > a');
			
			for (var i = 0; i < infoLinks.length; i++) {
				var link = infoLinks[i];
				if (id && id == getBAppIdFromHref(link.href)) {
					return link.getElementsByTagName('img')[0].src;	
				}
				
				if (link.href == document.location.href) {
					return link.getElementsByTagName('img')[0].src;	
				}
			}
			
			return $$('#DeveloperInfo div.developer-icon img')[0].src;
		} else if (document.getElementById('appDesc')) {
			return $$('#appDesc div.side-bar div.developer-icon img')[0].src;
		}
		
		return '';
	}
	
	function getFlashAppInfo() {
		var embed = document.getElementById('app_flash').getElementsByTagName('embed')[0];
		
		return {
			appUID: getAppUID(),
			appName: getAppName(),
			type: 2,
			iconUrl: getIconUrl(),
			appArgs: {
				location: embed.src,
				height: embed.offsetHeight,
				width: embed.offsetWidth
			}
		};
	}
	
	function getIframeAppInfo() {
		var iframe = document.getElementById('runtime');
		if (iframe.contentDocument.getElementsByTagName('iframe').length == 1) {
			iframe = iframe.contentDocument.getElementsByTagName('iframe')[0];
		}
		
		return {
			appUID: getAppUID(),
			appName: getAppName(),
			type: 2,
			iconUrl: getIconUrl(),
			appArgs: {
				location: iframe.src,
				height: iframe.offsetHeight > 500 ? 500 : iframe.offsetHeight,
				width: 560
			}
		};
	}
	
	var button = injectOpenButton();
	if (!button) {
		return;
	}
	
	button.addEventListener('click', function(event) {
		try {
		baiduAppHelper.openInAppCenter(appInfo);
		} catch (e) {
			window.alert(e);
		}
	}, false);
})();
