function Trim(str) {return str.replace(/(^\s*)|(\s*$)/g, "");  }
function E(id) {return document.getElementById(id);}
function V(id) {return document.getElementById(id).value;}


var tiph = {};
/*------------------------------------TIPH MESSAGES---------------------------------------*/
tiph.messages = {
	system_error : "System error, please try again!",
	need_login : "Please login first!",
	login_fail : "Login failed, please check your user name or password",
	change_password_ok : "Your password has been changed successfully!",
	change_password_fail : "Wrong password, please try again!",
	save_profile_ok : "Your profile has been saved successfully!",
	save_profile_fail : "Failed to save profile, please try again!",
	bad_avatar_file : "Invalid image file",
	save_avatar_ok : "Your avatar had been saved successfully"
};


/*------------------------------------TIPH COMMON---------------------------------------*/
tiph.common = {
	fillDefault : function(arr, defaults) {
		if (arr == null) arr = {};
		for (var k in defaults) {
			if (arr[k] == null) arr[k] = defaults[k];
		}
		return arr;
	}
};
/*------------------------------------TIPH SESSION---------------------------------------*/
tiph.session = {
	change : function(id) {
		tiph.ajax.request("sess/change/" + id, null, function(ret) {
			var user = tiph.user.get_user();
			if (user == null && ret == null) return;
			if (user != null && ret != null && ret.id == user.id) return;
			if (ret) {
				for(var k in tiph.user._login_hooks) {
					tiph.user._login_hooks[k]();
				}
			} else {
				for(var k in tiph.user._logout_hooks) {
					tiph.user._logout_hooks[k]();
				}
			}
		},
		function(no, msg) {}
		);
		//var back = location.href;
		//location.href = tiph.ajax.context_root + "sess/change/" + id + "?back=" + tiph.ajax.encoder.encode(back);
	},
	check : function(url) {
		document.write("<script language=javascript src=\""+url+"\"></s" + "cript>");
	}
};
/*------------------------------------TIPH AJAX---------------------------------------*/
tiph.ajax = {
	context_root : "/",
	create : function() {
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {}
		try { return new XMLHttpRequest();} catch(e) {}
		return null;
	},

	defaultErrorHandler : function(no, msg) {
		var win = window.top ? window.top : window;
		if (no == 2) {
			win.tiph.ui.alert(tiph.messages.need_login);
			win.location.href = tiph.ajax.context_root;
			return;
		}
		win.tiph.ui.alert(msg);
	},

	request : function(url, parameter, callback, errorhandler) {
		var p = "";
		if (parameter) {
			for(var key in parameter) {
				if (p != "") p += "&";
				var value = parameter[key];
				if (value ==null) {
					p += tiph.ajax.encoder.encode(key) + "=";
				} else {
					p += tiph.ajax.encoder.encode(key) + "=" + tiph.ajax.encoder.encode("" + parameter[key]);
				}
			}
		}
		var ajax = tiph.ajax.create();
		if (ajax == null) return;

		ajax.onreadystatechange = function (){
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					try {
						eval("var result=" + ajax.responseText + ";");
					} catch (ex) {
						if (errorhandler) errorhandler(-1, tiph.messages .system_error);
						else tiph.ajax.defaultErrorHandler(-1, tiph.messages .system_error);
						return;
					}
					if (result.no != 0) {
						if (errorhandler) errorhandler(result.no, result.msg);
						else tiph.ajax.defaultErrorHandler(result.no, result.msg);
					} else {
						if (callback) callback(result.data);
					}
				} else {
					if (errorhandler) errorhandler(-1, tiph.messages .system_error);
					else tiph.ajax.defaultErrorHandler(-1, tiph.messages .system_error);
					return;
				}
			}
		};
		if (url.substring(0, 1) != "/") url = tiph.ajax.context_root + url;
		if (url.indexOf("?") >= 0) {
			url += "&_ajax=";
		} else {
			url += "?_ajax=";
		}
		ajax.open("POST", url, true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(p);
	}
};

