function HSBtoRGB( h, s, v ) {
    
    if ( isArray( h ) ){
        s = h[1];
        v = h[2];
        h = h[0];
    }

    if ( s == 0 ) {
        return new Array( 255 , 255 , 255 );
    }

    h = h*6;
    var i = Math.floor(  h );     //Or ... var_i = floor( var_h )

    var p = v * ( 1 - s );
    var q = v * ( 1 - s * ( h - i ) );
    var t = v * ( 1 - s * ( 1 - ( h - i) ) );

    var r;
    var g;
    var b;
    
    if (i==0){
        r=v;    
        g=t; 
        b=p;
    }
    else if (i==1){
        r=q; 
        g=v;     
        b=p;
    }
    else if (i==2){
        r=p; 
        g=v;     
        b=t;
    }
    else if (i==3){
        r=p; 
        g=q; 
        b=v;
    }
    else if (i==4){
        r=t; 
        g=p; 
        b=v;
    }
    else{
        r=v;     
        g=p; 
        b=q;
    }

    r = Math.round(r*255);   //RGB results = From 0 to 255
    g = Math.round(g*255);
    b = Math.round(b*255);

    return new Array(r,g,b);
}

function fixRGB( val ){
    if ( val < 0 || val > 255 )
        val = 255;
    return val;
}

function RGBtoHex( r , g , b ){
    if ( isArray( r ) ){
        g = r[1];
        b = r[2];
        r = r[0];
    }
    
    r = fixRGB( r );
    g = fixRGB( g );
    b = fixRGB( b );

    return "#" + numToHex( r , 2 , "0" ) + numToHex( g , 2 , "0" ) + numToHex( b , 2 , "0" );
}

function xyToHSB( size , x , y ){

    var cartX = x - ( size / 2  );
    var cartY = ( size / 2 ) - y;
    
    var distFromCenter = Math.sqrt( ( cartX * cartX ) + ( cartY * cartY ) );
    var distFromCenterNormalized = distFromCenter / ( size / 2 );
    
    if ( distFromCenter == 0) {
        return new Array( 0,255,0);
    }
    
    var arad = Math.acos( cartX / distFromCenter );            //angle in radians 
    var aradc = ( cartY >= 0 ) ? arad : ( Math.PI * 2 ) - arad;  //correct below axis
    
    if (distFromCenterNormalized > 1) {    // outside circle
        return new Array(0,255,1);
    }
    
    if ( distFromCenterNormalized >= .5) {
        return new Array( aradc / ( Math.PI * 2 )  , Math.pow( 1 - (( distFromCenterNormalized - .5) * 2 ) , .7 ), 1 );
    }
    
    return new Array( aradc / ( Math.PI * 2 ) , 1 ,  Math.pow( distFromCenterNormalized * 2, .8 ) );
    
}

function pickColor( event ){
    var hex = selectColor( event );
    setSearchColor( hex );
}

function selectColor( event ){
    var x = getRelMouseX( event );
    var y = getRelMouseY( event );
    
    var hex = RGBtoHex( HSBtoRGB( xyToHSB( 256, x , y ) ) );

    //    setHtml( "foo" , x + "," + y + ":" + hex );
    getElement("color_selected").style.backgroundColor = hex;
    lastColor = hex;

    return hex;
}

function overColorSwatch( c ){
    getElement("color_selected").style.backgroundColor = c;    
    lastColor = c;
}

function selectGrayColor( event ){
    var x = getRelMouseX( event );

    var rat = 1 - ( x / 256 );
    var c = 256 * rat;
    var hc = numToHex( c , 2, "0" );

    var h = "#" + hc + hc + hc;

    overColorSwatch( h );
    return h;
}

function pickColorBarColor( event ){
    setSearchColor( selectColorBarColor( event ) );
}

var colorBarWidth = 200;
var rgbWidth = colorBarWidth * .86;
var numFlats = 2;
var flatWidth = ( colorBarWidth - rgbWidth ) / numFlats;


