/*** Slide Formatting Sidebar ***/

// Default values
var defaultSlideNotes="";
var defaultSlideBacks = ["61", "", "255", "255", "255"]; // Default Backgrounds
var defaultSlideLinks = ["0", "0", "20", "1", "0", "0"]; // Default Links
var defaultSlideLists = ["0", "2", "0", ""]; // Default Lists
var defaultSlideSpace = ["1", "120", "8"]; // Default Spacing
var defaultSlideLayout = ["2", ""]; // Default Layout
var defaultCustomFields = [["$AUTHOR", "(Your Name)"], ["$GENERATOR", getGenerator()]];
var defaultMetaData = ['', '', '', '', getGenerator(), '', '', '', 'S5 1.1', '', 0, 7, 2, 3];
var defaultSlideTransitions = [0];
var defaultSlideTiming = [false, 0, 30];

// New slide values
var newSlideNotes = "";
var newSlideBacks = [].concat(defaultSlideBacks);
var newSlideLinks = [].concat(defaultSlideLinks);
var newSlideLists = [].concat(defaultSlideLists);
var newSlideSpace = [].concat(defaultSlideSpace);
var newSlideLayout = [].concat(defaultSlideLayout);
var newSlideTransitions = [].concat(defaultSlideTransitions);
var newSlideTiming = [].concat(defaultSlideTiming);

// Sets new slide formatting to defaults
function resetDefaultSlideValues() {
  newSlideNotes = "";
  newSlideBacks = [].concat(defaultSlideBacks);
  newSlideLinks = [].concat(defaultSlideLinks);
  newSlideLists = [].concat(defaultSlideLists);
  newSlideSpace = [].concat(defaultSlideSpace);
  newSlideLayout = [].concat(defaultSlideLayout);
  newSlideTransitions = [].concat(defaultSlideTransitions);
  newSlideTiming = [].concat(defaultSlideTiming);
  customFields = [].concat(defaultCustomFields);
  g('customFieldsSelect').selectedIndex = 0;
  g('customFieldChildren').innerHTML = "";
  g('customField1').value = "";
  g('customField2').value = "";
  for (i=0; i<customFields.length; i++) {
    g('customFieldChildren').innerHTML += "<option>" + customFields[i][0] + "<\/option>";
  }
}

var slideBacks = [];
var slideLinks = [];
var slideLists = [];
var slideSpace = [];
var slideLayout = [];
var slideTrans = [];
var slideTiming = [];
var customFields = [].concat(defaultCustomFields);
var slideTemplates = [
  "",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"cell half\">\n      <div class=\"layoutBox\">Click to Add Text</div>\n      <div class=\"layoutBox\">Click to Add Text</div>\n    </div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"cell half\">\n      <div class=\"layoutBox\">Click to Add Text</div>\n      <div class=\"layoutBox\">Click to Add Text</div>\n    </div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"layoutBox title\">Click to Add Title\n    <div class=\"layoutBox subtitle\">Click to Add Subtitle</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"cell half\">\n      <div class=\"layoutBox\">Click to Add Text</div>\n      <div class=\"layoutBox\">Click to Add Text</div>\n    </div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"cell half\">\n      <div class=\"layoutBox\">Click to Add Text</div>\n      <div class=\"layoutBox\">Click to Add Text</div>\n    </div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n    <div class=\"layoutBox cell half\">Click to Add Text</div>\n  </div>\n</div>",
  "<div class=\"template\">\n  <div class=\"container\">\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n  </div>\n  <div class=\"container\">\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n    <div class=\"layoutBox cell third\">Click to Add Text</div>\n  </div>\n</div>"
];

var layoutCSS = "html {\n  overflow:auto;\n}\nhtml, body {\n  margin:0;\n  padding:0;\n  width:100%;\n}\ndiv.template {\n  width:100%;\n  text-align:center;\n  margin:0;\n  padding:0;\n}\ndiv.template div {\n  margin:0;\n  box-sizing:border-box;\n  -moz-box-sizing:border-box;\n  -webkit-box-sizing:border-box;\n}\ndiv.container ul, div.container ol {\n  text-align:left;\n  margin:0;\n}\ndiv.template div.container {\n  margin:1em 1%;\n}\ndiv.layoutBox {\n  border:1px dotted;\n  padding:.5em;\n  margin:0;\n  font-size:100%;\n}\ndiv.title {\n  font-size:200%;\n  width:90%;\n  display:block;\n  margin:1em auto !important;\n  font-family:'Arial Black', sans-serif;\n}\ndiv.subtitle {\n  font-size:50%;\n}\ndiv.cell {\n  display:inline-block;\n  vertical-align:text-top;\n  overflow:visible;\n  width:90%;\n}\ndiv.half {\n  width:45%;\n}\ndiv.third {\n  width:30%;\n}\ndiv.cell div.layoutBox:not(:first-child) {\n  margin-top:1em;\n}";

