/********************************************************
 * Summary:  This file has functions to generate an associative
 *           array mapping the IDs that will be created in getItemHtml 
 *           to the color values.  Example: If you wanted just 3 colors, red, 
 *           green and blue... you could replace the getIdToColorArr function... 
 *           //Example (Simpler version of getIdToColorArr)          
 *           function getIdToColorArr() 
 *           {
 *                var colorArr = new Array(); 
 *                colorArr["color1"] = "#FF0000"; //red  
 *                colorArr["color2"] = "#00FF00"; //green
 *                colorArr["color3"] = "#0000FF"; //blue 
 *                return colorArr;
 *           }
 *           //Given that this function's result is passed to the ColorSelector as
 *           //the DataMapper, the html elements created in getItemHtml must have 
 *           //respective ids... (id="color1", id="color2", id="color3")
 *********************************************************/


//produces the associative array
function getIdToColorArr()
{
   var colorArr = new Array();
   colorArr = colorArr.concat(getColorRow(11, 11));  //black to white
   colorArr = colorArr.concat(getColorRow(11, 6, "g", "b"));  //red to white
   colorArr = colorArr.concat(getColorRow(11, 6, "r", "b"));  //green to white
   colorArr = colorArr.concat(getColorRow(11, 6, "r", "g"));  //blue to white
   colorArr = colorArr.concat(getColorRow(11, 6, "r"));  //light blue to white
   colorArr = colorArr.concat(getColorRow(11, 6, "g"));  //light purple to white
   colorArr = colorArr.concat(getColorRow(11, 6, "b"));  //yellow to white
   colorArr = colorArr.concat(getColorRow(11, 1, "g99", "b"));  //orange
   colorArr = colorArr.concat(getColorRow(11, 1, "g99", "r"));  //blueish
   colorArr = colorArr.concat(getColorRow(11, 1, "b99", "g"));  //pinkish
   colorArr = colorArr.concat(getColorRow(11, 1, "b99", "r"));  //light greenish
   //colorArr = colorArr.concat(getColorRow(11, 1, "r99", "b"));  //???
   //colorArr = colorArr.concat(getColorRow(11, 1, "r99", "g"));  //???

   var idToColor = new Array();
   for(var m=0; m<colorArr.length; m++)
   {
      idToColor["color" + (m+1).toString()] = colorArr[m]; 
   }
   return idToColor;
}


/*
*   getColorRow:  This function builds a row (Array) to add to the color array.
*   It uses a weird algorithm that is based on "with-holding" certain colors 
*   until they are released.. It produces shades of red by witholding green and blue etc.
*   If a with-hold item has a number after it e.g. "r99", then when it is released 
*   it starts at that number.  Disclaimer: I'm sure there's a better way to do it.
*/
function getColorRow(numItems, releaseColorNum) //, withold1, withold2, withhold3
{
    var colorRow = new Array();
    var colorRed = "00";
    var colorGreen = "00";
    var colorBlue = "00";
    var rReleaseStart = "00";
    var gReleaseStart = "00";
    var bReleaseStart = "00";
    var releasedColor = false;
    var numBeforeRelease = numItems - (numItems - releaseColorNum);
    var numAfterRelease = numItems - releaseColorNum;
    
    for(var w=2; w<arguments.length; w++)
    {
        if(arguments[w])
        {
            var arg = arguments[w].toString();
            if(arg == "r")
            {
               colorRed = "";
            }
            else if(arg == "g")
            {
               colorGreen = "";
            }
            else if(arg == "b")
            {
               colorBlue = "";
            }
            else if(arg.substring(0,1) == "r")            
            {
               colorRed = "";
               rReleaseStart = arg.substring(1,2);
            }
            else if(arg.substring(0,1) == "g")
            {
               colorGreen = "";
               gReleaseStart = arg.substring(1,2);
            }
            else if(arg.substring(0,1) == "b")
            {
				colorBlue = "";
				bReleaseStart = arg.substring(1,2);                
            }            
        }
    }

    
    while(numItems > 0)
    {
       if(releaseColorNum == 0 && !releasedColor)
       {
          releasedColor = true;
          if(colorRed == "")
              colorRed = rReleaseStart;
          if(colorGreen == "")
              colorGreen = gReleaseStart;
          if(colorBlue == "")
              colorBlue = bReleaseStart;
       }
       
       colorRed = increaseColor(colorRed, (releasedColor) ? numAfterRelease : numBeforeRelease);
       colorGreen = increaseColor(colorGreen, (releasedColor) ? numAfterRelease : numBeforeRelease);
       colorBlue = increaseColor(colorBlue, (releasedColor) ? numAfterRelease : numBeforeRelease);

       colorRow[colorRow.length] = "#" +
               ((colorRed == "") ? "00" : colorRed) + 
               ((colorGreen == "") ? "00" : colorGreen) + 
               ((colorBlue == "") ? "00" : colorBlue);
              
       releaseColorNum--;
       numItems--;
       
       if(releasedColor)
           numAfterRelease--;
       else
           numBeforeRelease--;
    }
    
    return colorRow;    
}

/* Helper function for getColorRow
   It increases the color based on how many more times it will be increased (incrementsLeft)
   We are trying to get a gradual increase of the color upon multiple calls to this function.
*/
function increaseColor(currentHex, incrementsLeft)
{
    var ret = currentHex;
    if(currentHex == "")   //don't increase with-held colors
       return currentHex;

    var hexNum = getHexNum(currentHex.toString().substring(0,1));  //assumes both first and second hex are the same e.g. "99"
    var hexLeft = 15 - hexNum;
    if(hexLeft <= 0)
        return "FF";  //if already 'F' return 'FF'

    if(incrementsLeft == 0)
        ret = "FF";
    else
    {
       var increaseVal = Math.floor(hexLeft / incrementsLeft);
       ret = getNumHex(hexNum + increaseVal);
       ret = ret + ret;
    }
    
    return ret;    
}

//4=4, A=10, B=11, C=12 etc.
function getHexNum(hex)
{
   var ret = Number(hex);
   var hexVals = Array('A','B','C','D','E','F');
   for(var i=0; i < hexVals.length; i++)
   {
       if(hexVals[i] == hex)
           ret = 10 + i;
   }
   return ret;
}

//5=5,10=A,11=B,12=C etc.
function getNumHex(num)
{
   if(num > 15)
       num = 15;
   var ret;
   var hexVals = new Array('A','B','C','D','E','F');
   if(num > 9)
      ret = hexVals[num-10];
   else
      ret = num.toString();
   
   return ret;
}