/*
 * WEIP - Web-based Enterprise Information Platform
 * Copyright (C) 2005-2007 WEIP team
 * All rights reserved.
 * 未经许可，禁止用于商业用途！
 * 
 * 版本(Version):  0.1
 * 最后修改时间(Modified): 2007-08-01 13:09:00
 * 
 * 文件作者(File Authors):
 *      魏永增(Cator Wei) <catorwei@163.com>
 */
/**
 * add appendText extension methods to jQuery object
 */
(function($) {
	$.fn.extend({
		appendText: function(e) {
			if (typeof e == 'string')
				return this.append(document.createTextNode(e));
			return this;
		}
	});

})(jQuery);

/**
 * isNull (blank string or null or undefined)
 */
(function($) {
	$.extend({
		isNull: function(v) {
			return (typeof v == 'undefined' || v == null || v == '');
		}
	});

})(jQuery);

/**
 * get jQuery objects by id
 */
(function($) {
	$.extend({
		$: function(id) {
			return $('#' + id);
		}
	});

})(jQuery);

/**
 * global variable
 */
(function($) {
	$.extend({
		global: function(name, value) {
			if (typeof this.global.variables == 'undefined') {
    			$.global.variables = [];
    		}
    		if (typeof value == 'undefined') {
    			return (typeof $.global.variables[name] != 'undefined') ? $.global.variables[name] : null;
    		} else {
    			$.global.variables[name] = value;
    			return $;
    		}
    	}
    });

})(jQuery);

/**
 * get url parameters
 */
(function($) {
	$.extend({
		urlParam: function(name) {
			if (typeof this.params == 'undefined') {
    			this.params = [];
				var urlQueryString = location.search.substring(1, location.search.length);
				if (urlQueryString != '') {
					var kvs = urlQueryString.split('&');
					for (var i=0; i<kvs.length; i++) {
						var kv = kvs[i].split('=');
						this.params[kv[0]] = unescape(kv[1]);
					}
				}
    		}
    		return (typeof this.params[name] != 'undefined') ? this.params[name] : ''; 
    	}
    });

})(jQuery);

/**
 * get cookies
 */
(function($) {
	$.extend({
		cookie: function(name, value, expiration, path, domain, secure) {
			if (typeof $.cookie.cookies == 'undefined') {
    			$.cookie.cookies = [];
				var allcookies = document.cookie;
				if (allcookies != '') {
					var a = allcookies.split(';');
					for(var i=0; i < a.length; i++) {
						a[i] = a[i].split('=');
					}
					for(var i = 0; i < a.length; i++) {
						$.cookie.cookies[$.trim(a[i][0])] = unescape(a[i][1]);
					}
				}
    		}
    		if ($.isNull(name)) {
    			return false;
    		}
    		if (typeof value == 'undefined') {
    			return getCookieByName(name);
    		} else if ($.isNull(value)) { /* remove cookie */
    			setCookieByName(name, '', -1000, path, domain, secure);
    		} else {
    			setCookieByName(name, value, expiration, path, domain, secure);
    		}
		}
	});
	
	/* get value of cookie by name */
	var getCookieByName = function(name) {
		name = $.trim(name);
		return (typeof $.cookie.cookies[name] != 'undefined') ? $.cookie.cookies[name] : '';
	};
	
	/* set value of cookie by name */
	var setCookieByName = function(name, value, expiration, path, domain, secure) {
		var cookieString = name + '=' + escape(value);
		if (!$.isNull(expiration)) {
			expiration = new Date((new Date()).getTime() + expiration * 1000);
			cookieString += '; expires=' + expiration.toGMTString();
		}
		if (!$.isNull(path)) {
			cookieString += '; path=' + path;
		}
		if (!$.isNull(domain)) {
			cookieString += '; domain=' + domain;
		}
		if (secure === true) {
			cookieString += '; secure';
		}
		document.cookie = cookieString;
		$.cookie.cookies[name] = value;
	};

})(jQuery);


/**
 * add site to bookmark
 */
(function($) {
	$.extend({
		bookmark: function(title, url) {
			if (document.all) {
				window.external.AddFavorite(url, title);
			} else if (window.sidebar) {
				window.sidebar.addPanel(title, url, "");
			}
    	}
    });

})(jQuery);

