/****************************
Roboshrub Inc.
http://roboshrub.blogspot.com
*****************************/

// Global menu variables.
var loaded=-1, totaldel=-1, delchange=false, configToggle=false, fileToggle=false, holdSlogan="", E_TRANSITIONS=true, E_TWOWAY=true, E_24HOUR=false;
var in_transit=false, trans_speed=10, firston=false, min=false, ab=true, opaci_down=100, opaci_up=0;

function startUp() {
  g('encoder').focus();
  redrawConfig(g('cipherType').options[g('cipherType').selectedIndex].value);
  var thingsToMouseOver=new Array('logo', 'shadeBox', 'skinBox', 'aboutBox', 'closeBox', 'filter');
  for (i in thingsToMouseOver) {
    g(thingsToMouseOver[i]).onmouseover=function() {
      alterSlogan(this);
      if (navigator.appName.search("Microsoft")!=-1 && this.id!='logo') {
        this.style.filter="alpha(opacity='100')";
      }
    };
    g(thingsToMouseOver[i]).onmouseout=function() {
      g('sloganArea').innerHTML=holdSlogan;
      maybeCloseSlogan();
      if (navigator.appName.search("Microsoft")!=-1 && this.id!='logo') {
        this.style.filter="alpha(opacity='60')";
      }
    };
    g(thingsToMouseOver[i]).onfocus=function() {
      if (navigator.appName.search("Microsoft")!=-1 && this.id!='logo') {
        this.style.filter="alpha(opacity='100')";
      }
    };
    g(thingsToMouseOver[i]).onblur=function() {
      if (navigator.appName.search("Microsoft")!=-1 && this.id!='logo') {
        this.style.filter="alpha(opacity='60')";
      }
    };
    g(thingsToMouseOver[i]).onmousedown=function() {
      if (navigator.appName.search("Microsoft")!=-1) {
        this.style.borderStyle='inset';
      }
    };
    g(thingsToMouseOver[i]).onmouseup=function() {
      if (navigator.appName.search("Microsoft")!=-1) {
        this.style.borderStyle='outset';
      }
    };
  }
  
  // Toggle Buttons
  g('menuB1').onmouseover=function() {alterSlogan(g('innerB1'));};
  g('menuB1').onmouseout=function() {g('sloganArea').innerHTML=holdSlogan;maybeCloseSlogan();};
  g('menuB2').onmouseover=function() {alterSlogan(g('innerB2'));};
  g('menuB2').onmouseout=function() {g('sloganArea').innerHTML=holdSlogan;maybeCloseSlogan();};
  
  // Non-widget background.
  try {
    var widgTest=widget;
  }
  catch (e) {
    document.getElementsByTagName("HTML")[0].style.backgroundImage="url('Backgrounds/content_back.png')";
    document.getElementsByTagName("HTML")[0].style.backgroundRepeat="repeat";
  }
  loadSkin();
  
  // Firefox rounded borders for color preview.
  if (navigator.userAgent.search("Gecko")!=-1) {
    g('clubs').style.MozBorderRadius="35px";
  }
}

function maybeCloseSlogan() {
  if (!min) {g('sloganArea').style.display="none";}
}
function alterSlogan(a) {
  holdSlogan=g('sloganArea').innerHTML;
  g('sloganArea').innerHTML=a.alt;
  g('sloganArea').style.display="inline";
}

// Save Button hover behavior.
function hoverFile() {
  g('sloganArea').style.display="inline";
  if (activeFile>=0) {
    var temp=getCookie().split(":SPLIT:")[activeFile].split(":PARSE:")[0];
    if (temp.length>25) {
      temp=temp.substring(0,22)+"...";
    }
    g('sloganArea').innerHTML="Save File: \""+temp+"\"";
  }
  else {
    g('sloganArea').innerHTML="No File Is Loaded";
  }
}

