if(typeof window.Colorpicker == "undefined") {
/******************************************************************************
 * Javascriptový objekt pro výběr barvy
 *  
 * Autor: Tomáš Král, http://www.d2k.cz/
 * Kódování: UTF-8 (ěščřžýáíé) 
 * podpora v MSIE 6+, Firefox, Opera 
 *  
 ******************************************************************************/

window.undefined = window.undefined;
// konstruktor
var Colorpicker = function(id) {
  // If the context is global, return a new object
  if ( window == this || !this.init ) {
    return new Colorpicker(id);
  }
  var self = this;
  $(document).ready(function() { self.init(id); });
};

Colorpicker.prototype = {
  html: window.undefined,
  elem: window.undefined,
  // r,g,b values are from 0 to 1
  // h = [0,360], s = [0,1], v = [0,1]
  //		if s == 0, then h = 0
  c: window.undefined,
  map : {0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15},
  rmap: {0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'a',11:'b',12:'c',13:'d',14:'e',15:'f'},
  topleft: window.undefined,
  init: function(id) {
    var self = this;
    this.c = { hsv : {h:0,s:0,v:0},
               rgb : {r:0,g:0,b:0},
               html: '#000000'};
    this.topleft = {x:0,y:0};
    this.elem = $('#'+id);
    this.html = $("<div class='colorpicker'><div class='box'><span class='cursor'></span><div class='ereceiver'></div></div><div class='slider'><span class='cursor'></span><div class='ereceiver'></div></div><input type='button' class='close' value='zavřít' /></div>").appendTo('body');
    this.elem.css({'border':'1px solid #000', 'width':'9ex'}
            ).bind('change', function() { self.fromInput() }
            ).bind('click', function() { self.html.show(); });
    var obj = this.elem[0];
    while (obj.tagName != 'BODY' && obj.tagName != 'HTML') {
      this.topleft.x += obj.offsetLeft;
      this.topleft.y += obj.offsetTop;
      obj = obj.offsetParent;
    }
  	this.topleft.x += this.elem[0].offsetWidth;
    this.html.css({'position':'absolute','display':'block',
                   'background-color':'#efefed','border':'1px solid #000',
                   'top':this.topleft.y+'px','left':this.topleft.x+'px',
                   'padding':'5px'});
    this.html.find('.box').css({'width':'256px','height':'256px','float':'left','margin-right':'5px','position':'relative'});
    this.html.find('.box .ereceiver').css({'position':'absolute','top':'0','bottom':'0','left':'0','right':'0','cursor':'crosshair'}
                         ).bind('mousemove', function(e) { self.bmousemove(e); }
                         ).bind('mousedown', function(e) { self.bmousedown(e); }
                         ).bind('mouseup', function(e) { self.bmouseup(e); }
                         ).bind('click', function(e) { self.bmouseclick(e); });
    this.html.find('.slider').css({'width':'20px','height':'256px','float':'left','margin-right':'5px','position':'relative'});
    this.html.find('.slider .ereceiver').css({'position':'absolute','top':'0','bottom':'0','left':'0','right':'0','cursor':'crosshair'}
                         ).bind('mousemove', function(e) { self.smousemove(e); }
                         ).bind('mousedown', function(e) { self.smousedown(e); }
                         ).bind('mouseup', function(e) { self.smouseup(e); }
                         ).bind('click', function(e) { self.smouseclick(e); });
    if($.browser.msie && $.browser.version < 7) {
      this.html.find('.box').before('<p>Váš prohlížeč nepodporuje některé použité technologie, levou paletu proto vidíte v&nbsp;odstínech šedi.</p>');
      this.html.find('p').css({'font-size':'80%', 'width':'300px', 'margin-bottom':'5px'});
      this.html.find('.slider .ereceiver').css({'width':'20px','height':'256px'});
      this.html.find('.box .ereceiver').css({'width':'256px','height':'256px'});
    }
    this.html.find('.box .cursor').css({'width':'5px','height':'5px','font-size':'1px','display':'block',
                                        'position':'absolute','border':'1px solid #aaa'});
    this.html.find('.slider .cursor').css({'width':'26px','height':'3px','font-size':'1px','display':'block',
                                           'position':'absolute','left':'-4px','border':'1px solid #000'});
    this.fromInput();
    this.html.find('input.close').bind('click', function() { self.html.hide(); return false; }).css({'cursor':'pointer'});
    this.html.hide();
  },
  fromInput: function() {
    this.c.html = this.elem.val();
    this.c.rgb = this.HTML2RGB(this.c.html);
    this.c.hsv = this.RGB2HSV(this.c.rgb.r, this.c.rgb.g, this.c.rgb.b);
    var b = this.HSV2RGB(this.c.hsv.h, 1, 1);
    this.html.find('.box').css('background-color', this.RGB2HTML(b.r, b.g, b.b));
    var cursor = this.html.find('.box .cursor');
    var top = (256*(1-this.c.hsv.v)) - 4;
    var left = 256*this.c.hsv.s - 4;
    cursor.css({'top':top+'px','left':left+'px'});
    cursor = this.html.find('.slider .cursor');
    top = (256*this.c.hsv.h/360) - 3;
    cursor.css('top', top+'px');
    this.updateinputbox();
  },
  bclick: false,
  bmousemove: function(e) {
    if(this.bclick) {
      this.updatebcpos(e);
    }
  },
  bmousedown: function(e) {
    this.bclick = true;
    this.updatebcpos(e);
  },
  bmouseup: function(e) {
    this.bclick = false;
  },
  bmouseclick: function(e) {
    this.updatebcpos(e);
  },
  updatebcpos: function(e) {
    if(!e) e = window.event;
    var b = this.html.find('.box');
    var top = e.clientY - b[0].offsetTop - this.topleft.y;
    var left = e.clientX - b[0].offsetLeft - this.topleft.x;
    var c = this.html.find('.box .cursor');
    this.c.hsv.v = (256 - top) / 256;
    this.c.hsv.s = left / 256;
    top -= 4;
    left -= 4;
    if($.browser.msie) {
      top -= 2;
      left -= 2;
    }
    c.css({'top':top+'px', 'left':left+'px'});
    this.c.rgb = this.HSV2RGB(this.c.hsv.h, this.c.hsv.s, this.c.hsv.v);
    this.c.html = this.RGB2HTML(this.c.rgb.r, this.c.rgb.g, this.c.rgb.b);
    this.updateinputbox();
  },
  sclick: false,
  smousemove: function(e) {
    if(this.sclick) {
      this.updatescpos(e);
    }
  },
  smousedown: function(e) {
    this.sclick = true;
    this.updatescpos(e);
  },
  smouseup: function(e) {
    this.sclick = false;
  },
  smouseclick: function(e) {
    this.updatescpos(e);
  },
  updatescpos: function(e) {
    if(!e) e = window.event;
    var s = this.html.find('.slider');
    var top = e.clientY - s[0].offsetTop - this.topleft.y;
    var c = this.html.find('.slider .cursor');
    this.c.hsv.h = 360*top/256;
    top -= 3;
    if($.browser.msie) top -= 2;
    c.css({'top':top+'px'});
    this.c.rgb = this.HSV2RGB(this.c.hsv.h, this.c.hsv.s, this.c.hsv.v);
    this.c.html = this.RGB2HTML(this.c.rgb.r, this.c.rgb.g, this.c.rgb.b);
    var b = this.HSV2RGB(this.c.hsv.h, 1, 1);
    this.html.find('.box').css({'background-color':this.RGB2HTML(b.r, b.g, b.b)});
    this.updateinputbox();
  },
  updateinputbox: function() {
    this.elem.val(this.c.html);
    this.elem.css({'background-color':this.c.html, 'color':this.readablecolor(this.c.hsv)});
  },
  RGB2HSV: function(r,g,b) {
  	var min, max, delta;
   	min = Math.min(r,Math.min(g,b));
  	max = Math.max(r,Math.max(g,b));
  	var ret = {h:0,s:0,v:0};
    ret.v = max;				    // v
    delta = max - min;
    if(max!=0) { 
      ret.s = delta/max;   // s
  	} else {
  		// r = g = b = 0        // s = 0, h is 0
  		ret.s = 0;
  		ret.h = 0;
  		return ret;
  	}
  	
  	if( r == max ) {
  	  if(delta == 0) {
  	    ret.h = 0;
  	  } else {
  		  ret.h = ( g - b ) / delta;		// between yellow & magenta
  		}
  	} else if( g == max ) {
  		ret.h = 2 + ( b - r ) / delta;	// between cyan & yellow
  	} else {
  		ret.h = 4 + ( r - g ) / delta;	// between magenta & cyan
    }
    
  	ret.h *= 60;				// degrees
  	if( ret.h < 0 ) {
  		ret.h += 360;
  	}
  	return ret;
  },
  HSV2RGB: function(h,s,v) {
  	var i, f, p, q, t;
  	var ret = {r:0,g:0,b:0};
  	if( s == 0 ) {
  		// achromatic (grey)
  		ret.r = ret.g = ret.b = v;
  		return ret;
  	}
  
  	h /= 60;   // sector 0 to 5
  	i = Math.floor( h );
  	f = h - i;			// factorial part of h
  	p = v * ( 1 - s );
  	q = v * ( 1 - s * f );
  	t = v * ( 1 - s * ( 1 - f ) );
  
  	switch( i ) {
  		case 0:
  			ret.r = v; ret.g = t; ret.b = p;
  			break;
  		case 1:
  			ret.r = q; ret.g = v; ret.b = p;
  			break;
  		case 2:
  			ret.r = p; ret.g = v; ret.b = t;
  			break;
  		case 3:
  			ret.r = p; ret.g = q; ret.b = v;
  			break;
  		case 4:
  			ret.r = t; ret.g = p; ret.b = v;
  			break;
  		default:		// case 5:
  			ret.r = v; ret.g = p; ret.b = q;
  			break;
  	}
  	return ret;
  },
  HTML2RGB: function(html) {
    var ret = {r:0,g:0,b:0};
    if(html.match('^#[0-9a-fA-F]{6}$')){
      ret.r = (this.map[html.charAt(1)]*16+this.map[html.charAt(2)]) / 255;
      ret.g = (this.map[html.charAt(3)]*16+this.map[html.charAt(4)]) / 255;
      ret.b = (this.map[html.charAt(5)]*16+this.map[html.charAt(6)]) / 255;
    }  
    if(html.match('^#[0-9a-fA-F]{3}$')){
      ret.r = (this.map[html.charAt(1)]*16+this.map[html.charAt(1)]) / 255;
      ret.g = (this.map[html.charAt(2)]*16+this.map[html.charAt(2)]) / 255;
      ret.b = (this.map[html.charAt(3)]*16+this.map[html.charAt(3)]) / 255;
    }
    return ret;
  },
  RGB2HTML: function(r,g,b) {
    var str = '#';
    str += this.rmap[Math.floor(255*r/16)];
    str += this.rmap[Math.floor(255*r - 16*Math.floor(255*r/16))];
    str += this.rmap[Math.floor(255*g/16)];
    str += this.rmap[Math.floor(255*g - 16*Math.floor(255*g/16))];
    str += this.rmap[Math.floor(255*b/16)];
    str += this.rmap[Math.floor(255*b - 16*Math.floor(255*b/16))];
    return str.match('^#[0-9a-f]{6}$') ? str : '#000000';
  },
  readablecolor: function(hsv) {
    return ((hsv.s >= 0.5 || hsv.v <= 0.5)?'#ffffff':'#000000');
  }
};

} // -- ifdef Colorpicker