function webSafify(n) {
	if(n < 0) return 0;
	else if(n > 255) return 255;
}

function update(override, r, g, b) {
	var hex, rgb;
	if(!override || b == null) 
		rgb = getRGB();	 
	else {
		rgb = new Array(3);
		rgb.r = r; rgb.g = g; rgb.b = b;
	}
	document.getElementById('R').value = rgb.r;
	document.getElementById('G').value = rgb.g;
	document.getElementById('B').value = rgb.b;
	if(!hex) hex = RGBtoHex(rgb.r,rgb.g,rgb.b);
	setLayerBgcolor(document.getElementById('testLayer'), document.getElementById('webcolor').value = hex);
	document.getElementById('selectcolor').value = hex;
	var rS,gS,bS;
	rS = webSafify(rgb.r); 
	gS = webSafify(rgb.g); 
	bS = webSafify(rgb.b);
	rS = gS = bS = null;
	if(override)  setRGB(rgb.r,rgb.g,rgb.b);
	rgb = null;
}

function change(type, dst, val) {
	switch(type) 
	{
		case "RGB":
			switch(dst) {
				case "R": 
					if(val < 0) val = 0; 
					if(val > 255) val = 255;
					setRGB(val,-1,-1);
					break;
				case "G":
				    if(val < 0) val = 0; 
				    if(val > 255) val = 255;
				    setRGB(-1,val,-1);
				    break;
				case "B":
				    if(val < 0) val = 0; 
				    if(val > 255) val = 255;
				    setRGB(-1,-1,val);
				    break;
				default: break;
			}
			break;
		case "HEX":
			var rgb = HextoRGB(val);
			update(true, rgb.r, rgb.g, rgb.b);
			rgb = null;
			break;
		default: break;
	}
	update();
}


function updateFormat() {
	setZIndex(document.getElementById('tabRGB'), 0);
	setVisibility(document.getElementById('rgbLayer'), "visible");
//	setZIndex(document.getElementById('tabRGB'), 2);
//	setTop(document.getElementById('testLayer'), 77);
//	setLeft(document.getElementById('testLayer'), 490);
//	setClipHeight(document.getElementById('testLayer'), 180);
//	setClipWidth(document.getElementById('testLayer'), 153);
	setVisibility(document.getElementById('thumbR'), "visible");
	setVisibility(document.getElementById('thumbG'), "visible");
	setVisibility(document.getElementById('thumbB'), "visible");
	setVisibility(document.getElementById('red'), "visible");   
	setVisibility(document.getElementById('green'), "visible");
	setVisibility(document.getElementById('blue'), "visible");
}

var mPosition = 35;
function init() {
	if(!document.all) document.all = document.layers;
	db = new Behavior(true);
	db.setAction("MOUSEMOVE", update);
	db.vLock = false;
	db.hLock = true;
	db.setBounds(0,1000,mPosition,(mPosition+255));
	db.applyBehavior(document.getElementById('thumbR'));
	db.applyBehavior(document.getElementById('thumbG'));
	db.applyBehavior(document.getElementById('thumbB'));
	db = null;
	updateFormat();
}           

function HextoRGB(hex) {
  hex = hex.toUpperCase();
  if(hex.charAt(0) == "#") hex = hex.substring(1,hex.length);
  var rgb = new Array(3);
  rgb.r = hex.substring(0,2);
  rgb.g = hex.substring(2,4);
  rgb.b = hex.substring(4,6);
  rgb.r = parseInt(rgb.r,16);
  rgb.g = parseInt(rgb.g,16);
  rgb.b = parseInt(rgb.b,16);
  if(isNaN(rgb.r)) rgb.r = 0;
  if(isNaN(rgb.g)) rgb.g = 0;
  if(isNaN(rgb.b)) rgb.b = 0;
  return rgb;
}
function RGBtoHex(R, G, B) {
  var n = Math.round(B); 
  n += Math.round(G) << 8;
  n += Math.round(R) << 16;
  return DectoHex(n);
}
function DectoHex(num) {
  var i = 0; var j = 20;
  var str = "#";
  while(j >= 0) {
    i = (num >> j)%16;
    if(i >= 10) {
      if(i == 10) str += "A";
      else if(i == 11) str += "B";
      else if(i == 12) str += "C";
      else if(i == 13) str += "D";
      else if(i == 14) str += "E";
      else str += "F";
    } else
      str += i;
    j -= 4;
  }
  return str;
}


