/*
 * Copyright (c) 2005 Free Software Foundation
 * developed under the custody of the
 * Muffin Research Labs
 * http://www.muffinresearch.co.uk/lab/javascript/pickr/
 *
 * This file is part of the Pickr palette creator.
 * Pickr is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Pickr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * If you are not able to view the LICENSE, which should
 * always be possible within a valid and working Pickr release,
 * please write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * to get a copy of the GNU General Public License or to report a
 * possible license violation.
 */

window.onload = initevents;  
  
function initevents ()
{
  if ((document.getElementById)  && (document.getElementsByTagName))
  {
    document.onmousemove = getCurMousePos;
    if (document.captureEvents)
      document.captureEvents(Event.MOUSEMOVE);
 
    var palette = document.getElementById("palette");
    var links = palette.getElementsByTagName("a");
    for ( var i=0; i < links.length; i++) 
    {
      links[i].onclick = function() 
      {
        selectme(this.id);
        return false;
      }
    }
    
    document.getElementById("imgpickr").onclick = function()
    {
      getRGB('process.php?coord='+mousexpos+'|'+mouseypos);
      return false;
    }
    
    document.getElementById("reset").onclick = function()
    {
      clearAll();
      return false;
    }
  } 
}

function getRealLeft(elem) 
{
  xPos = eval(elem).offsetLeft;
  tempEl = eval(elem).offsetParent;
  	while (tempEl != null) 
    {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

function getRealTop(elem) 
{
	yPos = eval(elem).offsetTop;
	tempEl = eval(elem).offsetParent;
	while (tempEl != null) 
  {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
	return yPos;
}

// Initialise holder 
var holder = 'result1';

function getCurMousePos (e)
{
	// Get event object for IE
	if (!e) {e = window.event}
	
	if (e.pageX)
	{
		mousex = e.pageX;
		mousey = e.pageY;
	}
	else if (document.documentElement)
	{
		mousex = e.clientX + document.documentElement.scrollLeft;
		mousey = e.clientY + document.documentElement.scrollTop;
	}

  // Get coordinates of image
  var obj   = document.getElementById('imgpickr');
  var left  = getRealLeft(obj)
  var top  = getRealTop(obj)
  
 
  
  // Small fudge for IE which is always out by 2px
  if ((document.all) && (navigator.userAgent.indexOf("Opera")==-1))
  {
    mousex = mousex-2;
    mousey = mousey-2;
  }

  
  mousexpos = mousex - left;
  mouseypos = mousey - top;
}
    
  // If the user is using Mozilla/Firefox/Safari/etc
  if (window.XMLHttpRequest) 
  {
    //Intiate the object
    xmlhttp = new XMLHttpRequest();
    //Set the mime type - Opera doesn't seem to like this; why?
    xmlhttp.overrideMimeType('text/xml');
  }
  // If the user is using IE
  else if (window.ActiveXObject) 
  {
    //Intiate the object
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  function rgb2hex(r,g,b)
  {
    return '#'+num2hex(r)+num2hex(g)+num2hex(b);
  }
  
  function num2hex(n)
  {
    n = parseInt(n);
    if (n == 0 || n== null || isNaN(n))
    {
      return "00";
    }
    else
    {
      return n.toString(16)
    }
  }
  
  function selectme(id)
  {
    var obj = document.getElementById(id);
    obj.style.border = '1px solid blue';
    holder = id;
    i = 1;
    for ( var i=1; i < 9; i++) 
    {
      if (id != 'result'+i)
      {
        document.getElementById('result'+i).style.border = '1px solid #CCC';
      }
    }
  }
  
  function clearAll()
  {
    for ( var i=1; i < 9; i++) 
    {
      document.getElementById('result'+i).style.background = '#FFF';
    }
    
    document.getElementById(holder).style.border = '1px solid #CCC';
    holder = 'result1';
    document.getElementById('result1').style.border = '1px solid blue';
  }
  
  function getRGB(coordURL) 
  {
    // If the function isn't passed an empty string.
    if (coordURL !== "") 
    { 
      
      coordURL =  coordURL+'&j=1';
      //alert(coordURL);
      
      // Open the URL above using the GET method
      xmlhttp.open('GET', coordURL, true);
        
      // Check that the PHP script has finished sending us the result
      xmlhttp.onreadystatechange = function() 
      {
        if((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
        {  
          var rgbArr = xmlhttp.responseText.split('|');
          document.getElementById(holder).style.background = "rgb("+rgbArr[0]+","+rgbArr[1]+","+rgbArr[2]+")";
          document.getElementById(holder).title = rgb2hex(rgbArr[0],rgbArr[1],rgbArr[2]);
        }
      };
      xmlhttp.send(null);
    }  
  }