/**
 * listener
 */
(function($) {
	$.extend({
		listener: function(object, attribute, interval, fn) {
			// if object is null, stop all observer
			if (object == null) {
				clearInterval($.listener.observer);
				return true;
			}
			if ($.isNull(object) || $.isNull(attribute)) {
				return false;
			}
			var index = $.listener.instance.length;
			for (var i=0; i<$.listener.instance.length; i++) {
				if ($.listener.instance[i].object[0] == object[0]) {
					index = i;
					break;
				}
			}
			if (!$.isNull(interval)) {
				if ($.isFunction(interval)) {
	   				fn = interval;
	   				interval = 500;
	   			}
	   			$.listener.instance[index] = {
	   				object: object,
	   				attribute: attribute,
	   				oldValue: null,
	   				enabled: true,
	   				interval: interval,
	   				counter: 0,
	   				fn: fn
	   			}
	   			if (typeof $.listener.observer == 'undefined') {
	   				$.listener.observer = setInterval(observer, 500);
	   			}
	   		} else if (index != $.listener.instance.length) {
	    		$.listener.instance[index].enabled = false;
	    	}
    	}
    });
    
    $.listener.instance = [];

	/* observer for all listener */
	observer = function() {
		$.each($.listener.instance, function(i, instance) {
			var currentValue = null;
			if (instance.enabled) {
				instance.counter += 500;
				if (instance.counter >= instance.interval) {
					if (instance.attribute == 'value') {
						currentValue = instance.object.val();
					} else {
						instance.oldValue = null;
					}
					if (instance.oldValue != null && instance.oldValue != currentValue) {
						instance.fn(instance.oldValue, currentValue);
					}
					instance.oldValue = currentValue;
					instance.counter -= instance.interval;
				}
			}
		});
	}

})(jQuery);

/**
 * add listen extension methods to jQuery object
 */
(function($) {
	$.fn.extend({
		listen: function(attribute, interval, fn) {
			$.listener(this, attribute, interval, fn);
			return this;
		}
	});

})(jQuery);

/**
 * add top & left extension methods to jQuery object
 */
(function($) {
	$.fn.extend({
		top: function(n) {
			if (typeof n == 'undefined') {
				var object = this[0];
				var t = 0;
				if ($.browser.mozilla) {
					t = object.offsetTop;
					while (object && object.tagName != 'BODY') {
						t -= ((object.scrollTop) ? object.scrollTop : 0);
						object = $(object).parent()[0];
					}
				} else {
					while (object && object.tagName != 'BODY') {
						t += object.offsetTop - ((object.scrollTop) ? object.scrollTop : 0);
						object = object.offsetParent;
					}
				}
				return t;
			} else {
				this.css('top', (n && n.constructor == Number) ? n + 'px' : n);
				return this;
			}
		},
		
		left: function(n) {
			if (typeof n == 'undefined') {
				var object = this[0];
				var l = 0;
				if ($.browser.mozilla) {
					l = object.offsetLeft;
					while (object && object.tagName != 'BODY') {
						l -= ((object.scrollLeft) ? object.scrollLeft : 0);
						object = $(object).parent()[0];
					}
				} else {
					while (object && object.tagName != 'BODY') {
						l += object.offsetLeft - ((object.scrollLeft) ? object.scrollLeft : 0);
						object = object.offsetParent;
					}
				}
				return l;
			} else {
				this.css('left', (n && n.constructor == Number) ? n + 'px' : n);
				return this;
			}
		}
	});

})(jQuery);

/**
 * add UI extension methods to jQuery object
 */
