/* DHTML Color Picker v1.0.3, Programming by Ulyses, ColorJack.com */
/* Updated August 24th, 2007 */

function $(v) { return(document.getElementById(v)); }
function $S(v) { return(document.getElementById(v).style); }
function absPos(o) { var r={x:o.offsetLeft,y:o.offsetTop}; if(o.offsetParent) { var v=absPos(o.offsetParent); r.x+=v.x; r.y+=v.y; } return(r); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function toggle(v) { $S(v).display=($S(v).display=='none'?'block':'none'); }
function within(v,a,z) { return((v>=a && v<=z)?true:false); }
function XY(e,v) { var z=agent('msie')?[event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop]:[e.pageX,e.pageY]; return(z[zero(v)]); }
function XYwin(v) { var z=agent('msie')?[document.body.clientHeight,document.body.clientWidth]:[window.innerHeight,window.innerWidth]; return(!isNaN(v)?z[v]:z); }
function zero(v) { v=parseInt(v); return(!isNaN(v)?v:0); }
function jackCurr(v) { if($('hex'+selected)) $S('hex'+selected).background='#272727'; $S('hex'+v).background='#000'; selected=v; }
function jackPlug(v,hex) { var hex=$('x'+v).innerHTML,hsv=hex2hsv(hex); slideHSV=hsv; lastSlide=hex; HSVupdate(v,hex); loadA(); jackCurr(v); if(swatch) scheme(swatch[0],swatch[1]); }

/* PLUGIN */

var maxValue={'h':360,'s':100,'v':100}, HSV={0:360,1:100,2:100};
var hSV=165, wSV=162, hH=163, slideHSV={0:360,1:100,2:100}, zINDEX=15, stop=1;

function HSVslide(d,o,e) {

  function tXY(e) { tY=XY(e,1)-ab.y; tX=XY(e)-ab.x; }
  function mkHSV(a,b,c) { return(Math.min(a,Math.max(0,Math.ceil((parseInt(c)/b)*a)))); }
  function ckHSV(a,b) { if(within(a,0,b)) return(a); else if(a>b) return(b); else if(a<0) return('-'+oo); }
  function drag(e) { if(!stop) { if(d!='drag') tXY(e);

    if(d=='SVslide') { ds.left=ckHSV(tX-oo,wSV)+'px'; ds.top=ckHSV(tY-oo,wSV)+'px';

      slideHSV[1]=mkHSV(100,wSV,ds.left); slideHSV[2]=100-mkHSV(100,wSV,ds.top); HSVupdate();

    }
    else if(d=='Hslide') { var ck=ckHSV(tY-oo,hH), j, r='hsv', z={};

      ds.top=(ck-5)+'px'; slideHSV[0]=mkHSV(360,hH,ck);

      for(var i=0; i<=r.length-1; i++) { j=r.substr(i,1); z[i]=(j=='h')?maxValue[j]-mkHSV(maxValue[j],hH,ck):HSV[i]; }

      HSVupdate(z); $S('SV').backgroundColor='#'+hsv2hex([HSV[0],100,100]);

    }
    else if(d=='drag') { ds.left=XY(e)+oX-eX+'px'; ds.top=XY(e,1)+oY-eY+'px'; }

  }}

  if(stop) { stop=''; var ds=$S(d!='drag'?d:o);

    if(d=='drag') { var oX=parseInt(ds.left), oY=parseInt(ds.top), eX=XY(e), eY=XY(e,1); $S(o).zIndex=zINDEX++; }
    else { var ab=absPos($(o)), tX, tY, oo=(d=='Hslide')?2:4; ab.x+=10; ab.y+=22; if(d=='SVslide') slideHSV[0]=HSV[0]; }

    document.onmousemove=drag; document.onmouseup=function(){ stop=1; document.onmousemove=''; document.onmouseup=''; }; drag(e);

  }
}

function HSVupdate(v) { v=hsv2hex(HSV=v?v:slideHSV);
  $(currentBoxId).value='#'+v;
  return(ELUpdate('#' + v));
}

function ELUpdate(hex) {
  if(hex!='' && /#[a-f,A-F,0-9]{6}/.test(hex)==false) {
    $(currentBoxId).value='#??????';
    return false;
  }
  SpdzColors[currentType][currentId] = hex;
  $S(currentBoxId).borderColor = hex;
  switch(currentType) {
  case 'text':
    $S(currentId).color = hex;
    break;
  case 'bg':
    $S(currentId).backgroundColor = hex;
    break;
  case 'border':
    $S(currentId).borderColor = hex;
    break;
  }
  return(hex);
}

function loadSV() { var z='';

  for(var i=hSV; i>=0; i--) z+="<div style=\"BACKGROUND: #"+hsv2hex([Math.round((360/hSV)*i),100,100])+";\"><br /><\/div>";

  $('Hmodel').innerHTML=z;

  console.scrollTop = console.scrollHeight;
}

/* CONVERSIONS */

function toHex(v) { v=Math.round(Math.min(Math.max(0,v),255)); return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16)); }
function rgb2hex(r) { return(toHex(r[0])+toHex(r[1])+toHex(r[2])); }
function hsv2hex(h) { return(rgb2hex(hsv2rgb(h))); }