function setClip(layer, l, r, t, b) {
    layer.style.pixelWidth = r-l;
    layer.style.pixelHeight = b-t;
    layer.style.clip = "rect("+t+","+r+","+b+","+l+")";

}

function setClipHeight(layer, h) {
  layer.style.pixelHeight = h;
}

function setClipWidth(layer, w) {
    layer.style.pixelWidth = w;
    setClip(layer, 0, layer.style.pixelWidth,
                   0, layer.style.pixelHeight);
}

function setLeft(layer, l) {
  layer.style.pixelLeft = l;
}
function setTop(layer, t) {
  layer.style.pixelTop = t;
}
function setVisibility(layer, v) {
  layer.style.visibility = v;
}
function setZIndex(layer, z) {
  layer.style.zIndex = z;
}
function getLeft(layer) {
  return layer.style.pixelLeft;
}
function getTop(layer) {
  return layer.style.pixelTop;
}
function setLayerBgcolor(layer, b) {
  layer.style.backgroundColor = b;
}
function getRGB() {
  var ar = new Array(3);

  ar.r = getTop(document.getElementById('thumbR'))-mPosition;
  ar.g = getTop(document.getElementById('thumbG'))-mPosition;
  ar.b = getTop(document.getElementById('thumbB'))-mPosition;
  return ar;
}
function setRGB(r,g,b) {
  if(r >= 0 && r <= 255) setTop(document.getElementById('thumbR'), r*1+mPosition);
  if(g >= 0 && g <= 255) setTop(document.getElementById('thumbG'), g*1+mPosition);
  if(b >= 0 && b <= 255) setTop(document.getElementById('thumbB'), b*1+mPosition);
}


function Behavior(drag) {
  this.mouseDownAction = null;
  this.mouseUpAction = null;
  this.mouseMoveAction = null;
  this.mouseOverAction = null;
  this.mouseOutAction = null;
  this.draggable = drag;
  this.setAction = setAction;
  this.applyBehavior = applyBehavior;
  this.hLock = false;
  this.vLock = false;
  this.useBounds = false;
  this.setBounds = setBounds;
  this.removeBounds = removeBounds;
  this.bounds = false;
  this.containers = false;
}

function setBounds(l, r, t, b) {
  this.useBounds = true;
  if(this.bounds == false) this.bounds = new Array(4);
  this.bounds[0] = l;
  this.bounds[1] = r;
  this.bounds[2] = t;
  this.bounds[3] = b;
}

function removeBounds() {
  this.useBounds = false;
}

function setAction(action, func) {
  eval('switch(action) {'+
    'case "MOUSEDOWN": this.mouseDownAction = func; break;'+
    'case "MOUSEMOVE": this.mouseMoveAction = func; break;'+
    'case "MOUSEUP":   this.mouseUpAction = func;   break;'+
    'case "MOUSEOVER": this.mouseOverAction = func; break;'+
    'case "MOUSEOUT":  this.mouseOutAction = func;  break;'+
    'case "CONTAINERPUSH": this.containerPushAction = func; break;'+
    'case "CONTAINERPULL": this.containerPullAction = func; break;'+
  '}');
}

function applyBehavior(layer) {
  layer.behavior = this;
  layer.draggable = this.draggable;
  if(layer.captureEvents) {
    layer.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP|Event.MOUSEOVER|Event.MOUSEOUT);
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmouseup = _clearDBJ;
  layer.onmousedown = _handleMouseDown;
  layer.onmouseup = _handleMouseUp;
  document.onmousemove = _handleMouseMove;
  layer.onmouseover = _handleMouseOver;
  layer.onmouseout = _handleMouseOut;
  layer.containers = this.containers;
  layer.containerPushAction = this.containerPushAction;
  layer.containerPullAction = this.containerPullAction;
  layer.vLock = this.vLock;
  layer.hLock = this.hLock;
  layer.bounds = new Array(4);
  layer.bounds[0] = this.bounds[0];
  layer.bounds[1] = this.bounds[1];
  layer.bounds[2] = this.bounds[2];
  layer.bounds[3] = this.bounds[3];
  layer.useBounds = this.useBounds;
  layer.mouseDownAction = this.mouseDownAction;
  layer.mouseUpAction = this.mouseUpAction;
  layer.mouseMoveAction = this.mouseMoveAction;
  layer.mouseOverAction = this.mouseOverAction;
  layer.mouseOutAction = this.mouseOutAction;
}

function _handleMouseOver(e) {
    if(window.event.srcElement == this &&
       window.event.srcElement.tagName == "DIV") {
      window.event.cancelBubble = true;
    }
    else if(window.event.srcElement == this) return;
	if(this.mouseOverAction) this.mouseOverAction(e, "mouseover");
}

