var mem = "";
var bar = -1;

var qoute = [""];
var author = [""];
var timer = timerPY = timerPX = timerRH = timerRW = 0;
var move = 0;
var myWidth = myHeight = 0;

var count = 0;
var current_line = 0;
var command_hist = [""];

var timerID = "";
var loadingTimerMax = 0;  

var loadingTimer = 0;
var loadptr = -1;
var load = "";
var forceLoad = 0;

var preCount = 0;
var preTimer = 0;
var panelTimer = 0;

var jpegs  = new Array();
var pngs  = new Array();

var ptr = 0;
var img = new Array();
for(x = 0; x < 10; x++) {
 img[x] = new Image;
 img[x].src = 'images/'+x+'.png';
}//endFor

function changeFood(x) {
 if (x == 1) {
     ptr--;
 } else {
   ptr++;
 }//endIf
 if (ptr > 9) { ptr = 0; }
 if (ptr < 0) { ptr = 9; }
 document.getElementById('food').src = img[ptr].src;
 document.getElementById('img').value = ptr;
// document.getElementById('key').value = '';
 document.getElementById('key').focus();
}//endFunction

function shuffleX() {
  AjaxRequest.get(
    {
      'url':'index.php?a=shuffle'
      ,'onLoading':function() { 
      }
      ,'onSuccess':function(req){ 
        eval(req.responseText);
      }
      ,'timeout': 5000
      ,'onTimeout':function(req){ 
      }
    }
  );    
}//endFunction
  
qoute[0] = "Technological progress has merely provided us with more efficient means for going backwards";
author[0] = "Aldous Huxley";
qoute[1] = "If you think you can do a thing or think you can't do a thing, you're right";
author[1] = "Henry Ford";
qoute[2] = "It has become appallingly obvious that our technology has exceeded our humanity";
author[2] = "Albert Einstein";
qoute[3] = "Knowledge itself is power";
author[3] = "Sir Francis Bacon";
qoute[4] = "A minute's success pays the failure of years";
author[4] = "Robert Browning";
qoute[5] = "What now is proved was once only imagined";
author[5] = "William Blake";
qoute[6] = "Thinking is the hardest work there is, which is probably the reason so few engage in it";
author[6] = "Henry Ford";
qoute[7] = "Just because something doesn't do what you planned it to do doesn't mean it's useless";
author[7] = "Thomas A Edison";
qoute[8] = "The production of too many useful things results in too many useless people";
author[8] = "Karl Marx";
qoute[9] = "Technology presumes there's just one right way to do things and there never is";
author[9] = "Robert M. Pirsig";
qoute[10] = "The real problem is not whether machines think but whether men do";
author[10] = "B.F. Skinner";
qoute[11] = "Any sufficiently advanced technology is indistinguishable from magic";
author[11] = "Arthur C. Clarke";

function qoutes() {
  var rand = Math.floor((qoute.length)*Math.random());
  document.getElementById('qoute').innerHTML = qoute[rand];
  document.getElementById('author1').innerHTML = '&nbsp;- '+author[rand];
  setTimeout("qoutes()", 60000);  
}//endFunction

if  (document.getElementById) {
  (function() {

    if (window.opera){ document.write("<input type='hidden' id='Q' value=' '>"); }
    
    var n = 500;
    var dragok = false;
    var y,x,d,dy,dx;
    
    function move(e){
      if (!e) e = window.event;
      
       if (dragok){
        d.style.left = dx + e.clientX - x + "px";
        d.style.top  = dy + e.clientY - y + "px";
        return false;
      }//endIf
    }//endFucntion
    
    function down(e){
      if (!e) e = window.event;
      var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
      if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
       temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
       }
      if (temp.className == "dragclass"){
        if (window.opera){
          document.getElementById("Q").focus();
        }//endIf
        dragok = true;
        // temp.style.zIndex = n++;
        d = temp;
        dx = parseInt(temp.style.left+0);
        dy = parseInt(temp.style.top+0);
        x = e.clientX;
        y = e.clientY;

        document.onmousemove = move;
         
        return false;
      }//endIf
    }//endFunction
    
    function up(){
      dragok = false;
      document.onmousemove = null;
    }//endFunction    
    document.onmousedown = down;
    document.onmouseup = up;
  })();
}//End      

