function color_generator_handler(){};
color_generator_handler.prototype = {
	svg: null, // svg object
	jacket: function(color){
		this.svg.getElementById("jacket_tail").setAttribute("fill",color);
		this.svg.getElementById("jacket_arm_right").setAttribute("fill",color);
		this.svg.getElementById("jacket_arm_left").setAttribute("fill",color);
		this.svg.getElementById("jacket_body_left").setAttribute("fill",color);
		this.svg.getElementById("jacket_lapel_left").setAttribute("fill",color);
		this.svg.getElementById("jacket_body_right_bottom").setAttribute("fill",color);
		this.svg.getElementById("jacket_body_right_top").setAttribute("fill",color);
		this.svg.getElementById("jacket_lapel_right").setAttribute("fill",color);
	},
	pants: function(color){
		this.svg.getElementById("pants").setAttribute("fill",color);
	},
	shirt: function(color){
		this.svg.getElementById("shirt_body_right").setAttribute("fill",color);
		this.svg.getElementById("shirt_body_left").setAttribute("fill",color);
		this.svg.getElementById("shirt_collar_right").setAttribute("fill",color);
		this.svg.getElementById("shirt_collar_left").setAttribute("fill",color);
		this.svg.getElementById("shirt_sleeve").setAttribute("fill",color);
	},
	tie: function(color){
		this.svg.getElementById("tie_top").setAttribute("fill",color);
		this.svg.getElementById("tie_bottom").setAttribute("fill",color);
	},
	hair: function(color){
		this.svg.getElementById("hair").setAttribute("fill",color);
	},
	skin: function(color){
		this.svg.getElementById("neck").setAttribute("fill",color);
		this.svg.getElementById("face").setAttribute("fill",color);
		this.svg.getElementById("hand").setAttribute("fill",color);
	},
	stripe_jv: function(color){
		var os = this.svg.getElementById("stripes_vertical_jacket").getElementsByTagName("path");
		for(var n=0; n<os.length; n++){
			os[n].setAttribute("stroke",color);
		}
	},
	stripe_pv: function(color){
		var os = this.svg.getElementById("stripes_vertical_pants").getElementsByTagName("path");
		for(var n=0; n<os.length; n++){
			os[n].setAttribute("stroke",color);
		}
	},
	piping: function(color){
		var os = this.svg.getElementById("piping").getElementsByTagName("path");
		for(var n=0; n<os.length; n++){
			os[n].setAttribute("stroke",color);
		}
	}
}
var _svg = new color_generator_handler();


function COLOR_GENERATE(){}
COLOR_GENERATE.prototype = {
	locked: new Array(),
	selection: new Array(),
	init: function(){
		window.addEventListener("load",function(){
			_cp.make(); // make color picker
			document.getElementById("jacket").addEventListener("click",_cg.select,false);
			document.getElementById("pants").addEventListener("click",_cg.select,false);
			document.getElementById("jstripe").addEventListener("click",_cg.select,false);
			document.getElementById("pstripe").addEventListener("click",_cg.select,false);
			document.getElementById("shirt").addEventListener("click",_cg.select,false);
			document.getElementById("tie").addEventListener("click",_cg.select,false);
			document.getElementById("piping").addEventListener("click",_cg.select,false);
			document.getElementById("hair").addEventListener("click",_cg.select,false);
			document.getElementById("skin").addEventListener("click",_cg.select,false);
			
			document.getElementById("su_lock").addEventListener("click",_cg.lock,false);
			document.getElementById("st_lock").addEventListener("click",_cg.lock,false);
			_cg.locked.su = true;
			_cg.locked.st = true;
			
			_svg.svg = document.getElementById("svg").contentDocument;
		},false);
	},
	select: function(evt){
		for(var n=0; n<_cg.selection.length; n++){ _cg.selection[n].style.color = "black";}
		var selection = (evt.target.nodeName=="IMG")? evt.target.parentNode:evt.target;
		_cg.selection = Array(selection);
		if(selection.id=="jacket" && _cg.locked.su) _cg.selection = Array(selection,document.getElementById("pants"));
		if(selection.id=="pants" && _cg.locked.su) _cg.selection = Array(selection,document.getElementById("jacket"));
		if(selection.id=="jstripe" && _cg.locked.st) _cg.selection = Array(selection,document.getElementById("pstripe"));
		if(selection.id=="pstripe" && _cg.locked.st) _cg.selection = Array(selection,document.getElementById("jstripe"));
		for(var n=0; n<_cg.selection.length; n++){ _cg.selection[n].style.color = "red";}
		if(_cp.position.x==0 && _cp.position.y==0){
			_cp.position.x = evt.clientX;
			_cp.position.y = evt.clientY;
		}
		_cp.visible();
	},
	lock: function(evt){
		var img = (evt.target.nodeName=="IMG")? evt.target:evt.target.getElementsByTagName("img")[0];
		var id = (evt.target.nodeName=="IMG")? evt.target.parentNode.id:evt.target.id;
		if(id=="su_lock"){
			if(_cg.locked.su){
				_cg.locked.su = false;
				img.src = "color_generator/panel/key_unlocked.png";
			}else{
				_cg.locked.su = true;
				img.src = "color_generator/panel/key_locked.png";
			}
		}
		if(id=="st_lock"){
			if(_cg.locked.st){
				_cg.locked.st = false;
				img.src = "color_generator/panel/key_unlocked.png";
			}else{
				_cg.locked.st = true;
				img.src = "color_generator/panel/key_locked.png";
			}
		}
	},
	reflectColor: function(color){
		for(var n=0; n<_cg.selection.length; n++){
			if(_cg.selection[n].id=="jacket") _svg.jacket(color);
			if(_cg.selection[n].id=="pants") _svg.pants(color);
			if(_cg.selection[n].id=="jstripe") _svg.stripe_jv(color);
			if(_cg.selection[n].id=="pstripe") _svg.stripe_pv(color);
			if(_cg.selection[n].id=="shirt") _svg.shirt(color);
			if(_cg.selection[n].id=="tie") _svg.tie(color);
			if(_cg.selection[n].id=="piping") _svg.piping(color);
			if(_cg.selection[n].id=="hair") _svg.hair(color);
			if(_cg.selection[n].id=="skin") _svg.skin(color);
		}
	}
}
_cg = new COLOR_GENERATE();
_cg.init();