function _handleMouseOut(e) {
    if(window.event.srcElement == this &&
       window.event.srcElement.tagName == "DIV")
      window.event.cancelBubble = true;
    else if(window.event.srcElement == this) return;
    if(this.mouseOutAction) this.mouseOutAction(e, "mouseout");
}


var _dbj = new Array(); 

function _handleMouseDown(e) {
  	window.event.cancelBubble = true;
  	layer = window.event.srcElement;
  	if(layer.mouseDownAction) layer.mouseDownAction(e, "mousedown");
  	if(!layer.draggable) return true;
  	if(layer.containers) {
	    layer.wasContained = false;
	    for(var i = 0; i < layer.containers.length; i++) {
	      if(_contains(layer.containers[i], layer)) {
		layer.wasContained = layer.containers[i];
		break;
	      }
    	}
  	}
    layer.offsetX = window.event.clientX - layer.style.pixelLeft;
    layer.offsetY = window.event.clientY - layer.style.pixelTop;
	_dbj.layer = layer;
	_dbj.indrag = true;
	return false;
}

function _handleMouseMove(e) {
  	var ret = false;
	window.event.cancelBubble = true;
	if(!_dbj.layer) {
		if(window.event.srcElement.mouseMoveAction)
	  		window.event.srcElement.mouseMoveAction(e, "mousemove");
		return true;
	}
	if(_dbj.layer.mouseMoveAction)
		ret = _dbj.layer.mouseMoveAction(e, "mousemove");
	if(!_dbj.layer.draggable) return ret;
	if(!_dbj.indrag) return true;
	if(!_dbj.layer.vLock) {
    	var dstY;
		dstY = (window.event.clientY - _dbj.layer.offsetY);
		if((_dbj.layer.useBounds &&	(dstY >= _dbj.layer.bounds[2]) &&
			(dstY + _dbj.layer.style.pixelHeight <=	_dbj.layer.bounds[3])) ||
			!_dbj.layer.useBounds)
			_dbj.layer.style.pixelTop = dstY;
		else 
			if(_dbj.layer.useBounds) {
				if(dstY < _dbj.layer.bounds[2]) 
					_dbj.layer.style.pixelTop = _dbj.layer.bounds[2];
				else _dbj.layer.style.pixelTop = _dbj.layer.bounds[3] - _dbj.layer.style.pixelHeight;
    		}
  	}
	if(!_dbj.layer.hLock) {
		var dstX;
		dstX = (window.event.clientX - _dbj.layer.offsetX);
		if((_dbj.layer.useBounds &&	(dstX + _dbj.layer.style.pixelWidth <= _dbj.layer.bounds[1]) &&
			(dstX >= _dbj.layer.bounds[0])) || !_dbj.layer.useBounds)
			_dbj.layer.style.pixelLeft = dstX;
		else 
			if(_dbj.layer.useBounds) {
				if(dstX < _dbj.layer.bounds[0])
					_dbj.layer.style.pixelLeft = _dbj.layer.bounds[0];
				else _dbj.layer.style.pixelLeft = _dbj.layer.bounds[1] - _dbj.layer.style.pixelWidth;
			}
	}
  return false;
}

function _clearDBJ() {
  _dbj.indrag = false;
  _dbj.layer = null;
}

function _handleMouseUp(e) {
	window.event.cancelBubble = true;
	if(!_dbj.layer) { 
		if(this.mouseUpAction) this.mouseUpAction(e, "mouseup");
		return;
	}
	if(_dbj.layer.mouseUpAction) _dbj.layer.mouseUpAction(e, "mouseup");
	if(_dbj.layer.containers) {
		_dbj.layer.isContained = false;
		for(var i = 0; i < _dbj.layer.containers.length; i++) {
	  		if(_contains(_dbj.layer.containers[i], _dbj.layer)) {
			_dbj.layer.isContained = _dbj.layer.containers[i];
	  		}
		}
		if(_dbj.layer.wasContained != _dbj.layer.isContained) {
	  		if(_dbj.layer.containerPullAction && _dbj.layer.wasContained)
				_dbj.layer.containerPullAction(_dbj.layer.wasContained, "containerpull");
	  		if(_dbj.layer.containerPushAction && _dbj.layer.isContained)
				_dbj.layer.containerPushAction(_dbj.layer.isContained, "containerpush");
		}
	}
	_clearDBJ();
  return;
}


