/***********
 Skin Editor
************/

/** Savers **/
function saveSkinGlobal() {
  var d = getSkinGlobal();
  applySkinGlobal(d);
  data.set("Velt_2_skinGlobal", d.join("|N|"));
}
function saveSkinText() {
  stripReserved('tFont');
  stripReserved('tSize');
  var d = getSkinText();
  data.set("Velt_2_skinText", d.join("|N|"));
  applySkinText(d);
}
function saveSkinBack() {
  stripReserved('wBURL');
  data.set("Velt_2_skinBack", getSkinBack().join("|N|"));
  applySkinBack();
}

/** Loaders **/
function loadSkinGlobal() {
  var skinGlobal=data.get("Velt_2_skinGlobal").split("|N|");
  applySkinGlobal(skinGlobal);
}
function loadSkinText() {
  var skinText=data.get("Velt_2_skinText").split("|N|");
  applySkinText(skinText);
}

/** Appliers **/
function applySkinGlobal(arr) {
  g('Clock24').checked=(arr[0]=="1");
  g('aSlideList').checked=(arr[1]=="1");
  g('eCP').checked=(arr[2]=="1");
  g('dateTimeSelect').selectedIndex=parseInt(arr[3]);
  g('skinTheme').selectedIndex=parseInt(arr[4]);
  g('defaultFileType').selectedIndex=parseInt(arr[5]);
  g('newFileType').selectedIndex=parseInt(arr[5]);
  
  var tempColorSpace=parseInt(data.get('Velt_2_skinGlobal').split('|N|')[6]); // Old color space
  var changeColorType=arr[6]!=tempColorSpace; // RGB->HSL, HSL->RGB
  
  document.getElementsByName("colorType")[parseInt(arr[6])].checked=true;
  currentColorType=(parseInt(arr[6])==0) ? "RGB" : "HSL";
  
  // 24 hour clock
  clock24=(arr[0]=="1")?true:false;
  for (i=0; i<g('dateTimeSelect').options.length; i++) {
    g('dateTimeSelect').options[i].text=getDateTime(i);
  }
  
  // Always show slide list
  g('footStrip').getElementsByTagName('button')[g('footStrip').getElementsByTagName('button').length-1].style.display=(arr[1]=="1")?'none':'inline';
  if (arr[1] == "1") {
    g('slideFloater').style.display='block';
    resizeWidget();
  }
  
  // Backs up last session
  if (arr[2] == "1") {
    cpTimer = setInterval(cpBackup, 5000);
  }
  else {
    clearInterval(cpTimer);
    data.set("Velt_2_crashProtection", "");
  }
  
  /** Set up color ranges **/
  var rangeValues=null;
  try {
    if (isNaN(tempColorSpace)) {throw "First run, use default values.";}
	
    if (currentColorType=='HSL' && changeColorType) {
      rangeValues=[
        rgb2hsl(g('rule1').value, g('rule2').value, g('rule3').value),
        rgb2hsl(g('col1').value, g('col2').value, g('col3').value),
        rgb2hsl(g('high1').value, g('high2').value, g('high3').value),
        rgb2hsl(g('pageB1').value, g('pageB2').value, g('pageB3').value),
        rgb2hsl(g('wB1').value, g('wB2').value, g('wB3').value)
      ];
    }
	else if (currentColorType=='RGB' && changeColorType) {
	  rangeValues=[
        hsl2rgb(g('rule1').value, g('rule2').value, g('rule3').value),
        hsl2rgb(g('col1').value, g('col2').value, g('col3').value),
        hsl2rgb(g('high1').value, g('high2').value, g('high3').value),
        hsl2rgb(g('pageB1').value, g('pageB2').value, g('pageB3').value),
        hsl2rgb(g('wB1').value, g('wB2').value, g('wB3').value)
      ];
	}
	
	else if (!changeColorType) {
	  rangeValues=[
        [g('rule1').value, g('rule2').value, g('rule3').value],
        [g('col1').value, g('col2').value, g('col3').value],
        [g('high1').value, g('high2').value, g('high3').value],
        [g('pageB1').value, g('pageB2').value, g('pageB3').value],
        [g('wB1').value, g('wB2').value, g('wB3').value]
      ];
	}
  }
  // Default values
  catch (e) {
    if (parseInt(arr[6])==0) {
      rangeValues=[
	    [0, 0, 0],			// Rule color
	    [0, 0, 0],			// Text color
	    [255, 128, 0],		// Highlight color
	    [255, 255, 255],	// Slide background
	    [255, 255, 255]	// Widget background
      ];
	}
	if (parseInt(arr[6])==1) {
	  rangeValues=[
	    [0, 0, 0],
	    [0, 0, 0],
	    [30, 100, 50],
	    [0, 0, 100],
	    [0, 0, 100]
      ];
	}
  }

  if (rangeValues) {
    addColorBox("ruleRange", currentColorType, "rule", rangeValues[0][0], rangeValues[0][1], rangeValues[0][2]);
    addColorBox("colRange", currentColorType, "col", rangeValues[1][0], rangeValues[1][1], rangeValues[1][2]);
    addColorBox("highRange", currentColorType, "high", rangeValues[2][0], rangeValues[2][1], rangeValues[2][2]);
    addColorBox("pageBRange", currentColorType, "pageB", rangeValues[3][0], rangeValues[3][1], rangeValues[3][2]);
    addColorBox("wBRange", currentColorType, "wB", rangeValues[4][0], rangeValues[4][1], rangeValues[4][2]);

    updateColorBox("rule");
    updateColorBox("col");
    updateColorBox("high");
    updateColorBox("pageB");
  }
  
  /** Dialog Themes **/
  
  // Sets up dialog box theme CSS
  sT=parseInt(arr[4]);
  var diagCSS=".configLabels label, .colorSelector fieldset, #crashNotice, #sloganArea, #sidebar, div.dialogWindow {";
  
  // Only give a background image if one is defined
  if (tableColors[sT][8]>=0) {diagCSS+="background-image:url("+backSwatches[tableColors[sT][8]]+");";}
  
  // Dialog colors
  diagCSS+="background-color:"+tableColors[sT][2]+";";
  diagCSS+="color:"+tableColors[sT][6]+";";
  if (parseInt(arr[4])==37) {diagCSS+="color:#000;background-color:#fff";} // Fixes default
  
  // Slidelist coloration
  diagCSS+="} #slideList {background-color:"+tableColors[sT][0]+";}";
  diagCSS+="#slideList div:hover {color:"+tableColors[sT][0]+" !important;background-color:"+tableColors[sT][4]+" !important;}";
  
  // Color Ranges <HR>
  diagCSS+=".colorSelector hr, .previewBox {border-color:"+tableColors[sT][4]+";} .previewBox:hover {outline:2px dotted "+tableColors[sT][4]+";}";
  
  // Color legend borders
  diagCSS+="fieldset fieldset legend {border-color:"+tableColors[sT][6]+";}";
  
  // Textfield styles
  //diagCSS+="input[type=password]:disabled {background-color:"+tableColors[sT][2]+" !important;color:"+tableColors[sT][6]+" !important;}";
  diagCSS+="textarea {color:"+tableColors[sT][4]+" !important;background-color:"+tableColors[sT][0]+" !important;border:2px solid "+tableColors[sT][4]+";}";
  
  if (g('skinGlobalStyle').hasChildNodes()) {
    g('skinGlobalStyle').removeChild(g('skinGlobalStyle').childNodes[0]);
  }
  g('skinGlobalStyle').appendChild(document.createTextNode(diagCSS));
  
  slideListColors();
  applySkinBack();
}
function applySkinText(arr) {
  g('tFont').value=arr[0];
  g('tSize').value=arr[1];
  g('tUnits').selectedIndex=parseInt(arr[2]);
  g('tBold').checked=(arr[3]=="1") ? true : false;
  
  // Apply to all textareas
  var units=['px','pt','pc','cm','mm','in','em','ex','%'];
  var textStyle="textarea {font: "+((arr[3]=="1")?'bold':'normal')+" "+arr[1]+units[parseInt(arr[2])]+" "+arr[0]+";}";
  
  if (g('skinTextStyle').hasChildNodes()) {
    g('skinTextStyle').removeChild(g('skinTextStyle').childNodes[0]);
  }
  g('skinTextStyle').appendChild(document.createTextNode(textStyle));

}
function applySkinBack() {
  var arr=data.get("Velt_2_skinBack").split("|N|"); // Gets skin data

  var temp=[arr[2], arr[3], arr[4]];
  var backStyle="#mainbody>fieldset, #searchBar, #minBody fieldset, #crashProtect fieldset, #slideFloater {background:rgb("+temp[0]+","+temp[1]+","+temp[2]+")";
  
  // Correct 1st, 2nd, and 3rd color values for HSL(A).
  if (currentColorType.match("HSL")!=null) {
    temp=rgb2hsl(arr[2], arr[3], arr[4]);
  }
  arr[2]=temp[0];
  arr[3]=temp[1];
  arr[4]=temp[2];
  
  // Populates background form fields
  try {
    document.getElementsByName('wbBackground')[arr[0]].checked=true;
    g('wBURL').value=arr[1];
    g('wB1').value=temp[0];
    g('wB2').value=temp[1];
    g('wB3').value=temp[2];
    updateColorBox("wB");
    setFormatting();
  }
  catch (e) {}
  
  // Background swatches
  if (parseInt(arr[0])<backSwatches.length) {
    backStyle+=" url("+backSwatches[parseInt(arr[0])]+")";
  }
  // URL Image
  else if (parseInt(arr[0])==backSwatches.length) {
    backStyle+=" url("+arr[1]+")";
  }
  
  backStyle+=";}";
  
  if (g('skinBackStyle').hasChildNodes()) {
    g('skinBackStyle').removeChild(g('skinBackStyle').childNodes[0]);
  }
  g('skinBackStyle').appendChild(document.createTextNode(backStyle));
  
  // Update widget background color ranges.
  if (currentColorType.match('HSL')!=null) {
    updateHue('wB');
  }
  else if (currentColorType.match('RGB')!=null) {
    updateRed('wB');
    updateGreen('wB');
    updateBlue('wB');
  }
}

