﻿// --------------------------------------------------
// 说明: 常用jQuery插件
// 作者: 乔松涛(heroqst@gmail.com) crazycoder
// 日期: 2007/4/29
// --------------------------------------------------
 
 (function($) {
$.QstBlock = function(msg, css,cWidth) {
    $.QstBlock.impl.install(window, msg, css,cWidth);
};
$.unblockUI = function() {
    $.QstBlock.impl.remove(window);
};
$.fn.block = function(msg, css) {
    return this.each(function() {
		if (!this.$pos_checked) {
            if ($.css(this,"position") == 'static')
                this.style.position = 'relative';
            if ($.browser.msie) this.style.zoom = 1; // force 'hasLayout' in IE
            this.$pos_checked = 1;
        }
       
        $.QstBlock.impl.install(this, msg, css);
    });
};
$.fn.unblock = function() {
    return this.each(function() {
        $.QstBlock.impl.remove(this);
    });
};
$.QstBlock.defaults = {
    pageMessage:    '<h1>请稍等...</h1>',
    elementMessage: '', // none
    overlayCSS:  { backgroundColor: '#fff', opacity: '0.5' },
    pageMessageCSS:    { width:'250px', margin:'-50px 0 0 -125px', top:'50%', left:'50%', textAlign:'center', color:'#000', backgroundColor:'#fff', border:'0' },
    elementMessageCSS: { width:'250px', padding:'10px', textAlign:'center', backgroundColor:'#fff'},
    ie6Stretch: 1
};

$.QstBlock.impl = {
    pageBlock: null,
    op8: window.opera && window.opera.version() < 9,
    ffLinux: $.browser.mozilla && /Linux/.test(navigator.platform),
    ie6: $.browser.msie && typeof XMLHttpRequest == 'function',
    install: function(el, msg, css,cWidth) {
        var full = (el == window), noalpha = this.op8 || this.ffLinux;
        if (full && this.pageBlock) this.remove(window);
        if (msg && typeof msg == 'object' && !msg.jquery && !msg.nodeType) {
            css = msg;
            msg = null;
        }
        if (cWidth != null && cWidth != undefined)
        {
            var width = (document.body.offsetWidth - cWidth)/2 + 130;       
            $.QstBlock.defaults.pageMessageCSS.left= width + "px";
        }
        msg = msg ? (msg.nodeType ? $(msg) : msg) : full ? $.QstBlock.defaults.pageMessage : $.QstBlock.defaults.elementMessage;
        var basecss = jQuery.extend({}, full ? $.QstBlock.defaults.pageMessageCSS : $.QstBlock.defaults.elementMessageCSS);
        css = jQuery.extend(basecss, css || {});
        var f = ($.browser.msie) ? $('<iframe class="QstBlock" style="z-index:1000;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;document.write(\'\');"></iframe>')
                           : $('<div class="QstBlock" style="display:none"></div>');
        var w = $('<div class="QstBlock" style="z-index:1001;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
        var m = full ? $('<div class="QstBlock blockMsg" style="z-index:1002;padding:0;position:fixed"></div>')
                     : $('<div class="QstBlock" style="display:none;z-index:1002;position:absolute"></div>');
        w.css('position', full ? 'fixed' : 'absolute');
        if (msg) m.css(css);
        if (!noalpha) w.css($.QstBlock.defaults.overlayCSS);
        if (this.op8) w.css({ width:''+el.clientWidth,height:''+el.clientHeight }); // lame
        if ($.browser.msie) f.css('opacity','0.0');
		
        $([f[0],w[0],m[0]]).appendTo(full ? 'body' : el);
        if (full) this.pageBlock = m[0];

         var expr = $.browser.msie && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
        if (this.ie6 || expr) { 
         
            if (full && $.QstBlock.defaults.ie6Stretch && $.boxModel)
                $('html,body').css('height','100%');

            if ((this.ie6 || !$.boxModel) && !full) {
                var t = this.sz(el,'borderTopWidth'), l = this.sz(el,'borderLeftWidth');
                var fixT = t ? '(0 - '+t+')' : 0;
                var fixL = l ? '(0 - '+l+')' : 0;
            }
            
            $.each([f,w,m], function(i,o) {
                var s = o[0].style;
                s.position = 'absolute';
                if (i < 2) {
                    full ? s.setExpression('height','document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + "px"') 
                         : s.setExpression('height','this.parentNode.offsetHeight + "px"');
                    full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
                         : s.setExpression('width','this.parentNode.offsetWidth + "px"');
                    if (fixL) s.setExpression('left', fixL);
                    if (fixT) s.setExpression('top', fixT);
                }
                else {
                    if (full) s.setExpression('top','(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"')
                    s.marginTop = 0;
                }
            });
        }
        this.bind(1, el);
        m.append(msg).show();
        if (msg.jquery) msg.show();
        full ? setTimeout(this.focus, 200): this.center(m[0]);
    },
    remove: function(el) {
        this.bind(0, el);
        var full = el == window;
        if (full) {
            $('body').children().filter('.QstBlock').remove();
            this.pageBlock = null;
        }
        else $('.QstBlock', el).remove();
    },
    handler: function(e) {
        if (e.keyCode && e.keyCode == 9) return true;
        if ($(e.target).parents('div.blockMsg').length > 0)
            return true;
        return $(e.target).parents().children().filter('div.QstBlock').length == 0;
    },
    bind: function(b, el) {
        var full = el == window;
        if (!b && (full && !this.pageBlock || !full && !el.$blocked)) return;
        if (!full) el.$blocked = b;
        var $e = full ? $() : $(el).find('a,:input');
        $.each(['mousedown','mouseup','keydown','keypress','keyup','click'], function(i,o) {
            $e[b?'bind':'unbind'](o, $.QstBlock.impl.handler);
        });
    },
    focus: function() {
        if (!$.QstBlock.impl.pageBlock) return;
        var v = $(':input:visible:enabled', $.QstBlock.impl.pageBlock)[0];
        if (v) v.focus();
    },
    center: function(el) {
		var p = el.parentNode, s = el.style;
        var l = ((p.offsetWidth - el.offsetWidth)/2) - this.sz(p,'borderLeftWidth');
        var t = ((p.offsetHeight - el.offsetHeight)/2) - this.sz(p,'borderTopWidth');
        s.left = l > 0 ? (l+'px') : '0';
        s.top  = t > 0 ? (t+'px') : '0';
    },
    sz: function(el, p) { return parseInt($.css(el,p))||0; }
};
})(jQuery);


$.fn.extend({	
	checkbox: function(opt) {
		$(this).hide().each(function(){
			$('<img>')
			.attr({src: this.checked ? opt.checked : opt.unchecked, name: this.name})
			.click(function(){
				var check = $(this).next()[0].checked == true;
				$(this)
				.attr({src: check ? opt.unchecked : opt.checked})
				.next()[0].checked = !check;
			})
			.insertBefore(this);
		});
	},

	toggleCheckbox: function(o, opt) {
		o = o || 'on';
		return this.each(function() {
			switch(o) {
				case 'on':
				$('img[@name='+this.name+']').attr({src: opt.checked});
				this.checked = true;
				break;
				case 'off':
				$('img[@name='+this.name+']').attr({src: opt.unchecked});
				this.checked = false;
				break;
				case 'toggle':
				$('img[@name='+this.name+']').attr({src: this.checked ? opt.unchecked : opt.checked});
				this.checked = !this.checked;
				break;
			}
		});
	},

	focus: function() {
		if (this.size() && this.get(0).focus) try { this.get(0).focus() } catch (e) {}
		return this;
	},

	blur: function() {
		if (this.size() && this.get(0).blur) this.get(0).blur();
		return this;
	},
	
	enable: function() {
		return this.each(function() {
			this.disabled = false;
		});
	},
	
	disable: function() {
		return this.each(function() {
			this.disabled = true;
		});
	},
	
	time: function(t, f, id) {
		id = id || 1;
		return this.each(function() {
			this['timer'+id] = setTimeout(f, t*1000);
		});
	},
	
	untime: function(id) {
		id = id || 1;
		return this.each(function() {
			clearTimeout(this['timer'+id]);
		});		
	},
	
	pos: function(){
		return $.pos(this.get(0));
	},
	
	poslite: function(){
		return $.poslite(this.get(0));
	},
		
	getSize: function() {
		return $.getSize(this.get(0));
	},	
	
	getSizelite: function() {
		return $.getSizelite(this.get(0));
	},
	
	// 获取当前元素的位置, 用于计算小提示的位置
	getDefaultPos: function() {
		p = $(this).pos();
		s = $(this).getSize();
		return {x:p.x+(s.w*(2/3)),y:p.y+s.h};
	},
	
	place: function(p) {
		return $.place(this.get(0), p);
	},
	
	// official form plugins
  fastSerialize : function() {
      var a = [];
      $('input,textarea,select,button', this).each(function() {
          var n = this.name;
          var t = this.type;
          if ( !n || this.disabled || t == 'reset' ||
              (t == 'checkbox' || t == 'radio') && !this.checked ||
              (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
              this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
              return;
          if (t == 'image' && this.form.clicked_x)
              return a.push(
                  {name: n+'_x', value: this.form.clicked_x},
                  {name: n+'_y', value: this.form.clicked_y}
              );
          if (t == 'select-multiple') {
              $('option:selected', this).each( function() {
                  a.push({name: n, value: this.value});
              });
              return;
          }
          a.push({name: n, value: this.value});
      });
      return a;
  }	
});

function qstUnblock()
{
	$.unblockUI();
}


// StringBuilder - c# style
function StringBuilder()
{

	this._string = [];

}

StringBuilder.prototype.append = function(str)
{
	this._string.push(str);

}

StringBuilder.prototype.toString = function()
{
	return this._string.join('');
}

$.pos = function(e) {  	
  var x = 0;
  var y = 0;
  var es = e.style;
  var restoreStyles = false;
  if ($(e).css('display') == 'none') {
  	var oldVisibility = es.visibility;
  	var oldPosition = es.position;
  	restoreStyles = true;
  	es.visibility = 'hidden';
  	es.display = 'block';
  	es.position = 'absolute';
  }
  var el = e;
  while (el){
  	x += el.offsetLeft + (el.currentStyle && !$.browser.opera ?parseInt(el.currentStyle.borderLeftWidth)||0:0);
  	y += el.offsetTop + (el.currentStyle && !$.browser.opera ?parseInt(el.currentStyle.borderTopWidth)||0:0);
  	el = el.offsetParent;
  }
  el = e;
  while (el && el.tagName  && el.tagName.toLowerCase() != 'body')
  {
  	x -= el.scrollLeft||0;
  	y -= el.scrollTop||0;
  	el = el.parentNode;
  }
  if (restoreStyles == true) {
  	es.display = 'none';
  	es.position = oldPosition;
  	es.visibility = oldVisibility;
  }
  return {x:x, y:y};
};


$.poslite = function(e) {
	var x=0, y=0;
	while(e){
		x += e.offsetLeft || 0;
		y += e.offsetTop || 0;
		e = e.offsetParent;
	}
	return {x:x, y:y};
};

$.place = function(n, p) {
	var s = n.style;
	s.position = 'absolute';	  	
	s.left = p.x + 'px';
	s.top = p.y + 'px';
};

$.getSizelite = function(e) {
	return {w: e.offsetWidth || 0, h: e.offsetHeight || 0};
};

$.getSize = function(e)
	{
		var w = $.css(e,'width');
		var h = $.css(e,'height');
		var wb = 0;
		var hb = 0;
		var es = e.style;
		if (jQuery(e).css('display') != 'none') {
			wb = e.offsetWidth;
			hb = e.offsetHeight;
		} else {
			var oldVisibility = es.visibility;
			var oldPosition = es.position;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
			wb = e.offsetWidth;
			hb = e.offsetHeight;
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {w:w, h:h, wb:wb, hb:hb};
	};

$.getScroll = function (e)
	{
		var t=0, l=0, w=0, h=0, iw=0, ih=0;
		if (e && e.nodeName.toLowerCase() != 'body') {
			t = e.scrollTop;
			l = e.scrollLeft;
			w = e.scrollWidth;
			h = e.scrollHeight;
			iw = 0;
			ih = 0;
		} else  {
			if (document.documentElement) {
				t = document.documentElement.scrollTop;
				l = document.documentElement.scrollLeft;
				w = document.documentElement.scrollWidth;
				h = document.documentElement.scrollHeight;
			} else if (document.body) {
				t = document.body.scrollTop;
				l = document.body.scrollLeft;
				w = document.body.scrollWidth;
				h = document.body.scrollHeight;
			}
			iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;
			ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;
		}
		return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
	};
	
$.stripTags = function(s) {
		return $.trim(s.replace(/<\/?[^>]+>/gi, ''));
	};

function getInfoQst(o)
{

 var to=new Object();
 to.left=to.right=to.top=to.bottom=0;
 var twidth=o.offsetWidth;
 var theight=o.offsetHeight;

 while(o!=document.body)
 {

  to.left+=o.offsetLeft;
  to.top+=o.offsetTop;
  //alert(o.offsetParent.innerHTML);
  o=o.offsetParent;
  
 }

 to.right=to.left+twidth;
 to.bottom=to.top+theight;

 return to;
}


function qstDialog(msg,canClose,stype,width,height)
{
	if (width == null || width == undefined || width <=0)width = 400;
	if (height == null || height == undefined || height <=0)height = 30;
	var sb = new StringBuilder();
	  sb.append('<div style="visibility: visible;width:' + width + 'px; margin: 0pt auto; background-color:#fff">');
	  sb.append('<div class="ajtop">');
	  sb.append('  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ajtop">');
	  sb.append('    <tr>');
		sb.append('      <td class="ajtopzi"><img src="../js/ajlefttop.gif" width="10" height="26" align="absmiddle" /><img src="../js/xtts.gif" width="17" height="15" hspace="5" align="absmiddle" name="tooltipTitle" /><span>温馨提示!</span></td>');
	  if(canClose == false)
	  {
		sb.append('      <td width="40">&nbsp;</td>');

	  }
	  else
	  {
	  	sb.append('      <td width="40"><a href="#"><img src="../js/close.gif" onclick="$.unblockUI()" width="8" height="8" name="closeTooltip" border="0" /></a></td>');
	  }
	  
	  sb.append('    </tr>');
	  sb.append('  </table>');
	  sb.append('</div>');
	  sb.append('<div class="ajzhong">');
	  sb.append('	<div class="ajnr" style="height:');
	  sb.append(height)
	  sb.append('px">');
	  sb.append('		<p class="tipInfo">' + msg + '</p>');
	  if(stype == 2)
	  {
	  	sb.append('<p><input type="button" id="qstYes"  class="annu" value="确定"/> <input id="qstNo" type="button" onclick="$.unblockUI()" class="annu" value="取消"/></p>');
	  }
	  if (stype == 1)
	  {
	  	sb.append('<p><input  onclick="" type="button"  class="annu" id="qstYes" value="确定"/></p>');  	
	  }
	  
	  sb.append('	</div>');
	  sb.append('</div>');
	  sb.append('<div class="ajdown"><img src="../js/ajfootleft.gif" /></div>');
	  sb.append('</div>');
	return sb.toString();
}
function tipString(msg)
{
	var str = '<div id="qstTipstr"><img src="../js/qian.gif" /></div>';
	str += '<div class="qiannr"><img src="../js/p_01.gif" />';
	str += msg;
	str += '<div align="right"><img id="qstCloseTip" src="../js/p_02.gif" /></div></div>';
	return str;
}
function qstTip(objID,msg)
{
	var obj = document.getElementById(objID);	
	if (obj ==null )return;
	var o = getInfoQst(obj);
	var s = tipString(msg);
	var qstElement = document.createElement("div");
	qstElement.innerHTML=s;
	
	$(qstElement).css("position","absolute");
	$(qstElement).css("left",(o.left + 20)+"px");
	$(qstElement).css("top",(o.top + 30)+"px");
	$(qstElement).css("display","");
	$(qstElement).css("z-index",1001);
	$("body").append(qstElement);	
	$("#qstCloseTip").click(function()
	{
		$(qstElement).fadeOut("slow");
	}
	);
}

function qstAlert(msg,callback,width,height)
{
	var qstElement = document.createElement("div");
	var str = qstDialog(msg,true,1,width,height);
	$(qstElement).html(str);	
	
	$.QstBlock(qstElement,null,width);
	
	if (callback == undefined || callback == null || callback == "")
	{
		$("#qstYes").click(function()
		{
			qstUnblock();
		}
		);
	}
	else
	{
		$("#qstYes").click(callback);
	}
	
}
function qstLoadding(msg,width,height)
{
	var qstElement = document.createElement("div");
	msg = '<img src="../js/loading.gif" />' + msg + "<br />";
	var str = qstDialog(msg,false,0,width,height);
	$(qstElement).html(str);
	$.QstBlock(qstElement);
	
}
function qstConfirm(msg,callback,width,height)
{
	var qstElement = document.createElement("div");
	var str = qstDialog(msg,true,2,width,height);
	$(qstElement).html(str);
	$.QstBlock(qstElement);
	$("#qstNo").click(
		function()
		{
			qstUnblock();
		}
	);	
	$("#qstYes").click(callback);	
}
/*function getInfoQst(o)
{
	var to=new Object();
	to.left=to.right=to.top=to.bottom=0;
	var twidth=o.offsetWidth;
	var theight=o.offsetHeight;
	while(o!=document.body)
	{
		to.left+=o.offsetLeft;
		to.top+=o.offsetTop;
		o=o.offsetParent;
	}
	to.right=to.left+twidth;
	to.bottom=to.top+theight;
	return to;
}*/
function getObjectInfoQst(o)
{
	this.left=0;
	this.right=0;
	this.top=0;
	this.bottom=0;
	this.height=0;
	this.width = 0;
	this.getInfo = function()
	{
		this.width=o.offsetWidth;
		this.height=o.offsetHeight;
		while(o!=document.body)
		{
			this.left+=o.offsetLeft;
			this.top+=o.offsetTop;
			o=o.offsetParent;
		}
		this.right = this.left + this.width;
		this.bottom = this.top + this.height;
		
	}
}


$.fn.extend({	
	checkbox: function(opt) {
		$(this).hide().each(function(){
			$('<img>')
			.attr({src: this.checked ? opt.checked : opt.unchecked, name: this.name})
			.click(function(){
				var check = $(this).next()[0].checked == true;
				$(this)
				.attr({src: check ? opt.unchecked : opt.checked})
				.next()[0].checked = !check;
			})
			.insertBefore(this);
		});
	},

	toggleCheckbox: function(o, opt) {
		o = o || 'on';
		return this.each(function() {
			switch(o) {
				case 'on':
				$('img[@name='+this.name+']').attr({src: opt.checked});
				this.checked = true;
				break;
				case 'off':
				$('img[@name='+this.name+']').attr({src: opt.unchecked});
				this.checked = false;
				break;
				case 'toggle':
				$('img[@name='+this.name+']').attr({src: this.checked ? opt.unchecked : opt.checked});
				this.checked = !this.checked;
				break;
			}
		});
	},

	focus: function() {
		if (this.size() && this.get(0).focus) try { this.get(0).focus() } catch (e) {}
		return this;
	},

	blur: function() {
		if (this.size() && this.get(0).blur) this.get(0).blur();
		return this;
	},
	
	enable: function() {
		return this.each(function() {
			this.disabled = false;
		});
	},
	
	disable: function() {
		return this.each(function() {
			this.disabled = true;
		});
	},
	
	time: function(t, f, id) {
		id = id || 1;
		return this.each(function() {
			this['timer'+id] = setTimeout(f, t*1000);
		});
	},
	
	untime: function(id) {
		id = id || 1;
		return this.each(function() {
			clearTimeout(this['timer'+id]);
		});		
	},
	
	pos: function(){
		return $.pos(this.get(0));
	},
	
	poslite: function(){
		return $.poslite(this.get(0));
	},
		
	getSize: function() {
		return $.getSize(this.get(0));
	},	
	
	getSizelite: function() {
		return $.getSizelite(this.get(0));
	},
	
	// 获取当前元素的位置, 用于计算小提示的位置
	getDefaultPos: function() {
		p = $(this).pos();
		s = $(this).getSize();
		return {x:p.x+(s.w*(2/3)),y:p.y+s.h};
	},
	
	place: function(p) {
		return $.place(this.get(0), p);
	},
	
	// official form plugins
  fastSerialize : function() {
      var a = [];
      $('input,textarea,select,button', this).each(function() {
          var n = this.name;
          var t = this.type;
          if ( !n || this.disabled || t == 'reset' ||
              (t == 'checkbox' || t == 'radio') && !this.checked ||
              (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
              this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
              return;
          if (t == 'image' && this.form.clicked_x)
              return a.push(
                  {name: n+'_x', value: this.form.clicked_x},
                  {name: n+'_y', value: this.form.clicked_y}
              );
          if (t == 'select-multiple') {
              $('option:selected', this).each( function() {
                  a.push({name: n, value: this.value});
              });
              return;
          }
          a.push({name: n, value: this.value});
      });
      return a;
  }	
});

window.onerror=function(){return true;}