
var afcColors = new Array(
  "border",
  "bg",
  "link",
  "url"
);


function initializeAfcWizard() {
  _cp_registerPicker(onChangeAfcCallback, onMouseOverAfcCallback);
  setAfcColorFields(np_colors[0]); 
}

function onChangeAfcCallback(colorField, originalColor, newColor) {
  updateSinglePreview();
}


function onMouseOverAfcCallback(colorField, originalColor, newColor) {
  elem("colorfield-" + colorField).value = newColor;
  if (newColor != "") {
    elem("colorsample-" + colorField).style.backgroundColor = '#' + newColor;
  }
  updateSinglePreview();
}


function setAfcColorFields(colorObject) {
  _cp_synchronizeColorList(afcColors, colorObject);
  updateSinglePreview();
}

function updateSinglePreview() {
  if (getValue(document.forms.fieldForm.elements.adKind) == "links") {

    updateIndexedPreview(getSelectedColors()[0], 4);

    elem("4.bordertext").style.textDecoration = '';
    elem("4.bordertext").style.textDecoration = 'underline';
    elem("4.link").style.textDecoration = '';
    elem("4.link").style.textDecoration = 'underline';
  } else {

    if ((document.forms.fieldForm.elements.menu == undefined)
        || document.forms.fieldForm.elements.menu.disabled) {
      updateIndexedPreview(getSelectedColors()[0], 0);
    }
  }
}


function updateIndexedPreview(colorObject, previewIndex) {

  if (elem("np_border") == undefined) {
    return;
  }


  elem("np_border").style.visibility = "visible";
  elem("np_border").style.backgroundColor = "#" + _cp_toHex(colorObject.bg);
  elem("np_border").style.border = "1px solid #" + _cp_toHex(colorObject.border);

  elem("np_link_1").style.color = "#" + _cp_toHex(colorObject.link);
  elem("np_link_2").style.color = "#" + _cp_toHex(colorObject.link);
  elem("np_link_3").style.color = "#" + _cp_toHex(colorObject.link);
  elem("np_link_4").style.color = "#" + _cp_toHex(colorObject.link);
  elem("np_link_5").style.color = "#" + _cp_toHex(colorObject.link);
  
  elem("np_date_1").style.color = "#" + _cp_toHex(colorObject.url);
  elem("np_date_2").style.color = "#" + _cp_toHex(colorObject.url);
  elem("np_date_3").style.color = "#" + _cp_toHex(colorObject.url);
  elem("np_date_4").style.color = "#" + _cp_toHex(colorObject.url);
  elem("np_date_5").style.color = "#" + _cp_toHex(colorObject.url);
   
  do_code()
}


function getTextHex(color) {
  return (((0.3 * color[0]) + (0.59 * color[1]) + (0.11 * color[2])) <= 128)
          ? "FFFFFF"
          : "000000";
}


function getSelectedColors() {
  if ((document.forms.fieldForm.elements.menu == undefined)
      || document.forms.fieldForm.elements.menu.disabled) {

    var newColor = {name:"", custom: true};
    for (var index = 0; index < afcColors.length; index++) {
      newColor[afcColors[index]] = _cp_getLiteralColor(afcColors[index]);
    }
    return [newColor];
  } else {

    var opts = document.forms.fieldForm.elements.menu.options;
    var colorArray = [];
    var count = 0;
    for (var index = 0; (index < opts.length) && (count < 4); index++) {

    }
    return colorArray;
  }
}



function do_code () {
	
var np_chapter

var chapters = elem("f_chapter");
 
for (var index = 0; index < chapters.options.length; index++) {
	if (chapters.options[index] == chapters.options[chapters.selectedIndex]) {
		np_chapter = chapters.options[index].value
	}
}

if (np_chapter > 0) section = 'section_' + np_chapter + '/'
else section = ''


var f_type = elem("f_type");
 
for (var index = 0; index < f_type.options.length; index++) {
	if (f_type.options[index] == f_type.options[f_type.selectedIndex]) {
		np_type = f_type.options[index].value
	}
}

if (np_type > 0) np_type = 'type_popular/'
else np_type = ''

var code = '\n\
<!-- ' + site_name + ' -->\n\
\n\
<style>\n\
#np_border {clear: both; padding: 2px; border: 1px solid #' + elem("colorfield-border").value + '; background-color:#' + elem("colorfield-bg").value + ';}\n\
#np_cp {background-color: #940F04; font: bold 90% Arial, Helvetica, sans-serif; padding: 5px 5px 5px 8px; margin: 0px 0px 5px 0px;}\n\
#np_cp a{color:#FFFFFF; text-decoration: none;}\n\
#np_date {color: #' + elem("colorfield-url").value + '; font: 10px Arial, Helvetica, sans-serif; padding: 0px 5px 0px 8px;}\n\
#np_link {font: 100% Arial, Helvetica, sans-serif; padding: 0px 5px 0px 8px; margin:0px 0px 10px 0px;}\n\
#np_link a {color:#' + elem("colorfield-link").value + ';}\n\
</style>\n\
\n\
<script language="javascript">\n\
<!-- \n\
document.write(\'<script language="javascript" src="http://www.narodnapravda.com.ua/export/mode_view/' + section + np_type + '"></script>\')\n\
-->\n\
</script>\n\
<!--  ' + site_name + ' -->\n\n'

elem("np_ins").value=code
	}