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

/*****************
Skin Sections (Cookie Syntax):
1	-	Font Family (Selected Index)
2	-	Font Size (%)
3	-	Bold
4	-	Chosen Background Index
5	-	Background URL
6	-	Background solid- Red
7	-	Background solid- Green
8	-	Background solid- Blue
9	-	Dialog color theme
10	-	Transitions Enabled?
11	-	Two-Way Boxes Enabled?
12	-	24 Hour Clock Enabled?
*****************/

var f_Colors=new Array("000","fff","fff","000","000","fff","000");
var b_Colors=new Array("fff","000","016","dc9","ddd","067428","fe2");
var b_Grounds=new Array("content_back.png","black2.png","azul.png","dune2.png","diamond.png","greenscene.png","scrooge.png");
  
// Open Skin Window
function showSkin() {
  // Go to theme
  if (!in_transit && g('skinBox').src.search("back.gif")==-1) {
    in_transit=true;
    showWindow('skinWindow');
  }
  // Go back to Main
  else if (!in_transit) {
    goBackToMain();
  }
}

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

// Returns the value of the cookie.
function getSkin() {
  try {
    return widget.preferenceForKey('cryptSkin').toString();
  }
  catch (e) {
    c_start=document.cookie.indexOf("cryptSkin=");
    if (c_start!=-1) { 
      c_start=c_start+"cryptSkin".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));
    }
  }
}

// Selected value is valid text size.
function validZoom(a) {
  try {
    if (parseInt(a)<=1000 && parseInt(a)>0) {
      return true;
    }
    throw "Nope.";
  }
  catch (e) {
    return false;
  }
}

// Selected color is valid RGB.
function validColor(a) {
  return (!isNaN(parseInt(a)) && parseInt(a)<256 && parseInt(a)>=0);
}

// Color Preview
function updateClub() {
  if (validColor(g('backR').value) && validColor(g('backG').value) && validColor(g('backB').value)) {
    g('clubs').style.backgroundColor="rgb("+g('backR').value+","+g('backG').value+","+g('backB').value+")";
  }
}

// Searches for the selected Background radio button.
function findBack() {
  var temp=document.getElementsByName("backImg");
  for (i=0; i<temp.length; i++) {
    if (temp[i].checked) {
      return i;
    }
  }
  return -1;
}

// Applies the user's changes and saves the new skin.
function saveChanges() {
  applySkin();
  var tempSkin=new Array();
  tempSkin.push(g('skinFont').selectedIndex);
  tempSkin.push(g('textSize').value);
  tempSkin.push(g('embolden').checked);
  tempSkin.push(findBack());
  tempSkin.push(g('backBox').value);
  tempSkin.push(g('backR').value);
  tempSkin.push(g('backG').value);
  tempSkin.push(g('backB').value);
  tempSkin.push(g('diagColors').selectedIndex);
  tempSkin.push(g('enTrans').checked);
  tempSkin.push(g('enTwo').checked);
  tempSkin.push(g('en24Hour').checked);
  setSkin(tempSkin.join(":SPLIT:"));
}

// Resets all the default skin settings.
function resetDefault() {
  g('skinFont').selectedIndex=10;
  g('backR').value=0;
  g('backG').value=128;
  g('backB').value=255;
  g('backBox').value="http://";
  g('embolden').checked=false;
  document.getElementsByName("backImg")[0].checked=true;
  g('textSize').value=100;
  g('diagColors').selectedIndex=0;
  g('enTrans').checked=true;
  g('enTwo').checked=true;
  g('en24Hour').checked=true;
  updateClub();
  saveChanges();
}

// Load skin settings.
function loadSkin() {
  var tempSkin;
  try {
    tempSkin=getSkin().split(":SPLIT:");
    if (tempSkin.length<2) {
      throw "Nope. Too small.";
    }
  }
  catch (e) {
    resetDefault();
    return;
  }
  g('skinFont').selectedIndex=tempSkin[0];
  g('textSize').value=tempSkin[1];
  g('embolden').checked=(tempSkin[2]=="true");
  document.getElementsByName("backImg")[tempSkin[3]].checked=true;
  g('backBox').value=tempSkin[4];
  g('backR').value=tempSkin[5];
  g('backG').value=tempSkin[6];
  g('backB').value=tempSkin[7];
  g('diagColors').selectedIndex=tempSkin[8];
  g('enTrans').checked=(tempSkin[9]=="true");
  g('enTwo').checked=(tempSkin[10]=="true");
  g('en24Hour').checked=(tempSkin[11]=="true");
  applySkin();
}


