$(document).ready(function() {
    console_shortcuts_init();
    //createPopup('test');
    createPopup();
    Cli.setup();
});
var Cli = {
setup: function () {
	       this.clearCli();
		   $.myHistory={
			   history:'test'
		   }
	       $('#cli-form').bind('submit', function() {
			       var command = $('#cli-input').val();
			       if (command == "`") {
			       command = 'clear';
			       }
			       Cli.clearCli();
			       if (command != 'clear') {
			       //$.getJSON('http://li3.rad-dev.org/cmd/?jsoncallback=?', { cmd: command });
				   //Cli.addHistory(command);
			       $.getJSON('/page/cmd/'+command);
			       //$('#cli-display').html(command);
			       }
			       });
       },
addHistory: function (command) {
				//alert('Adding Hist' + test);
				$.myHistory.history += "\n" + command;
				alert($.myHistory.history);
},
showHistory: function () {
	alert('Showing Hist');
},
historyUp: function () {
	alert('Moving Up');
},
historyDown: function () {
	alert('Moving Down');
},
response: function(json) {
		  if (json.status == 'success') {
			  if (json.data.url) {
				  return window.location.href = json.data.url;
			  } else {
				  $currentHtml=$('#cli-display').html();
				  if($('#cli-display').html().length>0){$currentHtml=$currentHtml+'<br />';}
				  $('#cli-display').html($currentHtml + json.data.text);
				  $('#popupbody').scrollTop($('#popupbody')[0].scrollHeight);
			  }
		  } else {
			  // This is not my beautiful house... (How did I get here?)
			  $('#cli-display').html(response+': temporarily out of order.');
		  }
},

clearCli: function () {
		  $('#cli-input').val('');
		  $('#cli-input').focus();
},
resetCli: function () {
		  $('#cli-display').html('');
		  $('#cli-input').val('');
		  $('#cli-input').focus();
	}
}