tiph.ajax.encoder = {
	__hex_chars : "0123456789ABCDEF",
	_ok_uri_chars : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-",

	__utf8 : function(wide) {
		var c, s;
		var enc = "";
		var i = 0;
		while(i < wide.length) {
			c= wide.charCodeAt(i++);
			if (c>=0xDC00 && c<0xE000) continue;
			if (c>=0xD800 && c<0xDC00) {
				if (i>=wide.length) continue;
				s= wide.charCodeAt(i++);
				if (s<0xDC00 || c>=0xDE00) continue;
				c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
			}
			if (c<0x80) enc += String.fromCharCode(c);
			else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
			else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
			else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
		}
		return enc;
	},

	__tohex : function(n) {
		return tiph.ajax.encoder.__hex_chars.charAt(n>>4) + tiph.ajax.encoder.__hex_chars.charAt(n & 0xF);
	},

	encode : function(s) {
		s = s + "";
		var s = tiph.ajax.encoder.__utf8(s);
		var c;
		var enc = "";
		for (var i= 0; i < s.length; ++i) {
			if (tiph.ajax.encoder._ok_uri_chars.indexOf(s.charAt(i))==-1)
				enc += "%"+tiph.ajax.encoder.__tohex(s.charCodeAt(i));
			else
				enc += s.charAt(i);
		}
		return enc;
	}
};

/*------------------------------------TIPH USER-------------------------------------*/
tiph.user = {
	_login_hooks : {},
	_login_hooks_index : 0,
	_logout_hooks : {},
	_logout_hooks_index : 0,
	attachLogin : function(func) {
		++ tiph.user._login_hooks_index;
		tiph.user._login_hooks[tiph.user._login_hooks_index] = func;
		return tiph.user._login_hooks_index;
	},

	dettachLogin: function(handle) {
		delete tiph.user._login_hooks[tiph.user._login_hooks_index];
	},

	attachLogout : function(func) {
		++ tiph.user._logout_hooks_index;
		tiph.user._logout_hooks[tiph.user._logout_hooks_index] = func;
		return tiph.user._logout_hooks_index;
	},

	dettachLogout: function(handle) {
		delete tiph.user._logout_hooks[tiph.user._logout_hooks_index];
	},

	_onlogin : function(ret) {
		var user = tiph.user.get_user();
		if (user) {
			for(var k in tiph.user._login_hooks) {
				tiph.user._login_hooks[k]();
		 	}
			return true;
		} else {
			alert(tiph.messages.login_fail);
			return false;
		}
	},

	_onlogout : function() {
		for(var k in tiph.user._logout_hooks) {
			tiph.user._logout_hooks[k]();
		}
	},

	_onregister : function(user) {
		for(var k in tiph.user._login_hooks) {
			tiph.user._login_hooks[k]();
		}
	},

	register : function(name, email, password) {
		tiph.ajax.request("user/register", {"name":name, "email":email, "code":password}, tiph.user._onregister);
	},

	login : function(name, password) {
		tiph.ajax.request("user/login", {"name":name, "code":password}, tiph.user._onlogin);
	},

	logout : function() {
		tiph.ajax.request("user/logout", null, tiph.user._onlogout);
	},
	
	change_password : function(oldpsw, newpsw) {
		tiph.ajax.request("setting/changeCode", {"old":oldpsw, "new":newpsw}, function(ret) {
			if (ret == 1) {
				tiph.ui.alert(tiph.messages.change_password_ok);
			} else {
				tiph.ui.alert(tiph.messages.change_password_fail);
			}
		});
	},

	saveProfile : function(data) {
		tiph.ajax.request("setting/saveProfile", data, function(ret) {
			tiph.ui.alert(ret == "1" ? tiph.messages.save_profile_ok :tiph.messages.save_profile_fail);
		});
	},
	
	get_user : function() {
		var id = tiph.cookie.get("user_id");
		var name = tiph.cookie.get("user_name");
		var avatar = tiph.cookie.get("user_avatar");
		var cnt = tiph.cookie.get("user_newmail_cnt");
		if (id && name && avatar) {
			return {id:id, name:name, avatar:avatar, newmail_cnt:cnt};
		} else {
			return null;
		}
	},

	friend : {
		add : function(id, callback, error) {
			tiph.ajax.request("friend/add/" + id, null, callback, error);
		}
	}
};
/*------------------------------------TIPH UI---------------------------------------*/