function hsv2rgb(r) { // easyrgb.com/math.php?MATH=M21#text21

    var R,B,G,S=r[1]/100,V=r[2]/100,H=r[0]/360;

    if(S>0) { if(H>=1) H=0;

        H=6*H; F=H-Math.floor(H);
        A=Math.round(255*V*(1.0-S));
        B=Math.round(255*V*(1.0-(S*F)));
        C=Math.round(255*V*(1.0-(S*(1.0-F))));
        V=Math.round(255*V);

        switch(Math.floor(H)) {

            case 0: R=V; G=C; B=A; break;
            case 1: R=B; G=V; B=A; break;
            case 2: R=A; G=V; B=C; break;
            case 3: R=A; G=B; B=V; break;
            case 4: R=C; G=A; B=V; break;
            case 5: R=V; G=A; B=B; break;

        }

        return([R?R:0,G?G:0,B?B:0]);

    }
    else return([(V=Math.round(V*255)),V,V]);

}

/* LOAD */

window.onload=loadSV;

var prevColor="#FFFFFF";
var currentType="bg";
var currentId="content";
var currentBoxId="elcc0b";
var SpdzColors=new Array();
SpdzColors['bg'] = new Array();
SpdzColors['text'] = new Array();
SpdzColors['border'] = new Array();
SpdzColors['bg']['content']			= "#FFFFFF";
SpdzColors['bg']['resw']			= "#FEE3BF";
SpdzColors['text']['connection']	= "#FF9000";
SpdzColors['text']['ipires']		= "#000000";
SpdzColors['text']['status']		= "#003ABA";
SpdzColors['border']['status']		= "#003ABA";
SpdzColors['text']['dwtitle']		= "#000000";
SpdzColors['bg']['dwbar']			= "#FF9000";
SpdzColors['text']['dwtext']		= "#FF9000";
SpdzColors['text']['uptitle']		= "#000000";
SpdzColors['bg']['upbar']			= "#003ABA";
SpdzColors['text']['uptext']		= "#003ABA";
SpdzColors['text']['scale']			= "#003ABA";
SpdzColors['bg']['scale']			= "#DDC5A6";
SpdzColors['border']['scale']		= "#413C8E";
SpdzColors['text']['console']		= "#000000";
SpdzColors['bg']['console']			= "#EEEEEE";
SpdzColors['border']['console']		= "#333333";
SpdzColors['text']['results']		= "#000000";
SpdzColors['text']['stb']			= "#0000FF";
SpdzColors['bg']['stb']				= "";

function restore() {
  switch(currentType) {
  case 'text':
    $S(currentId).color = prevColor;
    break;
  case 'bg':
    $S(currentId).backgroundColor = prevColor;
    break;
  case 'border':
    $S(currentId).borderColor = prevColor;
    break;
  }
}

function selectElement(id, type, boxid) {
  document.getElementById(boxid).checked=true;
  currentId = id;
  currentType = type;
  currentBoxId = boxid+'b';
  prevColor = SpdzColors[type][id];
  switch(type) {
  case 'text':
    $S(id).color = '#1AFF00';
    break;
  case 'bg':
    $S(id).backgroundColor = '#1AFF00';
    break;
  case 'border':
    $S(id).borderColor = '#1AFF00';
    break;
  }
  setTimeout("restore();", 500);
}
var ctrlidx=0;
function addControl(target, type, text) {
  document.write('<div class="elcell">');
  document.write('<div class="elradio"><input type="radio" name="elr" id="elcc'+ctrlidx+'" onclick="selectElement(\''+target+'\', \''+type+'\', this.id);" '+(ctrlidx==0?'checked ':'')+' /></div>');
  document.write('<div class="elname" id="elcc'+ctrlidx+'n" onclick="selectElement(\''+target+'\', \''+type+'\', this.id.substring(0,this.id.length-1));" >'+text+'</div>');
  document.write('<div class="elcolor"><input type="text" id="elcc'+ctrlidx+'b" style="border-color: '+SpdzColors[type][target]+';" value="'+SpdzColors[type][target]+'" size="6" maxlength="7" onchange="ELUpdate(this.value);"  onclick="selectElement(\''+target+'\', \''+type+'\', this.id.substring(0,this.id.length-1));" /></div>');
  document.write('</div>');
  ctrlidx=ctrlidx+1;
}

function setCopy(file) {
	$S('imgcopy').backgroundImage = "url('spdz/img/"+file+"')";
	document.makecss.logo_copy.value = file;
}

function makeCSS() {
	document.makecss.bg_content.value = SpdzColors['bg']['content'];
	document.makecss.bg_resw.value = SpdzColors['bg']['resw'];
	document.makecss.text_connection.value = SpdzColors['text']['connection'];
	document.makecss.text_ipires.value = SpdzColors['text']['ipires'];
	document.makecss.text_status.value = SpdzColors['text']['status'];
	document.makecss.border_status.value = SpdzColors['border']['status'];
	document.makecss.text_dwtitle.value = SpdzColors['text']['dwtitle'];
	document.makecss.bg_dwbar.value = SpdzColors['bg']['dwbar'];
	document.makecss.text_dwtext.value = SpdzColors['text']['dwtext'];
	document.makecss.text_uptitle.value = SpdzColors['text']['uptitle'];
	document.makecss.bg_upbar.value = SpdzColors['bg']['upbar'];
	document.makecss.text_uptext.value = SpdzColors['text']['uptext'];
	document.makecss.text_scale.value = SpdzColors['text']['scale'];
	document.makecss.bg_scale.value = SpdzColors['bg']['scale'];
	document.makecss.border_scale.value = SpdzColors['border']['scale'];
	document.makecss.text_console.value = SpdzColors['text']['console'];
	document.makecss.bg_console.value = SpdzColors['bg']['console'];
	document.makecss.border_console.value = SpdzColors['border']['console'];
	document.makecss.text_results.value = SpdzColors['text']['results'];
	document.makecss.text_stb.value = SpdzColors['text']['stb'];
	document.makecss.bg_stb.value = SpdzColors['bg']['stb'];
	document.makecss.submit();
}