function selectColorBarColor( event ){
    
    var thingHeight = 66;

    inColorArea = true;
    var x = getRelMouseX( event ) + getRelativePosition( getTarget( event ) , getElement("colorContainer") , false , true ).l;
    var y = getRelativePosition( getTarget( event )  , getElement("colorContainer" ) ).t;
    
    if ( x > colorBarWidth + 10 || x < -8 ){
        return;
    }
    
    if ( x <= 0 )
        x = 1;
    if ( x > colorBarWidth )
        x = colorBarWidth;

    //trace( x + " rel mouse x : " + getRelMouseX( event ) + "  mouse relative event -> colorContainer : " + getRelativePosition( getTarget( event ) , getElement("colorContainer")).l );

    var h = 1 - (x/rgbWidth);
    var s = 1;
    var b = 1;

    var hex = RGBtoHex( HSBtoRGB( h , s , b ) );

    var flatNum = 0;
    if ( x > rgbWidth ){
        flatNum = ( x - rgbWidth ) / flatWidth;
        if ( y < 0 || y > thingHeight )
            y = thingHeight / 2;

        hex = RGBtoHex( colorBarSpecialYToRGB( flatNum , y , thingHeight ) );
    }
    
    if ( x <= rgbWidth && y > 0 && y < thingHeight ){
        hex = RGBtoHex( colorBarYToRGB( h , y , thingHeight ) );
    }

    overColorSwatch( hex );
    
    var html = "";
    for ( y=0; y<thingHeight; y+=2 ){
        var hex;
        if ( x <= rgbWidth ){
            hex = RGBtoHex( colorBarYToRGB( h , y , thingHeight ) );
        }
        else {
            hex = RGBtoHex( colorBarSpecialYToRGB( flatNum , y , thingHeight ) );
        }
        
        if ( madeColorVerticalHTML == 0 ){
            html += "<div id='color_vertical_" + y + "' style='background-color:" + hex + "; border: 0px solid white; position:absolute; width:15px; height:2px; left:0px; top:" + y + "px; border: 0px;'><img alt='' src='http://static.shopwiki.com/images/transparent.gif' width='15' height='2'border='0'  ></div>";
        }
        else {
            document.getElementById( "color_vertical_" + y ).style.background = hex;
        }
    }
    if ( madeColorVerticalHTML == 0 ){
        getElement("colorSelectorExtraBarIn").innerHTML = html;
    }
    madeColorVerticalHTML = 1;

    //trace( colorPrevX + "," + colorPrevY + " -->> " + x + "," + y );
    if ( x >= 0 && x <= colorBarWidth ){
        SWDom.setLeft( "colorSelectorExtraBar" , x - 8 );
        colorPrevX = x;
    }
    colorPrevY = y;
    
    return hex;
}

var colorPrevX = 0;
var colorPrevY = 0;

var madeColorVerticalHTML = 0;

function colorBarYToRGB( h , y , thingHeight ){
    var r = ( y / thingHeight );
    
    var tempS = 1;
    var tempB = 1;
    
    if ( y < thingHeight / 2 ){
        tempS = Math.pow( y / ( thingHeight / 2 ) , .8 );
    }
    else {
        tempB = 1 - Math.pow( ( y - ( thingHeight / 2 )  ) / ( thingHeight / 2 ) , 2 );
    }
    
    return HSBtoRGB( h , tempS , tempB );
}

function colorBarSpecialYToRGB( flatNum , y , thingHeight ){
    
    var h;
    var s;
    var b;
    
    var per = y / thingHeight;

    if ( flatNum < 1 ){
        h = 0.06944444444444;
        s = 1;
        b = .9 - ( per * .6 );
    }
    else {
        h = 0;
        s = .01;
        b = 1 - per;
    }
    
    return HSBtoRGB( h , s , b );
}

function setSearchColor( c ){
    if ( lastColor != null && lastColor.length > 0)
        SearchPage.filterParams.color = lastColor.substring(1); // searchColor no longer contains the '#', just the hex-code of the search color.
    else
        Search.filterParams.color = null;
    SearchPage.updateFilters();
}