(function($) {
	$.fn.extend({
		shadow: function(status, depth) {
			if (typeof status == 'undefined') {
				status = (this.css('display') != 'none');
			}
			if (typeof depth == 'undefined' || isNaN(parseInt(depth)) || depth < 3) {
				depth = 3;
			}
			var oShadow = [];
			var isCreated = $('#shadowDiv0').is('#shadowDiv0');
			var shadowStyle = 'position: absolute;z-index: 10000; border: 1px #666 solid;';
			for (var i=0; i<depth; i++) {
				oShadow.push(isCreated ? $('#shadowDiv' + i) 
											: $('<div id="shadowDiv' + i + '" style="' + shadowStyle + '"></div>')
												.appendTo(document.body) 
												.fadeTo(1, 0.15)
												.css('border-width', (i==0) ? ('0 ' + depth + 'px ' + (depth - 1) + 'px 0')
																		   : ((i==1) ?  ('0 ' + (depth - 1) + 'px ' + depth + 'px 0')
																					 :  ('0 ' + (depth - i) + 'px ' + (depth - i) + 'px 0')))
								);
			}
			if (status) {
				var top = this.top();
				var left = this.left();
				oShadow[0].top(top + depth + 3)
						.left(left + depth + 1)
						.height(this[0].offsetHeight - depth - 1)
						.width(this[0].offsetWidth - depth + 2);
				
				oShadow[1].top(top + depth + 2)
						.left(left + depth + 2)
						.height(this[0].offsetHeight - depth + 1)
						.width(this[0].offsetWidth - depth);
				
				for (var i=2; i<depth; i++) {
					oShadow[i].top(top + depth + i)
							.left(left + depth + i)
							.height(this[0].offsetHeight - depth - i)
							.width(this[0].offsetWidth - depth - i);
				}
				
				for (var i=0; i<depth; i++) {
					oShadow[i].show();
				}
			} else {
				for (var i=0; i<depth; i++) {
					oShadow[i].hide();
				}
			}
			return this;
		},
		
		shadowEx: function(status) { //image shadow, need $.global('uiImagePath')
			if (typeof status == 'undefined') {
				status = (this.css('display') != 'none');
			}
			if (typeof depth == 'undefined' || isNaN(parseInt(depth)) || depth < 3) {
				depth = 3;
			}
			var i = $.isNull(this[0].id) ? this[0].tagName : this[0].id;
			var oShadowRight = $('#__shadowRight' + i), oShadowBottom, oShadowCorner;
			if (oShadowRight.is('#__shadowRight' + i)) {
				oShadowBottom = $('#__shadowBottom' + i);
				oShadowCorner = $('#__shadowCorner' + i);
			} else {
				oShadowRight = $('<div id="__shadowRight' + i + '" class="shadowRight"></div>').appendTo(document.body)
								.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader (src="'
										+ $.global('uiImagePath') + 'shadow_right.png",sizingMethod="crop")');
				oShadowBottom = $('<div id="__shadowBottom' + i + '" class="shadowBottom"></div>').appendTo(document.body) 
								.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader (src="'
										+ $.global('uiImagePath') + 'shadow_bottom.png",sizingMethod="crop")');
				oShadowCorner = $('<div id="__shadowCorner' + i + '" class="shadowCorner"></div>').appendTo(document.body) 
								.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader (src="'
										+ $.global('uiImagePath') + 'shadow_corner.png",sizingMethod="crop")');
			}
			if (status) {
				var top = this.top();
				var left = this.left();
				oShadowRight.top(top + 3)
						.left(left + this[0].offsetWidth)
						.height(this[0].offsetHeight - 5).show();
				
				oShadowBottom.top(top + this[0].offsetHeight)
						.left(left + 3)
						.width(this[0].offsetWidth - 5).show();
				
				oShadowCorner.top(top + this[0].offsetHeight - 2)
						.left(left + this[0].offsetWidth - 2).show();
			} else {
				oShadowRight.add(oShadowBottom).add(oShadowCorner).hide();
			}
			return this;
		},
		
		shading: function(status, depth) {
			return $.isNull($.global('uiImagePath')) ? this.shadow(status, depth) : this.shadowEx(status);
		},
		
		slideAndShadow: function(status, speed, callback) {
			if (typeof status != 'boolean') {
				callback = speed;
				speed = status;
				status = (this.css('display') == 'none');
			}
			if (typeof speed == 'undefined') {
				speed = 'fast';
			} else if ($.isFunction(speed)) {
				callback = speed;
				speed = 'fast';
			}
			if (status) {
				(!$.isNull(this[0])) && (this[0].slideAndShadowCallback = callback);
				this.slideDown(speed, function() {
					$(this).shading(true);
					(this.slideAndShadowCallback) && this.slideAndShadowCallback.apply(this);
				});
			} else {
				this.shading(false);
				this.slideUp(speed, callback);
			}
			return this;
		},
		
		dropMenu: function(obj, options) {
			var oThis = $(this);
			(typeof options == 'undefined') && (options = {});
			if ($.isNull(obj.attr('oldWidth'))) {
				obj.css('width', 'auto').show();
				var w1 = obj[0].offsetWidth;
				var h1 = obj[0].offsetHeight;
				obj.hide();
				obj.attr('oldWidth', w1);
				var w2 = oThis[0].offsetWidth ;
				(w1 < w2) && obj.width(w2);
				if (typeof options.maxheight != 'undefined') {
					if (options.maxheight < h1) {
						obj.height(options.maxheight);
						if (w1 > w2) {
							obj.width(w1 + 16);
						} else if (w2-w1<20) {
							obj.width(w1 + 16 - (w2 - w1));
						}
					}
				}
			}
			$.showTipWindow(obj, oThis.left(), oThis.top() + oThis[0].offsetHeight - 1);
		}

	});

})(jQuery);

