// read position of an element in regular document flow
// (from O'Reilly JavaScript and DHTML Cookbook 15.9)
function getElementPosition(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function validateCookie(name)
{
	var i = name.indexOf(' ');
	while(i != -1)
	{
		name = name.substring(0,i) + '_' +  name.substring(i+1);
		i = name.indexOf(' ');
	}
	return name;
}

// cookie code from http://www.quirksmode.org/js/cookies.html + modifications
function createCookie(name,value,days)
{
	name = validateCookie(name); // added by morgaN
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var path = "; path=/";
	var domain = "; domain=.nwwo.com";
	document.cookie = name+"="+value+expires+path+domain; // "path=\/; // domain=.colorgoogle.com";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

currentElement = 0;

function colorBox(box, input)
{
  document.getElementById(box).style.background = "#" + input.value;
}
 
function showColorPicker(evt)
{
  evt = (evt) ? evt : event;
  if(evt)
  {
    if(document.getElementById("colorpicker").style.visibility != "visible")
    {
      var elem = (evt.target) ? evt.target : evt.srcElement;
      currentElement = elem; // added by morgaN
      var position = getElementPosition(elem.id);
      // shiftTo("colorpicker", position.left + elem.offsetWidth + 5, position.top);
      shiftTo("colorpicker", position.left + elem.offsetWidth - 270, position.top + 20);
      if(elem.basecolor)
        renderGradient(elem.basecolor);
      else if(currentBaseColor)
        renderGradient(currentBaseColor);
      show("colorpicker");
      // document.getElementById("q").focus();
    }
    else
    {
      hide("colorpicker");
      // added by morgaN:
      // document.getElementById("q").focus();
      var elem = (evt.target) ? evt.target : evt.srcElement;
      if(elem != currentElement)
        showColorPicker(evt);  
    }
  }
}

var currentBaseColor;

// the color bar was clicked
function colorButton(color)
{
  // update the gradient to the new base color
  renderGradient(color);
  // remember what gradient is being displayed
  currentBaseColor = color;
}

// received click from the gradient
function colorButtonGrad(color)
{
  // remember which gradient color was selected from
  currentElement.basecolor = currentBaseColor;
  // use the color that was selected
  currentElement.style.background = "#" + color;
  var elementName = currentElement.id.substring(7);
  document.getElementById(elementName).value = color; // skip "button_"
  // update sample
  updateSample(elementName, color);
}

function updateSample(element, color)
{
  // TODO: REPLACE THIS WITH CODE TO CHANGE THE CURRENT STYLESHEET
//alert(element);
  setStyleByClass("*", "content", element, "#" + color); // TODO: change the appropriate element, not just the background
  createCookie(element, color, 10000)
}

function renderGradient(color)
{
  var red = color.substring(0,2);
  var green = color.substring(2,4);
  var blue = color.substring(4,6);
  var r;
  var g;
  var b;
  var lesser;
  var difference;

  if(red == "00")		r = 0;
  else if(red == "ff")		r = 2;
  else				r = 1;
  if(green == "00")		g = 0;
  else if(green == "ff")	g = 2;
  else				g = 1;
  if(blue == "00")			b = 0;
  else if(blue == "ff")	b = 2;
  else				b = 1; 

  if(r == 1)		lesser = fromHex(red);
  else if(g == 1)	lesser = fromHex(green);
  else if(b == 1)	lesser = fromHex(blue);
  else			lesser = 0;

  difference = 15 - lesser;

  var gradient = "<table cellpadding=0 cellspacing=0 border=0 style=\"border: 1px solid #666666;\"><tr>";

  var tmpColor;

  for(i=0; i < 31; i++)
  {
    tmpColor = "";

    if(r==0)
    {
      if(i<=15) tmpColor = "00";
      else tmpColor = toHex(i-15);
    }
    else if(r==1)
    {
      if(i<=difference) tmpColor += "00";
      else
        if(i-difference <= 15) tmpColor += toHex(i-difference);
        else tmpColor += "ff";
    }
    else /* r==2 */
    {
      if(i<=15) tmpColor += toHex(i);
      else tmpColor += "ff";
    }
    if(g==0)
    {
      if(i<=15) tmpColor += "00";
      else tmpColor += toHex(i-15);
    }
    else if(g==1)
    {
      if(i<=difference) tmpColor += "00";
      else
        if(i-difference <= 15) tmpColor += toHex(i-difference);
        else tmpColor += "ff";
    }
    else /* g==2 */
    {
      if(i<=15) tmpColor += toHex(i);
      else tmpColor += "ff";
    }
    if(b==0)
    {
      if(i<=15) tmpColor += "00";
      else tmpColor += toHex(i-15);
    }
    else if(b==1)
    {
      if(i<=difference) tmpColor += "00";
      else
        if(i-difference <= 15) tmpColor += toHex(i-difference);
        else tmpColor += "ff";
    }
    else /* b==2 */
    {
      if(i<=15) tmpColor += toHex(i);
      else tmpColor += "ff";
    }

    gradient += "<td><a href=\"javascript:colorButtonGrad('" + tmpColor + "');\"><img src=blank.gif width=8 height=10 border=0 style=\"background:#" + tmpColor + "\"></a></td>";
  }

  gradient += "</tr></table>";

  var g = document.getElementById("gradienttable");
  g.innerHTML = gradient;
}

function fromHex(hex)
{
  var c = hex.substring(0,1);
  if((c >= 0) && (c <=9)) return c;
  if(c == "a") return 10;
  if(c == "b") return 11;
  if(c == "c") return 12;
  if(c == "d") return 13;
  if(c == "e") return 14;
  if(c == "f") return 15;
}

function toHex(num)
{
//    if((num >= 1) && (num <= 9)) return num*11;
  if(num == 0) return "00";
  if(num == 1) return "11";
  if(num == 2) return "22";
  if(num == 3) return "33";
  if(num == 4) return "44";
  if(num == 5) return "55";
  if(num == 6) return "66";
  if(num == 7) return "77";
  if(num == 8) return "88";
  if(num == 9) return "99";
  if(num == 10) return "aa";
  if(num == 11) return "bb";
  if(num == 12) return "cc";
  if(num == 13) return "dd";
  if(num == 14) return "ee";
  if(num == 15) return "ff";
}