function getFormatting() {
  getBackground();
  getLinks();
  getLists();
  getSpace();
  getLayout();
}
function getBackground() {
  slideBacks[curSlide] = [getCheckedValue('pBackground'), g('pageBURL').value];
  
  // Convert HSL
  var temp = [g('pageB1').value, g('pageB2').value, g('pageB3').value];
  if (currentColorType.match("HSL") != null) {
    temp = hsl2rgb(temp[0], temp[1], temp[2]);
  }
  
  slideBacks[curSlide] = slideBacks[curSlide].concat(temp);
}
function getLinks() {
  slideLinks[curSlide] = [(g('L_always').checked ? '1' : '0'), (g('L_visitedS').checked ? '1' : '0'), g('linkColorThemes').selectedIndex, (g('L_Ucheck').checked ? '1' : '0'), (g('L_Scheck').checked ? '1' : '0'), (g('L_Ocheck').checked ? '1' : '0')];
}
function getLists() {
  slideLists[curSlide] = [g('olSelect').selectedIndex, g('ulSelect').selectedIndex, getCheckedValue('ulSelected'), g('ulURL').value];
}
function getSpace() {
  slideSpace[curSlide] = [getCheckedValue('l_space'), g('LSP_proportional').value, g('LSP_Units').selectedIndex];
}
function getLayout() {
  slideLayout[curSlide] = [getCheckedValue('layoutButton'), g('layoutCustom').value];
}

// Loads individual slide formatting
function setFormatting() {
  // Layout
  var tempLayout = slideLayout[curSlide];
  document.getElementsByName('layoutButton')[parseInt(tempLayout[0])].checked = true;
  g('layoutCustom').value = tempLayout[1];
  
  // Slide Transition
  document.getElementsByName('transition-list-label')[slideTrans[curSlide][0]].checked = true;
  
  // Slide Timing
  g('advance_check').checked = slideTiming[curSlide][0];
  g('advance_minutes').value = slideTiming[curSlide][1];
  g('advance_seconds').value = slideTiming[curSlide][2];
  
  if (activeFileType == 2 || activeFileType == 3) {
    return;
  }
  
  // Background
  var tempBacks = [slideBacks[curSlide][0], slideBacks[curSlide][1]];
  
  var temp = [slideBacks[curSlide][2], slideBacks[curSlide][3], slideBacks[curSlide][4]];
  if (currentColorType.match("HSL") != null) {
    temp = rgb2hsl(temp[0], temp[1], temp[2]);
  }
  g('pageB1').value = temp[0];
  g('pageB2').value = temp[1];
  g('pageB3').value = temp[2];
  updateColorBox("pageB");
  
  document.getElementsByName('pBackground')[parseInt(tempBacks[0])].checked = true;
  g('pageBURL').value = tempBacks[1];
  
  // Links
  var tempLinks = slideLinks[curSlide];
  g('L_always').checked = (tempLinks[0] == '1');
  g('L_visitedS').checked = (tempLinks[1] == '1');
  g('linkColorThemes').selectedIndex = parseInt(tempLinks[2]);
  g('L_Ucheck').checked = (tempLinks[3] == '1');
  g('L_Scheck').checked = (tempLinks[4] == '1');
  g('L_Ocheck').checked = (tempLinks[5] == '1');
  
  // Bullets/Lists
  var tempLists = slideLists[curSlide];
  g('olSelect').selectedIndex = parseInt(tempLists[0]);
  g('ulSelect').selectedIndex = parseInt(tempLists[1]);
  document.getElementsByName('ulSelected')[parseInt(tempLists[2])].checked = true;
  g('ulURL').value = tempLists[3];
  
  // Line Spacing
  var tempSpace = slideSpace[curSlide];
  document.getElementsByName('l_space')[parseInt(tempSpace[0])].checked = true;
  g('LSP_proportional').value = tempSpace[1];
  g('LSP_Units').selectedIndex = parseInt(tempSpace[2]);
  
  listPreview();
  applyToSlide();
}

