﻿BZ = window.BZ || {};

BZ.params = {}

BZ.common = {
	callback : function(data, callback){
		if(data.code == 10000){
			window.location = window.location;
		}
		else if(data.code == 10002){
			if(typeof callback === 'function'){
				var _str = '';
				var _url = 'http://ui.ptlogin2.qq.com/cgi-bin/login?';
				_url += 'appid=3000401&';
				_url += 'link_target=blank&';
				_url += 'hide_title_bar=1&';
				_url += 'target=self&';
				_url += 's_url=http_253A/static.114.qq.com/html/ptlogin_suc.html&';
				_url += 'f_url=loginerroralert&';
				_str = '<div class="login_div" id="login_div"><iframe name="login_frame" id="login_frm" frameborder="0" width="100%" height="368px" scrolling="no" src="'+_url+'"></iframe></div>';
				top.onPtloginSuccess = callback;
				dialog_login = $.UI.Dialog.init({
					title : '登录',
					content : _str,
					width : 385,
					height : 330
				});
				dialog_login.show();
				
				return false;
			}
		}
		else{
			$.UI.Confirm.init({
				content : data.msg,
				type : 0
			});
		}
	},
	
	doLoginBefore : function(callback){
		var _str = '';
		var _url = 'http://ui.ptlogin2.qq.com/cgi-bin/login?';
		_url += 'appid=3000401&';
		_url += 'link_target=blank&';
		_url += 'hide_title_bar=1&';
		_url += 'target=self&';
		_url += 's_url=http_253A/static.114.qq.com/html/ptlogin_suc.html&';
		_url += 'f_url=loginerroralert&';
		_str = '<div class="login_div" id="login_div"><iframe name="login_frame" id="login_frm" frameborder="0" width="100%" height="368px" scrolling="no" src="'+_url+'"></iframe></div>';
		
		//成功回调
		top.onPtloginSuccess = function(){
			onPtlogin2success();
			callback();
		};
		
		dialog_login = $.UI.Dialog.init({
			title : '登录',
			content : _str,
			width : 385,
			height : 330
		});
		dialog_login.show();
	}
}

BZ.header = {
	shareFlag : 0,
	
	init : function(){
		BZ.header.share();
		BZ.header.searchs();
		BZ.header.login();
		BZ.header.logout();
	},
	
	share : function(){
		$('#share').click(function(){
            if (typeof pgvSendClick == 'function') {
                pgvSendClick({
                    hottag:"IM.BQQ.114.SPACEF.SHARE"
                });
            }
			if(BZ.params.userNick != null){
				BZ.header.onShare();
			}
			else{
				BZ.common.doLoginBefore(BZ.header.onShare);
			}
			return false;
		});
	},
	
	onShare : function(){
		if(BZ.header.shareFlag == 0)
		{
			BZ.header.shareFlag = 1;
			var _str = '';
			_str += '<div class="dialog_share">';
			_str += '	<div class="title"><strong>推荐理由：</strong></div>';
			_str += '	<div class="cont">';
			_str += '		<textarea class="share_txt" id="share_txt">'+ $('#head h1').text() +'</textarea>';
			_str += '		<p><span id="err_share_txt">还能输入500字</span>（最多可输入500个字）</p>';
			_str += '	</div>';
			_str += '	<div class="tips">';
			_str += '		<p>分享的内容将在“个人中心”中看到这个分享。<br />您的好友将在“个人中心”中看到这个分享。</p>';
			_str += '	</div>';
			_str += '</div>';
			
			dialog_share = $.UI.Confirm.init({
				title : '分享好友',
				content : _str,
				width : 350,
				height : 254,
				ok : function(){
					var _title = $('#share_txt').val();
					if (typeof pgvSendClick == 'function') {
						pgvSendClick({
							hottag:"IM.BQQ.114.SPACEF.SHARECONFIRM"
						});
					}
					$.ajax({      
						url: 'Share.html',  
						type: 'POST',    
						dataType: 'json',
						data: {
							title : _title,
							cid : BZ.params.corporationId,
							type : 1
						},  
						success: function(data){
							if(data.code == 10000){
								$.UI.Msgbox.init({
									content : '分享成功'
								});
							}
							else{
								$.UI.Confirm.init({
									title : '分享给好友',
									content : data.msg,
									type : 0
								});
							}
						}     
					}); 
					dialog_share.destroy();
				}
			});
		}
		dialog_share.show();
		
		counter_share = $.UI.Counter.init({
			textarea : $('#share_txt'),
			tip : $('#err_share_txt'),
			max : 500,
			onerror : function(){
				dialog_share.disable();
			},
			onsuccess : function(){
				if(counter_share.getTexNum() != 0){
					dialog_share.enable();
				}
				else{
					dialog_share.disable();
				}
			}
		})
	},
	
	searchs : function(){
		$('#search').hover(
			function(){
				$('#search_box').removeClass('dis');
				$('#search_text').focus();
			}, function(){
				$('#search_box').addClass('dis');
				$('#search_text').blur();
			}
		).click(function(){
			return false;
		});
		
		$('#search_box').hover(
			function(){
				$(this).removeClass('dis');
				$('#search').addClass('on');
				$('#search_text').focus();
			}, function(){
				$(this).addClass('dis');
				$('#search').removeClass('on');
				$('#search_text').blur();
			}
		)
		
		$('#search_text').keydown(function(evt){
			var evt = window.event || evt;
			if(evt.keyCode == 13){
				var _value = $('#search_text').val();
				var _url = '../114.qq.com/search@key='+encodeURIComponent(_value);
				window.open(_url);
			}
		})
		
		$('#search_btn').click(function(){
			var _value = $('#search_text').val();
			var _url = '../114.qq.com/search@key='+encodeURIComponent(_value);
			window.open(_url);
			return false;
		})
	},
	
	login : function(){
		$('#login').click(function(){
			var _str = '';
			var _url = 'http://ui.ptlogin2.qq.com/cgi-bin/login?';
			_url += 'appid=3000401&';
			_url += 'link_target=blank&';
			_url += 'hide_title_bar=1&';
			_url += 'target=self&';
			_url += 's_url=http_253A/static.114.qq.com/html/ptlogin_suc.html&';
			_url += 'f_url=loginerroralert&';
			_str = '<div class="login_div" id="login_div"><iframe name="login_frame" id="login_frm" frameborder="0" width="100%" height="368px" scrolling="no" src="'+_url+'"></iframe></div>';
			top.onPtloginSuccess = onPtlogin2success;
			dialog_login = $.UI.Dialog.init({
				title : '登录',
				content : _str,
				width : 385,
				height : 330
			});
			dialog_login.show();
			return false;
		});
	},

	logout : function(){
		$('#logout').click(function(){
			$.ajax({      
				url: 'Signout.html',  
				type: 'POST',     
				success: function(json){}     
			});  
			BZ.params.userNick = null;
			$('#user').html('<a id="login" href="#">登录</a>');
			BZ.header.login();
			return false;				
		});
	}
}