tiph.ui = {
	addCss : function(ele, name) {
		var css = ele.className;
		if (css) {
			var arr = css.split(" ");
			for (var i in arr) {
				if (arr[i] == name) return;
			}
		}
		if (css == "") ele.className = name;
		else ele.className = css + " " + name;
	},
	removeCss : function(ele, name) {
		var css = ele.className;
		if (!css) return;

		var find = false;
		var arr = css.split(" ");
		var css = "";
		for (var i in arr) {
			if (arr[i] == "") continue;
			if (arr[i] == name) {
				find = true;
			} else {
				if (css != "") css += " ";
				css += name;
			}
		}
		if (find) {
			ele.className = css;
		}
	},
	getPosition : function(el) {
		var ua = navigator.userAgent.toLowerCase();
		var isOpera = (ua.indexOf('opera') != -1);
		var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
		if(el.parentNode === null || el.style.display == 'none') return false;

		var parent = null;
		var pos;
		var box;

		if(el.getBoundingClientRect) {//IE
			box = el.getBoundingClientRect();
			var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
			var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
			return {x:box.left + scrollLeft, y:box.top + scrollTop};
		} else if(document.getBoxObjectFor) { // gecko
			box = document.getBoxObjectFor(el);	
			var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
			var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
			pos = [box.x - borderLeft, box.y - borderTop];
		} else { // safari & opera
			pos = [el.offsetLeft, el.offsetTop];
			parent = el.offsetParent;
			if (parent != el) {
				while (parent) {
					pos[0] += parent.offsetLeft;
					pos[1] += parent.offsetTop;
					parent = parent.offsetParent;
				}
			}
			if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) {
				pos[0] -= document.body.offsetLeft;
				pos[1] -= document.body.offsetTop;
			}
		}

		if (el.parentNode) { parent = el.parentNode; }
		else { parent = null; }

		while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors
			pos[0] -= parent.scrollLeft;
			pos[1] -= parent.scrollTop;
			if (parent.parentNode) { parent = parent.parentNode; }
			else { parent = null; }
		}
		return {x:pos[0], y:pos[1]};
	},

	setStyle : function(ele, styles) {
		for(var name in styles) eval("ele.style."+name+" = \"" + styles[name] + "\"");
	},
	
	setAlpha : function(ele, alpha) {
		ele.style.opacity = alpha / 100;
		try {ele.style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity='+alpha+')';} catch(e) {}
	},

	attachEvent : function(ele, event, handler) {
		if (ele.attachEvent) {
			ele.attachEvent("on" + event, handler);  
		} else if (ele.addEventListener) {
			ele.addEventListener(event, handler, false);
		}
	},

	dettachEvent : function(ele, event, handler) {
		if (ele.dettachEvent) {
			ele.dettachEvent("on" + event, handler);  
		} else if (ele.removeEventListener) {
			ele.removeEventListener(event, handler, true);
		}
	},

	alert : function(html, callback) {
		var div = E("tiph_alert");
		if (!div) {
			div = document.createElement("div");
			div.id = "tiph_alert";
			document.body.appendChild(div);
			div._top = -div.clientHeight;
			div.onmouseover = function() {
				this.waiting_cnt = -1;
			}
			div.onmouseout = function() {
				this.waiting_cnt = 0;
			}
		}
		
		if (div.intervalHandler) clearInterval(div.intervalHandler);
		div.waiting_cnt = 0;
		div.closing = false;

		div.intervalHandler = setInterval(function() {

			var end = 40;

			//var left = document.body.scrollLeft + Math.max(document.body.clientWidth, document.documentElement.clientWidth) - div.clientWidth - 40;
			var left = 40;
			div.style.left = left + "px";
			var top = div._top;
			if (top == end) {
				if (div.waiting_cnt == -1) return;
				++div.waiting_cnt;
				if (div.waiting_cnt < 200) {
					return;
				}
			}
			var height = div.clientHeight;
			var step = 5;
			if (div.closing) {
				top -= step;
				if (top <= -height) {
					top = -height;
					clearInterval(div.intervalHandler);
					div.intervalHandler = null;
				}
			} else {
				top += step;
				if (top >= end) {
					top = end;
					div.closing = true;
				}
			}
			div._top = top;
			var h =  Math.max(document.body.clientHeight, document.documentElement.clientHeight);
			div.style.top = (h - top - height) + "px";
			if (div.intervalHandler == null) {
				div.style.display = "none";
				if (callback) callback();
			}
		}, 10);


		div.innerHTML = "<div class=content>" + html + "</div>";
		div.style.display = "";
	}
};

