//jQuery(document).bind('keydown', 'Ctrl+x', jQuery('#terminal').jqmShow());
/*
 * Editable 1.3.3
 *
 * Copyright (c) 2009 Arash Karimzadeh (arashkarimzadeh.com)
 * Licensed under the MIT (MIT-LICENSE.txt)
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Date: Mar 02 2009
 */
//jQuery.fn.editable=function(options){var defaults={onEdit:null,onSubmit:null,onCancel:null,editClass:null,submit:null,cancel:null,type:"text",submitBy:"blur",editBy:"click",options:null};if(options=="disable"){return this.unbind(this.data("editable.options").editBy,this.data("editable.options").toEditable)}if(options=="enable"){return this.bind(this.data("editable.options").editBy,this.data("editable.options").toEditable)}if(options=="destroy"){return this.unbind(this.data("editable.options").editBy,this.data("editable.options").toEditable).data("editable.previous",null).data("editable.current",null).data("editable.options",null)}var options=$.extend(defaults,options);options.toEditable=function(){$this=$(this);$this.data("editable.current",$this.html());opts=$this.data("editable.options");$.editableFactory[opts.type].toEditable($this.empty(),opts);$this.data("editable.previous",$this.data("editable.current")).children().focus().addClass(opts.editClass);if(opts.submit){$("<button/>").appendTo($this).html(opts.submit).one("mouseup",function(){opts.toNonEditable($(this).parent(),true)})}else{$this.one(opts.submitBy,function(){opts.toNonEditable($(this),true)}).children().one(opts.submitBy,function(){opts.toNonEditable($(this).parent(),true)})}if(opts.cancel){$("<button/>").appendTo($this).html(opts.cancel).one("mouseup",function(){opts.toNonEditable($(this).parent(),false)})}if($.isFunction(opts.onEdit)){opts.onEdit.apply($this,[{current:$this.data("editable.current"),previous:$this.data("editable.previous")}])}};options.toNonEditable=function($this,change){opts=$this.data("editable.options");$this.one(opts.editBy,opts.toEditable).data("editable.current",change?$.editableFactory[opts.type].getValue($this,opts):$this.data("editable.current")).html(opts.type=="password"?"*****":$this.data("editable.current"));var func=null;if($.isFunction(opts.onSubmit)&&change==true){func=opts.onSubmit}else{if($.isFunction(opts.onCancel)&&change==false){func=opts.onCancel}}if(func!=null){func.apply($this,[{current:$this.data("editable.current"),previous:$this.data("editable.previous")}])}};this.data("editable.options",options);return this.one(options.editBy,options.toEditable)};$.editableFactory={text:{toEditable:function($this,options){$("<input/>").appendTo($this).val($this.data("editable.current"))},getValue:function($this,options){return $this.children().val()}},password:{toEditable:function($this,options){$this.data("editable.current",$this.data("editable.password"));$this.data("editable.previous",$this.data("editable.password"));$('<input type="password"/>').appendTo($this).val($this.data("editable.current"))},getValue:function($this,options){$this.data("editable.password",$this.children().val());return $this.children().val()}},textarea:{toEditable:function($this,options){$("<textarea/>").appendTo($this).val($this.data("editable.current"))},getValue:function($this,options){return $this.children().val()}},select:{toEditable:function($this,options){$select=$("<select/>").appendTo($this);$.each(options.options,function(key,value){$("<option/>").appendTo($select).html(value).attr("value",key)});$select.children().each(function(){var opt=$(this);if(opt.text()==$this.data("editable.current")){return opt.attr("selected","selected").text()}})},getValue:function($this,options){var item=null;$("select",$this).children().each(function(){if($(this).attr("selected")){return item=$(this).text()}});return item}}}

//jQuery('#terminal').jqmShow();

if (window.location.href.indexOf('dbg=1') >= 0)
	alert(0);