function catchEnterKey(e, ptr, other){
  var key;
  if (window.event) 
    key = window.event.keyCode; //IE
  else 
    key = e.which; //firefox
  if (key == 13) {
    if (ptr.id == "cmd") execute();
    if (ptr.id == "pwd") validate(ptr, other);
    return false;
  }//endIf

  if (document.getElementById('cmd').hasFocus == true) {
    if (key == 38 && current_line < command_hist.length-1) {
      if (current_line == -1) {
        current_line = current_line+2;
      } else {
        current_line++;
      }//endIf
      document.getElementById('cmd').value = command_hist[current_line];
    }//endIf
    if (key == 40 && current_line > 0) {
      current_line--;
      document.getElementById('cmd').value = command_hist[current_line];
    }//endIf
  }//endIf
  return true;
}//endFunction


function keyEvent(e, ptr, other){
  var key;
  if (window.event) 
    key = window.event.keyCode; //IE
  else 
    key = e.which; //firefox
    
//  alert(key);
  if (key == 38) { changeFood(); }
  if (key == 40) { changeFood(1); }
  return true;
}//endFunction

function execute(cmd, opt){
  mem = "[";
  
  if (cmd == undefined) 
    var cmd = document.getElementById('cmd').value;
  document.getElementById('cmd').value = "";
  
  var test = command_hist.toString();
  if (test.search(cmd) != -1) {  command_hist.splice(command_hist.indexOf(cmd), 1); }
  command_hist.reverse()
  command_hist.pop();
  command_hist.push(cmd);
  command_hist.push("");
  command_hist.reverse()
  current_line = -1;
  
  if (cmd == 'history') {
    var history = "";
    for (i = 1; i < command_hist.length; i++) {
      history += i+" &nbsp; "+command_hist[i]+"</br>";
    }//endFor
    document.getElementById('output').innerHTML = "<div style='color: #927657; font-size: .9em;'>"+history+"</div>"+document.getElementById('output').innerHTML;
    
    return true;
  }//endIf;
  
  if (cmd.substring(0, 1) == "!") {
    var i = parseInt(cmd.substring(1)) + 1;
    document.getElementById('cmd').value = command_hist[i];
    return true;
  }//endIf  
  
  if (
    cmd.substr(0, 4) == "open" || 
    cmd.substr(0, 4) == "ping" ||
    cmd.substr(0, 2) == "go"    ||
    cmd.substr(0, 6) == "google"     
  ) {  var timeout = 1024 * 1024 * 1024; }  
    else { var timeout = 10000; }
  
  AjaxRequest.get(
    {
      'url':'cmd.php?p='+cmd
      ,'onLoading':function() { 
        loading(1);
      }
      ,'onSuccess':function(req){ 
        loading(0);
        eval(req.responseText);
        if (opt == 'home') {
          document.getElementById('output').innerHTML = " \
          Welcome to the brown360 sudo shell <br/> \
          type `help' for a list of available commands <br/> \
          <hr size='1' style='color: #222; height:1px'> \
          "+document.getElementById('output').innerHTML;
        }//endif
      }
      ,'timeout': timeout
      ,'onTimeout':function(req){ 
        document.getElementById('output').innerHTML = " >"+cmd+": Timed out \
        "+document.getElementById('output').innerHTML;
        if (cmd != "sudo")        
          alert('Timed Out!'); 
      }
    }
  );  
  return;  
}//endFunction

function loading(x) {
    if (x == undefined) { var x = 1; }
    if (x == 9) { x = 1; }

    if (x == 1) { load = ">---"; }
    if (x == 2) { load = "->--"; }
    if (x == 3) { load = "-->-"; }
    if (x == 4) { load = "--->"; }
    if (x == 5) { load = "---<"; }
    if (x == 6) { load = "--<-"; }
    if (x == 7) { load = "-<--"; }
    if (x == 8) { load = "<---"; }
    
    if (x == 0 || loadingTimerMax > 6*6) { 
      document.getElementById('load').innerHTML = '';
      loadingTimerMax = 0;
      clearTimeout(loadingTimer); return; }
    
    document.getElementById('load').innerHTML = load;
    x++;
    loadingTimer = setTimeout("loading("+x+")", 150);
    loadingTimerMax++;    
}//endFunction