(function($) {
	$.extend({
		showTipWindow: function(obj, left, top) {
			if ($.global('__showedTipWindow')) {
				$.global('__showedTipWindow').hide();
			}
			(typeof top != 'undefined') && obj.top(top);
			(typeof left != 'undefined') && obj.left(left);
			obj.slideAndShadow(true, 100);
			$.global('__showedTipWindow', obj);
			return this;
		},
		
		hideTipWindow: function() {
			if ($.global('__showedTipWindow')) {
				$.global('__showedTipWindow').slideAndShadow(false, 100);
			}
			$.global('__showedTipWindow', null);
			return this;
		},
		
		hint: function() {
			if ($.isNull($.hint.box)) {
				$(document).ready(function(){
					$.hint.box = $('<div class="hint"></div>').fadeTo(1, 0).appendTo(document.body);
					$.hint.timer = null;
					$.hint.target = null;
					$(document.body).mouseover(function(event) {
						(!event.target && event.srcElement) && (event.target = event.srcElement);
						if ($.hint.target != event.target) {
							$.hint.target = event.target;
							var oTarget = $(event.target);
							var msg = oTarget.attr('weiphint');
							if ($.isNull(msg)) {
								var object = event.target;
								while (object && object.tagName != 'BODY' && $.isNull(msg)) {
									msg = $.isNull($(object).attr('title')) ? $(object).attr('alt') : $(object).attr('title');
									object = object.offsetParent;
								}
								oTarget.attr('weiphint', $.isNull(msg) ? (msg='!BLANK!') : msg).attr('title', '').attr('alt', '');
							}
							clearTimeout($.hint.timer);
							if (msg == '!BLANK!') {
								if ($.hint.box.css('display') != 'none') {
									$.hint.box.shading(false).fadeTo('fast', 0).hide();
								}
							} else {
								$.hint.box.html(msg).css('width', 'auto')
										.top(event.clientY + document.body.scrollTop + 5)
										.left(event.clientX + document.body.scrollLeft + 5);
								if ($.hint.box.css('display') != 'none') {
									$.hint.box.shading(true);
									$.hint.timer = setTimeout(function() {
											$.hint.box.shading(false).fadeTo('fast', 0).hide();
										}, 10000);
								} else {
									$.hint.timer = setTimeout(function() {
											$.hint.box.show().shading(true).fadeTo('normal', 0.8);
											$.hint.timer = setTimeout(function() {
													$.hint.box.shading(false).fadeTo('fast', 0).hide();
												}, 10000);
										}, 300);
								}
							}
						}
					});
				});
			}
		}
	});
	
	$(document.body).mousedown(function(event){
		if ($.global('__showedTipWindow')) {
			(!event.target && event.srcElement) && (event.target = event.srcElement);
			if (!$(event.target).parents().add(event.target).is('#' + $.global('__showedTipWindow')[0].id)) {
				$.hideTipWindow();
			}
		}
	});

})(jQuery);