function applyToSlide() {
  applyBackground();
  applyLinks();
  applyLists();
  applySpace();
  forceRepaint();
}

/*** Backgrounds ***/

function applyBackground() {
  stripReserved('pageBURL');
  getBackground(); // Save new background data
  
  var tempRadio = document.getElementsByName('pBackground');
  var contentBack = g('contentFrame').contentWindow.document.getElementsByTagName('html')[0];
  var temp = [slideBacks[curSlide][2], slideBacks[curSlide][3], slideBacks[curSlide][4]];  // Background colors

  g('contentFrame').style.backgroundColor = "#" + RGB2HEX(temp[0]) + RGB2HEX(temp[1]) + RGB2HEX(temp[2]);
  contentBack.style.backgroundColor = "#" + RGB2HEX(temp[0]) + RGB2HEX(temp[1]) + RGB2HEX(temp[2]);
  
  // Background swatches
  if (getCheckedValue('pBackground') < tempRadio.length-2) {
    contentBack.style.backgroundImage = "url(" + backSwatches[getCheckedValue('pBackground')] + ")";
  }
  // URL Image
  else if (getCheckedValue('pBackground') < tempRadio.length-1) {
    contentBack.style.backgroundImage = "url(" + g('pageBURL').value + ")";
  }
  // Solid color
  else {
    contentBack.style.backgroundImage = 'none'; // Clears earlier backgrounds.
  }
}

function applyNewBackground() {
  stripReserved('pageBURL');
  newSlideBacks = [getCheckedValue('pBackground'), g('pageBURL').value];
  
  // Convert HSL
  var temp = [g('pageB1').value, g('pageB2').value, g('pageB3').value];
  if (currentColorType.match("HSL") != null) {
    temp = hsl2rgb(temp[0], temp[1], temp[2]);
  }
  
  newSlideBacks = newSlideBacks.concat(temp);
}

function applyAllBackgrounds() {
  stripReserved('pageBURL');
  var temp = [getCheckedValue('pBackground'), g('pageBURL').value];
  
  // Convert HSL
  var tmp2 = [g('pageB1').value, g('pageB2').value, g('pageB3').value];
  if (currentColorType.match("HSL") != null) {
    tmp2 = hsl2rgb(tmp2[0], tmp2[1], tmp2[2]);
  }
  
  temp = temp.concat(tmp2);
  
  for (i=0; i<slideBacks.length; i++) {
    slideBacks[i] = [].concat(temp);
  }
  applyBackground();
  applyNewBackground();
}

/*** Links ***/

function applyLinks(preview) {
  var tempLinks = [(g('L_always').checked ? '1' : '0'), (g('L_visitedS').checked ? '1' : '0'), g('linkColorThemes').selectedIndex, (g('L_Ucheck').checked ? '1' : '0'), (g('L_Scheck').checked ? '1' : '0'), (g('L_Ocheck').checked ? '1' : '0')];
  var finalString = "";
  finalString += ((tempLinks[3] == '1' || tempLinks[0] == '1') ? ' underline' : '');
  finalString += (tempLinks[4] == '1' ? ' line-through' : '');
  finalString += (tempLinks[5] == '1' ? ' overline' : '');
  if (finalString == '') {
    finalString = 'none';
  }
  
  // :hover
  g('sampleLink').onmouseover = function() {
    g('sampleLink').style.textDecoration = finalString;
    g('sampleLink').style.color = tableColors[parseInt(tempLinks[2])][0];
  };
  // :link
  g('sampleLink').onmouseout = function() {
    g('sampleLink').style.textDecoration = (tempLinks[0] == "1") ? 'underline' : 'none';
    g('sampleLink').style.color = tableColors[parseInt(tempLinks[2])][1];
  };
  
  // Sample Link CSS
  g('sampleLink').style.textDecoration=(tempLinks[0] == "1") ? 'underline' : 'none';  // Always underline
  g('sampleLink').style.color = tableColors[parseInt(tempLinks[2])][1];
  g('sampleLink').style.backgroundColor = tableColors[parseInt(tempLinks[2])][5]; // Aesthetic background color
  
  // Don't save any of this yet!
  if (preview) {
    return;
  } 
  
  // Change link CSS for content window
  try {
    var cssString = "a:link {color:" + tableColors[parseInt(tempLinks[2])][1] + "; text-decoration:" + ((tempLinks[0] == "1") ? 'underline' : 'none') + ";}";
        cssString += "a:visited {color:" + tableColors[parseInt(tempLinks[2])][2] + ";"
        cssString += "text-decoration:" + ((tempLinks[0] == "1" || tempLinks[1] == "1") ? (((tempLinks[0] == "1") ? 'underline' : '') + ((tempLinks[1] == "1") ? ' line-through' : '')) : 'none') + ";}";
        cssString += "a:hover {color:" + tableColors[parseInt(tempLinks[2])][0] + "; text-decoration:" + finalString + ";}";
    var d = g('contentFrame').contentWindow.document;
    var listNode = d.createElement("style");
    listNode.setAttribute('type', 'text/css');
    listNode.appendChild(d.createTextNode(cssString));
    d.getElementsByTagName('head')[0].appendChild(listNode);
  }
  catch (e) {}
  
  getLinks(); // Save new link data
}