function complementaryHexColor( c ){
    var nc = "#" + 
        numToHex( hexCharToInt( c[1] , c[2] ) , 2 , "0" ) + 
        numToHex( hexCharToInt( c[3] , c[4] ) , 2 , "0" ) +
        numToHex( hexCharToInt( c[5] , c[6] ) , 2 , "0" ) ;
    return nc;
    
}

function hexCharToInt( c ){
    if ( c == '0' )
        return 0;
    if ( c == '1' )
        return 1;
    if ( c == '2' )
        return 2;
    if ( c == '3' )
        return 3;
    if ( c == '4' )
        return 4;
    if ( c == '5' )
        return 5;
    if ( c == '6' )
        return 6;
    if ( c == '7' )
        return 7;
    if ( c == '8' )
        return 8;
    if ( c == '9' )
        return 9;

    if ( c == 'a' || c == 'A' )
        return 10;
    if ( c == 'b' || c == 'B' )
        return 11;
    if ( c == 'c' || c == 'C' )
        return 12;
    if ( c == 'd' || c == 'D' )
        return 13;
    if ( c == 'e' || c == 'E' )
        return 14;
    if ( c == 'f' || c == 'F' )
        return 15;

    return 0;
    
}

function twoHexCharsToInt( c1 , c2 ){
    return ( hexCharToInt( c1 ) * 16 ) + hexCharToInt( c2 );
}

function largeColorSelector(){
    SWDom.large("colorSelectorExtraBar");
}


function enterColorArea(){
    inColorArea = true;
    setTimeout( "hadEntered()" , 200 );
}

function hadEntered(){
    if ( inColorArea ){
        largeColorSelector();
        if ( inIE )
            setTimeout( "inColorArea=true" , 10 );
    }
}

function leaveColorArea(){
    var was = inColorArea;
    inColorArea = false;
    if ( was ){
        setTimeout( "checkInColorArea()" , 200 );
    }
}

function checkInColorArea(){
    if ( ! inColorArea ){
        SWDom.small("colorSelectorExtraBar");
    }
}


// ----- stuff from js/common.js

function getMouseX( e ){
    if ( inIE || inOpera ){
	return e.clientX;
    }
    return e.pageX;
}

function getMouseY( e ){
    if ( inIE || inOpera ){
	return e.clientY;
    }
    return e.pageY;
}

function getRelMouseX( e ){
    if ( inIE || inOpera ){
	return e.offsetX;
    }
    return e.layerX;
}

function getRelMouseY( e ){
    if ( inIE || inOpera ){
	return e.offsetY;
    }
    return e.layerY;
}

function getRelativePosition( e , stop , onlyDivs , includeNegative ){
    e = getElement( e );
    stop = getElement( stop );

    if ( ! stop )
	return {t:0, l:0};
    
    var t = 0;
    var l = 0;

    while ( e ){
	if ( e == stop ){
	    break;
	}
	if ( onlyDivs && e.tagName != "div" && e.tagName != "DIV" ){
	    continue;
	}

	if ( ( e.offsetTop > 0 || includeNegative ) && e.offsetTop < 1000000 ){
	    t += e.offsetTop;
	}
	if ( ( e.offsetLeft > 0 || includeNegative ) && e.offsetLeft < 1000000 ){
	    l += e.offsetLeft;
	}

	e = e.parentNode;
    }

    return {t:t, l:l};
}

function getElement( e ){
    if ( typeof(e) == "string" ){
	return document.getElementById( e );
    }
    return e;
}

function getTarget( e ){
    if ( ! e )
        return e;

    if ( inIE ){
	return e.srcElement;
    }
    return e.target;
}

function numToHex( num , pad , padWith ){
    var s = num.toString(16);
    if ( pad ){
        if ( ! padWith ){
            padWith = " ";
        }
        while ( s.length < pad ){
            s = padWith + s;
        }
    }
    return s;
}