/*------------------------------------TIPH EFFECT---------------------------------------*/
tiph.ui.effect = {
	alphaFadeIn : function(ele, args) {
		args = tiph.common.fillDefault(args, {start:0, end:100, step:10, interval:50});
		if (ele._alpha_effect) {
			if (ele._alpha_effect.timerHandler) clearTimeout(ele._alpha_effect.timerHandler);
			if (ele._alpha_effect.callback) ele._alpha_effect.callback(ele, ele._alpha_effect.end);
		}
		ele._alpha_effect = args;
		tiph.ui.setAlpha(ele, args.start);
		if (ele._alpha_effect.callback) ele._alpha_effect.callback(ele, args.start);
		ele._alpha_effect.timerHandler = setTimeout(function() {tiph.ui.effect._alphaFadeIn(ele);}, args.interval);
	},

	_alphaFadeIn : function(ele) {
		if (!ele) return;
		var step = ele._alpha_effect.step;
		if (Math.abs(ele._alpha_effect.start - ele._alpha_effect.end) <= step) {
			tiph.ui.setAlpha(ele, ele._alpha_effect.end);
			if (ele._alpha_effect.callback) ele._alpha_effect.callback(ele, ele._alpha_effect.end);
			ele._alpha_effect = null;
		} else {
			if (ele._alpha_effect.start < ele._alpha_effect.end) {
				ele._alpha_effect.start += ele._alpha_effect.step
			} else {
				ele._alpha_effect.start -= ele._alpha_effect.step
			}
			tiph.ui.setAlpha(ele, ele._alpha_effect.start);
			if (ele._alpha_effect.callback) ele._alpha_effect.callback(ele, ele._alpha_effect.start);
			ele._alpha_effect.timerHandler = setTimeout(function() {tiph.ui.effect._alphaFadeIn(ele);}, ele._alpha_effect.interval);
		}
	},

	keepPosition : function(ele, transform) {
		if (ele._keep_position_effect) {
			tiph.ui.dettachEvent(window, "size", ele._keep_position_effect.relocate);
			tiph.ui.dettachEvent(window, "scroll", ele._keep_position_effect.relocate);
		}
		ele._keep_position_effect = {
			relocate : function() {
				if (ele.style.display != "") return;
				var w = ele.clientWidth;
				var h = ele.clientHeight;
				var pos = transform(w, h);
				tiph.ui.setStyle(ele, {
					"left" : pos.left + "px",
					"top" : pos.top + "px"
				});
			}
		}
		tiph.ui.attachEvent(window, "resize", ele._keep_position_effect.relocate);
		tiph.ui.attachEvent(window, "scroll", ele._keep_position_effect.relocate);
		setTimeout(ele._keep_position_effect.relocate, 10);
	},
	
	relocate : function(ele) {
		if (ele && ele._keep_position_effect && ele._keep_position_effect.relocate)
			ele._keep_position_effect.relocate();
	}
};