function applyNewLinks() {
  newSlideLinks = [(g('L_always').checked ? '1' : '0'), (g('L_visitedS').checked ? '1' : '0'), g('linkColorThemes').selectedIndex, (g('L_Ucheck').checked ? '1' : '0'), (g('L_Scheck').checked ? '1' : '0'), (g('L_Ocheck').checked ? '1' : '0')];
}

function applyAllLinks() {
  var temp = [(g('L_always').checked ? '1' : '0'), (g('L_visitedS').checked ? '1' : '0'), g('linkColorThemes').selectedIndex, (g('L_Ucheck').checked ? '1' : '0'), (g('L_Scheck').checked ? '1' : '0'), (g('L_Ocheck').checked ? '1' : '0')];
  for (i=0; i<slideLinks.length; i++) {
    slideLinks[i] = [].concat(temp);
  }
  applyLinks();
  applyNewLinks();
}

/*** Lists ***/

// Updates the sample <ul> list
function listPreview() {
  var temp = document.getElementsByName('ulSelected')[0].checked;
  g('ulSample').style.listStyleImage=(temp) ? 'none' : 'url(' + g('ulURL').value + ')'; // List style image
  if (temp) {
    g('ulSample').style.listStyleType = g('ulSelect').options[g('ulSelect').selectedIndex].text;
  }
}

function applyLists() {
  stripReserved('ulURL');
  getLists(); // Save new list data
  
  // Changes sample
  g('ulSample').style.listStyleType = g('ulSelect').options[g('ulSelect').selectedIndex].value;
  g('olSample').style.listStyleType = g('olSelect').options[g('olSelect').selectedIndex].value;
  g('ulSample').style.listStyleImage = ''; // Resets image
  
  if (getCheckedValue('ulSelected') > 0) {
    g('ulSample').style.listStyleImage = "url(" + g('ulURL').value + ")";
  }
  
  // Change list CSS for content window
  try {
    var cssString = "ul {list-style-type:" + g('ulSelect').options[g('ulSelect').selectedIndex].value + "}";
        cssString += "ol {list-style-type:" + g('olSelect').options[g('olSelect').selectedIndex].value + "}";
        cssString += "ul {list-style-image:"+ ((getCheckedValue('ulSelected') > 0) ? "url(" + g('ulURL').value + ")" : "none") + "}";
	// <style> element
	var listNode = g('contentFrame').contentWindow.document.createElement("style");
    listNode.type = 'text/css';
    listNode.innerHTML = cssString;
    g('contentFrame').contentWindow.document.getElementsByTagName('head')[0].appendChild(listNode);
  }
  catch (e) {}
}

function applyNewLists() {
  stripReserved('ulURL');
  newSlideLists = [g('olSelect').selectedIndex, g('ulSelect').selectedIndex, getCheckedValue('ulSelected'), g('ulURL').value];
}

function applyAllLists() {
  stripReserved('ulURL');
  var temp = [g('olSelect').selectedIndex, g('ulSelect').selectedIndex, getCheckedValue('ulSelected'), g('ulURL').value];
  for (i=0; i<slideLists.length; i++) {
    slideLists[i] = [].concat(temp);
  }
  applyLists();
  applyNewLists();
}

