$(document).ready(function(){
  $(".pic-wrapper").each(function(i, item){
    $(item).css("backgroundColor", $(item).attr("bg")).removeAttr("bg");
  });
});

function addColor()
  {
    var last = null;
    var flag = false;
    $("#color_selector option").each(function(i, item){
      last = $(item).val();
      if($(item).css("backgroundColor") == $(".color").css("backgroundColor"))
      {
        alert("Такой цвет уже есть");
        flag = true;
      }
    });
    if(flag)
      return;
    $("#color_selector").append("<option></option>");
    $("#color_selector option:last").attr("value", parseInt(last)+1).css("backgroundColor", $(".color").css("backgroundColor"));
    $.post("palette.php", {op:"add", color: $(".color").css("backgroundColor")});
  }
  
  function delColor()
  {
    var color = $(".color").css("backgroundColor");
    $("#color_selector option").each(function(i, item){
      if($(item).css("backgroundColor") == color)
      {
        $(item).remove();
        $.post("palette.php", {op:"del", color: color});
        $(".color").css("backgroundColor", "white");
      }
    });
  }
  
  function changeColor(el)
  {
    $(".color").css("backgroundColor", el.options[el.selectedIndex].style.backgroundColor);
    $(".color").val("");
  }