/*------------------------------------TIPH COMPONENT---------------------------------------*/
tiph.ui.popup = {
	_popup_mask : null,
	_popup_container : null,
	
	openPage : function(url, args) {
		var html = "<table id=tiph_popup_border><tr><td>";
		if (args && args.title) {
			html += "<div id=tiph_popup_title><a id=tiph_popup_close href='#' onclick='tiph.ui.popup.close();return false;'></a>" + args.title + "</div>";
		}
		html += "<iframe id=_tiph_popup_iframe src=\"" + tiph.ajax.context_root + url + "\" frameborder=no style=\"background:#fff;width:400px;height:300px;\"></iframe></td></tr></table>";
		tiph.ui.popup.open(html, args);
	},
	
	resizePage : function(width, height, force) {
		if (force) {
			var div = E("_tiph_popup_iframe");
			if (div) {
				div.style.width = width + "px";
				div.style.height = height + "px";
			}
			tiph.ui.effect.relocate(tiph.ui.popup._popup_container);
		} else {
			tiph.ui.effect.size2fit(E("_tiph_popup_iframe"), {end_w:width, end_h:height, step_callback:function(ele) {tiph.ui.effect.relocate(tiph.ui.popup._popup_container)}});
		}
	},

	open : function(html, args) {
		args = tiph.common.fillDefault(args, {auto_close:true});
		if (tiph.ui.popup._popup_mask == null) {
			tiph.ui.popup._popup_mask = document.createElement("div");
			tiph.ui.popup._popup_mask.style.display="none";
			document.body.appendChild(tiph.ui.popup._popup_mask);
			tiph.ui.setStyle(tiph.ui.popup._popup_mask, {
				"position" : "absolute",
				"width" : "100%",
				"height" : "100%",
				"left" : "0px",
				"top" : "0px",
				"background" : "#000",
				"zIndex" : "1001"
			});
			tiph.ui.popup._popup_mask.style.display="";
			tiph.ui.effect.keepPosition(tiph.ui.popup._popup_mask, function(w, h) {
				return {left: document.body.scrollLeft, top: Math.max(document.body.scrollTop, document.documentElement.scrollTop)};
			});


			tiph.ui.popup._popup_container = document.createElement("div");
			tiph.ui.popup._popup_container.style.display="none";
			document.body.appendChild(tiph.ui.popup._popup_container);
			tiph.ui.setStyle(tiph.ui.popup._popup_container, {
				"position" : "absolute",
				"zIndex" : "1002",
				"left" : "-10000px",
				"top" : "-10000px"
			});
			tiph.ui.popup._popup_container.style.display="";


			tiph.ui.effect.keepPosition(tiph.ui.popup._popup_container, function(w, h) {
				return {left: document.documentElement.scrollLeft + Math.max(0, (document.documentElement.clientWidth - w) / 2), top: Math.max(document.body.scrollTop, document.documentElement.scrollTop) + Math.max(0, (document.documentElement.clientHeight - h) / 2)};
			});

			tiph.ui.attachEvent(document, "keydown", function(event) {
				if (!tiph.ui.popup._popup_mask.isopen) return;
				event = event ? event : window.event;
				if (27 == event.keyCode) {
					tiph.ui.popup.close();
				}
			});
		}
		var objs = document.getElementsByTagName("object");
		for (var i = 0; i < objs.length; ++i) {
			objs[i].__width = objs[i].style.width;
			objs[i].__height = objs[i].style.height;
			objs[i].style.width = "1px";
			objs[i].style.height = "1px";
		}
		var objs = document.getElementsByTagName("embed");
		for (var i = 0; i < objs.length; ++i) {
			objs[i].__width = objs[i].style.width;
			objs[i].__height = objs[i].style.height;
			objs[i].style.width = "1px";
			objs[i].style.height = "1px";
		}
		tiph.ui.popup._popup_mask._popup_args = args;
		tiph.ui.dettachEvent(tiph.ui.popup._popup_mask, "click", tiph.ui.popup.close);
		if (tiph.ui.popup._popup_mask._popup_args.auto_close) {
			tiph.ui.attachEvent(tiph.ui.popup._popup_mask, "click", tiph.ui.popup.close);
		}

		tiph.ui.popup._popup_mask.style.display = "";
		tiph.ui.popup._popup_mask.isopen = true;

//		tiph.ui.popup._popup_mask._overflow = document.body.style.overflow;
//		document.documentElement.style.overflow = "hidden";

		if (args.direct) {
			tiph.ui.setAlpha(tiph.ui.popup._popup_mask, 80);
		} else {
			tiph.ui.effect.alphaFadeIn(tiph.ui.popup._popup_mask, {end:80,  callback : function(ele, alpha) {
				tiph.ui.setAlpha(tiph.ui.popup._popup_container, alpha + 20);
			}});
		}

		tiph.ui.popup._popup_container.innerHTML = html;
		setTimeout("tiph.ui.effect.relocate(tiph.ui.popup._popup_container);",10);
		tiph.ui.popup._popup_container.style.display = "";
		tiph.ui.popup._popup_container.focus();
	},

	close : function() {
		if (tiph.ui.popup._popup_mask == null || !tiph.ui.popup._popup_mask.isopen) return;
		tiph.ui.popup._popup_mask.isopen = false;
		tiph.ui.dettachEvent(tiph.ui.popup._popup_mask, "click", tiph.ui.popup.close);

		if (tiph.ui.popup._popup_mask._popup_args.direct) {
			tiph.ui.popup._popup_container.style.left = "-10000px";
			tiph.ui.popup._popup_container.style.top = "-10000px";
			tiph.ui.popup._popup_container.style.width = "";
			tiph.ui.popup._popup_container.style.height = "";
			tiph.ui.popup._popup_container.innerHTML = "";
			tiph.ui.popup._popup_container.style.display = "none";
			tiph.ui.popup._popup_mask.style.display = "none";
		} else {
			tiph.ui.effect.alphaFadeIn(tiph.ui.popup._popup_mask, {start:80, end:0, step:20, interval:5, callback : function(ele, alpha) {
				if (alpha == 0) {
					tiph.ui.popup._popup_container.style.left = "-10000px";
					tiph.ui.popup._popup_container.style.top = "-10000px";
					tiph.ui.popup._popup_container.style.width = "";
					tiph.ui.popup._popup_container.style.height = "";
					tiph.ui.popup._popup_container.innerHTML = "";
					tiph.ui.popup._popup_container.style.display = "none";
					tiph.ui.popup._popup_mask.style.display = "none";
					tiph.ui.setAlpha(tiph.ui.popup._popup_container, 100);
					//document.documentElement.style.overflow = tiph.ui.popup._popup_mask._overflow;
				} else {
					tiph.ui.setAlpha(tiph.ui.popup._popup_container, alpha);
				}
			}});
		}

		var objs = document.getElementsByTagName("object");
		for (var i = 0; i < objs.length; ++i) {
			objs[i].style.width = objs[i].__width;
			objs[i].style.height = objs[i].__height;
			objs[i].__width = null;
			objs[i].__height = null;
		}
		var objs = document.getElementsByTagName("embed");
		for (var i = 0; i < objs.length; ++i) {
			objs[i].style.width = objs[i].__width;
			objs[i].style.height = objs[i].__height;
			objs[i].__width = null;
			objs[i].__height = null;
		}
	}
}