/*** Line Spacing ***/

function applySpace() {
  stripReserved('LSP_proportional');
  getSpace(); // Save new spacing data
  var tempSpaces=['100%', '150%', '200%', g('LSP_proportional').value+g('LSP_Units').options[g('LSP_Units').selectedIndex].text];
  
  // <style> element
  var spaceNode = g('contentFrame').contentWindow.document.createElement("style");
  spaceNode.type = 'text/css';
  spaceNode.appendChild(document.createTextNode("* {line-height:" + tempSpaces[getCheckedValue('l_space')] + ";}"));
  g('contentFrame').contentWindow.document.getElementsByTagName('head')[0].appendChild(spaceNode);
  
  forceRepaint();
}

function applyNewSpace() {
  stripReserved('LSP_proportional');
  newSlideSpace=[getCheckedValue('l_space'), g('LSP_proportional').value, g('LSP_Units').selectedIndex];
}

function applyAllSpace() {
  stripReserved('LSP_proportional');
  var temp = [getCheckedValue('l_space'), g('LSP_proportional').value, g('LSP_Units').selectedIndex];
  for (i=0; i<slideSpace.length; i++) {
    slideSpace[i] = [].concat(temp);
  }
  applySpace();
  applyNewSpace();
}

/*** Layout ***/

// Custom slide layout textarea
function getCurrentSlideData() {
  g('layoutCustom').value = (HTMLToggle) ? g('contentFrame').contentWindow.document.body.innerHTML : g('textWindow').value;
}

function applyLayout() {
  stripReserved('layoutCustom');
  var temp = getCheckedValue('layoutButton');
  g('contentFrame').contentWindow.document.getElementsByTagName('head')[0].innerHTML += "<style type='text/css'>" + layoutCSS + "</style>";
  
  // New Template layout
  if (temp < 20) {
	g('contentFrame').contentWindow.document.body.innerHTML = slideTemplates[temp] + "<br>";
    g('textWindow').value = slideTemplates[temp];
  }
  // Date Time
  else if (temp == 20) {
    g('contentFrame').contentWindow.document.body.innerHTML = getDateTime(parseInt(data.get("Velt_2_skinGlobal").split("|N|")[3])) + "<br /><br />LOG";
    g('textWindow').value = getDateTime(parseInt(data.get("Velt_2_skinGlobal").split("|N|")[3])) + "<br /><br />LOG";
  }
  // Custom layout
  else {
    g('contentFrame').contentWindow.document.body.innerHTML = g('layoutCustom').value;
    g('textWindow').value = g('layoutCustom').value;
  }
}

function applyNewLayout() {
  stripReserved('layoutCustom');
  newSlideLayout[0] = getCheckedValue('layoutButton');
  newSlideLayout[1] = g('layoutCustom').value;
}

/*** Slide Transitions ***/
function getCurrentSlideTransition() {
  return getCheckedValue('transition-list-label');
}

function applySlideTransition() {
  slideTrans[curSlide] = [getCurrentSlideTransition()];
}

function applyNewSlideTransition() {
  newSlideTransitions = [getCurrentSlideTransition()];
}

function applyAllSlideTransition() {
  for (var i=0; i<slideTrans.length; i++) {
    slideTrans[i] = [].concat([getCurrentSlideTransition()]);
  }
  applyNewSlideTransition();
}

/*** Slide Timing ***/
function getSlideTiming() {
  var tmp = parseInt(g('advance_minutes').value);
  g('advance_minutes').value = isNaN(tmp) ? 0 : tmp;
  
  tmp = parseInt(g('advance_seconds').value);
  g('advance_seconds').value = isNaN(tmp) ? 0 : tmp;
  
  return [g('advance_check').checked, parseInt(g('advance_minutes').value), parseInt(g('advance_seconds').value)];
}

function applySlideTiming() {
  slideTiming[curSlide] = getSlideTiming();
}

function applyNewSlideTiming() {
  newSlideTiming = getSlideTiming();
}

function applyAllSlideTiming() {
  for (var i=0; i < slideTiming.length; i++) {
    slideTiming[i] = getSlideTiming();
  }
  applyNewSlideTiming();
}