function validate(ptr, user){
  count++;
  if (count <= 3) {
    AjaxRequest.get({
      'url': 'cmd.php?p=validate ' + user + ' ' + ptr.value,
      'onLoading': function(){
      },
      'onSuccess': function(req){
        loading(0);
        eval(req.responseText);
      },
      'timeout': 4000,
      'onTimeout': function(req){
        alert('Timed Out!');
      }
    });
  } else {
    count = 0;
    document.getElementById('output').innerHTML = "360: 3 incorrect password attempts <br/>"+document.getElementById('output').innerHTML;
    document.getElementById('pwddiv').innerHTML = "";
    document.getElementById('pwddiv').style.display = "none";
    var cmd = document.getElementById('cmd');
    cmd.disabled = false;
    document.getElementById('cmd').focus();
  }//endIf
  return;  
}//endFunction

function checkFormContact() {
  if (!isValidEmail(document.getElementById('email').value)) {
    alert("Please enter a valid email address!")  
    return;
  }//endIf
  if (document.getElementById('message').value == "message") {
    alert("Please enter your message!")
    return;    
  }//endIf
  document.getElementById('emailsubmit').click();
  document.getElementById('send').disabled = "true";
}//endFunction

function isValidEmail(str) {
  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}//endFunction

function search() {
  var q = prompt("Search Google for?");
  if (q != undefined && q.length > 1) {
    location.href = "http://www.google.com/search?hl=en&q="+q+"&btnG=Google+Search&aq=f&oq=";
  }//endIf
}//endFunction

function breakFrame() {
  if (top.location != location) {
    top.location.href = document.location.href ;
  }//endIf
}//endFunction

function init() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }//endIf
  
  document.getElementById("main").style.top = (myHeight/2)-120;
  document.getElementById("main").style.left = (myWidth/2)-300;

  document.getElementById('desk').style.height = (myHeight-105)+"px";
  document.getElementById('desk').style.width = (myWidth-50)+"px";
  document.getElementById('desk').style.backgroundColor = "#111";

  document.getElementById('closeFullFrame').style.left = (myWidth / 2) - 50;

  document.getElementById('cloud').style.height = (myHeight-105)+"px";

  preCount = 20;
  preLoadPng("tab");        preLoadPng("tabb");
  preLoadPng("about");      preLoadPng("aboutb");
  preLoadPng("resume");      preLoadPng("resumeb");
  preLoadPng("contact");    preLoadPng("contactb");
  preLoadPng("resume");      preLoadPng("resumeb");
  preLoadPng("b360");        preLoadPng("b360b");
  preLoadJpeg("welcome");
  
  preLoadJpeg("solutions");    preLoadJpeg("solutions_blur");
  preLoadJpeg("b360");        preLoadJpeg("b360_blur");
  preLoadJpeg("desktop");      preLoadJpeg("desktop_blur");
  preLoadJpeg("about");        preLoadJpeg("about_blur");
  preLoadJpeg("resume");      preLoadJpeg("resume_blur");
  preLoadJpeg("contact");      preLoadJpeg("contact_blur");
  preLoadJpeg("terminal");    preLoadJpeg("terminal_blur");
  preLoadJpeg("brown360");    preLoadJpeg("brown360_blur");
  preLoadJpeg("clients");      preLoadJpeg("clients_blur");
  preLoadJpeg("demo");        preLoadJpeg("demo_blur");

  preTimer = setTimeout("preLoadDone()", 250);  

  return;
}//endFunction

function preLoadJpeg(idx, file) {
  jpegs[idx]         = new Image();
  jpegs[idx].src     = "images/"+idx+".jpg";            
  jpegs[idx].onload = function() { preCount--; }
}//endFunction

function preLoadPng(idx, file) {
  pngs[idx]         = new Image();
  pngs[idx].src     = "images/"+idx+".png";            
  pngs[idx].onload   = function() { preCount--; }
}//endFunction

function preLoadDone() {
  forceLoad++;
//  if (preCount <= 1 || forceLoad > 20) {
  if (preCount <= 1) {
    clearTimeout(preTimer); 
    clearTimeout(loadingTimer); 
    qoutes();
    document.getElementById("main").style.display = "block";
    document.getElementById("signature").style.display = "block";
    document.getElementById("address").style.display = "block";
    javascriptFooter();
    return;
  }//endIf
  preTimer = setTimeout("preLoadDone()", 250);  
}//endFunction

function noPX(num) {
   num = num.substr(0, num.length - 2);
  return num/1;
}//endFunction

