/*
colorpalette.js
20080102 Mugi
*/

/*Mini ajax*/
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
/*Mini ajax*/

var ColorPalette = function(button, input, callback){
	ColorPalette.div = document.getElementById('palette_div');
	if(!ColorPalette.div){
		ColorPalette.div = document.createElement("div");
		ColorPalette.div.id = 'palette_div';
		document.body.appendChild(ColorPalette.div);
	}
	ColorPalette.div.innerHTML = ColorPalette.create();
	var _this = this;
	this.button = button;
	button.onclick = function(e){_this.toggle(e)};
	this.input = input;
	this.onchange = callback;
	ColorPalette.setEvent();
	ColorPalette.activeObject = this;
	return this;
}
ColorPalette.init = function(form, array_palette, callback){
	function _(o, ev){var _=o[ev];o[ev]=function(e){callback(o.form);if(_)return _.call(o,e);}}
	var elem = form.elements;
	for(var i=0,n=elem.length; i<n; i++) {
		var e = elem[i];
		switch(e.type){
			case 'text':
				_(e, 'onchange');
				break;
			case 'checkbox':
			case 'radio':
				_(e, 'onclick');
				_(e, 'onkeyup');
				break;
			case 'select-one':
				_(e, 'onchange');
				break;
		}
	}
	for(var i=0; i<array_palette.length; i++){
		var o = new ColorPalette($(array_palette[i][0]), elem[array_palette[i][1]], callback);
	}
}
ColorPalette.create = function(){
	var source = '<table><tbody>';
	var color='';
	var hex = new Array('f','c','9','6','3','0')
	for(var j=0;j<6;j++){
		for(var k=0;k<6;k++){
			for(var l=0;l<6;l++){
				var hexColor = hex[j]+hex[j]+hex[k]+hex[k]+hex[l]+hex[l];
				color+='<td bgcolor="#'+hexColor+'"></td>';
			}
		}
		source+='<tr>'+color+'</tr>';
		color='';
	}
	for(var i=0;i<6;i++){
		var hexColor = hex[i]+hex[i]+hex[i]+hex[i]+hex[i]+hex[i];
		color+='<td bgcolor="#'+hexColor+'" colspan="6"></td>';
	}
	source+='<tr>'+color+'</tr>';
	source+='</tbody></table>';
	return source;
}
ColorPalette.hide = function(){
	ColorPalette.hide_select(false);
	ColorPalette.div.style.display = 'none';
}
ColorPalette.hide_select = function(a) {
	/*@cc_on
	var select = document.getElementsByTagName("select");
	for(var i=0; i<select.length; i++){
		select[i].style.visibility = (a ? 'hidden' : '');
	}
	@*/
}
ColorPalette.setEvent = function(){
	document.body.onmousedown = function(e){
		e = e || window.event;
		var element = e.target || e.srcElement;
		if(element == ColorPalette.activeObject.button){
			return;
		}
		var tag = element.tagName.toLowerCase();
		if(tag == 'td' && element.offsetParent.parentNode.id == ColorPalette.div.id){
			var o = ColorPalette.activeObject;
			o.input.value = element.getAttribute('bgcolor');
			o.onchange(o.input.form);
			ColorPalette.hide();
		}else /*if(tag == 'body')*/{
			ColorPalette.hide();
		}
	}
	document.onkeydown = function(e){
		e = e || window.event;
		if(e.keyCode == 27){ // 'Esc'
			ColorPalette.hide();
		}
	}
}
ColorPalette.prototype = {
	show:function(){
		if(this.input.disabled)return;
		ColorPalette.hide_select(true);
		var palette = ColorPalette.div.style;
		var top = 0, left = 0;
		var e = this.button;while(e){top += e.offsetTop; e = e.offsetParent;}
		var e = this.button;while(e){left += e.offsetLeft; e = e.offsetParent;}
		palette.top = top + 18 + 'px';
		palette.left = left + 'px';
		palette.display = 'block';
		ColorPalette.activeObject = this;
	},
	toggle:function(){
		if(ColorPalette.div.style.display != 'block'){
			this.show();
		}else{
			ColorPalette.hide();
		}
	},
	onchange:function(form){
	}
}