tiph.ui.effect.size2fit = function(ele, args) {
	//var w1 = ele.clientWidth;
	var w1 =  ele.scrollWidth ? ele.scrollWidth : ele.clientWidth;
	var h1 = ele.clientHeight;
	
	var w2 = ele.scrollWidth ? ele.scrollWidth : ele.clientWidth;
	var h2 = ele.scrollHeight ? ele.scrollHeight : ele.clientHeight;
	if (!args) args = {};

	if (!args.end_w && args.end_w != 0) {
		args.auto_end_w = true;
	}
	if (!args.end_h && args.end_h != 0) {
		args.auto_end_h = true;
	}
	
	args = tiph.common.fillDefault(args, {start_w:w1, start_h:h1, end_w:w2, end_h:h2, step_w:10, step_h:10, interval:1});
	//args.step_w = (args.end_w-args.start_w) / 10;
	//args.step_h = (args.end_h-args.start_h) / 10;
	if (ele._size2fit_effect) {
		if (ele._size2fit_effect.timerHandler) clearInterval(ele._size2fit_effect.timerHandler);
	}
	ele._size2fit_effect = args;
	ele._size2fit_effect.current_w = args.start_w;
	ele._size2fit_effect.current_h = args.start_h;
	if (args.end_w != args.start_w) ele.style.width = args.start_w + "px";
	if (args.end_h != args.start_h) ele.style.height = args.start_h + "px";
	ele.style.overflow = "hidden";
	ele.style.display = "";
	ele._size2fit_effect.timerHandler = setInterval(function() {tiph.ui.effect._size2fit(ele);}, args.interval);
	ele._size2fit_effect.lastTime = new Date().getTime();
};