/** Getters **/
function getSkinGlobal() {
  return [g('Clock24').checked ? '1' : '0', g('aSlideList').checked ? '1' : '0', g('eCP').checked ? '1' : '0', g('dateTimeSelect').selectedIndex, g('skinTheme').selectedIndex, g('defaultFileType').selectedIndex, getCheckedValue('colorType')];
}
function getSkinText() {
  return [g('tFont').value, g('tSize').value, g('tUnits').selectedIndex, g('tBold').checked ? '1' : '0'];
}
function getSkinBack() {
  var temp=[getCheckedValue('wbBackground'), g('wBURL').value];
  var tmp2=[g('wB1').value, g('wB2').value, g('wB3').value];
  if (currentColorType.match("HSL")!=null) {
    tmp2=hsl2rgb(tmp2[0], tmp2[1], tmp2[2]);
  }
  return temp.concat(tmp2);
}

/** Resetters **/
function resetSkinGlobal() {
  applySkinGlobal([0, 1, 0, 5, 37, 0, 1]);
  data.set("Velt_2_skinGlobal", [0, 1, 0, 5, 37, 0, 1].join("|N|"));
}
function resetSkinText() {
  applySkinText(['monospace', 12, 1, 1]);
  saveSkinText();
}
function resetSkinBack() {
  data.set("Velt_2_skinBack", [32, '', 255, 255, 255].join("|N|"));
}