var nextPalettePos = 0;

function addFavoriteColor(rgb, name, id, hersteller)
{
    addFavoriteColorComplete(rgb, name, id, hersteller, "");
}

function addFavoriteColorComplete(rgb, name, id, hersteller, kollektion)
{
    var cookieName = "farbtonhistory";
    var content = getCookie(cookieName);
    
    var colors = new Array();
    
    if(content != "")
        colors = content.split(";");
    
    if(colors.length >= 8)
        colors.shift();
    
    var splitter = "###S###";
    var tempString = rgb + splitter + name + splitter + id + splitter + hersteller + splitter + kollektion;
    
    colors.push(tempString);
    
    var output = colors.join(";");
    
    setCookie(cookieName, output, 1);	
    
    updatePalette(rgb);
}

function updatePalette(rgb)
{

	var hexrgb = convertColorToHex(rgb);
    
    var hiddenId = "hiddenpalette_" + nextPalettePos;
    var hiddenDiv = document.getElementById(hiddenId);
    if(hiddenDiv)
        hiddenDiv.style.display = "block";
    
    var fieldId = "palettefield_" + nextPalettePos;
    var fieldDiv = document.getElementById(fieldId);
    if(fieldDiv)
        fieldDiv.style.backgroundColor = hexrgb;
    
    nextPalettePos++;
    
    if(nextPalettePos > 7)
        nextPalettePos = 0;
}

function addFavoriteColorByID(id)
{
    var cookieName = "farbtonhistory";
    var content = getCookie(cookieName);
    
    var colors = new Array();
    
    if(content != "")
        colors = content.split(";");
    
    if(colors.length > 8)
        colors.shift();
    
    colors.push(id);
    
    var output = colors.join(";");
    
    setCookie(cookieName, output, 1);
}

function getFavoriteColors()
{
    var cookieName = "farbtonhistory";
    var content = getCookie(cookieName);
    
    var colors = new Array();
    
    if(content.equals != "")
        colors = content.split(";");
    
    for(var i = 0; i < colors.length; i++)
        alert(colors[i]);
    
    return colors;
}

function getCookie(name)
{
    if (document.cookie.length > 0)
    {
        c_start=document.cookie.indexOf(name + "=");
        
        if (c_start != -1)
        {
            c_start = c_start + name.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));
        }
    }
    
    return "";
}

function setCookie(name,value,expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function convertColorToHex(rgb)
{
    var hex_table = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");

    var r = Math.floor(rgb/0x10000) % 256;
    var g = Math.floor(rgb/0x100) % 256;
    var b = rgb % 256;

    var rx = hex_table[Math.floor(r/16)] + hex_table[Math.floor(r % 16)];
    var gx = hex_table[Math.floor(g/16)] + hex_table[Math.floor(g % 16)];
    var bx = hex_table[Math.floor(b/16)] + hex_table[Math.floor(b % 16)];
    
    return "#" + rx + gx + bx;
}
