
var colorsChanged = false;

function togglePrivate(value)
{
  if (value)
  {
  document.getElementById('killboardPrivate').disabled = "";
  } 
  else
  {
  document.getElementById('killboardPrivate').disabled = "disabled";
  }
}

function ilcrp_setUpPicker()
{
  var colorPickerTable = document.getElementById('forumColorPicker');
  
  var inputElements = colorPickerTable.getElementsByClassName('colorPreview');
  
  for (var elementCount = 0; elementCount < inputElements.length; elementCount ++)
  {
    attachColorPicker(inputElements[elementCount]);
  }
}
  
function ilcrp_setFollowUps(select)
{
  var selected = parseInt(select.value);
  if (selected == 0)
  {
    document.getElementById('followUpCell').style.display = "none";
  }
  else
  {
    document.getElementById('followUpCell').style.display = "block";
    for (var count = 1; count <= 5; count ++)
    {
      if (count <= selected)
      {
        document.getElementById('followUp' + count).style.display = "block";
      }
      else
      {
        document.getElementById('followUp' + count).style.display = "none";
      }
    }
  }
}

function ilcrp_setOutput(select) 
{
  var selected = select.value;
  if (selected == "forum")
  {
    document.getElementById('colorPickerContainer').style.display = "block";
  }
  else
  {
    document.getElementById('colorPickerContainer').style.display = "none";
  }
} 

function selectSkin(skinNumber)
{
  var allowChange = true;
  if (colorsChanged)
  { 
    allowChange = confirm("Loading a skin will loose all custom changes, continue to load?")
		colorsChanged = false;
  }
  
  if (parseInt(skinNumber) != "-1")
  {
    if (allowChange)
    {
      var skinIndex = "skin" + parseInt(skinNumber);
      if (skins[skinIndex])
      {
        document.getElementById('saveAsName').value = skins[skinIndex]['name'];
        document.getElementById('saveAsName').disabled = "disabled";
        
        document.getElementById('attackerNColor').style.backgroundColor = skins[skinIndex]['attackerNColor'];
        document.getElementById('attackerNColorInput').value = skins[skinIndex]['attackerNColor'];
        
        document.getElementById('defenderNColor').style.backgroundColor = skins[skinIndex]['defenderNColor'];
        document.getElementById('defenderNColorInput').value = skins[skinIndex]['defenderNColor'];
        
        document.getElementById('aUnitColor').style.backgroundColor = skins[skinIndex]['aUnitColor'];
        document.getElementById('aUnitColorInput').value = skins[skinIndex]['aUnitColor'];
        
        document.getElementById('aUnitNColor').style.backgroundColor = skins[skinIndex]['aUnitNColor'];
        document.getElementById('aUnitNColorInput').value = skins[skinIndex]['aUnitNColor'];
        
        document.getElementById('dUnitColor').style.backgroundColor = skins[skinIndex]['dUnitColor'];
        document.getElementById('dUnitColorInput').value = skins[skinIndex]['dUnitColor'];
                
        document.getElementById('dUnitNColor').style.backgroundColor = skins[skinIndex]['dUnitNColor'];
        document.getElementById('dUnitNColorInput').value = skins[skinIndex]['dUnitNColor'];
        
        document.getElementById('numberColor').style.backgroundColor = skins[skinIndex]['numberColor'];
        document.getElementById('numberColorInput').value = skins[skinIndex]['numberColor'];
        
        document.getElementById('textColor').style.backgroundColor = skins[skinIndex]['textColor'];
        document.getElementById('textColorInput').value = skins[skinIndex]['textColor'];
          
        document.getElementById('resultsColor').style.backgroundColor = skins[skinIndex]['resultsColor'];
        document.getElementById('resultsColorInput').value = skins[skinIndex]['resultsColor'];
      }
    }
  }
  else
  {
    document.getElementById('saveAsName').disabled = "";
    document.getElementById('saveAsName').value = "Provide Name To Save..";
  }
}


/*
** Javascript Color Picker (Simple)
**
** Created with the aid of: http://www.webreference.com/programming/javascript/mk/column3/ 
**     by Mark Kahn
**
** Noteable changes; 
**   - removed the large picker completely
**   - tied the preview to the input elements "Preview" div:
**      - this removes the preview from the cp object, and instead uses a div with 
**        the id of the input box to which the picker is tied to + "Preview"
**        This allows for a little more customised look; however does genereate an
**        additionally dependancy.
*/

