/*  WallColorPicker JavaScript uses Prototype JavaScript framework, version 1.6.0.2
 *  (c) 2008 Dexter Balboa - www.belvg.com
 *
 *  WallColorPicker is free.
 *  For details, see the BELVG web site: http://www.belvg.com/
 *
 *--------------------------------------------------------------------------*/
 function showWallColorPicker()
 {
   $('wallcolorpicker').setStyle({
           "z-index"  : "5555"
    });
    
    $('wallcolorpicker').show();
 }
 
 function hideWallColorPicker()
 {
    $('wallcolorpicker').hide();
    
    $('wallcolorpicker').setStyle({
           "z-index"  : "-111"
    });
 }

    $('color_wheel_B').hide();


    changeView_B = function() 
    {
        $('color_wheel_B').toggle();
        $('color_picker_B').toggle();
    }

    hue_B = 60;
    adeg_B = 60;
    sat_B = 1;
    val_B = 1;
    squarecolor_B = "#ffff00";//starting hue_B
    pickindex_B = 0;
    threec_B = new Array("#666666", "#EEEEF0", "#545657");// the three colors
    prevc_B = threec_B[2];
    initary_B = new Array("#444444", "#777777", "#aaaaaa",  "#bbbbbb", "#cccccc", "#dddddd", "#eeeeee");

function BrowserDetectXLite_B() 
{
  var ua = navigator.userAgent.toLowerCase();
  this.ua = ua;
  // browser name
  this.isIE = ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) );
  this.isSafari = (ua.indexOf('safari') != - 1);
  // browser version
  this.versionMinor = parseFloat(navigator.appVersion);
  // correct version number for IE4+ 
  if (this.isIE && this.versionMinor >= 4) 
  {
    this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
  }
  this.versionMajor = parseInt(this.versionMinor);
  // platform
  this.isWin   = (ua.indexOf('win') != -1);
  this.isWin32 = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1) );
  this.isMac   = (ua.indexOf('mac') != -1);
  this.isIE4x = (this.isIE && this.versionMajor == 4);
  this.isIE4up = (this.isIE && this.versionMajor >= 4);
  this.isIE5x = (this.isIE && this.versionMajor == 5);
  this.isIE55 = (this.isIE && this.versionMinor == 5.5);
  this.isIE5up = (this.isIE && this.versionMajor >= 5);
  this.isIE6x = (this.isIE && this.versionMajor == 6);
  this.isIE6up = (this.isIE && this.versionMajor >= 6);
  this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser_B = new BrowserDetectXLite_B();