tiph.ui.effect.size2fit_stop = function(ele) {
	if (ele._size2fit_effect && ele._size2fit_effect.timerHandler) {
		clearInterval(ele._size2fit_effect.timerHandler);
		ele._size2fit_effect = null;
	}
};

tiph.ui.effect._size2fit = function(ele) {
	if (!ele) return;
	var now = new Date().getTime();
	var offset = ele._size2fit_effect.lastTime < now ? ((now - ele._size2fit_effect.lastTime) / 80) : 1;
	var times = offset;
	
	var w = ele._size2fit_effect.current_w;
	if (w != ele._size2fit_effect.end_w) {
		if (ele._size2fit_effect.end_w > ele._size2fit_effect.start_w) {
			w = Math.min(ele._size2fit_effect.end_w, w + ele._size2fit_effect.step_w * times);
		} else {
			w = Math.max(ele._size2fit_effect.end_w, w - ele._size2fit_effect.step_w * times);
		}
		ele.style.width = w + "px";
		ele._size2fit_effect.current_w = w;
	}
	var h = ele._size2fit_effect.current_h;
	if (h != ele._size2fit_effect.end_h) {
		if (ele._size2fit_effect.end_h > ele._size2fit_effect.start_h) {
			h = Math.min(ele._size2fit_effect.end_h, h + ele._size2fit_effect.step_h * times);
		} else {
			h = Math.max(ele._size2fit_effect.end_h, h - ele._size2fit_effect.step_h * times);
		}
		ele.style.height = h + "px";
		ele._size2fit_effect.current_h = h;
	}
	
	if (w != ele._size2fit_effect.end_w || h != ele._size2fit_effect.end_h)	{
		if (ele._size2fit_effect.step_callback) ele._size2fit_effect.step_callback(ele);
	} else {
		if (ele._size2fit_effect.auto_end_w) ele.style.width = "auto";
		if (ele._size2fit_effect.auto_end_h) ele.style.height = "auto";
		clearInterval(ele._size2fit_effect.timerHandler);	
		if (ele._size2fit_effect.callback) ele._size2fit_effect.callback(ele);
		ele._size2fit_effect = null;
	}
};

tiph.ui.load = function(ele, router, params) {
	if (!ele) return;
	//	ele.style.height = h + "px";
	//	tiph.ui.effect.size2fit(ele, {start_h : h, end_h : 200});
	tiph.ui.effect.size2fit_stop(ele);
	ele.innerHTML = "<div class=req_loading><span></span></div>";
	//ele.style.height = "140px";
	
	tiph.ajax.request(router, params, function(data) {
					  ele.innerHTML = data;
					  tiph.ui.effect.size2fit(ele);
					  });
};