var terminal; // = '<!-- Please wait... <img src="images/ajax-loader.gif" alt="loading" /> -->	';
terminal = '<div class="shade" id="shade">&nbsp;</div>\n';
terminal += '<div class="terminal" id="terminal"><div class="closeButton" onClick="jQuery(\'#terminal\').hide();jQuery(\'#shade\').hide();">X</div>\n';
terminal += '	*************************\n';
terminal += '	* Terminal mode - Press *\n';
terminal += '	* Ctrl-Shift-Z to hide  *\n';
terminal += '	* this window.          *\n';
terminal += '	*************************\n';
terminal += '&gt; <input id="cmdline" name="cmdline" type="text" autocomplete="off" style="text-align: left;" size="55" /></div>';


jQuery('#container').append(terminal);

if (window.location.href.indexOf('dbg=1') >= 0)
	alert('terminal added; ');

jQuery('td.editable').editable({onSubmit:update});
jQuery('div.editable').editable({onSubmit:update});

function update(content){
    this; //this is the current jQuery object
    if (confirm(jQuery(this).attr("id")+' >> '+content.current+' : '+content.previous)) {
		jQuery.ajax({
			url: "ajax.php",
			processData: false,
			data: "do=upd&on="+jQuery(this).attr("id")+"&data="+content.current,
			success: function() {  }
		});
	}
}

/*
jQuery('#container').keypress(function(event) {
  alert ('keypress was '+event.which);
});
*/
var keepChecking = true;

jQuery(document).bind('keypress', function(e) { 
	if (window.location.href.indexOf('dbg=1') >= 0) {
		if (keepChecking)
			keepChecking = confirm ('keypress: '+e.which);
	}
	
	switch (e.which) {
		case 24:
		case 88:
//		alert('will popup terminal');
			jQuery('#shade').show();
			jQuery('#terminal').show();
			jQuery('#cmdline').focus();
			break;
		case 26:
		case 90:
			jQuery('#terminal').hide();
			jQuery('#shade').hide();
			break;
	}
	//return false;
});

function binder () {
	jQuery('#cmdline').bind('keypress', function(e) {
		if (e.which == 13) {
			var cmd = jQuery('#cmdline').val();
			if (cmd=='clr') {
				jQuery('#terminal').replaceWith('<div class="terminal" id="terminal"><div class="closeButton" onClick="jQuery(\'#terminal\').hide();">X</div>&gt; <input id="cmdline" name="cmdline" type="text" autocomplete="off" size="55" /></div>');
					jQuery('#terminal').show();
					jQuery('#cmdline').focus();
					binder();
			} else if (cmd == 'exit') {
				jQuery('#cmdline').val('');
				jQuery('#terminal').hide();
				jQuery('#shade').hide();
			} else if (cmd == 'login') {
				jQuery('#terminal').append('<br>user: <input type="text" name="user" id="user" autocomplete="off">');
				jQuery('#user').bind('keypress', function(ev) {
					if (ev.which == 13) {
						jQuery('#terminal').append('<br>pass: <input type="password" name="pass" id="pass">');
						jQuery('#pass').focus();
						jQuery('#pass').bind('keypress', function(ev) {
							if (ev.which == 13) {
								jQuery.ajax({
									url: "terminal.php",
									processData: false,
									data: "cmdln=login&user="+jQuery('#user').val()+"&pass="+jQuery('#pass').val(),
									success: function(resp) { 
										jQuery('#user').replaceWith(''); 
										jQuery('#pass').replaceWith(''); 
										jQuery('#cmdline').replaceWith(cmd+''); 
										jQuery('#terminal').append(resp+'<br /><br />&gt; <input id="cmdline" name="cmdline" type="text" autocomplete="off" size="55" />'); 
										jQuery('#cmdline').focus();
										binder();
									}
								});
							}
						});
					}
				});
				jQuery('#user').focus();
			} else {
				jQuery.ajax({
					url: "terminal.php",
					processData: false,
					data: "cmdln="+cmd,
					success: function(resp) { 
						jQuery('#cmdline').replaceWith(cmd+'<br />'); 
						jQuery('#terminal').append(resp+'<br /><br />&gt; <input id="cmdline" name="cmdline" type="text" autocomplete="off" size="55" />'); 
						jQuery('#cmdline').focus();
						binder();
					}
				});
			}
			return false;
		}
	});
}

binder();

if (window.location.href.indexOf('dbg=1') >= 0)
	alert(99);