//end of browser detector
//fix for IE's png stupidity
function deMoronize_B() {//now the onload function
// detect moronic browsers
  if (!((browser_B.isIE55 || browser_B.isIE6up) && browser_B.isWin32)) {
     // demoronize div#wheel image if browser is non-moronic
     thesmartversion_B = "<a href='javascript://' onclick='javascript:pickColor_B();return false;'><img src='../resources/images/framer/hsvwheel.png' alt='ASD' width='140' height='140' border='0'></a>";
  $("wheel_B").innerHTML = thesmartversion_B;
  }
  // keypress stuff
  if (browser_B.isSafari) document.onkeypress = rotate_B;//safari repeat
  else document.onkeydown = rotate_B;
  // start capturing the mouse
  capture_B();
}
// HSV conversion algorithm adapted from easyrgb.com
function hsv2rgb_B(Hdeg,S,V) {
  H = Hdeg/360;    // convert from degrees to 0 to 1
  if (S==0) {       // HSV values = From 0 to 1
    R = V*255;    // RGB results = From 0 to 255
    G = V*255;
    B = V*255;}
  else {
    var_h = H*6;
    var_i = Math.floor( var_h );    //Or ... var_i = floor( var_h )
    var_1 = V*(1-S);
    var_2 = V*(1-S*(var_h-var_i));
    var_3 = V*(1-S*(1-(var_h-var_i)));
    if (var_i==0)      {var_r=V ;   var_g=var_3;var_b=var_1}
    else if (var_i==1) {var_r=var_2;var_g=V;    var_b=var_1}
    else if (var_i==2) {var_r=var_1;var_g=V;    var_b=var_3}
    else if (var_i==3) {var_r=var_1;var_g=var_2;var_b=V}
    else if (var_i==4) {var_r=var_3;var_g=var_1;var_b=V}
    else               {var_r=V;    var_g=var_1;var_b=var_2}
    R = Math.round(var_r*255);  //RGB results = From 0 to 255
    G = Math.round(var_g*255);
    B = Math.round(var_b*255);
  }
  return new Array(R,G,B);
}
function rgb2hex_B(rgbary) {
  cary = new Array;
  cary[3] = "#";
  for (i=0;i < 3;i++) {
    cary[i] = parseInt(rgbary[i]).toString(16);
    if (cary[i].length < 2) cary[i] = "0"+ cary[i];
    cary[3] = cary[3] + cary[i];
    cary[i+4] = rgbary[i];//save dec values for later
  }
  // function returns hex color as an array of three two-digit strings
  // plus the full hex color and original decimal values
  return cary;
}
function webRounder_B(c,d) {//d is the divisor
  //safe divisor is 51, smart divisor is 17 
  thec = "";
  for (i=0;i<3;i++) {
      num = Math.round(c[i+4]/d) * d;//use saved rgb value
      numc = num.toString(16);
      if (String(numc).length < 2) numc = "0" + numc;
      thec += numc;
  }
  return thec;
}
function hexColorArray_B(c) { //now takes string hex value with #
    threec_B[2] = c[3];
    threec_B[1] = webRounder_B(c,17);
    threec_B[0] = webRounder_B(c,51);
    return false;
}
function capture_B() {
 hoverColor_B();
 initColor_B();
 if(document.layers) {
  layobj = document.layers['wheel_B'];
  layobj.document.captureEvents(Event.MOUSEMOVE);
  layobj.document.onmousemove = mouseMoved_B;
 }
 else if (document.all) {
  layobj = document.all["wheel_B"];
  layobj.onmousemove = mouseMoved_B;
   }
 else if ($("wheel_B")) {
  $("wheel_B").onmousemove = mouseMoved_B;
 }
}
function greyMoved_B(x,y) {
    adeg_B = hue_B;
    xside = (x<=553)?x - 296:256;
    yside = (y<=256)?y:256;
    sat_B = xside/256;
    val_B = 1 - (yside/256);
    c = rgb2hex_B(hsv2rgb_B(hue_B,sat_B,val_B));
    hexColorArray_B(c);
    hoverColor_B();
    return false;
}
function mouseMoved_B(e) {
 if (document.layers) {
  x = e.layerX;
  y = e.layerY;
 }
 else if (document.all) {
  x = event.offsetX;
  y = event.offsetY;
 }
 else if ($("wheel_B")) {
  offsets = Position.positionedOffset($("wheel_B"));
  x = (e.pageX - offsets[0]);
  y = (e.pageY - offsets[1]);
 }
 if (x >= 140) {greyMoved_B(x,y);
   return false;}
 if (y > 140) {return false;}
    cartx = x - 70;
    carty = 70 - y;
    cartx2 = cartx * cartx;
    carty2 = carty * carty;
    cartxs = (cartx < 0)?-1:1;
    cartys = (carty < 0)?-1:1;
    cartxn = cartx/70;                     //normalize x
    rraw = Math.sqrt(cartx2 + carty2);      //raw radius
    rnorm = rraw/78;                       //normalized radius
    if (rraw == 0) {
      sat_B = 0;
      val_B = 0;
      rgb = new Array(0,0,0);
      }
    else {
      arad = Math.acos(cartx/rraw);           //angle in radians 
      aradc = (carty>=0)?arad:2*Math.PI - arad; //correct below axis
      adeg_B = 360 * aradc/(2*Math.PI); //convert to degrees
      if (rnorm > 1) {    // outside circle
            rgb = new Array(255,255,255);
            sat_B = 1;
            val_B = 1;
            }
      //else rgb = hsv2rgb_B(adeg_B,1,1);
            else if (rnorm >= .5) {
        sat_B = 1 - ((rnorm - .5) *2);
              val_B = 1;
        rgb = hsv2rgb_B(adeg_B,sat_B,val_B);
        }
              else {
                   sat_B = 1;
             val_B = rnorm * 2;
             rgb = hsv2rgb_B(adeg_B,sat_B,val_B);}
   }
   c = rgb2hex_B(rgb);
   hexColorArray_B(c);
   hoverColor_B();
   return false;
}
function hoverColor_B() {
 if (document.layers) {
  document.layers["demob_B"].bgColor = threec_B[1];
 } else if (document.all) {
  document.all["demob_B"].style.backgroundColor = threec_B[1];
 } else if ($("demob_B")) {
  $("demob_B").style.backgroundColor = threec_B[1];
 }
 return false;
}
pickColor_B = function() {
  art_project.wallcolor.style.backgroundColor = '#' + threec_B[1];
  setSquare_B(adeg_B);
  return false;
}
function setSquare_B(deg) {
  hue_B = deg;
  adeg_B = deg;
  c = rgb2hex_B(hsv2rgb_B(hue_B,1,1));
  squarecolor_B = c[3];
}
function initColor_B() {
 for (i=0;i<7;i++) {
  thecontents = "<tt class='wtxt'>" + initary_B[i] +"</tt><br><tt class='btxt'>" + initary_B[i] +"</tt>";
 }
}
function theToggle_B(divid,disp) {
  var elements = document.getElementsByTagName("div");
  for(var i = 0;i < elements.length;i++) {
  if (elements.item(i).id == divid) {
     elements.item(i).style.display = disp;}}}