function resizeWidth(obj, w, step, delay){
  var ow = noPX(document.getElementById(obj).style.width);
  var x = w - ow;
  if (x < 0) {
    while(ow > w) {
      ow = ow - step;
      if (ow < w) { ow = w; }
      document.getElementById(obj).style.width = ow;
      timerRW = setTimeout("resizeWidth('"+obj+"', "+w+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  } else {
    while(ow < w) {
      ow = ow + step;
      if (ow > w) { ow = w; }
      document.getElementById(obj).style.width = ow;
      timerRW = setTimeout("resizeWidth('"+obj+"', "+w+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  }//endIf
  clearTimeout(timerRW);
  move = move - 1;  
  return;
}//endFunction

function resizeHeight(obj, h, step, delay){
  var oh = noPX(document.getElementById(obj).style.height);
  var y = h - oh;
  if (y < 0) {
    while(oh > h) {
      oh = oh - step;
      if (oh < h) { oh = h; }
      document.getElementById(obj).style.height = oh;
      timerRH = setTimeout("resizeHeight('"+obj+"', "+h+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  } else {
    while(oh < h) {
      oh = oh + step;
      if (oh > h) { oh = h; }
      document.getElementById(obj).style.height = oh;
      timerRH = setTimeout("resizeHeight('"+obj+"', "+h+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  }//endIf
  clearTimeout(timerRH);
  move = move - 1;  
  return;
}//endFunction
  
function positionX(obj, x, step, delay){
  var ox = noPX(document.getElementById(obj).style.left);
  var tx = x - ox;
  if (tx < 0) {
    while(ox > x) {
      ox = ox - step;
      if (ox < x) { ox = x; }            
      document.getElementById(obj).style.left = ox;
      timerPX = setTimeout("positionX('"+obj+"', "+x+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  } else {
    while(ox < x) {
      ox = ox + step;
      if (ox > x) { ox = x; }            
      document.getElementById(obj).style.left = ox;
      timerPX = setTimeout("positionX('"+obj+"', "+x+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  }//endIf
  clearTimeout(timerPX);
  move = move - 1;
  return;
}//endFunction

function positionY(obj, y, step, delay){
  var oy = noPX(document.getElementById(obj).style.top);
  
  if (oy == y) { 
    clearTimeout(timerPY);
    move = move - 1;
    return;
  }//endIf
  
  var ty = y - oy;
  if (ty < 0) {
    while(oy > y) {
      oy = oy - step;
      if (oy < y) { oy = y; }
      document.getElementById(obj).style.top = oy;
      timerPY = setTimeout("positionY('"+obj+"', "+y+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  } else {
    while(oy < y) {
      oy = oy + step;
      if (oy > y) { oy = y; }
      document.getElementById(obj).style.top = oy;
      timerPY = setTimeout("positionY('"+obj+"', "+y+", "+step+", "+delay+")", delay);
      return;
    }//endWhile
  }//endIf
  clearTimeout(timerPY);
  move = move - 1;
  return;
}//endFunction

function resizeAndMove(obj, x, y, w, h, step) {
  
  var delay = 15;
  
//  var ow = document.getElementById(obj).getAttribute("width");

  var ow = noPX(document.getElementById(obj).style.width);
  var oh = noPX(document.getElementById(obj).style.height);

  if (ow == 0) { document.getElementById(obj).style.width = 450; }  
  if (oh == 0) { document.getElementById(obj).style.height = 200; }

  var posX = noPX(document.getElementById(obj).style.left);
  var posY = noPX(document.getElementById(obj).style.top);

  if (posX == 0) { document.getElementById(obj).style.left = 0; }  
  if (posY == 0) { document.getElementById(obj).style.top = 25; }

  move = 4;
  positionX(obj, parseInt(x), step, delay);
  positionY(obj, parseInt(y), step, delay);
  resizeHeight(obj, parseInt(h), step, delay);
  resizeWidth(obj, parseInt(w), step, delay);  
  return;
}//endFunction

function onPanel(obj, callBack) {
  if (move == 0) {
    clearTimeout(panelTimer); 
    eval(callBack);
    return; 
  } else {
    panelTimer = setTimeout("onPanel('" + obj + "', \"" + callBack + "\")", 50);
    return;
  }//endIf
}//endFunction

function reset(obj) {
  if (
    document.getElementById('photo').src == jpegs[obj].src
  ) { 
    document.getElementById('photo').src = jpegs['welcome'].src; 
  }  //endIf
}//endFunction

function menu(item, opt, x, y, w, h, d) {
  if (opt == "close") {
    document.getElementById(item).className = "closing";    
    document.getElementById(item+"Content").style.display = "none";    
    resizeAndMove(item, 1, 31, 450, 200, 55);
    onPanel(item, "menu('"+item+"', 'onClose')");
    
  } else if (opt == "open"){
    document.getElementById(item).style.display = "block";
    document.getElementById(item).className = "opening";
    resizeAndMove(item, x, y, w, h, d);
    onPanel(item, "menu('"+item+"', 'onOpen')");
//    document.getElementById('photo').src = jpegs[item+"_blur"].src;    
    
  } else if(opt == "onClose") {
    document.getElementById(item).style.display = "none";    
    document.getElementById(item).className = "closed";    
    document.getElementById("disable").style.display = "none";
    document.getElementById("photo").src= "images/" + item + ".jpg";
    document.getElementById('photo').src = jpegs["welcome"].src;    

  } else if(opt == "onOpen") {
    document.getElementById(item).className = "opened";    
    document.getElementById(item+"Content").style.display = "block";
    document.getElementById("disable").style.display = "block";
    document.getElementById("disable").style.zIndex = 8;      
    document.getElementById('photo').src = jpegs[item+"_blur"].src;    

    if (item == "desktop") {
      document.getElementById(item).style.opacity = "1.00";    
    }//endIf

    if (item == "b360") {
      document.getElementById("photo").style.border = "2x solid #222";
      shuffleX(); 
      changeFood();
    }//endIf
    
    if (item == "terminal") {
      document.getElementById("cmd").focus();
      document.getElementById("output").style.height = myHeight-145+"px";
      document.getElementById("output").style.backgroundColor = "#000";
    }//endif

    if (item == "resume") {
      document.getElementById("resumeView").style.height = myHeight-125+"px";
    }//endif
    
    if (item == "solutions") {
      document.getElementById("wiki").style.height = myHeight-98+"px";
    }//endif

    if (item == "clients") {
      document.getElementById("clientsFrame").style.height = myHeight-98+"px";
    }//endif
    
  }//endIf    
}//endFunction

function fullFrame(frame) {
  try {
    document.getElementById("fullFrameSrc").src = frames[frame].location.href;
  } catch(err) {
    document.getElementById("fullFrameSrc").src = "360/desktop.php";
  }//endTry
  document.getElementById("closeFullFrame").setAttribute("onclick", "closeFullFrame('"+frame+"');");
  // IE FIX
  document.getElementById("closeFullFrame").onclick = function(){ closeFullFrame(frame);  };
  document.getElementById("fullFrame").style.display = "block";
}//endFunction

function closeFullFrame(frame) {
  try {
    document.getElementById(frame).src = frames["fullFrameSrc"].location.href;
  } catch(err) {
    document.getElementById("fullFrameSrc").src = "360/desktop.php";
  }//endTry
  document.getElementById("fullFrame").style.display = "none";
  document.getElementById("fullFrameSrc").src = "";
}//endFunction

function loadingDots(x) {
    if (x == undefined) { var x = 1; }
    if (x == 6) { x = 1; }

    if (x == 1) { load = " "; }
    if (x == 1) { load = "."; }
    if (x == 2) { load = ".."; }
    if (x == 3) { load = "..."; }
    if (x == 4) { load = "...."; }
    if (x == 5) { load = "....."; }
    
    if (x == 0 || loadingTimerMax > 10) { 
      document.getElementById('load').innerHTML = '.....';
      loadingTimerMax = 0;
      clearTimeout(loadingTimer); 
      
      clearTimeout(preTimer); 
      qoutes();
      document.getElementById("main").style.display = "block";
      document.getElementById("signature").style.display = "block";
      javascriptFooter();
      
      return;
    }//endIf
    if (document.getElementById('loadingDots') != undefined) {
      document.getElementById('loadingDots').innerHTML = load;
      x++;
      loadingTimer = setTimeout("loadingDots(" + x + ")", 500);
      loadingTimerMax++;
    }//endIf
}//endFunction

function logOff360(){
  AjaxRequest.get(
    {
      'url':'index.php?a=logoff360'
    }
  );    
}//endFunction

