/**
 * @name		Vimperator Plugin For "IE Tab"
 * @author		Chappell.Wat
 * @version		0.0.1
 * @date		2008-2-1
 * @link		http://www.QuChao.com/?=161
 * @desc		Adding command and mapping for "IE Tab"
 *
 * "IE Tab" is a firefox extension 
 *  developed by PCMan and yuoo2k
 *  http://ietab.mozdev.org/
 *
 * Released under the GPL license
 *  http://www.gnu.org/copyleft/gpl.html
 *
 * ver 0.0.1 @ 2008-2-1
 *  Experimental Release
 *
 * Usage: (Suppose 'alwaysNewTab' = false)
 *  - in NORMAL mode:
 *		:ie		Switch rendering engine of current page;
 *		:ie!	Use new tab to switch rendering engine;
 *		:ie e	View Page in the External Application;
 *		:ie o	Open IE Tab Options.
 *  - in HINT mode:
 *		i		Open Link in IE Tab;
 *		I		Open Link in a new IE Tab;
 *		e		Open Link in Ext.App;
 *
 */

(function(){

/**
 * IE Tab CMD
 */
vimperator.commands.add(new vimperator.Command(['ie[tab]', 'IE[TAB]'],
	function(args, special){
		if ('object' == typeof(gIeTab)) {
			args = args.toLowerCase();
			if (special) {
				gIeTab.switchEngine(!gIeTab.getBoolPref('ietab.alwaysNewTab', false));
			} else if (!args) {
				gIeTab.switchEngine(gIeTab.getBoolPref('ietab.alwaysNewTab', false));
			} else {
				var matches = args.match(/^\s*(e|o)?\s*$/);
				if (!matches) {
					vimperator.echoerr('Unknown option: ' + args);
				} else {
					if ('e' == args) {
						gIeTab.viewPageInExtApp();
					} else {
						gIeTab.openPrefDialog();
					}
				}
			}
		} else {
			vimperator.echoerr('IE Tab is not installed or disabled');
		}
		return;
	}, {
		usage: ['ie[tab][!] {arg}', 'IE[TAB][!] {arg}'],
		shortHelp: 'IE Tab Functions',
		help: '<code class=\"command\">:ie</code> is the easiest way to switch the rendering engine。Whether to open a new tab (or in current tab) is based on the IETab setting.<br />' +
			  'If the <code class=\"argument\">[!]</code> is specified, it will refer to the opposed value.<br /><br />' +
			  'Usage: (Suppose \'alwaysNewTab\' = false)<br />' +
			  '<ol>' +
			  "<li><code class=\"command\">:ie</code>\t\t\t\tSwitch rendering engine of current page;</li>" +
			  "<li><code class=\"command\">:ie!</code>\t\tUse new tab to switch rendering engine;</li>" +
			  "<li><code class=\"command\">:ie e</code>\tView Page in the External Application;</li>" +
			  "<li><code class=\"command\">:ie o</code>\tOpen IE Tab Option Panel.</li>" +
			  '</ol>' +
			  'Note: Case insensitive.'
	}
));


/**
 * IE Tab HINTS
 */
vimperator.mappings.add(new vimperator.Map(vimperator.modes.HINTS, ['i'],
	function(){
		if ('object' == typeof(gIeTab)) {
            var elems = vimperator.hints.hintedElements;
			var tmp = '';
			for (var i = 0; i < elems.length; i++) {
				tmp = elems[i].refElem.href;
				if ('undefined' != typeof(tmp) && 0 < tmp.length) {
					gIeTab.switchEngine(gIeTab.getBoolPref('ietab.alwaysNewTab', false));
				}
			}
		} else {
			vimperator.hints.disableHahMode(null,true);
			vimperator.echoerr('IE Tab is not installed or disabled');
		}
		return;
	}, {
		cancelMode:true,
		alwaysActive:false
	}
));

vimperator.mappings.add(new vimperator.Map(vimperator.modes.HINTS, ['I'],
	function(){
		if ('object' == typeof(gIeTab)) {
            var elems = vimperator.hints.hintedElements;
			var tmp = '';
			for (var i = 0; i < elems.length; i++) {
				tmp = elems[i].refElem.href;
				if ('undefined' != typeof(tmp) && 0 < tmp.length) {
					gIeTab.switchEngine(!gIeTab.getBoolPref('ietab.alwaysNewTab', false));
				}
			}
		} else {
			vimperator.hints.disableHahMode(null,true);
			vimperator.echoerr('IE Tab is not installed or disabled');
		}
		return;
	}, {
		cancelMode:true,
		alwaysActive:false
	}
));

vimperator.mappings.add(new vimperator.Map(vimperator.modes.HINTS, ['e'],
	function(){
		if ('object' == typeof(gIeTab)) {
            var elems = vimperator.hints.hintedElements;
			var tmp = '';
			for (var i = 0; i < elems.length; i++) {
				tmp = elems[i].refElem.href;
				if ('undefined' != typeof(tmp) && 0 < tmp.length) {
					gIeTab.viewPageInExtApp();
				}
			}
		} else {
			vimperator.hints.disableHahMode(null,true);
			vimperator.echoerr('IE Tab is not installed or disabled');
		}
		return;
	}, {
		cancelMode:true,
		alwaysActive:false
	}
));

})();
