var tk = { };

function tk_getObj(id) { return document.getElementById(id); }

tk.animate = function() {
	return {
		test:function(testStr) {
			alert("Testing: " + testStr);
		},
		shrink:function(id, rate) {
			if (isNaN(rate) || rate <= 0) { rate = 10; }
			while (rate >= 1) { rate = rate / 10; }
			var obj = tk_getObj(id);
			if(obj.growT) { window.clearInterval(obj.growT); }
			obj.startHeight = obj.style.height;
			//alert("StartH: " + obj.startHeight);
			obj.startOverflow = obj.style.overflow;
			obj.style.overflow = 'hidden';
			obj.startDisplay = obj.style.display;
			obj.shrinkT = window.setInterval("tk.animate.shrinkTimer('" + id + "', " + rate + ")", 1);
		},
		shrinkTimer:function(id, rate) {
			var obj = tk_getObj(id, rate);
			try {
				var h = obj.style.height.substr(0, obj.style.height.indexOf("px"));
				if (!h) { h = obj.offsetHeight; }
				if (h > 0) {
					var c = Math.floor(h - (h * rate));
					obj.style.height = (c < h) ? c + "px" : "0px";
				} else {
					window.clearInterval(obj.shrinkT);
					obj.style.display = 'none';
					obj.style.height = "0px";
				}
			} catch (ex) {
				window.clearInterval(obj.shrinkT);
			}
		},
		grow:function(id, target, rate) {  //grow in increments of 50 or something until it gets big enought to decelerate
			if (isNaN(rate) || rate <= 0) { rate = 10; }
			while (rate >= 1) { rate = rate / 10; }
			var obj = tk_getObj(id);
			if(obj.shrinkT) { window.clearInterval(obj.shrinkT); }
			if(obj.startDisplay) { obj.style.display = obj.startDisplay; } else { obj.style.display = ''; }
			if (obj.style.height == "") { obj.style.height = "0px"; }
			obj.style.overflow = 'hidden';
			if (!isNaN(target) && target != '') {
				obj.growT = window.setInterval("tk.animate.growTimer('" + id + "', " + target + ", " + rate + ")", 1);
			} else {
				obj.style.height = target;
			}
		},
		growTimer:function(id, target, rate) {
			var obj = tk_getObj(id);
			try {
				var h = obj.style.height.substr(0, obj.style.height.indexOf("px"));
				if (parseInt(h) < target) {
					obj.style.height = Math.ceil(parseInt(h) + ((target - h) * rate)) + "px";
				} else {
					//obj.style.height = target + "px";
					obj.style.height = obj.startHeight;
					window.clearInterval(obj.growT);
					if (obj.startOverflow) {
						obj.style.overflow = obj.startOverflow;
						obj.startOverflow = null;
					}
				}
			} catch (ex) {
				window.clearInterval(obj.growT);
			}
		},
		show:function(id) {
			tk_getObj(id).style.display = '';
		},
		hide:function(id) {
			tk_getObj(id).style.display = 'none';
		},
		popup:function() {
			var bg = document.createElement('div');
			bg.style.backgroundcolor = "#333333";
			bg.style.width = document.style.width;
			bg.style.height = document.style.height;
		}
	};
}();

tk.assign = function() {
	return {
		test:function(id, strMsg) {
			tk_getObj(id).onclick = function() { tk.animate.test(strMsg); };
		},
		shrinkNgrow:function(button, targetObj, rate, upBGimage, dnBGimage) {
			if (isNaN(rate) || rate <= 0) { rate = 10; }
			if (rate >= 1) { rate = rate / 100; }
			var tobj = tk_getObj(targetObj);
			var h = tobj.style.height;
			if (!h) { h = tobj.offsetHeight + "px"; tobj.style.height = h; }
			tobj.targetHeight = h.substr(0, h.indexOf("px"));
			tk_getObj(button).onclick = function() {
				var d = new Date();
				d.setDate(d.getDate() + 7);
				if (this.shrink != true) {
					tk.SetCookie(this.id, 1, d);
					try { if(upBGimage != null) { this.style.backgroundImage = upBGimage; } } catch(e) { alert(e); }
					tk.animate.shrink(targetObj, rate);
					this.shrink = true;
				} else {
					tk.SetCookie(this.id, 0, d);
					try { if(dnBGimage != null) { this.style.backgroundImage = dnBGimage; } } catch(e) { alert(e); }
					tk.animate.grow(targetObj, tobj.targetHeight, rate);
					this.shrink = false;
				}
			};
			if(tk.GetCookie(button) == 1) {
				var sd = tk_getObj(targetObj).style.display;
				tk_getObj(targetObj).style.display = 'none';
				tk_getObj(button).onclick();
				tk_getObj(targetObj).startDisplay = sd;
			}
			//if (collapse) { tobj.style.display = 'none'; }
		},
		showNhide:function(button, targetObj) {
			tk_getObj(button).onclick = function() {
				obj = tk_getObj(targetObj);
				if (obj.collapsed != true) {
					obj.collapsed = true;
					tk.animate.hide(targetObj);
				} else {
					obj.collapsed = false;
					tk.animate.show(targetObj);
				}
			};
		}
	};
}();

//tk.trim = function(inString) { inString = inString.replace(/\s*/, "").replace(/\s*$/, ""); };

tk.SetCookie = function(Name, value, expireDate) {
	document.cookie = Name.trim() + "="+escape(value)+(expireDate ? ";expires="+expireDate.toUTCString() : "")+";domain="+window.location.host+";path=/";
};

tk.GetCookie = function(Name) {
	var Cookies = document.cookie.split(";");
	for(c=0; c < Cookies.length; c++) {
		if (Cookies[c].indexOf('=') > -1) {
			var vals = Cookies[c].split("=");
			if(vals[0].trim() == Name.trim()) { return unescape(vals[1].trim()); }
		}
	}
	return false;
};

/*tk.ClearCookies = function() {
	var d = new Date();
	d.setMonth(d.getMonth() - 1);
	var Cookies = document.cookie.split(";");
	for(c=0; c < Cookies.length; c++) {
		var vals = Cookies[c].split("=");
		document.cookie = vals[0].trim() + "=null; expires="+d.toUTCString();
	}
};*/

tk.addLoadEvent = function(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') { window.onload = func; }
    else {
        window.onload = function() {
            try {
                if (oldonload) { oldonload(); }
                func();
            } catch (e) { }
        };
    }
};

String.prototype.trim = function() { return this.replace(/\s*/, "").replace(/\s*$/, ""); };