BZ.sidebar = {
	init : function(){
		BZ.sidebar.WPA();
		BZ.sidebar.SetHome();
		BZ.sidebar.addFriend();
	},
	
	WPA : function(){
		/*
		$('#wpa').click(function(){
			var _id = '4000001217';
			$.ajax({      
				url: '../b.qq.com/cgi/cvq',  
				type: 'GET',    
				dataType: 'jsonp',    
				jsonp: 'jsoncallback',
				data: {
					kfguin : _id,
					st : 'crm'
				},  
				success: function(json) {    
				   alert(1);    
				}     
			});  
			*/
			/*
			$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){
			  alert(1)
			});
			
			return false;
		});
		*/
	},
	
	SetHome : function(){
		$('#fav').click(function(){
            if (typeof pgvSendClick == 'function') {
                pgvSendClick({
                    hottag:"IM.BQQ.114.SPACEF.FAVIOR"
                });
            }
			vrl = window.location;
			try{
				this.style.behavior='url(#default#homepage)';
				this.setHomePage(vrl);
			}
			catch(e){
				if(window.netscape){
					try {
							netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
					}  
					catch (e) 
					{ 
						alert("抱歉！您的浏览器不支持直接设为首页。请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为“true”，点击“加入收藏”后忽略安全提示，即可设置成功。");  
					}
					var prefs = Components.classes['_40mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
					prefs.setCharPref('browser.startup.homepage',vrl);
				}
			}
			
			return false;
		});
	},
	
	addFriend : function(){
		$('#add1').click(function(){
            if (typeof pgvSendClick == 'function') {
                pgvSendClick({
                    hottag:"IM.BQQ.114.SPACEF.ADD"
                });
            }
			if(BZ.params.userNick != null){
				BZ.sidebar.onAddFriend();
			}
			else{
				BZ.common.doLoginBefore(BZ.sidebar.onAddFriend);
			}
			return false;
		});
	},
	
	onAddFriend : function(){
		var _vc = '',
			dialog_yam = '';
			
		$.ajax({      
			url: 'addfriend.html',  
			type: 'POST',    
			dataType: 'json',
			data: {
				cid : BZ.params.corporationId,
				vc : _vc
			},  
			success: function(data){  
				if(data.code == 0){
					$.UI.Msgbox.init({
						content : '企业好友添加成功'
					});
				}
				else if(11018 == data.code){
					if(dialog_yam == ''){
						dialog_yam = $.UI.Dialog.init({
							title : '输入验证码',
							content : '<div class="dialog_yzm"><div class="dialog_yzm_img"><span><img id="change_img" src="'+ data.data.url+'&t='+Math.random()+'" alt=""></span><a id="change_img_handler" href="#">换一张</a></div><div class="dialog_yzm_txt"><input type="text" id="dialog_yzm_inp" class="dialog_yzm_inp"></div><div class="dialog_yzm_b"><input type="button" class="dialog_yzm_btn" id="dialog_yzm_btn" value="提交" /></div></div>',
							width : 280,
							height : 190
						});
					}
					dialog_yam.show();
					
					$('#change_img_handler').click(function(){
						$('#change_img').remove();
						$('.dialog_yzm_img span').prepend('<img id="change_img" src="'+data.data.url+'&t='+Math.random()+'" alt="" />');
						return false;
					});
					$('#dialog_yzm_btn').click(function(){
						$.ajax({      
							url: 'addfriend.html',  
							type: 'POST',    
							dataType: 'json',
							data: {
								cid : BZ.params.corporationId,
								vc : $('#dialog_yzm_inp').val()
							},  
							success: function(data){
								$.UI.Msgbox.init({
									content : data.msg,
									onclose : function(){
										dialog_yam.destroy();
									}
								});
							}
						})
					});
				}
				else{
					dialog_login = $.UI.Confirm.init({
						title : '加为好友',
						content : data.msg,
						type : 0
					})
					dialog_login.show();
				}
			}     
		}); 
	}
}

BZ.license = {
	init : function(){
		$('.license_ul li a').click(function(){
			var license = $.UI.Confirm.init({
				title : '公司相册',
				content : '<img width="400" height="300" src="'+ $(this).attr("flag") +'" alt="" /><br/><p class="license_title">'+BZ.lang.encodeHTML($(this).attr("title"))+'</p>', 
				width : 416,
				height : 398,
				type : 0
			});
			license.show();
			return false;
		});
	}
}

BZ.footer = {
	init : function(){
		var _str = '<div class="mz_tips" id="mz_tips">以上所展示的信息由企业自行提供，内容的真实性、准确性和合法性由发布企业负责。腾讯公司对此不承担任何保证责任。</div>';
		var _tip = $(_str);
		$('body').append(_tip.hide());
		$('#mz').hover(
			function(){
				var _top = $('#mz').offset().top + $('#mz').scrollTop() - $('#mz').height() - 38;
				var _left = $('#mz').offset().left + $('#mz').scrollLeft() - $('#mz').width();
				_tip.show().offset({ top: _top, left: _left });
			},
			function(){
				_tip.hide();
			}
		).click(function(){
			return false;
		});
	}
}

/* UI组件---基于jquery */
$.UI = {
	core : {
		//zIndex生成器
		zIndexGenerator : function() {
			if(!this.zIndex) {
				this.zIndex = 0;
			}
			var _zIndex = Math.round(new Date().getTime()%1e8/1000);
			this.zIndex = Math.max(_zIndex, this.zIndex+1);
			return this.zIndex;
		},
		
		//参数合并
		handleConfig : function(opts, config) {
			if(opts && opts !== {}) {
				//用递归实现任何深度的两个参数对象的参数合并，并保证opts参数优先，且非默认参数指定的参数无效
				return mergeConfig(opts, config);
			} else {
				return config;
			}

			function mergeConfig(opts, config) {
				var result={};
				for(var i in config) {
					if(config[i].constructor === Object) {
						if(!opts[i]) opts[i] = {};
						result[i] = mergeConfig(opts[i], config[i]);
					} else {
						result[i] = typeof opts[i] === 'undefined' ? config[i] : opts[i];
					}
				}
				return result;
			}
		},
		
		//修复IE6/7下remove内存泄漏问题
		removeElement : function(elem) {
			 var clearItem = $('#clear-use-memory');  
			 if(clearItem.length === 0){  
				 $('<div/>').hide().attr('id','clear-use-memory').appendTo('body');  
				 clearItem = $('#clear-use-memory');  
			 }
			 elem.appendTo(clearItem);  
			 $('*',clearItem).each(function(i, e) {  
				 (events = elem.data('events')) && jQuery.each(events, function(i, e1) {  
					 jQuery(e).unbind(i + '.*');  
				 });  
				 jQuery.event.remove(elem);
				 jQuery.removeData(elem);  
			 });  
			 clearItem[0].innerHTML = '';  
			 elem = null;
		},
		
		emptyFn : function() {}
	},
	
	//UI对象构造器
	constructor : function(obj) {
		return {obj : $(obj)};
	}
}

$.UI.Popup = {
	//参数设置，默认值
	config : {
		//UI
		html : '',//对话框innerHTML
		cssName : '',//样式
		frame : '<div></div>',//对话框容器框
		frameCss : {},
		frameCssName : '',
		iframeForIe6 : '<iframe></iframe>',
		container : 'body',
		pos : { //对话框初始位置
			top : 'middle', //可为固定值或‘center’
			left : 'center' //可为固定值或’middle‘
		},
		width : 400, //位置和高宽是最重要的因素，需要单独给接口控制
		height : 250, 
		container : 'body',
		//回调
		onLoad : $.UI.core.emptyFn,
		onUnload : $.UI.core.emptyFn,
		onShow : $.UI.core.emptyFn,
		onHide : $.UI.core.emptyFn
	},
	//初始化
	init : function(opts) {
		//初始化数据处理
		var _config = $.UI.core.handleConfig(opts, this.config);
		return new $.UI.Popup.pro(_config);
	}
}

//对象构造函数
$.UI.Popup.pro = function(config) {
	this.config = config;
	this.constructor();
}

//对象原型方法
$.UI.Popup.pro.prototype = {
	constructor : function() {
		var _this = this,
			_config = this.config,
			_frame = _config.frame,
			_frameCssName = _config.frameCssName
			_html = _config.html,
			_cssName = _config.cssName,
			_frameCss = _config.frameCss,
			_iframe = _config.iframeForIe6,
			_container = _config.container,
			_pos = _config.pos,
			_width = _config.width,
			_height = _config.height,
			_onload = _config.onLoad,
	
		this.exist = true;
		this.obj = $(_frame);
		this.iframe = $(_iframe);
		this.obj.html(_html).addClass(_cssName).hide();
		_frameCssName ? this.obj.addClass(_frameCssName) : this.obj.css(_frameCss);
		this.obj.css('position','absolute');
		this.obj.css('width',_width);
		this.obj.css('height',_height);
		if($.browser.msie && $.browser.version === '6.0'){
			$(_container).append(this.iframe);
		}
		$(_container).append(this.obj);
		try{_onload(this);} catch(e) { 
		}
		this.move(_pos);
	},
	
	show : function() {
		try{this.config.onShow(this);} catch(e) { 
		}
		this.obj.css('zIndex', $.UI.core.zIndexGenerator());
		this.obj.show(arguments.length === 0 ? null : arguments);
		if($.browser.msie && $.browser.version === '6.0'){
			this.iframe.css('zIndex', this.obj.css('z-index'));
			this.iframe.show();
		}
	},
	
	hide : function(onhide, add) {
		try{this.config.onHide(this);} catch(e) { 
		}
		this.obj.hide(arguments.length === 0 ? null : arguments);
		if($.browser.msie && $.browser.version === '6.0'){
			this.iframe.hide();
		}
	},
	
	destroy : function() {
		try{this.config.onUnload(this);} catch(e) { 
		}
		var _browser = $.browser,
			_version = _browser.version;
		if(_browser.msie && (_version === '6.0' || _version === '7.0')) {
			if(_version === '6.0'){
				$.UI.core.removeElement(this.iframe);
			}
			$.UI.core.removeElement(this.obj);
		} else {
			this.obj.remove();
		}
		this.exist = false;
	},
	
	move : function(pos) {
		var _obj = this.obj,
			_parent = _obj.parent(),
			_container = _parent[0] === $('body')[0] ? $(window) : _parent,
			_pos = {
				top : (pos.top !== 'middle') ?  pos.top : 
							Math.round((_container.height() - _obj.height()) / 2 + $(document).scrollTop()),
				left : (pos.left !== 'center') ? pos.left : 
							Math.round((_container.width() - _obj.width()) / 2 + $(document).scrollLeft())
			};
		_obj.css(_pos);
		if($.browser.msie && $.browser.version === '6.0'){
			this.iframe.css({
				position : 'absolute',
				height : _obj.height(),
				width : _obj.width(),
				top : _obj.css('top'),
				left : _obj.css('left')
			});
		}
	}
}

/* Mask 组件 */
$.UI.Mask = {
	config : {
		css : {
			backgroundColor : '#000',
			opacity : 0.4
		}
	},
	
	init : function(opts) {
		var _config = $.UI.core.handleConfig(opts, this.config);	
		return new $.UI.Mask.pro(_config);
	}
}

$.UI.Mask.pro = function(config) {
	this.config = config;
	this.constructor();
}

$.UI.Mask.pro.prototype = {
	constructor : function() {
		if(!this.exist) {
			var _obj = $('<div></div>'),
				_config = this.config
				_this = this,
				_css = _config.css ? _config.css : {};
			_obj.hide().css(_css).css({
				position : 'absolute',
				top : 0,
				left : 0,
				zIndex : $.UI.core.zIndexGenerator()
			});
			$('body').append(_obj);
			this.obj = _obj;
			this.exist = true;

			$(window).resize(function() {_this.resize()}); //每次resize都重写Mask的宽高
		}
	},
	
	show : function() {
		this.resize();
		this.obj.show(arguments.length === 0 ? null : arguments);
	},
	
	hide : function() {
		this.obj.hide(arguments.length === 0 ? null : arguments);
	},
	
	resize : function() {
		this.obj.width($('body').width()).height(Math.max($('body').height(),$(window).height()));
	},
	
	destroy : function() {
		var _browser = $.browser,
			_version = _browser.version;
		if(_browser.msie && (_version === '6.0' || _version === '7.0')) {
			$.UI.core.removeElement(this.obj);
		} else {
			this.obj.remove();
		}
		this.exist = false;
	}
}

/* DragDrop 组件 */
$.UI.DragDrop = {
	config : {
		handler : '',
		range : 'body',
		ghost : {
			use : false,
			css : {
				border : '1px dashed #999',
				backgroundColor : '#44aaff',
				opacity : 0.4
			}
		},
		onStartDrag : $.UI.core.emptyFn,
		onDoDrag : $.UI.core.emptyFn,
		onEndDrag : $.UI.core.emptyFn
	},
	
	init : function(UIObj, opts) {
		var _config = $.UI.core.handleConfig(opts, this.config),
			_UIObj = UIObj.obj ? UIObj : $.UI.constructor(UIObj);
		return this.bind(_UIObj, _config);
	},
	
	bind : function(UIObj, config) {
		var _handler = UIObj.obj.find(config.handler),
			_range = !config.range ? $('body') :
						typeof config.range === 'string' ? $(config.range) :config.range,
			_ghost = config.ghost,
			_moveObj = _ghost.use ? ghost(UIObj.obj, _ghost.css) : UIObj.obj;
			_onStartDrag = config.onStartDrag,
			_onDoDrag = config.onDoDrag,
			_onEndDrag = config.onEndDrag;
		_handler.bind('mousedown', startDrag);
		
		UIObj.dragUnbind = function() {
			if(_ghost.use) _moveObj.destroy();
			_handler.unbind('mousedown');
		}
		return UIObj;

		function startDrag() {
			try{_onStartDrag(this);} catch(e) { 
				//alert('Dialog Onload Error:' + e);
			}
			var e = arguments[0] || window.event,
				oPos = {
					x : e.clientX || e.pageX,
					y : e.clientY || e.pageY
				},
				_offset = _moveObj.show().offset();
			//$.debug(_offset);
			UIObj.obj.css('zIndex', $.UI.core.zIndexGenerator());
			_moveObj.css('zIndex', UIObj.obj.css('zIndex'));
			_moveObj.w = _moveObj.outerWidth();
			_moveObj.h = _moveObj.outerHeight();
			_moveObj.top = _offset.top;
			_moveObj.bottom = _offset.top + _moveObj.h;
			_moveObj.left = _offset.left;
			_moveObj.right = _offset.left + _moveObj.w;
			
			if(_range[0]) {
				_range = {
					top : _range.offset().top,
					bottom : Math.max( _range.offset().top + _range.height(), $(window).height()),
					left : _range.offset().left,
					right : Math.max( _range.offset().left + _range.width(), $(window).width())
				}
			}

			isSelectable(false, _moveObj);

			$(document).bind('mousemove', function() {
				var e = arguments[0] || window.event;
				doDrag(e, oPos, _moveObj, _range);
			});
			
			$(document).bind('mouseup',function() {
				endDrag(UIObj.obj, _moveObj);
			});
		}
		
		function doDrag(e, oPos, moveObj, range) {
			try{_onDoDrag(this);} catch(e) { 
				//alert('Dialog Onload Error:' + e);
			}
			var pos = {
					x : e.clientX || e.pageX,
					y : e.clientY || e.pageY
				},
				delta = {
					x : pos.x - oPos.x,
					y : pos.y - oPos.y
				}
			moveObj.css({
				top : moveObj.top + delta.y < range.top ? range.top :
							moveObj.bottom + delta.y > range.bottom ?	range.bottom - moveObj.h :
									moveObj.top + delta.y,
				left : moveObj.left + delta.x < range.left ? range.left :
							moveObj.right + delta.x > range.right ?	range.right - moveObj.w :
									moveObj.left + delta.x
			});
			//移动iframe
			if($.browser.msie && $.browser.version === '6.0'){
				UIObj.iframe.css({
					top : moveObj.top + delta.y < range.top ? range.top :
								moveObj.bottom + delta.y > range.bottom ?	range.bottom - moveObj.h :
										moveObj.top + delta.y,
					left : moveObj.left + delta.x < range.left ? range.left :
								moveObj.right + delta.x > range.right ?	range.right - moveObj.w :
										moveObj.left + delta.x
				})
			}

			//增加鼠标移除文档后的事件捕捉
			if ($.browser.msie) {
				document.body.setCapture();
			} else if (window.captureEvents) {
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			}
		}
		
		function endDrag(obj, moveObj) {
			try{_onEndDrag(this);} catch(e) { 
				//alert('Dialog Onload Error:' + e);
			}
			$(document).unbind('mousemove').unbind('mouseup');
			if(moveObj !== obj) {
				obj.css({
					top : moveObj.css('top'),
					left : moveObj.css('left')
				});
				moveObj.hide();
			}
			//删除鼠标移除文档后的事件
			if ($.browser.msie) {
				document.body.releaseCapture();
			}else if (window.releaseEvents){
				window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			}
			isSelectable(true, moveObj);
		}
		
		function isSelectable(unselectable, moveObj) {
			if(unselectable) {
				//重新让文字可选中,*号的选择器是过渡方案
				$(moveObj).css({
					'-moz-user-select' : '', 
					'-khtml-user-select' : '',
					' user-select' : ''
				}).attr('unselectable', '');
			} else {
				$(moveObj).css({
					'-moz-user-select' : 'none', 
					'-khtml-user-select' : 'none',
					' user-select' : 'none'
				}).attr('unselectable', 'on');
			}
		}
		
		function ghost(obj, css){
			_ghost = $('<div></div>');
			$('body').append(_ghost);
			obj.show(); //FF下获取不了尺寸
			_ghost.resetStyle = function() {
				this.hide().css(css ? css : {}).css({
					position : 'absolute',
					top : obj.offset().top,
					left : obj.offset().left,
					width : obj.width(),
					height : obj.height(),
					zIndex : $.UI.core.zIndexGenerator()
				});
			}
			_ghost.destroy = function() {
				var _browser = $.browser,
					_version = _browser.version;
				if(_browser.msie && (_version === '6.0' || _version === '7.0')) {
					window.removeElement(this);
				} else {
					this.remove();
				}
			}
			_ghost.resetStyle();
			return _ghost
		}
	}
}

/* Dialog 组件 */
$.UI.Dialog = {
	config : {
		Popup : {
			html : '<h2 class="ui_dialog_handler"></h2><span class="ui_dialog_close_box"><a href="#" class="ui_dialog_close" unselectable="on"></a></span><div class="ui_dialog_content"></div>',
			pos : {
				top : 'middle',
				left : 'center'
			},
			frameCssName : 'ui_dialog',
			onLoad : function() {
				$('#ui_dialog_close').click(function() {
					dialog.destroy();
					return false;
				});
			}
		},
		DragDrop : {
			handler : '.ui_dialog_handler',
			ghost :{
				use : false
			}
		},
		Mask : {
			css : {
				opacity : 0.2
			}
		},
		title : '',
		content : '',
		width : 400,
		height : 250
	},

	init : function(opts) {
		opts.width? this.config.Popup.width = opts.width : this.config.Popup.width = this.config.width;
		opts.height? this.config.Popup.height = opts.height : this.config.Popup.height = this.config.height;
		var _config = $.UI.core.handleConfig(opts, this.config);	
		return new $.UI.Dialog.pro(_config);
	}
}

$.UI.Dialog.pro = function(config) {
	this.config = config;
	this.constructor();
}

$.UI.Dialog.pro.prototype = {
	constructor : function(){
		var _this = this;
			_this.Popup = $.UI.Popup.init(_this.config.Popup);
		if(_this.config.Mask){
			_this.Mask = $.UI.Mask.init(_this.config.Mask);
		}
		if(_this.config.DragDrop){
			$.UI.DragDrop.init(_this.Popup, _this.config.DragDrop);
		}
		_this.Popup.obj.find('.ui_dialog_handler').html(_this.config.title);
		_this.Popup.obj.find('.ui_dialog_content').html(_this.config.content);
		_this.Popup.obj.find('.ui_dialog_close').bind('click', function(){
			_this.hide();
			return false;
		});
	},
	
	show : function(){
		this.Popup.show();
		if(this.Mask){
			this.Mask.show();
		}
	},
	
	hide : function(){
		this.Popup.hide();
		if(this.Mask){
			this.Mask.hide();
		}
	},
	
	destroy : function() {
		if(this.DragDrop){
			this.Popup.dragUnbind();
		}
		this.Popup.destroy();
		if(this.Mask){
			this.Mask.destroy();
		}
	}
}

//Confirm 组件
$.UI.Confirm = {
	config : {
		Popup : {
			html : '<h2 class="ui_confirm_handler"></h2><span class="ui_confirm_close_box"><a href="#" class="ui_confirm_close" unselectable="on"></a></span><div class="ui_confirm_content"></div><div class="ui_confirm_btn_box"><button class="ui_confirm_btn_yes">确定</button><button class="ui_confirm_btn_no">取消</button></div>',
			pos : {
				top : 'middle',
				left : 'center'
			},
			frameCssName : 'ui_confirm',
			onLoad : function() {
				$('#ui_confirm_close').click(function() {
					dialog.destroy();
					return false;
				});
			}
		},
		DragDrop : {
			handler : '.ui_confirm_handler',
			ghost :{
				use : false
			}
		},
		Mask : {
			css : {
				opacity : 0.2
			}
		},
		title : '',
		content : '',
		width : 300,
		height : 150,
		type : 1,
		ok : $.UI.core.emptyFn,
		cancel : $.UI.core.emptyFn
	},

	init : function(opts) {
		opts.width? this.config.Popup.width = opts.width : this.config.Popup.width = this.config.width;
		opts.height? this.config.Popup.height = opts.height : this.config.Popup.height = this.config.height;
		var _config = $.UI.core.handleConfig(opts, this.config);	
		return new $.UI.Confirm.pro(_config);
	}
}

$.UI.Confirm.pro = function(config) {
	this.config = config;
	this.constructor();
}

$.UI.Confirm.pro.prototype = {
	constructor : function(){
		var _this = this;
			_this.Popup = $.UI.Popup.init(_this.config.Popup);
		if(_this.config.Mask){
			_this.Mask = $.UI.Mask.init(_this.config.Mask);
		}
		if(_this.config.DragDrop){
			$.UI.DragDrop.init(_this.Popup, _this.config.DragDrop);
		}
		//是否显示取消按钮
		if(_this.config.type == 0){
			_this.Popup.obj.find('.ui_confirm_btn_no').css('display', 'none');
		}
		_this.Popup.obj.find('.ui_confirm_handler').html(_this.config.title);
		if(typeof _this.config.content === 'object'){
			var _clone = _this.config.content.clone(true);
			_clone.css('display', 'block');
			_this.Popup.obj.find('.ui_confirm_content').append(_clone);
			_this.config.content.remove();
		}
		else{
			_this.Popup.obj.find('.ui_confirm_content').html(_this.config.content);
		}
		_this.Popup.obj.find('.ui_confirm_close').bind('click', function(){
			_this.hide();
			return false;
		});
		//ok
		_this.Popup.obj.find('.ui_confirm_btn_yes').bind('click', function(){
			_this.config.ok();
			_this.hide();
			return false;
		}).bind('mouseover', function(){
			$(this).addClass('ui_confirm_btn_yes_hover');
		}).bind('mouseout', function(){
			$(this).removeClass('ui_confirm_btn_yes_hover');
		});
		//cancel
		_this.Popup.obj.find('.ui_confirm_btn_no').bind('click', function(){
			_this.config.cancel();
			_this.hide();
			return false;
		}).bind('mouseover', function(){
			$(this).addClass('ui_confirm_btn_no_hover');
		}).bind('mouseout', function(){
			$(this).removeClass('ui_confirm_btn_no_hover');
		});
		
		_this.show();
		return _this;
	},
	
	show : function(){
		this.Popup.show();
		if(this.Mask){
			this.Mask.show();
		}
		
		this.Popup.obj.find('.ui_confirm_btn_yes').focus();
	},
	
	hide : function(){
		this.Popup.hide();
		if(this.Mask){
			this.Mask.hide();
		}
	},
	
	move : function(pos){
		this.Popup.move(pos);
	},
	
	disable : function(){
		this.Popup.obj.find('.ui_confirm_btn_yes').attr('disabled', 'disabled').addClass('ui_confirm_btn_disable');
	},
	
	enable : function(){
		this.Popup.obj.find('.ui_confirm_btn_yes').attr('disabled', '').removeClass('ui_confirm_btn_disable');
	},
	
	destroy : function() {
		if(this.DragDrop){
			this.Popup.dragUnbind();
		}
		this.Popup.destroy();
		if(this.Mask){
			this.Mask.destroy();
		}
	}
}

$.UI.Msgbox = {
	config : {
		Popup : {
			html : '<span class="ui_msg_box_content"></span>',
			pos : {
				top : 'middle',
				left : 'center'
			},
			frameCssName : 'ui_msg_box'
		},
		content : '',
		timeout : 2000,
		width : 300,
		height : 150,
		onclose : $.UI.core.emptyFn
	},

	init : function(opts) {
		opts.width? this.config.Popup.width = opts.width : this.config.Popup.width = this.config.width;
		opts.height? this.config.Popup.height = opts.height : this.config.Popup.height = this.config.height;
		var _config = $.UI.core.handleConfig(opts, this.config);	
		return new $.UI.Msgbox.pro(_config);
	}
}

$.UI.Msgbox.pro = function(config) {
	this.config = config;
	this.constructor();
}

$.UI.Msgbox.pro.prototype = {
	constructor : function(){
		var _this = this;
			_this.Popup = $.UI.Popup.init(_this.config.Popup);
			_this.Popup.obj.find('.ui_msg_box_content').html(_this.config.content);
			_this.show();
	},
	
	show : function(){
		_this = this;
		this.Popup.show();
		setTimeout(function(){
			_this.config.onclose();
			_this.destroy();
		}, _this.config.timeout);
	},
	
	hide : function(){
		this.Popup.hide();
	},
	
	destroy : function() {
		this.Popup.destroy();
	}
}

$.UI.Counter = {
	config : {
		textarea : '',
		tip : '',
		max : 140,
		onerror : $.UI.core.emptyFn,
		onsuccess : $.UI.core.emptyFn
	},
	
	init : function(opts) {		
		var _config = $.UI.core.handleConfig(opts, this.config);	
		return new $.UI.Counter.pro(_config);
	}
}

$.UI.Counter.pro = function(config) {
	this.config = config;
	this.constructor();
}

$.UI.Counter.pro.prototype = {
	constructor : function(){
		var _this = this;
		setInterval(function(){
			_this.count();
		}, 250);
	},
	
	count : function(){
		var _this = this;
		var val = _this.config.textarea.val(), count = 0, html;
			count = jQuery.trim(val).length;
		if (count > _this.config.max) {
			html = '<span style="color:#ff0000;">超出'+ Math.abs(_this.config.max - count) +'字</span>';
			_this.config.onerror();
		} else {
			html = '还能输入'+ Math.abs(_this.config.max - count) +'字';
			_this.config.onsuccess();
		}
		_this.config.tip.html(html);
	},
	
	getTexNum : function(){
		return jQuery.trim(this.config.textarea.val()).length;
	}
}

BZ.lang = {
	trim : function(str,charlist){var whitespace,l=0,i=0;str+='';if(!charlist){whitespace=" \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";}else{charlist+='';whitespace=charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g,'$1');}
l=str.length;for(i=0;i<l;i++){if(whitespace.indexOf(str.charAt(i))===-1){str=str.substring(i);break;}}
l=str.length;for(i=l-1;i>=0;i--){if(whitespace.indexOf(str.charAt(i))===-1){str=str.substring(0,i+1);break;}}
return whitespace.indexOf(str.charAt(0))===-1?str:'';},

	encodeHTML : function(str){
		str = str.replace(/&/g, '&amp;');
		str = str.replace(/</g, '&lt;');
		str = str.replace(/>/g, '&gt;');
		str = str.replace(/'/g, '&#039;');
		str = str.replace(/"/g, '&quot;');
		return str;
	}
}

function onPtlogin2success(){
	$.ajax({      
		url: 'Sign.html',  
		type: 'POST',
		success: function(json) {    
		   $('#user').html('欢迎您，'+ json +' <a id="logout" href="#">[退出]</a>');
		   BZ.params.userNick = json;
		   BZ.header.logout();
		   dialog_login.destroy();
		},
        dataType:'json'
	});  
}

function ptlogin2_onResize(width, height) {
	var login_wnd = document.getElementById("login_div");
	if (login_wnd) {
		// 重新设置大小注意，一定要加px，否则firefox下有问题
		login_wnd.style.width = width + "px";
		login_wnd.style.height = height + "px";
		// 最好重新调整登录框的位置， 这儿略....
		// 先隐藏，在显示，这样可以避免滚动条的出现
		login_wnd.style.visibility = "hidden"
		login_wnd.style.visibility = "visible"
	}
}

function ptlogin2_onLogin() {
	// 如果需要继续登录操作，则返回true, 否则，请返回false
	return true;
}

function ptlogin2_onClose() {
	// alert("ptlogin2_onClose");
}

$(function(){
    BZ.header.init();
	BZ.sidebar.init();
	BZ.license.init();
	BZ.footer.init();
});