/*** Custom Fields ***/

function addCustomField() {
  stripReserved('customField1');
  stripReserved('customField2');
  customFields.push([g('customField1').value, g('customField2').value]);
  g('customFieldChildren').innerHTML += "<option>" + g('customField1').value + "</option>";
  g('customFieldsSelect').selectedIndex = g('customFieldsSelect').options.length-1;
  g('cfDeleteButton').disabled = false;
}

function removeCustomField() {
  var sel = g('customFieldsSelect').selectedIndex;
  if (sel < 1) {
    return;
  }
  g('customFieldsSelect').selectedIndex = sel-1;
  customFields.splice(sel-1, 1);
  g('customFieldsSelect').options[sel] = null;
  changeCustomField();
}

function saveCustomField() {
  var sel = g('customFieldsSelect').selectedIndex;
  if (sel < 1) {
    addCustomField();
    return;
  }
  stripReserved('customField1');
  stripReserved('customField2');
  customFields[sel-1] = [g('customField1').value, g('customField2').value];
  g('customFieldsSelect').options[sel].text = customFields[sel-1][0];
}

function changeCustomField() {
  var k = g('customFieldsSelect').selectedIndex;
  g('cfDeleteButton').disabled = (k < 1);
  g('customField1').value= k<1 ? '' : customFields[k-1][0];
  g('customField2').value= k<1 ? '' : customFields[k-1][1];
}

/*** File Info Metadata ***/

function resetMetaData() {
  var temp = userProfiles[0]>0;
  g('efiN').value = temp ? userProfiles[userProfiles[0]][1] : defaultMetaData[0];
  g('efiC').value = temp ? userProfiles[userProfiles[0]][3] : defaultMetaData[1];
  g('efiE').value = temp ? userProfiles[userProfiles[0]][2] : defaultMetaData[2];
  g('efiD').value = defaultMetaData[3];
  g('efiS').value = temp ? userProfiles[userProfiles[0]][6] : defaultMetaData[5];
  g('efiL').value = temp ? userProfiles[userProfiles[0]][5] : defaultMetaData[6];
  g('efiV').value = defaultMetaData[7];
  g('efiW').value = temp ? userProfiles[userProfiles[0]][4] : defaultMetaData[9];
  g('editFileSelectTL').selectedIndex = defaultMetaData[10];
  g('editFileSelectTR').selectedIndex = defaultMetaData[11];
  g('editFileSelectBL').selectedIndex = defaultMetaData[12];
  g('editFileSelectBR').selectedIndex = defaultMetaData[13];
  updateOverlay();
}

/*** Theme Previews ***/
function applyPreview() {
  var diagCSS = ".s5Overlay {font-weight:bold;background-color:" + tableColors[g('s5PreviewColorThemes').selectedIndex][2] + ";color:" + tableColors[g('s5PreviewColorThemes').selectedIndex][6] + ";}";
  diagCSS += ".s5Overlay a:link, .s5Overlay a:visited {color: " + tableColors[g('s5PreviewColorThemes').selectedIndex][6] + ";} .s5Overlay a:hover {color:" + tableColors[g('s5PreviewColorThemes').selectedIndex][1] + ";}";
  
  g('contentFrame').style.backgroundColor = tableColors[g('s5PreviewColorThemes').selectedIndex][0];
  g('contentFrame').contentWindow.document.getElementsByTagName('html')[0].style.background = '';
  g('contentFrame').contentWindow.document.getElementsByTagName('html')[0].style.backgroundColor = tableColors[g('s5PreviewColorThemes').selectedIndex][0];
  
  // Link colors
  var t = document.createElement("style");
  t.type = "text\/css";
  t.appendChild(document.createTextNode("a:link, a:visited {text-decoration: underline; color:" + tableColors[g('s5PreviewColorThemes').selectedIndex][5] + ";} a:hover {text-decoration: none; color:" + tableColors[g('s5PreviewColorThemes').selectedIndex][1] + ";}"));
  g('contentFrame').contentWindow.document.getElementsByTagName('html')[0].getElementsByTagName('head')[0].appendChild(t);
  
  if (g('overlayStyle').hasChildNodes()) {
    g('overlayStyle').removeChild(g('overlayStyle').childNodes[0]);
  }
  g('overlayStyle').appendChild(document.createTextNode(diagCSS));
}