addNamespace("KY.Web.DivComboBox");

KY.Web.DivComboBox = Class.create();

Object.extend(KY.Web.DivComboBox.prototype, {
	array: null,
	objXML: null,
	url: null,
	timer: null,
	count: 10,
	pos: -1,
	children: null,
	constID: "",
	constID2: "",
	masterXML: null,
	timer: -1,
	isTimerActive: 0,
	changeHighlight: function() {
		var l = this.getLength();
		for(var i=0; i<this.count && i<l; i++) {
			if(i == this.pos)
				this.div.childNodes[i].className = "item selected";
			else
				this.div.childNodes[i].className = "item"
		}
	},
	getItem: function(idx) {
		if(idx >= 0 && idx < this.array.length)
			return this.array[idx];
		return null;
	},
	getLength: function() {
		if(this.array != null) {
			return this.array.length;
		}
		return 0;
	},
	getDisplay: function(item) {
		return item.ComboText;
	},
	getValue: function(item) {
		return item.ComboValue;
	},
	getValue2: function(item) {
		return item.ComboValue2;
	},
	hide: function() {
		this.div.style.display = "none";
	},
	release: function() {
		this.timer = -1;
		this.hide();
	},
	show: function(objFavBtn, id, id2) {
		if (objFavBtn) {
			var r = MS.Position.getBounds(objFavBtn);
			r.top += r.height;
			MS.Position.setLocation(this.div, r);
			if (this.div.style.display == "inline" && this.constID == id) {
				this.hide();
				this.constID = id;
				this.constID2 = id2;
			} else {
				this.constID = id;
				this.constID2 = id2;
				this.changeHighlight();
				this.div.style.display = "inline";
			}
		} else {
			if (this.div.style.display == "inline") {
				this.hide();
			}
			else {
				this.changeHighlight();
				this.div.style.display = "inline";
			}
		}

	},
	select: function() {
		if(this.pos > -1) {
			var v = this.getValue(this.getItem(this.pos));
			//alert(v + "  " + this.constID);
			if (this.constID != "" && v != "---------------") {
				if (window.XMLHttpRequest) {
					this.objXML = new XMLHttpRequest();
				}
				else if (window.ActiveXObject) {
					try 
					{ 
						this.objXML = new ActiveXObject("Microsoft.XMLHTTP"); 
					}
					catch (err) 
					{ 
						return false; 
					}
				}

				if (this.objXML) {
					try
					{
						//alert(window.location.protocol + "//" + window.location.hostname + "/servlet/globalfunction.asp?t=favorichngDoIt&action=" + v + "&item=" + this.constID + "|" + this.constID2);
						this.objXML.open("GET", window.location.protocol + "//" + window.location.hostname + "/servlet/globalfunction.asp?t=favorichngDoIt&action=" + v + "&item=" + this.constID + "|" + this.constID2, false);
						this.objXML.setRequestHeader("Accept-Charset", "iso-8859-9");
						this.objXML.setRequestHeader("Accept-Language", "tr");
						this.objXML.send(null);	

						if(this.objXML.readyState == 4)
						{
							if ( this.objXML.responseText.length != 0 )
							{
								alert(this.objXML.responseText);
								if (v.startsWith("1|")) {
									if (this.objXML.responseText == "Ürün başarı ile taşındı.") {
										$(this.url+"Item_"+this.constID).style.visibility = "hidden";
										$(this.url+"Item_"+this.constID).style.display = "none";
									}
								}
							}
						}
					}
					catch (err)
					{

					}
				}		
			} else {
				window.location.href = v;
			}
		}
		this.hide();	
	},
	onmouseover: function(e) {
		if (this.timer != -1) { clearTimeout(this.timer); }
		this.pos = MS.getTarget(e).idx;
		this.changeHighlight();
	},
	onmouseout: function(e) {
		if (this.timer == -1) {
			this.timer = setTimeout(this.release.bind(this),1000);
		}
	},
	onclick: function(e) {
		this.pos = MS.getTarget(e).idx;
		this.changeHighlight();
		this.select();
	},
	onparentchange: function(v) {
		this.ele.value = "";
	},
	updateList: function() {
		var l = this.getLength();
		for(var i=0; i<l && i<this.count; i++) {
			this.div.childNodes[i].innerHTML = this.getDisplay(this.getItem(i));
			this.div.childNodes[i].style.display = "block";
		}
		if(this.pos > l) this.pos = this.getLength() -1;
		
		for(var i=l; i<this.count; i++) {
			this.div.childNodes[i].style.display = "none";
		}
		if(this.pos > l -1) {
			this.pos = l -1;
		}

		if(this.div.childNodes[0].clientHeight == 0) {
			this.show();
			this.updateList();
			return;
		}

		var h = (l < this.count ? l : this.count) * this.div.childNodes[0].clientHeight;
		
		if(h > 0) {
			this.div.style.height = h + "px";
			this.show();
		} else 
			this.hide();
	},
	getData: function() {
			this.objXML = null;

			if (window.XMLHttpRequest) {
				this.objXML = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) {
				this.objXML = new ActiveXObject("Microsoft.XMLHTTP");
			}	
			
			if (this.objXML) {
				this.objXML.onreadystatechange = this.ondata.bind(this);
			}
			this.objXML.open("GET", window.location.protocol + "//" + window.location.hostname + this.url, true);
			this.objXML.setRequestHeader("Accept-Charset", "iso-8859-9");
			this.objXML.setRequestHeader("Accept-Language", "tr");

			if(!MS.Browser.isIE) {
				this.objXML.setRequestHeader("Connection", "close");
			}
			this.objXML.send(null);
	},
	ondata: function() {
		if(this.objXML.readyState == 4)
		{
			if(this.objXML.status != 200) { return; }
		}
		else 
		{
			return;
		}

		if (this.objXML.responseXML.documentElement == null) { this.hide(); return; }

		a = new Array();
		var doc = this.objXML.responseXML.documentElement;
		var nodes = doc.getElementsByTagName("Result");

		if (nodes.length == 0) { this.hide(); return;  }
		for (i = 0; i < nodes.length ; i++) 
		{
			if (nodes[i] == null) { continue; }
			else 
			{
				var node = nodes[i];
				var obj = new KY.Web.DivComboBoxItem(node.getElementsByTagName("Value2")[0].firstChild.nodeValue, node.getElementsByTagName("Value1")[0].firstChild.nodeValue);
				a[i] = obj;
			}
		}
		
		this.array = a;
		this.updateList();
		this.show();
	},
	loadPreparedXml: function() {
		if (this.masterXML.documentElement == null) { this.hide(); return; }

		a = new Array();
		var doc = this.masterXML.documentElement;
		var nodes = doc.getElementsByTagName("Result");
		if (nodes.length == 0) { this.hide(); return;  }
		for (i = 0; i < nodes.length ; i++) 
		{
			if (nodes[i] == null) { continue; }
			else 
			{
				var node = nodes[i];
				var obj = new KY.Web.DivComboBoxItem(node.getElementsByTagName("Value2")[0].firstChild.nodeValue, node.getElementsByTagName("Value1")[0].firstChild.nodeValue);
				a[i] = obj;
			}
		}
		
		this.array = a;
		this.updateList();
		//this.show();
	},
	onresize: function() {
		var r = MS.Position.getBounds(this.ele);
		this.ele.rect = r;
		r.top += r.height;
		MS.Position.setLocation(this.div, r);
	},
	initialize: function(id, count, url, id2, xmlData) {
		this.ele = $(id);
		this.count = count != null && !isNaN(count) ? count : this.count;
		this.masterXML = xmlData;
		this.constID = id2;
		if (typeof this.constID == "undefined") {
			this.constID = "";
		}
		this.url = url;

		if ($(this.ele.id + "_list")) {
			this.div = $(this.ele.id + "_list");
			this.array = null;
			this.updateList();
			this.hide();
		} else {
			this.div = document.createElement("div");
			document.body.appendChild(this.div);

			this.div.id = this.ele.id + "_list";
			addEvent(this.div, "mouseout", this.onmouseout.bind(this));

		}
		
		this.div.style.display = "none";
		this.div.style.border = "1px solid #557DA5";
		
		var r = MS.Position.getBounds(this.ele);
		this.ele.rect = r;
		r.top += r.height;
		MS.Position.setBounds(this.div, r);

		this.div.style.width = (r.width -2) + "px";

		for(var i=0; i<this.count; i++) {
			var d = document.createElement("div");
			this.div.appendChild(d);

			d.style.width = this.ele.clientWidth;
			d.style.whiteSpace = "nowrap";
			d.style.overflow = "hidden";
			d.className = this.ele.id + "_item";
			d.idx = i;
			
			addEvent(d, "mouseover", this.onmouseover.bind(this));
			addEvent(d, "mouseout", this.onmouseout.bind(this));
			addEvent(d, "click", this.onclick.bind(this));

			MS.setText(d, " ");
		}
		addEvent(window, "resize", this.onresize.bind(this));
		if (xmlData) {
			this.loadPreparedXml();
		} else {
			this.getData();	
		}
		delete r;
	}
}, false);

addNamespace("KY.Web.DivComboBoxItem");

KY.Web.DivComboBoxItem = Class.create();

Object.extend(KY.Web.DivComboBoxItem.prototype, {
	ComboText: "",
	ComboValue: "",
	ComboValue2: "",
	initialize: function(Str,Value,Value2) {
		this.ComboText = Str;
		this.ComboValue = Value;
		this.ComboValue2 = Value2;
	}
}, false);