function createPopup(divId)
{
    if(!divId) {
	var divId = 'popup';
    }

    if($("#"+divId).length==0)
    {
	$(document.body).append('<div id="'+divId+'"></div>');
	$("#"+divId).css({'order' : 'solid 1px #333', 'font-family' : 'Tahoma, Verdana, Arial, Helvetica, sans-serif', 'font-size' : '12px', 'display' : 'none', 'position' : 'absolute', 'width' : '820px', 'z-index' : '60'});
	$("#"+divId).append('<div id="popuptitle">Command</div>');
	$("#popuptitle").css({'background' : 'blue', 'color' : 'white', 'font-weight' : 'bold', 'height' : '15px', 'padding' : '5px'});
	$("#"+divId).append('<div id="popupbody"><div id="cli-display"></div><form id="cli-form" onSubmit="return false"><input type="text" id="cli-input" /></form></div>');
	$("#cli-form").css({'background' : 'none', 'padding' :'0'});
	$("#cli-display").css({'padding' : '.5em 1em .5em 1em', 'textAlign' : 'left'});
	$("#popupbody").css({'background' : '#000', 'padding' : '5px', 'height' : '500px', 'text-align' : 'center', 'color' : '#BBB', 'overflow-y' : 'scroll'});
	$("#cli-input").css({'width' : '780px', 'margin' : '0 0 0 1em', 'padding' : '0 0', 'background' : '#000', 'height' : '18px', 'border' : 'none', 'color' : 'white', 'font-family' : 'monospace', 'font-size' : '14px'});
	$("#"+divId).css({'top' : '100px', 'left' : '50px'});
    }
}
function console_shortcuts_init() {
  shortcut.add("Ctrl+slosh", function() {
			alert('Possible Spanish Keyboard');
	  });
  shortcut.add("ctrl+tld", function() {
		  showPopup('popup');
		  $('#cli-input').focus();
		  shortcut.add("Ctrl+L",function(){Cli.resetCli();},{'type':'keypress','propagate':false});
		  shortcut.add("Ctrl+U",function(){$("#cli-input").val('')},{'type':'keypress','propagate':false});
		  shortcut.add("Ctrl+H",function(){Cli.showHistory();},{'type':'keypress','propagate':false});
		  shortcut.add("Ctrl+up",function(){Cli.historyUp();},{'type':'keypress','propagate':false});
		  shortcut.add("Ctrl+down",function(){Cli.historyDown();},{'type':'keypress','propagate':false});
		  shortcut.add("Ctrl+D",function(){
			  if(document.getElementById('greyout').style.display = 'block') {
				  shortcut.remove("Ctrl+L");
				  shortcut.remove("Ctrl+U");
				  shortcut.remove("Ctrl+D");
				  shortcut.remove("Ctrl+H");
				  shortcut.remove("up");
				  shortcut.remove("down");
				  hidePopup('popup');
			  }
		  });
		  shortcut.add("esc", function() {
			  if(document.getElementById('greyout').style.display = 'block') {
			  shortcut.remove("Ctrl+L");
			  shortcut.remove("Ctrl+U");
			  shortcut.remove("Ctrl+D");
			  shortcut.remove("Ctrl+H");
			  hidePopup('popup');
			  }
		  });
	  });
}
//----------
function addEvent(obj ,evt, fnc)
{
  if (obj.addEventListener)
    obj.addEventListener(evt,fnc,false);
  else if (obj.attachEvent)
    obj.attachEvent('on'+evt,fnc);
  else
    return false;
  return true;
}
function removeEvent(obj ,evt, fnc)
{
  if (obj.removeEventListener)
    obj.removeEventListener(evt,fnc,false);
  else if (obj.detachEvent)
    obj.detachEvent('on'+evt,fnc);
  else
    return false;
  return true;
}
//----------
function appendElement(node,tag,id,htm)
{
  var ne = document.createElement(tag);
  if(id) ne.id = id;
  if(htm) ne.innerHTML = htm;
  node.appendChild(ne);
}
//----------
function showPopup(p)
{
  greyout(true);
  document.getElementById(p).style.display = 'block';
}
function hidePopup(p)
{
  greyout(false);
  document.getElementById(p).style.display = 'none';
}
//----------
function greyout(d,z)
{
  var obj = document.getElementById('greyout');

  if(!obj)
  {
    appendElement(document.body,'div','greyout');
    obj = document.getElementById('greyout');
    obj.style.position = 'absolute';
    obj.style.top = '0px';
    obj.style.left = '0px';
    obj.style.background = '#111';
    obj.style.opacity = '.5';
    obj.style.filter = 'alpha(opacity=50)';
  }
  if(d)
  {
    var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
    var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
    if(document.body.scrollHeight) sh = Math.max(sh,document.body.scrollHeight)
      var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
    if(document.body.scrollWidth) sh = Math.max(sh,document.body.scrollWidth)
      var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
    if(!z){ z = 50 }
    obj.style.zIndex = z;
    obj.style.height = Math.max(wh,Math.max(sh,ch))+'px';
    obj.style.width  = Math.max(sw,cw)+'px';
    obj.style.display = 'block';
    addEvent(window,'resize',greyoutResize);
  }
  else
  {
    obj.style.display = 'none';  
    removeEvent(window,'resize',greyoutResize);
  }
}
function greyoutResize()
{
  var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
  var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
  var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
  if(document.body.scrollHeight) sh = Math.max(sh,document.body.scrollHeight)
    var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
  if(document.body.scrollWidth) sh = Math.max(sh,document.body.scrollWidth)
    var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
  var obj = document.getElementById('greyout');
  obj.style.height = ch+'px';
  obj.style.width  = cw+'px';
  obj.style.height = Math.max(wh,Math.max(sh,ch))+'px';
  obj.style.width  = Math.max(sw,cw)+'px';
}
//----------