/*------------------------------------TIPH COOKIE---------------------------------------*/
tiph.ui.component = {};
tiph.ui.component.date = {
	install : function(args) {
		args = tiph.common.fillDefault(args, {value:"1995-03-02", id:"_"});
		var html = "<select id=\"" + args.id + "_year\" onchange=\"tiph.ui.component.date.resetDate('"+args.id+"');\" class=d_year>";
		var parts = args.value.split("-");
		if (parts.length != 3) parts = "1995-01-01".split("-");
		var year = 1940;
		var today = new Date();
		for (var year = 1940; year <= today.getFullYear(); ++year) {
			if (year == parts[0])
				html += "<option value=\"" + year + "\" selected>" + year + "</option>";
			else
				html += "<option value=\"" + year + "\">" + year + "</option>";
		}
		html += "</select>&nbsp;";
		
		html += "<select id=\"" + args.id + "_month\" onchange=\"tiph.ui.component.date.resetDate('"+args.id+"');\" class=d_month>";
		for (var month = 1; month <= 12; ++month) {
			if (month == parseInt(parts[1]))
				html += "<option value=\"" + month + "\" selected>" + month + "</option>";
			else
				html += "<option value=\"" + month + "\">" + month + "</option>";
		}
		html += "</select>&nbsp;";

		html += "<select id=\"" + args.id + "_date\" class=d_date></select>";
		document.write(html);
		tiph.ui.component.date.resetDate(args.id, parseInt(parts[2]));
	},

	resetDate : function(id, value) {
		var year = V(id + "_year");
		var month = parseInt(V(id + "_month")) - 1;
		var comp = E(id + "_date");
		if (!value) value = comp.selectedIndex + 1;
		var date = new Date(year, month, 1);
		comp.options.length = 0;
		var selectedIndex = 0;
		while (true) {
			comp.options.add(new Option(date.getDate(),date.getDate()));
			date.setTime(date.getTime() + 60 * 60 * 24 * 1000);
			if (month != date.getMonth()) break;
		}
		if (value && value <= comp.options.length) {
			comp.selectedIndex = value - 1;
		}
	},

	getValue : function(id) {
		return V(id + "_year") + "-" + V(id + "_month") + "-" + V(id + "_date");
	}
};

/*------------------------------------TIPH COOKIE---------------------------------------*/
tiph.cookie = {
	get : function(name) {
		var result = null;
		name += "=";
		var start = document.cookie.indexOf(name);
		var end;
		if (start >= 0) {
			start += name.length;
			end = document.cookie.indexOf(";", start);
			if (end >= start) {
				result = unescape(document.cookie.substring(start, end));
			} else {
				result = unescape(document.cookie.substring(start));
			}
		}
		return result;
	},

	set : function(name, value, expire, domain, path) {
		var item = name + "="+ escape(value);
		if (path) item += ";path=" + path;
		if (domain) item += ";domain=" + domain;
		if (expire) {
			var exp  = new Date();
			exp.setTime(exp.getTime() + expire);
			item +=  ";expires=" + exp.toGMTString();
		}
		document.cookie = item;
	},

	remove : function(name, domain, path){
		if (tiph.cookie.get(name) == null) return;
		var item = name + "=";
		if (path) item += ";path=" + path;
		if (domain) item += ";domain=" + domain;
		item += ";expires=Thu,   01-Jan-70   00:00:01   GMT";
		document.cookie = item;
	}   
};

/*------------------------------------TIPH COOKIE---------------------------------------*/
tiph.browser = {
	is_ie6 : function() {
		var browser = navigator.appName 
		var b_version = navigator.appVersion 
		var version = b_version.split(";"); 
		var trim_Version = version[1].replace(/[ ]/g,""); 
		return browser=="Microsoft Internet Explorer" && trim_Version=="MSIE6.0";
	},

	is_ie7 : function() {
		var browser = navigator.appName 
		var b_version = navigator.appVersion 
		var version = b_version.split(";"); 
		var trim_Version = version[1].replace(/[ ]/g,""); 
		return browser=="Microsoft Internet Explorer" && trim_Version=="MSIE7.0";
	}
};
