var Conditions = Class.create();
Conditions.prototype = {
	initialize: function() {
	},
	

	initLib: function() {
		var n = document.getElementsByName("tags");
		if (n.length) {
			for (var i = 0; i < n.length; i++) {
				Event.observe(n[i], "click", (function(c) {
					return function(e) {
					var className = null;
					if (Element.hasClassName(this.parentNode, "Blue"))
						className = "Blue";
					if (Element.hasClassName(this.parentNode, "Red"))
						className = "Red";
					if (Element.hasClassName(this.parentNode, "Gray"))
						className = "Gray";
					if (className)
						c.setCheckboxStatus(className, this.checked);
					}
				})(this)
				, false);
			}
		}
		//Initialise();
	},

	resetChecboxStatus: function() {
		this.clear();
		var n = document.getElementsByName("tags");
		if (n.length) {
			for (var i = 0; i < n.length; i++) {
				n[i].checked = false;
				n[i].removeAttribute("disabled");
				Element.removeClassName(n[i].parentNode, "BlueDisabled");
				Element.removeClassName(n[i].parentNode, "RedDisabled");
				Element.removeClassName(n[i].parentNode, "GrayDisabled");
					if (n[i].className == "InputHidden") {
						Element.removeClassName(n[i].getElementsByTagName("a").item(0), "CheckboxCheckedWHITE");
						var className = n[i].parentNode.getElementsByTagName("a").item(0).className.replace(" ddcolor", "");
						var cn1 = className.replace("Checked", "Unchecked");
						var cn2 = className.replace("Unchecked", "Checked");
						Element.addClassName(n[i].parentNode.getElementsByTagName("a").item(0), cn1);
						Element.removeClassName(n[i].parentNode.getElementsByTagName("a").item(0), cn2);
					}
			}
		}
	},

	setCheckboxStatus: function(className, checked) {
		var cnt = 0;
		var color = ["Blue", "Red", "Gray"];
		for (var i = 0; i < color.length; i++) {
			var n = document.getElementsByClassName(color[i]);
			for (var j = 0; j < n.length; j++) {
				if (color[i] == className) {
					Element.removeClassName(n[j], color[i] + "Disabled");
					n[j].firstChild.removeAttribute("disabled");
					if (n[j].firstChild.checked)
						cnt++;
				} else {
					Element.addClassName(n[j], color[i] + "Disabled");
					n[j].firstChild.setAttribute("disabled", "true");
				}
			}
		}
		if (!checked && !cnt) {
			for (var i = 0; i < color.length; i++) {
				var n = document.getElementsByClassName(color[i]);
				for (var j = 0; j < n.length; j++) {
					Element.removeClassName(n[j], color[i] + "Disabled");
					n[j].firstChild.removeAttribute("disabled");
				}
			}
		}
		if (cnt) 
			$(className).firstChild.checked = true;
	},

	clear: function() {
		var url = document.location.protocol + "//" + window.location.host + '/item/tagclear';
		new Ajax.Request(url, {
		method:"get",
		asynchronous:false,
		onFailure : function(Response) {
			alert("ajax request failure.");
		},
		onException : function (Response) {
			alert("ajax request exception.");
		},
		onComplete : function (Response) {
			//alert("ajax request complete.");
		}
		});		
	},
	
	get: function() {
		//xhr
		var url = document.location.protocol + "//" + window.location.host + "/item/tagget";
		new Ajax.Request(url, {
		method:"get",
		asynchronous:true,
		onFailure : function(Response) {
			alert("ajax request failure.");
		},
		onException : function (Response) {
			alert("ajax request exception.");
		},
		onComplete : (function(co) {
			return function (Response, json) {
				if (json) {
					for (tag1 in json) {
						if (json[tag1].length) {
							for (var i = 0; i < json[tag1].length; i++) {
								var id = "tag_" + tag1 + "_" + json[tag1][i];
								if($(id)) {
									$(id).checked = true;
								}
							}
						}
					}
				}
				if($("tag_1_1").checked)
					co.setCheckboxStatus("Blue", true);
				else if ($("tag_1_2").checked)
					co.setCheckboxStatus("Red", true);
				else if ($("tag_1_3").checked)
					co.setCheckboxStatus("Gray", true);
					
				
				if ($("tag_5_60").checked)
					$("tag_5_60").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedWHITE";
				if ($("tag_5_61").checked)
					$("tag_5_61").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedBLACK";
				if ($("tag_5_62").checked)
					$("tag_5_62").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedRED";
				if ($("tag_5_63").checked)
				$("tag_5_63").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedORANGE";
				if ($("tag_5_64").checked)
					$("tag_5_64").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedBROWN";
				if ($("tag_5_65").checked)
					$("tag_5_65").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedIVORY";
				if ($("tag_5_66").checked)
					$("tag_5_66").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedYELLOW";
				if ($("tag_5_67").checked)
					$("tag_5_67").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedGREEN";
				if ($("tag_5_68").checked)
					$("tag_5_68").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedBLUE";
				if ($("tag_5_69").checked)
					$("tag_5_69").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedPURPLE";
				if ($("tag_5_70").checked)
					$("tag_5_70").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedPINK";
				if ($("tag_5_71").checked)
					$("tag_5_71").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedSILVER";
				if ($("tag_5_72").checked)
					$("tag_5_72").parentNode.getElementsByTagName("a")[0].className = "CheckboxCheckedGOLD";
				
					
					
	 		}
		})(this)
		});
	},

	set: function() {
		//json
		var n = document.getElementsByName("tags");
		if (n.length) {
			var json = {};
			for (var i = 0; i < n.length; i++) {
				var key = String(n[i].id.match(/(\d)_\d/)[1]);
				if (!(key in json)) {
					json[key] = [];
				}
				if (n[i].checked) {
					json[key].push(n[i].value);
				}
			}
		}
		alert("tag=" + Object.toJSON(json));
		//xhr
		var url = document.location.protocol + "//" + window.location.host + '/item/tagset';
		alert(url);
		new Ajax.Request(url, {
		parameters: "tag="+Object.toJSON(json),
		method:"get",
		asynchronous:false,
		onFailure : function(Response) {
			alert("ajax request failure.");
		},
		onException : function (Response) {
			alert("set:ajax request exception.");
		},
		onComplete : (function(c) {
			return function (Response) {
				if (Response.responseText == "true") {
					DisabledWindowSearch();
					var url = document.location.protocol + "//" + window.location.host + '/item/list';
					window.location.href = url;
				} else {
					var o = $("slider1").cumulativeOffset();
					var d = $("slider1").getDimensions();
					var n = document.createElement("div");
					n.innerHTML = "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>Sorry... No item.<br/><br/>検索されたアイテムは登録されておりません。<br/>申し訳ございませんが、検索項目を変更して再度検索をお願いします。";
					Element.setStyle(n, {
					position:"absolute",
					left:o[0] + "px",
					top:o[1] + "px",
					width:d.width + "px",
					height:d.height + "px",
					zIndex:1000,
					color:"white",
					fontWeight:"bold",
					backgroundColor:"black",
					textAlign:"center",
					fontSize:"12px"
					});
					new Effect.Opacity(n, { from:0.5, to:0.9 });
					Event.observe(n, "click", function() {
						this.parentNode.removeChild(this);
					}, false);
					Event.observe(n, "mouseout", function() {
						this.parentNode.removeChild(this);
					}, false);
					document.body.appendChild(n);
				}
			}
		})(this)
		});
	}
}