// Random slogans.
var slogans=new Array();
slogans.push("The Ultimate &#8364;ncrypti&#248;n Program");
slogans.push("1337 And Proud Of It");
slogans.push("New And Improved");
slogans.push("A Parable On Preclusion");
slogans.push("For All Your Encoding Needs");
slogans.push("Beyond Binary");
slogans.push("Confuse Your Friends!");
slogans.push("Now With su% More &#8364;ncrypti&#248;n");
slogans.push("Coded With Care");
slogans.push("eXtreme Encapsulation");
slogans.push("More Than Just A Pretty Interface");
slogans.push("With Encryption, Power");
slogans.push("Hiding In Plain Sight");
slogans.push("Premium Obfuscation At Discount Prices");
slogans.push("Suppress Your Secrets");
slogans.push("Steady Your Fingers");
slogans.push("Warning: May Cause You To See Patterns");
slogans.push("Not Recommended For Daily Use By Linguists");
slogans.push("Camouflage Your Compendia");
slogans.push("Do You Know Where Your Ciphers Are?");
slogans.push("An Algorithm With An Attitude");
slogans.push("The Secret Ingredient Is Javascript");
slogans.push("There&#8217;s No Substitute For Substitution");
slogans.push("Transpos&#233; Your Expos&#233;");
slogans.push("Now With 1%00 More Transposition");
slogans.push("Paste In, Cipher On, Copy Out");
slogans.push("Proud To Be A Polybian");
slogans.push("Unescape Responsibly");
slogans.push("All Your Base64 Are Belong To Us");
slogans.push("Finally Find Out What &#8220;Redrum&#8221; Means");
slogans.push("Put The Filter To Your Ear");
slogans.push("Steeped In Steganography");

var backSwatches=new Array();
backSwatches.push("Backgrounds/atlantic.png");
backSwatches.push("Backgrounds/content_back.png");
backSwatches.push("Backgrounds/diamond.png");
backSwatches.push("Backgrounds/tan1.png");
backSwatches.push("Backgrounds/tan2.png");
backSwatches.push("Backgrounds/tan3.png");
backSwatches.push("Backgrounds/firechain.png");
backSwatches.push("Backgrounds/redscratch.png");
backSwatches.push("Backgrounds/hyperplaid.png");
backSwatches.push("Backgrounds/volcanis.png");
backSwatches.push("Backgrounds/pumpkin.png");
backSwatches.push("Backgrounds/neurosis.png");
backSwatches.push("Backgrounds/leopardskin.png");
backSwatches.push("Backgrounds/bamboo.png");
backSwatches.push("Backgrounds/oldquilt.png");
backSwatches.push("Backgrounds/scrooge.png");
backSwatches.push("Backgrounds/dune1.png");
backSwatches.push("Backgrounds/dune2.png");
backSwatches.push("Backgrounds/greensplotch.png");
backSwatches.push("Backgrounds/camouflage.png");
backSwatches.push("Backgrounds/veridian_elephant.png");
backSwatches.push("Backgrounds/shingle.png");
backSwatches.push("Backgrounds/greenscene.png");
backSwatches.push("Backgrounds/shallow_waters.png");
backSwatches.push("Backgrounds/combspace.png");
backSwatches.push("Backgrounds/blue1.png");
backSwatches.push("Backgrounds/blue2.png");
backSwatches.push("Backgrounds/blue3.png");
backSwatches.push("Backgrounds/azul.png");
backSwatches.push("Backgrounds/pink_wisp.png");
backSwatches.push("Backgrounds/curtain.png");
backSwatches.push("Backgrounds/partychess.png");
backSwatches.push("Backgrounds/flume.png");
backSwatches.push("Backgrounds/spyglass.png");
backSwatches.push("Backgrounds/amaze.png");
backSwatches.push("Backgrounds/black1.png");
backSwatches.push("Backgrounds/black2.png");
backSwatches.push("Backgrounds/black3.png");
backSwatches.push("Backgrounds/blackworm.png");
backSwatches.push("Backgrounds/overcast.png");

// Checks to see if the user pressed enter in the input box.
function checkForEnter(e) {
  var q;
  if (!e) {
    var e=window.event;
  }
  if (e.keyCode) {
    q=e.keyCode;
  }
  if (q==13) {
    console.log("WHAT?");
  }
}

// Returns array of all elements with specified class name.
function getElementsByClassName(c_name) {
  var found=new Array();
  var el_length=document.getElementsByTagName('*').length; // Total number of elements.
  
  // Selects all elements and checks for name match. If match is found, saves it to stack.
  for (i=0; i<el_length; i++) {
    var obj=document.getElementsByTagName('*')[i];
    if (obj.className==c_name) {
      found.push(obj);
    }
  }
  return found;
}