// Applies all skin UI.
function applySkin() {
  var S_font=g('skinFont').options[g('skinFont').selectedIndex].value;
  var S_size=g('textSize').value;
  var S_isBold=g('embolden').checked;
  var S_backRadio=findBack();
  var S_URL=g('backBox').value;
  var S_Red=g('backR').value;
  var S_Green=g('backG').value;
  var S_Blue=g('backB').value;
  var S_trans=g('enTrans').checked;
  var S_two=g('enTwo').checked;
  var S_24hour=g('en24Hour').checked;
  
  var backsLength=document.getElementsByName("backImg").length;
  
  // User chose solid color.
  if (S_backRadio==backsLength-2) {
    if (validColor(S_Red) && validColor(S_Green) && validColor(S_Blue)) {
      if (g('mainBody1')!=null) {
        g('mainBody1').style.backgroundImage="url()";
        g('mainBody1').style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
      }
      else {
        document.getElementsByTagName('fieldset')[0].style.backgroundImage="url()";
        document.getElementsByTagName('fieldset')[0].style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
      }
      g('footer').style.backgroundImage="url()";
      g('minBox').style.backgroundImage="url()";
      g('minBox').style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
    }
  }
  
  // Custom background.
  else if (S_backRadio==backsLength-1) {
    if (validColor(S_Red) && validColor(S_Green) && validColor(S_Blue)) {
      if (g('mainBody1')!=null) {
        g('mainBody1').style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
      }
      else {
        document.getElementsByTagName('fieldset')[0].style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
        g('minBox').style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
      }
    }
    if (g('mainBody1')!=null) {
      g('mainBody1').style.backgroundImage="url("+S_URL+")";
    }
    else {
      document.getElementsByTagName('fieldset')[0].style.backgroundImage="url("+S_URL+")";
    }
    g('footer').style.backgroundImage="url("+S_URL+")";
    g('minBox').style.backgroundImage="url("+S_URL+")";
  }
  
  // Prefabricated backgrounds.
  else {
    if (g('mainBody1')!=null) {
      g('mainBody1').style.backgroundImage="url("+backSwatches[S_backRadio]+")";
    }
    else {
      document.getElementsByTagName('fieldset')[0].style.backgroundImage="url("+backSwatches[S_backRadio]+")";
    }
    g('footer').style.backgroundImage="url("+backSwatches[S_backRadio]+")";
    g('minBox').style.backgroundImage="url("+backSwatches[S_backRadio]+")";
  }
  
  // Background Color for footer and Minbox.
  if (validColor(S_Red) && validColor(S_Green) && validColor(S_Blue)) {
    g('footer').style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
    g('minBox').style.backgroundColor="rgb("+S_Red+","+S_Green+","+S_Blue+")";
  }
  
  var temp=document.getElementsByTagName("textarea");
  
  // Font Face
  for (i=0; i<temp.length; i++) {
    temp[i].style.fontFamily=S_font;
  }
    
  // Font Size
  if (validZoom(S_size)) {
    for (i=0; i<temp.length; i++) {
      temp[i].style.fontSize=S_size+"%";
    }
  }
  
  // Boldness
  if (S_isBold) {
    for (i=0; i<temp.length; i++) {
      temp[i].style.fontWeight="bold";
    }
  }
  else {
    for (i=0; i<temp.length; i++) {
      temp[i].style.fontWeight="normal";
    }
  }
  
  // Dialog colors.
  if (navigator.appName.search("Microsoft")==-1) {
    invertColors(g('diagColors').selectedIndex);
  }
  
  // Toggles faded transitions.
  E_TRANSITIONS=S_trans;
  
  // Toggles Two-Way textboxes.
  E_TWOWAY=(g('enTwo').checked);
  if (E_TWOWAY) {
    g('D_Button').value="<< Decrypt";
    g('decoder').readOnly=false;
    g('decoder').style.backgroundColor=b_Colors[g('diagColors').selectedIndex];
    g('decoder').style.color=f_Colors[g('diagColors').selectedIndex];
  }
  else {
    g('D_Button').value="Decrypt >>";
    g('decoder').readOnly=true;
    g('decoder').style.backgroundColor="#ccc";
    g('decoder').style.color="#000";
  }
  
  // 24 Hour Clock?
  E_24HOUR=S_24hour;
  
  updateClub();
}

// Sets the page theme according to color selection.
function invertColors(theme) {
  var a=f_Colors[theme];
  var b=b_Colors[theme];
  var c=b_Grounds[theme];
  
  var styles="body {color:#"+a+" !important;} #delPrompt, #loadPrompt, #savePrompt, #sloganArea, .containerTop, .containerBottom, .configLabels label, .skinMiniBox, .backgroundTablePart {background-image:url(Backgrounds/"+c+")} textarea {background:#"+b+";color:#"+a+"} input[type='text'], select, option {background:#"+b+";color:#"+a+";border-color:#"+a+";}";

  try {
    var invBack=document.createElement("style");
    invBack.type="text/css";
    invBack.appendChild(document.createTextNode(styles));
    document.body.appendChild(invBack);
  }
  catch (e) {}
  
  // Non-widget background.
  try {
    var widgTest=widget;
  }
  catch (f) {
    document.getElementsByTagName("HTML")[0].style.backgroundImage="url('Backgrounds/"+c+"')";
  }
}