/**
 * weebox.js
 */ 
(function($) {
	var weebox = function(content, options) {
		var self = this;
		this._dragging = false;
		this._content = content;
		this._options = options;
		this.dh = null;
		this.mh = null;
		this.dt = null;
		this.dc = null;
		this.bo = null;
		this.bc = null;
		this.selector = null;	
		this.ajaxurl = null;
		this.options = null;
		this.defaults = {
			boxid: null,
			boxclass: null,
			type: 'dialog',
			title: '',
			width: 0,
			height: 0,
			timeout: 0, 
			draggable: true,
			modal: true,
			focus: null,
			position: 'center',
			overlay: 50,
			showTitle: true,
			showButton: true,
			showCancel: true, 
			showOk: true,
			okBtnName: '确定',
			cancelBtnName: '取消',
			contentType: 'text',
			contentChange: false,
			clickClose: false,
			zIndex: 9999,
			animate: false,
			trigger: null,
			onclose: null,
			onopen: null,
			onok: null		
		};
		this.types = new Array(
			"dialog", 
			"error", 
			"warning", 
			"success", 
			"prompt",
			"box"
		);
		this.titles = {
			"error": 	"!! Error !!",
			"warning": 	"Warning!",
			"success": 	"Success",
			"prompt": 	"Please Choose",
			"dialog": 	"Dialog",
			"box":		""
		};
		this.initOptions = function() {	
			if (typeof(self._options) == "undefined") {
				self._options = {};
			}
			if (typeof(self._options.type) == "undefined") {
				self._options.type = 'dialog';
			}
			if(!$.inArray(self._options.type, self.types)) {
				self._options.type = self.types[0];
			}
			if (typeof(self._options.boxclass) == "undefined") {
				self._options.boxclass = self._options.type+"box";
			}
			if (typeof(self._options.title) == "undefined") {
				self._options.title = self.titles[self._options.type];
			}
			if (content.substr(0, 1) == "#") {
				self._options.contentType = 'selector';
				self.selector = content; 
			}
			self.options = $.extend({}, self.defaults, self._options);
		};
		this.initBox = function() {
			var html = '';	
			if (self.options.type == 'wee') {
				html =  '<div class="weedialog">' +
				'	<table width="100%" border="0" cellspacing="0" cellpadding="0" >' +
				'		<tr>' +
				'			<td>' +
				'				<div class="dialog-header">' +
				'					<div class="dialog-title"></div>' +
				'					<div class="dialog-close"></div>' +
				'				</div>' +
				'				<div class="dialog-content"></div>' +
				'				<div class="dialog-button clearfix">' +
				'					<button class="ui-button dialog-cancel btn btn-default" rel="dialog-cancel">取消</button>' +
				'					<button class="ui-button dialog-ok btn btn-primary" rel="dialog-ok">确定</button>' +				
				'				</div>' +
				'			</td>' +
				'		</tr>' +
				'	</table>' +
				'</div>';
			} else {
				html = "<div class='dialog-box'>" +
							"<div class='dialog-main'>"+
								"<div class='dialog-header'>" +
								    "<div class='dialog-titles'><i><img src='"+ROOT_PATH+"Public/HanChaoUI/images/prompt_icon.png' /></i>温馨提示</div>" +
									"<div class='dialog-close'></div>" +
								"</div>" +
								"<div class='dialog-content mt15 ta_c'></div>" +	
								"<div style='clear:both'></div>" +				
								"<div class='dialog-button theme_"+THEME_COLOR+"'>" +
								'	<button class="ui-button dialog-cancel btn btn-default" rel="dialog-cancel">关闭</button>' +
								'	<button class="ui-button dialog-ok btn btnprimaryok" rel="dialog-ok">确定</button>' +
								"</div>" +
							"</div>"+
						"</div>";
			}
			self.dh = $(html).appendTo('body').css({
				position: 'absolute',	
				overflow: 'hidden',
				zIndex: self.options.zIndex
			});	
			self.dt = self.dh.find('.dialog-title');
			self.dc = self.dh.find('.dialog-content');
			self.bo = self.dh.find('.dialog-ok');
			self.bc = self.dh.find('.dialog-cancel');
			if (self.options.boxid) {
				self.dh.attr('id', self.options.boxid);
			}	
			if (self.options.boxclass) {
				self.dh.addClass(self.options.boxclass);
			}
			if (self.options.height>0) {
				self.dc.css('height', self.options.height);
			}
			if (self.options.width>0) {
				self.dh.css('width', self.options.width);
			}
			//self.dh.bgiframe();	
		}
		this.initMask = function() {							
			if (self.options.modal) {
				self.mh = $("<div class='dialog-mask'></div>")
				.appendTo('body').hide().css({
					opacity: self.options.overlay/100,
					filter: 'alpha(opacity='+self.options.overlay+')',
					width: self.bwidth(),
					height: self.bheight(),
					zIndex: self.options.zIndex-1
				});
			}
		}
		this.initContent = function(content) {
			self.dh.find(".dialog-ok").val(self.options.okBtnName);
			self.dh.find(".dialog-cancel").val(self.options.cancelBtnName);	
			self.dh.find('.dialog-title').html(self.options.title);
			self.dh.find('.dialog-titles').html('<i><img src="'+ROOT_PATH+'Public/HanChaoUI/images/prompt_icon.png"/></i>'+self.options.title+'</div>');
			if (!self.options.showTitle) {
				self.dh.find('.dialog-title').hide();
			}	
			if (!self.options.showButton) {
				self.dh.find('.dialog-button').hide();
			}
			if (!self.options.showCancel) {
				self.dh.find('.dialog-cancel').hide();
			}							
			if (!self.options.showOk) {
				self.dh.find(".dialog-ok").hide();
			}			
			if (self.options.contentType == "selector") {
				self.selector = self._content;
				self._content = $(self.selector).html();
				self.setContent(self._content);
				//if have checkbox do
				var cs = $(self.selector).find(':checkbox');
				self.dh.find('.dialog-content').find(':checkbox').each(function(i){
					this.checked = cs[i].checked;
				});				
				$(self.selector).empty();
				self.onopen();
				self.show();
				self.focus();
			} else if (self.options.contentType == "ajax") {	
				self.ajaxurl = self._content;			
				self.setContent('<div class="dialog-loading"></div>');				
				self.show();
				$.get(self.ajaxurl, function(data) {
					self._content = data;
			    	self.setContent(self._content);
			    	self.onopen();
			    	self.focus();		  	
			    	if (self.options.position == 'center') {
						self.setCenterPosition();
			    	}
				});
			} else {
				self.setContent(self._content);
				self.onopen();	
				self.show();	
				self.focus();					
			}
		}
		this.initEvent = function() {
			self.dh.find(".dialog-close, .dialog-cancel, .dialog-ok").unbind('click').click(function(){
				self.close();
			});			
			if (typeof(self.options.onok) == "function") {
				self.dh.find(".dialog-ok").unbind('click').click(self.options.onok);
			} 
			if (typeof(self.options.oncancel) == "function") {
				self.dh.find(".dialog-cancel").unbind('click').click(self.options.oncancel);
			}			
			if (self.options.timeout>0) {
				window.setTimeout(self.close, (self.options.timeout * 1000));
			}	
			this.draggable();			
		}
		this.draggable = function() {	
			if (self.options.draggable && self.options.showTitle) {
				self.dh.find('.dialog-header').mousedown(function(event){
					self._ox = self.dh.position().left;
					self._oy = self.dh.position().top;					
					self._mx = event.clientX;
					self._my = event.clientY;
					self._dragging = true;
				});
				if (self.mh) {
					var handle = self.mh;
				} else {
					var handle = $(document);
				}
				$(document).mousemove(function(event){
					if (self._dragging == true) {
						//window.status = "X:"+event.clientX+"Y:"+event.clientY;
						self.dh.css({
							left: self._ox+event.clientX-self._mx, 
							top: self._oy+event.clientY-self._my
						});
					}
				}).mouseup(function(){
					self._mx = null;
					self._my = null;
					self._dragging = false;
				});
				var e = self.dh.find('.dialog-header').get(0);
				e.unselectable = "on";
				e.onselectstart = function() { 
					return false; 
				};
				if (e.style) { 
					e.style.MozUserSelect = "none"; 
				}
			}	
		}
		this.onopen = function() {							
			if (typeof(self.options.onopen) == "function") {
				self.options.onopen();
			}	
		}
		this.show = function() {	
			if (self.options.position == 'center'||self.options.position == 'fixed') {
				self.setCenterPosition();
			}
			if (self.options.position == 'element') {
				self.setElementPosition();
			}		
			if (self.options.animate) {				
				self.dh.fadeIn("slow");
				if (self.mh) {
					self.mh.fadeIn("normal");
				}
			} else {
				self.dh.show();
				if (self.mh) {
					self.mh.show();
				}
			}	
		}
		this.focus = function() {
			if (self.options.focus) {
				self.dh.find(self.options.focus).focus();
			} else {
				self.dh.find('.dialog-cancel').focus();
			}
		}
		this.find = function(selector) {
			return self.dh.find(selector);
		}
		this.setTitle = function(title) {
			self.dh.find('.dialog-title').html(title);
		}
		this.getTitle = function() {
			return self.dh.find('.dialog-title').html();
		}
		this.setContent = function(content) {
			self.dh.find('.dialog-content').html(content);
		}
		this.getContent = function() {
			return self.dh.find('.dialog-content').html();
		}
		this.hideButton = function(btname) {
			self.dh.find('.dialog-'+btname).hide();			
		}
		this.showButton = function(btname) {
			self.dh.find('.dialog-'+btname).show();	
		}
		this.setButtonTitle = function(btname, title) {
			self.dh.find('.dialog-'+btname).val(title);	
		}
		this.close = function() {
			if (self.animate) {
				self.dh.fadeOut("slow", function () { self.dh.hide(); });
				if (self.mh) {
					self.mh.fadeOut("normal", function () { self.mh.hide(); });
				}
			} else {
				self.dh.hide();
				if (self.mh) {
					self.mh.hide();
				}
			}
			if (self.options.contentType == 'selector') {
				if (self.options.contentChange) {
					//if have checkbox do
					var cs = self.find(':checkbox');
					$(self.selector).html(self.getContent());						
					if (cs.length > 0) {
						$(self.selector).find(':checkbox').each(function(i){
							this.checked = cs[i].checked;
						});
					}
				} else {
					$(self.selector).html(self._content);
				} 
			}								
			if (typeof(self.options.onclose) == "function") {
				self.options.onclose();
			}
			self.dh.remove();
			if (self.mh) {
				self.mh.remove();
			}
		}
		this.bheight = function() {
			if ($.browser.msie && $.browser.version < 7) {
				var scrollHeight = Math.max(
					document.documentElement.scrollHeight,
					document.body.scrollHeight
				);
				var offsetHeight = Math.max(
					document.documentElement.offsetHeight,
					document.body.offsetHeight
				);
				if (scrollHeight < offsetHeight) {
					return $(window).height();
				} else {
					return scrollHeight;
				}
			} else {
				return $(document).height();
			}
		}
		this.bwidth = function() {
			if ($.browser.msie && $.browser.version < 7) {
				var scrollWidth = Math.max(
					document.documentElement.scrollWidth,
					document.body.scrollWidth
				);
				var offsetWidth = Math.max(
					document.documentElement.offsetWidth,
					document.body.offsetWidth
				);
				if (scrollWidth < offsetWidth) {
					return $(window).width();
				} else {
					return scrollWidth;
				}
			} else {
				return $(document).width();
			}
		}
		this.setCenterPosition = function() {
			var wnd = $(window), doc = $(document),
				pTop = doc.scrollTop(),	pLeft = doc.scrollLeft(),
				minTop = pTop;	
			pTop += (wnd.height() - self.dh.height()) / 2;
			pTop = Math.max(pTop, minTop);
			pLeft += (wnd.width() - self.dh.width()) / 2;
			self.dh.css({top: pTop, left: pLeft});
		}	
		this.setElementPosition = function() {
			var trigger = $(self.options.trigger);	
			if (trigger.length == 0) {
				alert('请设置位置的相对元素');
				self.close();				
				return false;
			}
			var left = trigger.offset().left;
			var top = trigger.offset().top + 25;
			self.dh.css({top: top, left: left});
			return true;
		}	
		//窗口初始化	
		this.initialize = function() {
			self.initOptions();
			self.initMask();
			self.initBox();		
			self.initContent();
			self.initEvent();
			return self;
		}
		//初始化
		this.initialize();
	};
    var weeboxs = function() {
		var self = this;
		this._onbox = false;
		this._opening = false;
		this.boxs = new Array();
		this.zIndex = 9999;
		this.push = function(box) {
			this.boxs.push(box);
		}
		this.pop = function() {
			if (this.boxs.length > 0) {
				return this.boxs.pop();
			} else {
				return false;
			}
		}
		this.open = function(content, options) {
			self._opening = true;
			if (typeof(options) == "undefined") {
				options = {};
			}
			if (options.boxid) {
				this.close(options.boxid);
			}
			options.zIndex = this.zIndex;
			this.zIndex += 10;
			var box = new weebox(content, options);
			box.dh.click(function(){
				self._onbox = true;
			});
			this.push(box);
			return box;
		}
		this.close = function(id) {
			if (id) {
				for(var i=0; i<this.boxs.length; i++) {
					if (this.boxs[i].dh.attr('id') == id) {
						this.boxs[i].close();
						this.boxs.splice(i,1);
					}
				}
			} else {
				this.pop().close();
			}
		}
		this.length = function() {
			return this.boxs.length;
		}
		this.getTopBox = function() {
			return this.boxs[this.boxs.length-1];
		}	
		this.find = function(selector) {
			return this.getTopBox().dh.find(selector);
		}		
		this.setTitle = function(title) {
			this.getTopBox().setTitle(title);
		}		
		this.getTitle = function() {
			return this.getTopBox().getTitle();
		}		
		this.setContent = function(content) {
			this.getTopBox().setContent(content);
		}		
		this.getContent = function() {
			return this.getTopBox().getContent();
		}		
		this.hideButton = function(btname) {
			this.getTopBox().hideButton(btname);			
		}		
		this.showButton = function(btname) {
			this.getTopBox().showButton(btname);	
		}		
		this.setButtonTitle = function(btname, title) {
			this.getTopBox().setButtonTitle(btname, title);	
		}
		$(window).scroll(function() {
			if (self.length() > 0) {
				var box = self.getTopBox();
				if (box.options.position == "center") {
					self.getTopBox().setCenterPosition();
				}
			}			
		});
		$(document).click(function() {
			if (self.length()>0) {
				var box = self.getTopBox();
				if(!self._opening && !self._onbox && box.options.clickClose) {
					box.close();
				}
			}
			self._opening = false;
			self._onbox = false;
		});
	}
	$.extend({weeboxs: new weeboxs()});		
})(jQuery);
(function(jQuery){   
if(jQuery.browser) return;   
jQuery.browser = {};   
jQuery.browser.mozilla = false;   
jQuery.browser.webkit = false;   
jQuery.browser.opera = false;   
jQuery.browser.msie = false;   
var nAgt = navigator.userAgent;   
jQuery.browser.name = navigator.appName;   
jQuery.browser.fullVersion = ''+parseFloat(navigator.appVersion);   
jQuery.browser.majorVersion = parseInt(navigator.appVersion,10);   
var nameOffset,verOffset,ix;   
// In Opera, the true version is after "Opera" or after "Version"   
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {   
jQuery.browser.opera = true;   
jQuery.browser.name = "Opera";   
jQuery.browser.fullVersion = nAgt.substring(verOffset+6);   
if ((verOffset=nAgt.indexOf("Version"))!=-1)   
jQuery.browser.fullVersion = nAgt.substring(verOffset+8);   
}   
// In MSIE, the true version is after "MSIE" in userAgent   
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {   
jQuery.browser.msie = true;   
jQuery.browser.name = "Microsoft Internet Explorer";   
jQuery.browser.fullVersion = nAgt.substring(verOffset+5);   
}   
// In Chrome, the true version is after "Chrome"   
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {   
jQuery.browser.webkit = true;   
jQuery.browser.name = "Chrome";   
jQuery.browser.fullVersion = nAgt.substring(verOffset+7);   
}   
// In Safari, the true version is after "Safari" or after "Version"   
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {   
jQuery.browser.webkit = true;   
jQuery.browser.name = "Safari";   
jQuery.browser.fullVersion = nAgt.substring(verOffset+7);   
if ((verOffset=nAgt.indexOf("Version"))!=-1)   
jQuery.browser.fullVersion = nAgt.substring(verOffset+8);   
}   
// In Firefox, the true version is after "Firefox"   
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {   
jQuery.browser.mozilla = true;   
jQuery.browser.name = "Firefox";   
jQuery.browser.fullVersion = nAgt.substring(verOffset+8);   
}   
// In most other browsers, "name/version" is at the end of userAgent   
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <   
(verOffset=nAgt.lastIndexOf('/')) )   
{   
jQuery.browser.name = nAgt.substring(nameOffset,verOffset);   
jQuery.browser.fullVersion = nAgt.substring(verOffset+1);   
if (jQuery.browser.name.toLowerCase()==jQuery.browser.name.toUpperCase()) {   
jQuery.browser.name = navigator.appName;   
}   
}   
// trim the fullVersion string at semicolon/space if present   
if ((ix=jQuery.browser.fullVersion.indexOf(";"))!=-1)   
jQuery.browser.fullVersion=jQuery.browser.fullVersion.substring(0,ix);   
if ((ix=jQuery.browser.fullVersion.indexOf(" "))!=-1)   
jQuery.browser.fullVersion=jQuery.browser.fullVersion.substring(0,ix);   
jQuery.browser.majorVersion = parseInt(''+jQuery.browser.fullVersion,10);   
if (isNaN(jQuery.browser.majorVersion)) {   
jQuery.browser.fullVersion = ''+parseFloat(navigator.appVersion);   
jQuery.browser.majorVersion = parseInt(navigator.appVersion,10);   
}
jQuery.browser.version = jQuery.browser.majorVersion;   
})(jQuery);
/* bgIframe Version 2.1.1*/
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement('html'),this.firstChild);});}return this;};})(jQuery);
//用于未来扩展的提示正确错误的JS
$.showErr = function(str,timeout,typename,funcok,funcclose,id,title)
{
    var okfunc = function(){
		$.weeboxs.close("hanchao_error_box");
		if(funcok)funcok.call();
	};
    if(!typename)typename='wee';
    if(!id)id='hanchao_error_box';
    if(!title)title='温馨提示';
	$.weeboxs.open(str, {boxid:id,contentType:'text',timeout:timeout,position:'center',showButton:true, showCancel:false, showOk:true,title:title,width:250,type:typename,onok:okfunc,onclose:funcclose});
};
$.showSuccess = function(str,timeout,typename,funcok,funcclose,showok,id,title)
{
    var okfunc = function(){
		$.weeboxs.close("hanchao_success_box");
		if(funcok)funcok.call();
	};var tpldefault=false;
    if(!typename){typename='wee';tpldefault=true;}
    if(!id)id='hanchao_success_box';
    if(!title)title='温馨提示';if(showok){tpldefault=true;}
	$.weeboxs.open(str, {boxid:id,contentType:'text',timeout:timeout,position:'center',showButton:true, showCancel:false, showOk:tpldefault,title:title,width:250,type:typename,onok:okfunc,onclose:funcclose});
};
$.showConfirm = function(str,funcok,typename,funcclose)
{
	var okfunc = function(){
		$.weeboxs.close("hanchao_confirm_box");
		if(funcok)funcok.call();
	};
    if(!typename)typename='wee';
	$.weeboxs.open(str, {boxid:'hanchao_confirm_box',contentType:'text',showButton:true, showCancel:true, showOk:true,title:'确认',width:250,type:typename,onclose:funcclose,onok:okfunc});
};
$.openUrl=function(url,str,id,width,typename,funcopen,funcok,funcclose)
{
    if(!url){$.showErr('URL错误');return false;}
    if(!str)str='温馨提示';
    if(!typename)typename='wee';
    if(!width)width=580;
	var okfunc = function(){
		$.weeboxs.close("hanchao_url_box"+id);
		if(funcok)funcok.call();
	};
	$.weeboxs.open(url, {boxid:"hanchao_url_box"+id,contentType:'ajax',showButton:false,title:str,width:width,type:typename,onopen:funcopen,onok:okfunc,onclose:funcclose});	
}