addNamespace("KY.Web.TabControl");

KY.Web.TabControl = Class.create();

Object.extend(KY.Web.TabControl.prototype, {
	objXML: null,
	ele: null,
	array: null,
	arrayUrl: null,
	arrayScript: null,
	width: 525,
	height: null,
	count: 0,
	lastId: 1,
	selectedId: 0,
	selectedNum: 0,
	selectedObj: null,
	tabStyle: 2,
	tabSize: 90,
	bgColor: "#C9E0ED",
	onmouseover: function(e) {
		var obj = MS.getTarget(e);
		if (obj.tagName != "TD") {
			while (obj.tagName != "TD")
			{
				obj = obj.parentElement;
			}
		}

		if (obj.id != this.selectedId) {
			obj.style.backgroundImage = "url('/resim/tab/" + this.tabStyle + "/tab_1.gif')";
		}
	},
	onclick: function(e) {
		var obj = e?MS.getTarget(e):this.selectedObj;
		var i = 0;
		var sHttpMethod = "GET";
		var sPostData = "";
		if (obj.id == this.selectedId && !this.selectedObj) { return; }

		$(this.selectedId).style.backgroundImage = "url('/resim/tab/" + this.tabStyle + "/tab_0.gif')"; 
		$(this.selectedId).style.width = eval(eval(this.tabSize) + 2) + "px";
		$(this.selectedId).width = eval(eval(this.tabSize) + 2) + "px";
		$(this.selectedId).style.color = "#666666";
		i = this.selectedId.replace(this.ele.id + "_tab_", "");
		$(this.ele.id + "_tab_alt_" + i).backgrounColor = "";
		$(this.ele.id + "_tab_alt_" + i).style.backgroundImage = "url('/resim/" + this.tabStyle + "/dis_cerceve_ust.gif')"; 

		obj.style.backgroundImage = "url('/resim/tab/" + this.tabStyle + "/tab_2.gif')"; 
		obj.style.color = "#000000";
		obj.style.width = this.tabSize + "px";
		obj.width = this.tabSize + "px";

		i = obj.id.replace(this.ele.id + "_tab_", "");
		if ($(this.ele.id + "_tab_" + (eval(i)-1))) {
			$(this.ele.id + "_tab_" + (eval(i)-1)).width = eval(eval(this.tabSize) + 1) + "px";
			$(this.ele.id + "_tab_" + (eval(i)-1)).style.width = eval(eval(this.tabSize) + 1) + "px";
		}
		if (eval(i)+1 <= this.count) {
			$(this.ele.id + "_tab_" + (eval(i)+1)).width = eval(eval(this.tabSize) + 1) + "px";
			$(this.ele.id + "_tab_" + (eval(i)+1)).style.width = eval(eval(this.tabSize) + 1) + "px";
		}
		$(this.ele.id + "_tab_alt_" + i).style.backgroundColor = this.bgColor;
		$(this.ele.id + "_tab_alt_" + i).style.backgroundImage = "";

		$(this.ele.id + "_tab_Content").innerHTML = "<table width=\"100%\" heigth=\"100%\" align=\"center\" valign=\"middle\"><tr><td width=\"100%\" heigth=\"100%\" align=\"center\" valign=\"middle\">&nbsp;</td></tr><tr><td width=\"100%\" heigth=\"100%\" align=\"center\" valign=\"middle\"><img alt=\"Yükleniyor\" src=\"/resim/ajax-loader-2.gif\" border=\"0\"></td></tr><tr><td width=\"100%\" heigth=\"100%\" align=\"center\" valign=\"middle\">Yükleniyor</td></tr><tr><td width=\"100%\" heigth=\"100%\" align=\"center\" valign=\"middle\">&nbsp;</td></tr></table>";
		if(this.height!=null) this.setHeight(this.height);
		this.selectedId = obj.id;

		this.objXML = null;
		if(this.arrayUrl[eval(i)-1]!=''){
			if (window.XMLHttpRequest) {
				this.objXML = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) {
				this.objXML = new ActiveXObject("Microsoft.XMLHTTP");
			}	
			
			if (this.objXML) {
				this.objXML.onreadystatechange = this.ongetUrl.bind(this);
			}

			this.selectedNum = eval(i);
			if (this.arrayUrl[eval(i)-1].substring(0,4) == "POST") { 
				sHttpMethod = "POST"; 
				sPostData = this.arrayUrl[eval(i)-1].substring(4,this.arrayUrl[eval(i)-1].indexOf("#"));
				this.objXML.open(sHttpMethod, window.location.protocol + "//" + window.location.hostname + "/" + this.arrayUrl[eval(i)-1].substring(this.arrayUrl[eval(i)-1].indexOf("#")+1), true);
				this.objXML.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			} else {
				this.objXML.open(sHttpMethod, window.location.protocol + "//" + window.location.hostname + "/" + this.arrayUrl[eval(i)-1], true);
			}

			this.objXML.setRequestHeader("Accept-Charset", "iso-8859-9");
			this.objXML.setRequestHeader("Accept-Language", "tr");

			if(!MS.Browser.isIE) {
				this.objXML.setRequestHeader("Connection", "close");
			}
			if (sHttpMethod == "POST") {
				this.objXML.send(sPostData);
				if (this.objXML.overrideMimeType) { this.objXML.overrideMimeType('text/html; charset=iso-8859-9'); }
			} else {
				this.objXML.send(null);
			}
		} else {
			this.selectedNum = eval(i);
			this.ongetUrl();
		}
	},
	onmouseout: function(e) {
		var obj = MS.getTarget(e);
		if (obj.tagName != "TD") {
			while (obj.tagName != "TD")
			{
				obj = obj.parentElement;
			}
		}
		if (obj.id != this.selectedId) {
			obj.style.backgroundImage = "url('/resim/tab/" + this.tabStyle + "/tab_0.gif')";
		}
	},
	ongetUrl: function() {
		if(this.objXML){
			if(this.objXML.readyState == 4)
			{
				if(this.objXML.status != 200) { return; }
			}
			else 
			{
				return;
			}
			if (this.objXML.responseText == null) { return; }

			$(this.ele.id + "_tab_Content").innerHTML = this.objXML.responseText;		
			if(this.height!=null) this.setHeight(this.height);
		}
		if (this.arrayScript[eval(this.selectedNum)-1] != "") {
			eval(this.arrayScript[eval(this.selectedNum)-1])
		}
	},
	containerDiv:function(){
		return $(this.ele.id + "_tab_Content");
	},
	setContent:function(shtml){
		$(this.ele.id + "_tab_Content").innerHTML = shtml;
	},
	select:function(tabIndex){
		if(tabIndex>=this.array.length || tabIndex<=0) return;
		this.selectedObj = $(this.ele.id + "_tab_" + tabIndex);
		this.onclick();
	},
	addTab: function(header, url, script) {
		this.array.push(header);
		this.arrayUrl.push(url);
		this.arrayScript.push(script);
		addEvent($(this.ele.id + "_tab_" + this.lastId), "mouseover", this.onmouseover.bind(this));
		addEvent($(this.ele.id + "_tab_" + this.lastId), "mouseout", this.onmouseout.bind(this));
		if (url != "" || script != "") {
			addEvent($(this.ele.id + "_tab_" + this.lastId), "click", this.onclick.bind(this));
		}
		this.lastId++;
	},
	setHeight:function(h){
		this.height = h;
		this.containerDiv().style.height = h + 'px';
	},
	initialize: function(id, count, width, selected, tabStyle, tabSize, bgColor) {
		var iSelected = 1;
		if (selected) {
			iSelected = selected;
		}

		this.ele = $(id);
		this.count = count;
		this.width = width;
		this.array = Array();
		this.arrayUrl = Array();
		this.arrayScript = Array();
		this.selectedId = id + "_tab_" + iSelected;
		if (tabStyle) { this.tabStyle = tabStyle; }
		if (tabSize) { this.tabSize = tabSize; }
		if (bgColor) { this.bgColor = bgColor; }
	}
}, false);