(function colorPickerNamespace(){
	var cp = null;

	var imgBase = 'images/';

	function hex(c){
		c=parseInt(c).toString(16);
		c=c.toUpperCase();
		return c.length<2?"0"+c:c
	}

	function mouseCoordinates(ev){
		ev = ev || window.event;
		if(ev.pageX || ev.pageY)
			return {x:ev.pageX, y:ev.pageY};
		return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
				  y:ev.clientY + document.body.scrollTop  - document.body.clientTop};
	}

	function getPosition(obj){
		var left = 0;
		var top  = 0;

		while (obj.offsetParent){
			left += obj.offsetLeft;
			top  += obj.offsetTop;
			obj   = obj.offsetParent;
		}
		left += obj.offsetLeft;
		top  += obj.offsetTop;

		return {x:left, y:top};
	}

	function $DOM(A){
		var aL = A.length, node, child, ref={}, bRef=false;
		if(aL>=1){
			node = cE(A[0]);
			if(aL>=2){
				for(var arg in A[1]){
					if(arg.indexOf('on')==0){
						node[arg] = A[1][arg];
					}else if(arg=='ref'){
						ref[A[1][arg]] = node;
						ref['DOM']     = node;
						bRef           = true;
					}else{
						if(arg=='style'){
							node.style.cssText   = A[1][arg];
						} else if(arg.toLowerCase()=='classname'){
							node.style.className = A[1][arg];
						} else {
							node.setAttribute(arg, A[1][arg]);
						}
					}
				}
			}
			for(var i=2; i<aL; i++){
				if(typeof(A[i])=='string'){
					node.appendChild(document.createTextNode(A[i]));
				} else {
					child = $DOM(A[i]);
					if(child.DOM){
						bRef = true;
						for(n in child){
							if(n=='DOM'){
								node.appendChild(child[n]);
							}else{
								ref[n] = child[n];
							}
						}
						ref['DOM'] = node;
					}else{
						node.appendChild(child);
					}
				}
			}
			return bRef?ref:node;
		}
		return null;
	}

	function cE(){
		var A = arguments;

		if(!cE.cache[A[0]]) cE.cache[A[0]]=document.createElement(A[0]);
		return cE.cache[A[0]].cloneNode(false);
	}
	cE.cache    = {};

	function createColorPicker(inputElement){
		if(cp) return;
		cp = $DOM(
			['DIV', {style:'', ref:'ColorPicker'},
				['DIV', {style:'background-color:#FFF;line-height:2px;width:350px;height:21px;border:1px solid #000;margin:0px auto 0px auto;', ref:'hColorPicker'},
					['IMG', {style:'cursor:crosshair;', src:imgBase+'cp_horizontal_picker.png', width:350, height:21, onmousemove:hColorPickerMouseMove, onmousedown:hColorPickerMouseDown, ref:'hColorImg'}],
				]
			]);

		document.onmousemove         = cpMouseMove;
		cp.baseColor                 = {r:0, g:0, b:0};

		var colorPickerLocation = document.getElementById('colorPickerLocation');
		colorPickerLocation.innerHTML = " ";
		colorPickerLocation.appendChild(cp.ColorPicker);
		cp.ColorPicker.style.display = 'none';
	}

	function getHorizColor(i, width, height){
		var sWidth = (width)/7;         // "section" width
		var C=i%width;                  // column
		var R=Math.floor(i/(sWidth*7)); // row
		var c=i%sWidth;                 // column in current group
		var r, g, b, h;

		var l=(255/sWidth)*c;           // color percentage

		if(C>=sWidth*6){
			r=g=b=255-l;
		} else {
			h=255-l;

			r=C<sWidth?255:C<sWidth*2?h:C<sWidth*4?0:C<sWidth*5?l:255;
			g=C<sWidth?l:C<sWidth*3?255:C<sWidth*4?h:0;
			b=C<sWidth*2?0:C<sWidth*3?l:C<sWidth*5?255:h;

			if(R<(height/2)){
				var base = 255-(255*2/height)*R;

				r=base+(r*R*2/height);
				g=base+(g*R*2/height);
				b=base+(b*R*2/height);
			}else if(R>(height/2)){
				var base = (height-R)/(height/2);

				r=r*base;
				g=g*base;
				b=b*base;
			}
		}

		return hex(r)+hex(g)+hex(b);
	}

	function getVertColor(i, sZ){
		var n=sZ/6, j=sZ/n, C=i, c=C%n;

		r=C<n?255:C<n*2?255-c*j:C<n*4?0:C<n*5?c*j:255;
		g=C<n*2?0:C<n*3?c*j:C<n*5?255:255-c*j;
		b=C<n?c*j:C<n*3?255:C<n*4?255-c*j:0;

		return {r:r, g:g, b:b};
	}

	function hColorPickerMouseMove(ev){
		ev            = ev || window.event;
		var hCPImg    = ev.target || ev.srcElement;

		var mousePos  = mouseCoordinates(ev);
		cp.colorPos   = getPosition(hCPImg);

		var x         = mousePos.x-cp.colorPos.x;
		var y         = mousePos.y-cp.colorPos.y;
		var width     = parseInt(hCPImg.offsetWidth);
		var height    = parseInt(hCPImg.offsetHeight);

		var color     = getHorizColor(y*width+x, width, height);
		
		cp.cpInput.style.backgroundColor = '#'+color;
		
		var inputBox = document.getElementById(cp.cpInput.id + "Input")
		if (inputBox)
		{
			inputBox.value = '#'+color;
		}
	}

	function hColorPickerMouseDown()
	{
		cusomizeSkin()
		hideColorPicker();
	}

	function attachColorPicker(object)
	{
		createColorPicker(object);
		
		object.onfocus = showColorPicker;
		object.onblur  = tryHideColorPicker;
		object.onclick = showColorPicker;
	}

	function showColorPicker(ev, size){
		ev         = ev        || window.event;
		var input  = ev.target || ev.srcElement;
		
		cp.ColorPicker.style.display  = 'block';
		
		cp.cpInput = input;
		var inpPos = getPosition(cp.cpInput);
		
		cp.ColorPicker.style.left = inpPos.x;
		cp.ColorPicker.style.top  = inpPos.y+parseInt(cp.cpInput.offsetHeight);
	}

	function tryHideColorPicker(){
		if(!cp.clicked) hideColorPicker();
	}

	function hideColorPicker(){
		cp.ColorPicker.style.display  = 'none';
	}

	function cpMouseDown(ev){
		cp.cpPos       = getPosition(cp.fColorImg);
		cp.cpMouseDown = true;

		return false;
	}

	function cpMouseUp(ev){
		cp.cpMouseDown = false;
	}

	function cpMouseClick(ev){
		ev           = ev || window.event;
		var mousePos = mouseCoordinates(ev);

		var x        = mousePos.x-cp.cpPos.x-1;
		var y        = mousePos.y-cp.cpPos.y-1;

		setCPColor(getGradientColor(x, y, cp.baseColor));
	}

		function cpMouseMove(ev){
		 // fired when mouse moves over the color picker
		if(cp.cpMouseDown){
			cpMouseClick(ev);
		}

		return false;
	}
	
	function setCPColor(color){
		if(color.srcElement || color.target) color=null;
		if(color && (!color.r && (color.r!=0))) color = parseColor(color);
		if(!color){
			color = {
				r:parseInt(cp.rInput.value),
				g:parseInt(cp.gInput.value),
				b:parseInt(cp.bInput.value)
			}
		}
		var wsColor = getWebSafeColor(color)

		if(wsColor && !cp.websafeCheckbox.checked){
			cp.websafeImg.style.display         = 'block';
			cp.websafeImg.style.backgroundColor = '#'+hex(wsColor.r)+hex(wsColor.g)+hex(wsColor.b);
		}else{
			if(wsColor && cp.websafeCheckbox.checked) color = wsColor;
			cp.websafeImg.style.display         = 'none';
		}

		cp.rInput.value = color.r;
		cp.gInput.value = color.g;
		cp.bInput.value = color.b;

		cp.cpColor      = color;
		cp.curColorDiv.style.backgroundColor = '#'+hex(color.r)+hex(color.g)+hex(color.b);
	}

	function documentMouseDown(ev){
		ev         = ev            || window.event;
		var target = ev.srcElement || ev.target;

		while(target)
		{
			if(target==cp.ColorPicker) return;
				target = target.parentNode;
		}
		cp.ColorPicker.style.display = 'none';
	}

	function documentMouseUp(ev){
		cpMouseUp(ev);
	}
	
	function cusomizeSkin()
	{
		document.getElementById('saveAsName').disabled = "";
		document.getElementById('saveAsName').value = "Provide Name To Save..";
		
		document.getElementById('skinSelect').value = "-1";
		
		colorsChanged = true;
	}
	
	document.onmousedown     = documentMouseDown;
	document.onmouseup       = documentMouseUp;
	window.attachColorPicker = attachColorPicker;
})();