// keyboard tricks adapted from
// http://www.ipwebdesign.net/kaelisSpace/useful_keypress.html
// see also http://sniptools.com/jskeys
function rotate_B(e) {
    if (!e) e = window.event;
    var key = (typeof e.which == 'number')?e.which:e.keyCode;
  //  var key = e.keyCode;
    handleKP_B(key);
    }

function handleKP_B(key) {
    switch (key) {
    case 13:  reHue_B(hue_B);pickColor_B();break;// enter key
    case 112: reHue_B(hue_B);pickColor_B();break;// p to pick
    case 114: reHue_B(0);break;//r for red
    case 121: reHue_B(60);break;//y for yellow
    case 103: reHue_B(120);break;//g for green
    case 99: reHue_B(180);break;//c for cyan
    case 98: reHue_B(240);break;//b for blue
    case 109: reHue_B(300);break;//m for magenta
    case 106: reHue_B(hue_B+1);break;//j increases
    case 104: reHue_B(hue_B+1);break;//h increases (dvorak)
    case 107: reHue_B(hue_B+355);break;//k decreases more
    case 116: reHue_B(hue_B+355);break;//t decreases more (dvorak)
    case 108: reHue_B(hue_B+359);break;//l decreases
    case 110: reHue_B(hue_B+359);break;//n decreases (dvorak)
    // need second set for capital letters
    case 80: reHue_B(hue_B);pickColor_B();break;// P 
    case 82: reHue_B(0);break;//R 
    case 89: reHue_B(60);break;//Y 
    case 71: reHue_B(120);break;//G
    case 67: reHue_B(180);break;//C
    case 66: reHue_B(240);break;//B
    case 77: reHue_B(300);break;//M
    case 74: reHue_B(hue_B+1);break;//J
    case 72: reHue_B(hue_B+1);break;//H
    case 75: reHue_B(hue_B+355);break;//K
    case 84: reHue_B(hue_B+355);break;//T
    case 76: reHue_B(hue_B+359);break;//L
    case 78: reHue_B(hue_B+359);break;//N
    }
    return false;
}
function reHue_B(deg) {
    deg = deg % 360;
    setSquare_B(deg);
    rgb = hsv2rgb_B(deg,sat_B,val_B);
    c = rgb2hex_B(rgb);
    hexColorArray_B(c);
    hoverColor_B();
    return false;
}


var cpicker_B = $("cpicker_B");
var cpickerPalette_B = "";
var col=new Array();
col[0]=new Array('FFFFFF','FFF7F7','FFFFF7','FFEFDE','FFE7D6','FFDED6','FFE7D6','FFE7D6','F7EFCE','EFF7E7','E7F7E7','F7FFFF','F7F7FF','FFF7FF');
col[1]=new Array('FFF7FF','F7EFF7','F7EFF7','FFE7CE','EFBDA5','FFCEC6','F7CEC6','FFF7C6','E7DE9C','E7F7CE','CEEFD6','EFFFF7','EFF7FF','F7EFF7');
col[2]=new Array('F7EFF7','E7E7E7','E7E7E7','FFEFDE','EFC6B5','FFBDB5','E7BDB5','FFF7AD','D6BD5A','D6F7CE','A5E7BD','D6E7DE','DEE7F7','E7DEEF');
col[3]=new Array('E7E7E7','CECECE','FFE7D6','FFCEAD','E7BD9C','EFADA5','CE9494','FFEF7B','BD9C10','C6F7A5','8CE7AD','BDDECE','C6D6E7','D6C6D6');
col[4]=new Array('9C9C9C','9C8C9C','FFEFDE','FFB584','CE845A','FF3131','CE4A4A','FFD69C','AD9C5A','D6E7CE','94B59C','73A57B','737B9C','9C8C9C');
col[5]=new Array('000000','212121','F7CEBD','FF9C52','B55218','B50000','943939','DE9C10','7B6B10','316B08','316339','293929','292939','423142');

function drawCell_B(color) {
  cpickerPalette_B +='<TD HEIGHT=12 WIDTH=12 style="border: 1px solid #BDBDBD; cursor: hand; cursor: pointer;" BGCOLOR="#' + color + '" onclick="javascript:art_project.wallcolor.style.backgroundColor = \'#' + color + '\';">' +
  "<img src='../resources/images/framer/t.gif' width=12 height=12></TD>";
}
function drawColors_B() {
  cpickerPalette_B +='<center><table border=0 style="border-color:#000000" cellspacing=0 cellpadding=0 >' +
  '<tr><td><TABLE CELLPADDING=0 CELLSPACING=4 BORDER=0>';
  for (var j = 0; j <6; j++) {
    cpickerPalette_B +='<tr style="border: 1px solid #BDBDBD;">';
    for (var i = 0; i <14; i++) {
      drawCell_B(col[j][i]);
      cpickerPalette_B +='</td>';
    }
    cpickerPalette_B +='</tr>';
  }
  cpickerPalette_B +='</TABLE></td></tr></table></center>';
  cpicker_B.innerHTML = cpickerPalette_B;
}

drawColors_B();
deMoronize_B();
