(function($) {
    var ColorHex=new Array('00','33','66','99','CC','FF');
    var SpColorHex=new Array('FF0000','00FF00','0000FF','FFFF00','00FFFF','FF00FF');
    $.fn.colorpicker = function(options) {
        var opts = jQuery.extend({}, jQuery.fn.colorpicker.defaults, options);
        initColor();
        return this.each(function(){
            var obj = $(this);
            obj.bind(opts.event,function(){
                //定位
                var ttop  = $(this).offset().top;     //触发点击控件的定位点高
                var theight  = $(this).height();  //触发点击控件本身的高
                var tleft = $(this).offset().left;    //触发点击控件的定位点宽
				var divtop = (opts.offsettop>=0?ttop+theight+5+opts.offsettop:ttop-5+opts.offsettop);
				var divleft = tleft+opts.offsetleft;
                $("#colorpanel").css({
                    top:divtop,
                    left:divleft
                }).show();
                var target = opts.target ? $(opts.target) : obj;
                if(target.data("color") == null){
                    target.data("color",target.css("color"));
                }
          
                $("#_creset").bind("click",function(){
                    target.css("color", target.data("color"));
                    $("#colorpanel").hide();
                    opts.reset(obj);
                });
          
                $("#CT tr td").unbind("click").mouseover(function(){
                    var color=$(this).css("background-color");
                    $("#DisColor").css("background",color);
                    $("#HexColor").val($(this).attr("rel"));
                }).click(function(){
                    var aaa=$(this).css("background");
                    var color = opts.ishex ? $(this).attr("rel") : aaa;
                    if(opts.fillcolor) target.val(color);
                    target.css("color",aaa);
                    $("#colorpanel").hide();
                    $("#_creset").unbind("click");
                    opts.success(obj,color);
                });
          
            });
        });
    
        function initColor(){
            $("body").append('<div id="colorpanel" style="position: absolute; display: none;"></div>');
            var colorTable = '';
			var colorArray = new Array();
            var colorValue = '';
			colorArray.push('<table width=253 border="0" cellspacing="0" cellpadding="0" style="border:1px solid #000;">');
            colorArray.push('<tr height=30><td colspan=21 bgcolor=#cccccc>');
            colorArray.push('<table cellpadding="0" cellspacing="1" border="0" style="border-collapse: collapse">');
            colorArray.push('<tr><td width="3"><td><input type="text" id="DisColor" size="6" disabled style="border:solid 1px #000000;background-color:#ffff00"></td>');
            colorArray.push('<td width="3"><td><input type="text" id="HexColor" size="7" style="border:inset 1px;font-family:Arial;" value="#000000"><a href="javascript:void(0);" id="_cclose">关闭</a> | <a href="javascript:void(0);" id="_creset">清除</a></td></tr></table></td></table>');
            colorArray.push('<table id="CT" border="1" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="000000"  style="cursor:pointer;">');
            for(i=0;i<2;i++){
                for(j=0;j<6;j++){
                    colorArray.push('<tr height=12>');
                    colorArray.push('<td width=11 rel="#000000" style="background-color:#000000">');
                    colorValue = i==0 ? ColorHex[j]+ColorHex[j]+ColorHex[j] : SpColorHex[j];
                    colorArray.push('<td width=11 rel="#');
					colorArray.push(colorValue);
					colorArray.push('" style="background-color:#');
					colorArray.push(colorValue);
					colorArray.push('">');
                    colorArray.push('<td width=11 rel="#000000" style="background-color:#000000">');
                    for (k=0;k<3;k++){
                        for (l=0;l<6;l++){
                            colorValue = ColorHex[k+i*3]+ColorHex[l]+ColorHex[j];
                            colorArray.push('<td width=11 rel="#');
							colorArray.push(colorValue);
							colorArray.push('" style="background-color:#');
							colorArray.push(colorValue);
							colorArray.push('">');
                        }
                    }
                }
            }
            colorArray.push('</table>');
			colorTable = colorArray.join('');
            $("#colorpanel").html(colorTable);
            $("#_cclose").live('click',function(){
                $("#colorpanel").hide();
                return false;
            }).css({
                "font-size":"12px",
                "padding-left":"20px"
            });
        }
    };
    jQuery.fn.colorpicker.defaults = {
		offsettop:0,//div控件top值
		offsetleft:0,//div控件left值
        ishex : true, //是否使用16进制颜色值
        fillcolor:false,  //是否将颜色值填充至对象的val中
        target: null, //目标对象
        event: 'click', //颜色框显示的事件
        success:function(){}, //回调函数
        reset:function(){}
    };
})(jQuery);