// Sets the cookie.
function setCookie(value) {
  try {
    widget.setPreferenceForKey(value,'cryptor');
  }
  catch (e) {
    var expires=new Date();
    expires.setDate(expires.getDate()+1000000);
    document.cookie="cryptor="+escape(value)+";expires="+expires;
  }
}

// Returns the value of the cookie.
function getCookie() {
  try {
    return widget.preferenceForKey('cryptor').toString();
  }
  catch (e) {
    c_start=document.cookie.indexOf("cryptor=");
    if (c_start!=-1) { 
      c_start=c_start+"cryptor".length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) {
        c_end=document.cookie.length;
      }
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
}

// Makes the specified element appear.
function turnOn(a) {
  g(a).style.display='inline';
}

// Makes the specified element disappear.
function turnOff(a) {
  g(a).style.display='none';
}

// Resets all field values in the file windows and returns to the main screen.
function goBackToMain() {
  main=g('mainWin');
  in_transit=true;
  var windowsToUnload=new Array('saveWindow', 'loadWindow', 'delWindow', 'skinWindow');
  loaded=-1;
  setValue('saveInput','');
  setValue('passEnter','');
  g('passEnter').disabled="disabled";
  g('passBox').checked=false;
  
  if (!E_TRANSITIONS) {
    for (i in windowsToUnload) {
      turnOff(windowsToUnload[i]);
    }
    turnOn('mainWin');
    (g('mainWin').filters)?g('mainWin').filters.alpha.opacity=100:g('mainWin').style.opacity=1;
    g('promptSpacer').style.display="none";
    in_transit=false;
    justEndLoad();
    g('skinBox').src='images/skin.gif';
    g('skinBox').alt="Edit Skin";
    g('skinBox').title="Edit Skin";
    return;
  }
  
  // Lowers the opacity of all windows by a predetermined amount, then hides them all.
  if (opaci_down>0) {
    opaci_down-=trans_speed;
    for (i in windowsToUnload) {
      (g(windowsToUnload[i]).filters)?g(windowsToUnload[i]).filters.alpha.opacity=opaci_down:g(windowsToUnload[i]).style.opacity=(opaci_down/100);
    }
    c_timer=setTimeout("goBackToMain()",24);
    return;
  }
  if (opaci_down==0) {
    for (i in windowsToUnload) {
      turnOff(windowsToUnload[i]);
    }
    turnOn('mainWin');
    g('promptSpacer').style.display="none";
  }
  
  // Increase the opacity of the main window until it reaches full.
  if (opaci_up<101) {
    opaci_up+=trans_speed;
    (main.filters)?main.filters.alpha.opacity=opaci_up:main.style.opacity=(opaci_up/100);
    c_timer=setTimeout("goBackToMain()",24);
    return;
  }
  else {
    opaci_down=100;
    opaci_up=0;
    in_transit=false;
    justEndLoad();
    g('skinBox').src='images/skin.gif';
    g('skinBox').alt="Edit Skin";
    g('skinBox').title="Edit Skin";
  }
}

// Sets up any window.
function showWindow(a) {
  main=g('mainWin');
  selWindow=g(a);
  g('skinBox').src='images/back.gif';
  g('skinBox').alt="Go Back to Main Screen";
  g('skinBox').title="Go Back to Main Screen";
  
  if (!E_TRANSITIONS) {
    turnOff('mainWin');
    (selWindow.filters)?selWindow.filters.alpha.opacity=100:selWindow.style.opacity=1;
    turnOn(a);
    g('promptSpacer').style.display="block";
    in_transit=false;
    if (a=="saveWindow") {
      g('saveInput').focus();
    }
    if (a=="skinWindow") {
      g('enTrans').focus();
    }
    return;
  }
  
  // Lowers the opacity of the main window until it reaches zero.
  if (opaci_down>0) {
    opaci_down-=trans_speed;
    (main.filters)?main.filters.alpha.opacity=opaci_down:main.style.opacity=(opaci_down/100);
    c_timer=setTimeout(function() {showWindow(a);},24);
    return;
  }
  if (opaci_down<=0) {
    turnOff('mainWin');
    turnOn(a);
    g('promptSpacer').style.display="block";
  }
  
  // Raises the opacity of the load window until it reaches full.
  if (opaci_up<101) {
    opaci_up+=trans_speed;
    (selWindow.filters)?selWindow.filters.alpha.opacity=opaci_up:selWindow.style.opacity=(opaci_up/100);
    c_timer=setTimeout(function() {showWindow(a);},24);
    return;
  }
  else {
    opaci_down=100;
    opaci_up=0;
    in_transit=false;
    if (a=="saveWindow") {
      g('saveInput').focus();
    }
    if (a=="skinWindow") {
      g('enTrans').focus();
    }
  }
}

// Minimize/Maximize toggle.
function shade() {
  (min)?min=false:min=true; // Toggles min/max status.
  // Minimizes the window.
  if (min) {
    try {
      var widgTest=widget;
      document.getElementsByTagName('fieldset')[0].style.display="none";
      g('minBox').style.display="block";
      if (!ab) {about();}
    }
    catch (e) {
      g('toBeMin').style.display="none";
      g('shadeBox').src="images/max.gif";
      g('shadeBox').alt="Maximize";
      g('shadeBox').title="Maximize";
      g('sloganArea').style.display="inline";
      var tempSlog=slogans[Math.round(Math.random()*(slogans.length-1))];
      g('sloganArea').innerHTML=tempSlog;
      holdSlogan=tempSlog;
    }
  }
  // Maximizes the window.
  else {
    try {
      var widgTest=widget;
      document.getElementsByTagName('fieldset')[0].style.display="block";
      g('minBox').style.display="none";
      g('sloganArea').style.display="none";
    }
    catch (e) {
      g('toBeMin').style.display="inline";
      g('shadeBox').src="images/min.gif";
      g('shadeBox').alt="Minimize";
      g('shadeBox').title="Minimize";
      g('sloganArea').style.display="none";
    }
  }
}

// Shows the about box.
function about() {
  (ab)?ab=false:ab=true;
  (ab)?g('footer').style.display="none":g('footer').style.display="block";
}

// Toggle Configuration Toolbar
function toggleConfigRow() {
  // Makes row visible
  if (!configToggle) {
    try {
      g('configRow').style.display="table-row";
      g('lineRow').style.display="table-row";
    }
    catch (e) {
      g('configRow').style.display="inline";
      g('lineRow').style.display="inline";
    }
    configToggle=true;
  }
  // Makes row invisible.
  else {
    g('configRow').style.display="none";
    if (!fileToggle) {
      g('lineRow').style.display="none";
    }
    configToggle=false;
  }
}

// Toggle File Menu Toolbar
function toggleButtonRow() {
  // Makes row visible
  if (!fileToggle) {
    try {
      g('buttonRow').style.display="table-row";
      g('lineRow').style.display="table-row";
    }
    catch (e) {
      g('buttonRow').style.display="inline";
      g('lineRow').style.display="inline";
    }
    fileToggle=true;
  }
  // Makes row invisible.
  else {
    g('buttonRow').style.display="none";
    if (!configToggle) {
      g('lineRow').style.display="none";
    }
    fileToggle=false;
  }
}

// On enable change.
function smartFilter(a) {
  if (a.checked || checkCiphersEnabled()) {
    g('filter').src='images/cancel.gif';
    g('filter').alt="Turn OFF all ciphers";
    g('filter').title="Turn OFF all ciphers";
  }
  else {
    g('filter').src='images/ok.gif';
    g('filter').alt="Turn ON all ciphers";
    g('filter').title="Turn ON all ciphers";
  }
}

// Is another cipher on?
function checkCiphersEnabled() {
  var ciphers=new Array('b64', 'esc', 'morse', 'sub', 'polybius', 'trans', 'whiteNoise');
  for (i in ciphers) {
    if (g(ciphers[i]).checked) {return true;}
  }
  return false;
}

// Smart toggles ciphers
function toggleCiphers(a, b, c) {
  var ciphers=new Array('b64', 'esc', 'morse', 'sub', 'polybius', 'trans', 'whiteNoise');
  for (i in ciphers) {
    g(ciphers[i]).checked=a;
  }
  g('filter').src='images/'+b+'.gif';
  g('filter').alt="Turn "+c+" all ciphers";
  g('filter').title="Turn "+c+" all ciphers";
}

function redrawConfig(a) {
  var ciphers=new Array('b64Enable', 'escEnable', 'morEnable', 'subEnable', 'traEnable', 'polEnable', 'wnEnable');
  for (i in ciphers) {
    g(ciphers[i]).style.display="none";
  }
  g(a).style.display="inline";
}
