var msie6 = function(){
	if( $.browser.msie && $.browser.version <= 6 )
		return true;
}
var mailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/,
	htmlReg = /^[\u4e00-\u9fa5_ a-zA-Z0-9]+$/,
	mathReg = /^-?\d+$/;
/*
window.onload=function(){
	for(var i=0; i<document.links.length; i++)
		document.links[i].onfocus = function(){
			this.blur();
		}
}
*/
/* 在jQuery框架底层里用到 */
var joenixTimeout;
var joenixload = function(){
	var nload = $('<div class="noload"></div>'),
		iload = $('<div id="no"></div>').append(nload),
		speed = 'fast';
	joenixTimeout = setTimeout(function(){
			iload.fadeIn(speed);
			nload.fadeIn(speed);
			clearTimeout(joenixTimeout);
		},1000);
	$(document.body).append(iload);
}
var joenixover = function(){
	var iload = $('#no'),
		nload = iload.find('.noload'),
		speed = 'fast';
	nload.fadeOut(speed,function(){
		iload.fadeOut(speed,function(){
			clearTimeout(joenixTimeout);
			nload.remove();
			iload.remove();
		});
	});
}
var cnzz = function(){
	//$('[title=站长统计]').remove();
	var cnzz    = $('[title=站长统计]'),
		cnzzimg = cnzz.nextAll('img');
	cnzzimg.each(function(){
		if( $(this).attr('src').indexOf('cnzz.com') != -1 )
			$(this).remove();
	});
	cnzz.remove();
}
$.fn.extend({
	//渐入式动画
	grandin : function( num ){
		//初始化存储
		var oWidth  = this.width(),
			oHeight = this.height(),
			oTop    = Number( this.css('margin-top').replace('px','') ),
			oLeft   = Number( this.css('margin-left').replace('px','') );
		
		//初始化执行
		this.css({
			'width'       : oWidth - num*2,
			'height'      : oHeight - num*2,
			'margin-top'  : oTop + num,
			'margin-left' : oLeft + num
		});
		
		//执行动画
		this
		.show(0)
		.animate({
			'width'       : oWidth,
			'height'      : oHeight,
			'margin-top'  : oTop + 'px',
			'margin-left' : oLeft + 'px'
		},250);
		
		return this;
	},
	//弹出层
	ipop : {
		//弹出动作
		popup : function( params , fn ){
		/*
		 * width  : 宽度 / Number
		 * height : 高度 / Number
		 * speed  : 速度 / Number or 'fast'.'normal'.'slow'
		 * title  : 标题 / String
		 * url    : 地址 / http
		 */
			var __this = this,
				icss  = '';
				icss += '<style type="text/css">';
				
				//popup
				icss += '.popup{';
				msie6() ?
					icss += 'position:absolute;' :
					icss += 'position:fixed;' ;
				icss += 'margin-top:-' + params.height/2 + 'px;';
				icss += 'margin-left:-' + params.width/2 + 'px;';
				icss += 'overflow:hidden;';
				icss += 'top:50%;left:50%;z-index:100000;';
				icss += 'border:7px solid #ababab;';
				icss += 'background:white;';
				icss += '}';
				
				//popup -> title
				icss += '.popup .t{';
				icss += 'height:46px;line-height:44px;padding-left:1em;';
				icss += 'font-size:14px;font-weight:bold;color:#242424;';
				icss += 'cursor:pointer;';
				icss += 'background:url(' + static_domain + '/image/popup_t.png) repeat-x;';
				icss += '}';
				
				//popup -> cont
				icss += '.popup .c{';
				icss += 'width:' + params.width + 'px;';
				icss += 'height:' + params.height + 'px;';
				icss += 'background-color:#ebebeb;';
				//icss += 'padding:.5em;';
				icss += '}';
				
				//popup -> close
				icss += '.popup a.pclose{';
				icss += 'width:9px;height:9px;';
				icss += 'top:18px;right:16px;';
				icss += 'background:url(' + static_domain + '/image/close.gif) no-repeat;';
				icss += '}';
				
				//mask
				icss += '.mask{';
				msie6() ?
					icss += 'height:' + $(document).height() + 'px;' :
					icss += 'height:100%;';
				msie6() ?
					icss += 'position:absolute;' :
					icss += 'position:fixed;';
				icss += 'width:100%;top:0;left:0;z-index:99999;';
				icss += 'background:black;';
				icss += '}';
				
				icss += '</style>';
				icss  = $(icss);
			
			var clos = $('<a class="pclose position_ab"></a>'),
				titl = $('<div class="t">' + params.title + '</div>').prepend( clos ),
				cont = $('<div class="c"></div>');
				
				this.icss = icss;
				this.oDiv = $('<div class="popup dis_none"></div>').append( titl ).append( cont );
				this.mask = $('<div class="mask alpha0"></div>');
				this.speed = params.speed;
			
			var db = $(document.body);
			
			db.prepend( this.icss )
			  .prepend( this.mask )
			  .prepend( this.oDiv );
			this.mask.animate({opacity:'.5'},this.speed);
			
			/* Ie6 Patch */
			if( msie6() )
				if( this.oDiv.width() > params.width )
					this.oDiv.width( params.width )
			
			/*
			 * 拖拽
			 */
			titl.mousedown(function(e){
				var iw = e.clientX - __this.oDiv.offset().left,
					ih = e.clientY - __this.oDiv.offset().top;
				
				db.mousemove(function(e){
					var mt = e.clientY - ih - $(document).scrollTop(),
						ml = e.clientX - iw;
					__this.oDiv.css({
						'top'  : mt,
						'left' : ml,
						'margin-top' : 0,
						'margin-left' : 0
					});
					
				});
			});
			db.mouseup(function(){
				db.unbind('mousemove');
			});

			window.document.onkeydown = function(e){
				var e = e || window.event;
				if( e.keyCode == 27 )
					__this.close();
			}
			
			cont.load(params.url,function(){
				__this.oDiv.grandin( 50 );
				if( fn ) fn( $.fn.ipop );
			});
			
			clos.unbind().bind('click',function(){
				__this.close();
			});
			
			return $.fn.ipop;
		},
		operat : function( params , fn ){
		/*
		 * pond  : 绑定对象 || this
		 * dir   : 方向 / left || right
		 * turn  : 微调.左右 / Number
		 * upon  : 微调.上下 / Number
		 * leav  : 距离值 || false or Number
		 * ivs   : 逆 || true or false
		 * speed : 速度 / Number
		 * url   : 地址 / http
		 */
		 	
		 	if( $('.operat').length >= 1 ) return;
		 
			var __this = this,
				icss  = '<style type="text/css">';
				icss += '.operat{';
				icss += 'z-index:100;';
				icss += 'position:absolute;';
				icss += 'background:url(' + static_domain + '/image/no.gif) repeat;';
				icss += '}';
				icss += '</style>';
				icss  = $(icss);
			
			params.turn === undefined ? params.turn = 0 : params.turn;
			params.upon === undefined ? params.upon = 0 : params.upon;
			params.leav === undefined ? params.leav = false : params.leav;
			params.ivs  === undefined ? params.ivs  = false : params.ivs;
			
			this.icss = icss;
			this.oDiv = $('<div class="operat dis_none"></div>');
			this.speed = params.speed;
			
			$(document.body).prepend( this.icss )
							.prepend( this.oDiv );
			
			var ppo_t = params.pond.offset().top,
				ppo_l = params.pond.offset().left,
				ppo_w = params.pond.outerWidth(),
				ppo_h = 0;
			
			!params.leav ?
				ppo_h = params.pond.height():
				ppo_h = params.leav;
			
			window.document.onkeydown = function(e){
				var e = e || window.event;
				if( e.keyCode == 27 )
					$.fn.ipop.close();
			}
			
			this.oDiv.load(params.url,function(){
				switch( params.dir ){
					case 'left' :
						params.ivs ?
							__this.oDiv.css('padding-bottom',ppo_h):
							__this.oDiv.css('padding-top',ppo_h);
							
						__this.oDiv.css({
							'top'        : ppo_t  + params.upon,
							'left'       : ppo_l + params.turn
						});
					break;
					case 'right' :
						params.ivs ?
							__this.oDiv.css('padding-bottom',ppo_h):
							__this.oDiv.css('padding-top',ppo_h);
						
						__this.oDiv.css({
							'top'        : ppo_t + params.upon,
							'left'       : ppo_l - __this.oDiv.outerWidth() + ppo_w + params.turn
						});
					break;
					default: break;
				}
				__this.oDiv.fadeIn(params.speed,function(){
					if( fn ) fn( $.fn.ipop );
					__this.oDiv.mouseleave('mouseout',function(){
						$.fn.ipop.close();
					});
				});
			});
			
			return $.fn.ipop;
		},
		//滑动层
		marvel : function(speed){
			var	$doc     = $(document.body),
				$marvel  = $('[name=marvel]'),
				$content = $('<div class="mcontent"></div>'),
				$object  = $('<div class="marvel"></div>');
				$object.append($content);
			
			if( !speed )
				var speed = 'fast';
			
			var msin,  // 鼠标进入时
				mson,  // 鼠标移置弹出层之上时
				msout, // 鼠标移出时
				delay = 777; // 延时时间
			
			$marvel.die('mouseenter').live('mouseenter',function(){
				var $this = $(this);
				clearTimeout( msout );
				msin = setTimeout(function(){
					$doc.prepend($object);
					var $width  = $object.outerWidth(),
						$height = $object.outerHeight(),
						$top    = $this.offset().top,
						$left   = $this.offset().left,
						$type   = 'downright',
						$uid    = $this.parents('[uid]').attr('uid'),
						$url    = iknowing_domain + '/user/' + $uid + '/archive.html';
					
					$top < $height + $(document).scrollTop() ?
						$left < $width ?
							$type = 'downright' :
							$type = 'downleft'		:
						$left < $width ?
							$type = 'upright' :
							$type = 'upleft'		;
					
					$object.removeClass().addClass('marvel_'+$type);
					
					switch( $type ){
						case 'downright' :
							$object.css({
								'top'  : $top + $this.height(),
								'left' : $left + $this.width()/2 - 38
							});
							break;
						case 'downleft' :
							$object.css({
								'top'  : $top + $this.height(),
								'left' : $left - $width + $this.width()/2 + 38
							});
							break;
						case 'upright' :
							$object.css({
								'top'  : $top - $height,
								'left' : $left + $this.width()/2 - 38
							});
							break;
						case 'upleft' :
							$object.css({
								'top'  : $top - $height,
								'left' : $left - $width + $this.width()/2 + 38
							});
							break;
						default : break;
					}
					
					$content.load($url,function(){
						$object.fadeIn(speed,function(){
							
							/* Marvel School */
							$('#marvelschool li').mouseenter(function(){
								var $this = $(this);
								$this.siblings().stop().animate({
									'width' : 50
								},'normal');
								$this.stop().animate({
									'width' : 100
								},'normal');
							});
														
							/*
							var $w   = $object.outerWidth(),
								$h   = $object.outerHeight(),
								$t   = $object.offset().top - $(document).scrollTop(),
								$l   = $object.offset().left;
							$doc.click(function(e){
								if( (e.clientX < $l || e.clientX > $l+$w) || (e.clientY < $t || e.clientY > $t+$h) ){
									$object.fadeOut(speed,function(){
										$object.remove();
									});
								}
							});
							*/
						});
					});
				},delay);
			}).live('mouseleave',function(){
				clearTimeout( msin );
				msout = setTimeout(function(){
					$object.fadeOut(speed,function(){
						$object.remove();
					});
				},delay);
				$content.mouseenter(function(){
					clearTimeout( msout );
				}).mouseleave(function(){
					msout = setTimeout(function(){
						$object.fadeOut(speed,function(){
							$object.remove();
						});
					},delay);
				});
			});
			return this;
		},
		//气泡提示
		tip : function(){
			/*
			$('*[tip]').mouseenter(function(){
				var __this = $(this);
				var	icss  = '<style type="text/css">';
					icss += '.tip{';
					icss += 'z-index:88888;';
					icss += 'position:absolute;';
					icss += 'border:1px solid black;';
					icss += '}';
					icss += '</style>';
				var html  = $('<div class="tip"></div>');
				
				__this.css({
					'top'  : __this.offset().top  + __this.outerHeight() + html.height(),
					'left' : __this.offset().left + __this.outerWidth/2
				});
			});
			*/
		},
		//连环提示
		chaintips : function( name ){
			if( getCookie('chaintips') == null ){
				setCookie('chaintips','joenix');

				var chainarr = [],
					$object  = $('[chname='+name+']'),
					$now     = 0,
					$end     = false,
					$tips    = $('<div class="chaintips"></div>');
				
				if( $object.length > 0 ){
					$object.each(function(){
						chainarr.push( $(this).attr('val').split(',') );
					});
					
					function getHtml( params , begin , end ){
						var prev = '上一条',
							next = '下一条';
						if( end ) next = '关闭';
						var html  = '<a class="del" name="chdel" href="javascript:;"></a>';
							html += '<div class="til">'+params[0]+'</div>';
							html += '<div class="con">'+params[1]+'</div>';
							html += '<div class="men">';
						if( begin != 0 )
							html += '<a name="chprev" href="javascript:;">'+prev+'</a>';
							html += '<a name="chnext" href="javascript:;">'+next+'</a>';
							html += '</div>';
						return $(html);
					}
					function doAnimate( o , index ){
						o.stop().animate({
							'top'  : $object.eq(index).offset().top + $object.eq(index).height(),
							'left' : $object.eq(index).offset().left - 12
						},'slow');
					}
					$tips.append( getHtml( chainarr[$now] , $now , $end ) );
					$(document.body).prepend($tips);
					doAnimate( $tips , $now );
					
					// Prev
					$('[name=chprev]').live('click',function(){
						if( $now > 0 ) $now--;
						$end = false;
						
						doAnimate( $tips , $now );
						$tips.html( getHtml( chainarr[$now] , $now , $end ) );
					});
					
					// Next
					$('[name=chnext]').live('click',function(){
						if( $(this).html() == '关闭' ){
							$tips.fadeOut('normal',function(){
								$tips.remove();
							});
						}
						else{
							if( $now < chainarr.length-1 ) $now++;
							if( $now == chainarr.length-1 ) $end = true;
							doAnimate( $tips , $now );
							$tips.html( getHtml( chainarr[$now] , $now , $end ) );
						}
					});
					
					// Delete
					$('[name=chdel]').live('click',function(){
						$tips.fadeOut('normal',function(){
							$tips.remove();
						});
					});
				}
			}
		},
		//关闭动作
		close : function( fn ){
			var o = $.fn.ipop;
			o.oDiv.fadeOut(o.speed,function(){
				$(this).remove();
				o.icss.remove();
			});
			if( o.mask != undefined ){
				o.mask.fadeOut(o.speed,function(){
					$(this).remove();
					if( fn ) fn();
				});
			}
			
			return $.fn.ipop;
		},
		//拓展执行函数
		ifn : function( fn ){ fn ? fn( $.fn.ipop ) : fn }
	},
	// AutoComplete
	autocomplete : function( url ){
		$(this).die('keyup').live('keyup',function(){
			var $this = $(this),
				$val  = $this.val();
			$.ajax({
				url      : url,
				type     : 'POST',
				data     : 'word=' + $val,
				dataType : 'text',
				success  : function( data ){
					data=eval('('+data+')');
					if( data.error == 'true' ) trace( data.msg )
					else{
						$('#autocomplete').remove();
						if( data.length > 0 ){
							var html = '';
								html += '<div id="autocomplete">';
							for( var i=0; i<data.length; i++ ) html += '<a href="javascript:;">' + data[i] + '</a>';
								html += '</div>';
							html = $(html);
							html.css({
								'width' : $this.outerWidth()-2,
								'top'   : $this.offset().top + $this.outerHeight(),
								'left'  : $this.offset().left
							});
							$this.after( html );
							
							html.mouseenter(function(){
								html.find('a').click(function(){
									var $$this = $(this),
										$$val  = $$this.html();
									$this.unbind('focusout')
									$this.val( $$val );
									html.remove();
								});
							});
							$(document).click(function(e){
								var eX = e.clientX,
									eY = e.clientY,
									oT = $this.offset().top,
									oL = $this.offset().left,
									oW = $this.outerWidth(),
									oH = $this.outerHeight();
								if( (eX < oL || eX > oL+oW) || (eY < oT || eY > oT+oH) ){
									html.remove();
								}
							});
						}
					}
				}
			});
		});
	},
	// Ajax Save
	ajaxSave : function( fn ){
		var	$categoryId = $('.nd_select').val(),
			$noteId = $('#windows_content').attr('nid'),
			$title = $('.nd_text').val(),
			$notecontent = xedit.getSource(),
			$description = $('#edit_des').attr('title'),
			$sourceUrl   = $('#source').attr('title'),
			$privacy     = $('.nd_privacy input:checked').val(),
			$tags        = '',
			$tagObject   = $('#plus_tag a');
			
			for( var i=0 ; i<$tagObject.length ; i++ )
				$tags += $tagObject.eq(i).html().split('<b>&nbsp;</b>')[0] + '|' + $tagObject.eq(i).attr('tagid') + ',';
			
			if($title.length <= 0)
				$title = '创建新笔记';
		
		function vf(){
			// Verification
			if( $categoryId == '' ){
				return false;
			}
			if( $noteId == '' ){
				return false;
			}
			if( $title.length > 128 ){
				return false;
			}
			if( $notecontent ){
				//return true;
			}
			if( $description == undefined ){
				$description = '';
			}
			if( $description.length > 200 ){
				return false;
			}
			if( $sourceUrl != '' && $sourceUrl != undefined && $sourceUrl.indexOf('http://') ){
				return false;
			}
			if( $privacy != 0 && $privacy != 2 ){
				return false;
			}
			if( $tags.split(',').length > 11 ){
				return false;
			}
			return true;
		}
		
		if( vf() ){
			var $data = 'categoryId='+$categoryId+'&noteId='+$noteId+'&title='+$title+'&notecontent='+$notecontent+'&description='+$description+'&sourceUrl='+$sourceUrl+'&privacy='+$privacy+'&tags='+$tags;
			$.ajax({
				url      : iknowing_domain + '/home/note/quick_save.html',
				type     : 'POST',
				data     : $data,
				dataType : 'text',
				success  : function( data ){
					data=eval('('+data+')');
					if( data.error == 'true' ) trace( data.msg )
					else{
						if( fn ) fn();
					}
				}
			});
		}
	},
	// 上传附件
	fileupload : function( url , id , fn ){
		$.ajaxFileUpload({
			url           : url,
			secureuri     : false,
			fileElementId : id,
			dataType      : 'text',
			beforeSend    : joenixload(),
			complete      : joenixover(),
			success       : function (data,status){
				data = eval('(' + data + ')');
				if( data.error == 'true' ) trace( data.msg )
				else{
					if( fn ) fn( data );
				}
			}
		});
	},
	//Text输入框默认提示
	defaultTxT : function( msg ){
		var $this = $(this);
			$this
				.die('focusin').live('focusin',function(){
					if( $this.val() == msg )
						$this.val('');
				})
				.die('focusout').live('focusout',function(){
					if( $this.val() == '' || $this.val() == msg )
						$this.val(msg);
				});
		return this;
	},
	//输入字最大限制
	defaultInput : function(o,num){
		var $this = $(this);
		$this.keyup(function(){
			var $len = $this.val().length,
				$dfc = num - $len;
			o.text($dfc);
			$dfc < 0 ?
				o.css('color','#c80000') :
				o.css('color','');
		});
	},
	//maxTxT
	maxTxT : function( o , num ){
		var $this  = $(this),
			$color = function(s){
				s < 0 ?
					o.find('.special').css('color','#c80000') :
					o.find('.special').css('color','') ;
			}
		$this.live('keyup',function(){
			var $len = $this.val().length,
				$dfc = num-$len;
			o.find('.special').text($dfc);
			$color($dfc);
		});
	},
	//树节点编辑
	treenEdit : function(){
		var $menu = $(this).find('bdo[name=edit]');
		$menu.die('click').live('click',function(){
			var $this   = $(this),
				$cid    = $this.parents('a').attr('cid'),
				$object = $this.parent().prev(),
				$old    = $object.html(),
				$val    = $object.find('b').eq(0).html(),
				$num    = $object.find('b').eq(1).html(),
				$html   = $('<input type="text" class="tree_text" maxlength="50" value="'+$val+'">');
			$object.html( $html );
			$html.focus()
				.focusout(function(){
					oDo( $(this) );
				})
				.keydown(function(event){
					if( event.keyCode == 13 )
						oDo( $(this) );
				});
			function oDo( o ){
				var $this$ = o,
					$val$  = $this$.val();
				if( $val$ == $val || $val$ == '' ){
					$object.html( $old );
				}

				if( !htmlReg.test($val$) ){
					trace('输入字符非法！',function(){
						$object.html( $old );
					});
					return;
				}
				else{
					var $data = 'name=' + encodeURIComponent($val$);
					$.ajax({
						type    : 'GET',
						url     : iknowing_domain + '/category/'+$cid+'/update.html',
						data    : $data,
						success : function(data){
							data = eval('(' + data + ')');
							if( data.error == 'true' ) trace( data.msg )
							else{
								var $html$ = $('<b class="name">'+$val$+'</b><b class="tree_num">'+$num+'</b>');
								$object.html( $html$ );
							}
						}
					});
				}
			}
		});
	},
	//记录OS Tree 位置
	treeScroll : function(){
		var $href = window.location.href,
			$sTop = 0;
		if( $href.indexOf('home') != -1 ){
			window.onunload = function(){
				$sTop = $('#tree').scrollTop();
				setCookie('sTop',$sTop);
			}
			$('#tree').scrollTop( getCookie('sTop') );
		}
	},
	//Note拖拽
	notemove : function(){
		var $note = $(this),
			$file = $('<div class="filemove"><bdo></bdo></div>');
			$dob  = $(document.body),
			$icss = '';
			$icss += '<style type="text/css">';
			$icss += 'body{';
			$icss += 'user-select:none;'; /* CSS3 */
			$icss += '-moz-user-select:-moz-none;'; /* mozzilla firefox 2+ */
			$icss += '-webkit-user-select:none;'; /* safari 3+ / chrome */
			$icss += 'cursor:pointer;';
			$icss += '}';
			$icss += '</style>';
			$icss = $($icss);
			
			var $targ = $('.tree_info[name=my_lib] .tree_cot'),
				$tarr = [],
				$type = false;
			
			$note.mousedown(function(event){
				var $this  = $(this),
					$minX  = $targ.offset().left,
					$minY  = $targ.offset().top,
					$maxX  = $minX + $targ.width(),
					$maxY  = $minY + $targ.height(),
					$index = -1,
					$nid   = $this.attr('nid'),
					$ocid  = $this.attr('cid'),
					$moveSwitch = false;
					$file.css('opacity',100);
				
				for( var i=0 ; i<$targ.find('a').length ; i++ )
					$targ.find('a').eq(i).hasClass('hasmenu_on') ?
						$tarr.push( $targ.find('a').eq(i) ) :
						$tarr.push(0);
				
				$type = true;
				$file.stop();
				
				var thanX = event.clientX,
					thanY = event.clientY;
				$dob.mousemove(function(event){
					if( event.clientX == thanX ){
						return;
					}
					else{
						var notelist = $('[name=my_note_list] li');
						if( $this.find('input:checked').length <= 0 )
							$this.find(':checkbox').attr('checked',true);
						
						var	notehascheck = notelist.find('input:checked');
						notehascheck.length > 1 ?
							$file.children().html('您选择了' + notelist.find('input:checked').length + '个文件') :
							$file.children().html( $this.find('.txt').html() );
						
						$dob.prepend($file).prepend($icss);
						filePosition(event);
						
						var clientX = event.clientX,
							clientY = event.clientY;
						if( (clientX >= $minX && clientY >= $minY) && (clientX < $maxX && clientY < $maxY) ){
							$index = parseInt((clientY-$minY)/28);
							targetHover( $targ.find('a') , $index , 'hasmenu' , 'hasmenu_on' );
							$dob.unbind('mouseup').bind('mouseup',function(){
								$file.remove();
								$icss.remove();
								$dob.unbind('mousemove').unbind('mouseup');
								clearTargetHover( $targ.find('a') , 'hasmenu_on' , 'hasmenu' );
								var $cid = $targ.find('a').eq($index).attr('cid');
								if( $ocid == $cid ){
									$dob.unbind('mousemove').unbind('mouseup');
									clearTargetHover( $targ.find('a') , 'hasmenu_on' );
									$file.remove();
									$icss.remove();
									return;
								}
								if( $type == true ){
									$type = false;
									if( notehascheck.length <= 1 ){
										$.ajax({
											type    : 'POST',
											url     : iknowing_domain + '/note/'+$nid+'/move.html?catId='+$cid,
											data    : '',
											success : function(data){
												data = eval('(' + data + ')');
												if( data.error == 'true' ) trace( data.msg )
												else{
													$this.slideUp('normal',function(){
														$this.remove();
														fresh();
													});
												}
											}
										});
									}
									else{
										var $ajaxType = false;
										for( var i=0; i<notehascheck.length; i++ ){
											$.ajax({
												type    : 'POST',
												url     : iknowing_domain + '/note/'+notehascheck.eq(i).parents('li').attr('nid')+'/move.html?catId='+$cid,
												data    : '',
												success : function(data){
													data = eval('(' + data + ')');
													if( data.error == 'true' ) trace( data.msg )
													else{
														if( i == notehascheck.length-1 ){
															$ajaxType = true;
															notehascheck.parents('li').each(function(){
																var $this = $(this);
																$this.slideUp('normal',function(){
																	$this.remove();
																});
															});
														}
													}
												}
											});
										}
										var interval = setInterval(function(){
											if( $ajaxType ){
												fresh();
												clearInterval( interval );
											}
										},50);
									}
								}
							});
						}
						else{
							$index = -1;
							clearTargetHover( $targ.find('a') , 'hasmenu_on' );
							
							$dob.unbind('mouseup').bind('mouseup',function(){
								$dob.unbind('mousemove').unbind('mouseup');
								clearTargetHover( $targ.find('a') , 'hasmenu_on' );
								$file.stop().animate({
									'top'     : $this.offset().top,
									'left'    : $this.offset().left,
									'opacity' : 0
								},'slow',function(){
									$file.remove();
									$icss.remove();
								});
							});
						}
					}
				});
				$dob.unbind('mouseup').bind('mouseup',function(){
					$dob.unbind('mousemove').unbind('mouseup');
				});
			});

			function filePosition(event){
				$file.css({
					'top'  : event.clientY - 12,
					'left' : event.clientX - 12
				});
			}
			function targetHover( o , index , n , h ){
				for( var i=0 ; i<o.length ; i++ ){
					if( $tarr[i] == 0 )
						o.eq(i).removeClass(h).addClass(n);
				}
				if( $tarr[index] == 0 )
					o.eq(index).removeClass(n).addClass(h);
			}
			function clearTargetHover( o , h , n ){
				for( var i=0 ; i<o.length ; i++ ){
					if( $tarr[i] == 0 )
						o.eq(i).removeClass(h).addClass(n)
				}
			}
	},
	picwall : function( params ){
		/*
		 * pando  : 照片墙在什么对象上显示
		 * swidth : 照片宽度
		 * url    : 照片墙请求的地址
		 */
		var win     = $(window),
			dob     = $(document.body),
			arrnum  = parseInt(($(document).width() - 32)/params.swidth),
			pageId  = 1,
			wallarr = [];
		
		params.pondo.css('padding-left',( dob.width() - (arrnum)*params.swidth )/2 - 16);
		params.pandin = params.pondo.children();
		
		/* 墙的列 */
		for( var i=0; i<arrnum; i++ ){
			// 生成列
			params.pandin.prepend('<div class="colum" style="width:'+params.swidth+'px;"></div>');
			// 初始化数组
			wallarr.push(0);
		}
		var colum  = params.pandin.find('.colum'),
			minNum = -1;
		
		/* 开始Ajax */
		var ajaxType = true; // 控制是否允许下一次Ajax触发
		function doAjax( pageId , colSize ){
			$.ajax({
				type    : 'GET',
				url     : params.url,
				data    : 'pageId=' + pageId + '&colSize=' + params.size,
				success : function(data){
					data = eval('(' + data + ')');
					if( data.error == 'true' ) trace( data.msg )
					else{
						if( data == '' && pageId == 1 ){
							$('.ifnoimage').css('display','block');
						}
						var i = 0,
							interval = setInterval(function(){
								if( i<data.length ){
									doAnimate(data[i]);
									i++;
								}
								else{
									ajaxType = true;
									clearInterval( interval );
								}
							},120);
					}
				}
			});
		}
		function doAnimate( data ){
			minNum = getMin(wallarr); // 返回数组中最小值的下标
			
			var $html = doHTML( data );
			
			colum.eq(minNum).append($html);
			if( !$.browser.msie )
				$html.fadeOut(0).fadeIn('fast');
			
			var oHeight = colum.eq(minNum).outerHeight();
			
			// 初始加载时会有拿不到image高度的问题
			if( $html.find('[iheight]').height() == 0 ){
				oHeight += Number( $html.find('[iheight]').attr('iheight') );
			}

			wallarr.splice( minNum , 1 , oHeight );
			doHover($html);
		}
		function doHTML( data ){
			var html  = '<div class="one" nid="' + data.noteId + '" uid="' + data.userId + '">';
				html += ' <div class="position_re">';
				html += '  <div class="topmenu">';
				html += '   <a href="javascript:;" name="like" title="喜欢" class="onelike"></a>';
				html += '   <a href="javascript:;" name="fav" title="收藏" class="onefav"></a>';
				html += '  </div>',
				html += ' </div>';
				html += ' <div class="onecover">';
				html += ' <div class="onecoverin">';
				html += '  <div class="oneimage"><img name="ja" href="' + iknowing_domain + '/note/' + data.noteId + '.html" iheight="' + data.height + '" src="' + data.picUrl + '"></div>';
				html += '  <a class="onetitle" href="' + iknowing_domain + '/note/' + data.noteId + '.html" title="' + data.title + '">' + data.title + '</a>';
				html += '  <div class="onedes">' + data.desc + '</div>';
				html += ' </div>';
				html += '  <div class="oneauthor">';
				html += '   <a class="oneauthorhead" href="' + iknowing_domain + '/user/' + data.userId + '.html"><img src="' + data.userPic + '"></a>';
				html += '   <div class="oneauthorcont">';
				html += '    <a name="marvel" href="' + iknowing_domain + '/user/' + data.userId + '.html">' + data.userName + '</a>';
				html += '    <b>' + data.createTime + '</b>';
				html += '   </div>';
				html += '  </div>';
				html += '  <div class="onetag">';
				html += '   <b>标签：</b>';
				for( var i=0; i<data.tagList.length; i++ ){
					html += '<a href="javascript:;">'+data.tagList[i]+'</a>';
				}
				html += '  </div>'
				html += '  <div class="onehd"><a href="javascript:;">评论' + data.commentList.length + '</a>　<a name="like" href="javascript:;">喜欢<b>' + data.likeCount + '</b></a>&nbsp;<a name="fav" href="javascript:;">收藏<b>' + data.collectCount + '</b></a></div>';
				html += ' <div class="onepl">';
				html += '  <ul>';
				for( var j=0; j<data.commentList.length; j++ ){
					html += '   <li uid="' + data.commentList[j].userId + '" commid="' + data.commentList[j].commentId + '">';
					html += '    <div class="cloosii">';
					html += '     <a class="onehead" href="' + iknowing_domain + '/user/' + data.commentList[j].userId + '.html"><img src="' + data.commentList[j].userImageUrl + '"></a>';
					html += '     <div class="oneright">';
					html += '      <a class="oneplname" href="' + iknowing_domain + '/user/' + data.commentList[j].userId + '.html">' + data.commentList[j].userName + '</a>';
					html += '      <div>' + data.commentList[j].content + '</div>';
					html += '     </div>';
					html += '    </div>';
					html += '   </li>';
				}
				html += '  </ul>';
				html += ' </div>';
				html += ' </div>';
				html += ' <div class="oneshandow"></div>';
				html += '';
				html += '</div>'
			return $(html);
		}
		
		function doHover(o){
			o.hover(
				function(){
					$(this).find('.topmenu').css('display','block');
				},
				function(){
					$(this).find('.topmenu').css('display','none');
				}
			);
		}
		
		doAjax(pageId);
		win.scroll(function(){
			if( ajaxType && minNum != -1 && win.scrollTop() + win.height() >= colum.eq(minNum).outerHeight() - win.height() ){
				ajaxType = false;
				pageId++;
				doAjax(pageId);
			}
		});
	},
	picwalleseebaby : function( params ){
		/*
		 * pando  : 照片墙在什么对象上显示
		 * swidth : 照片宽度
		 * url    : 照片墙请求的地址
		 */
		var win     = $(window),
			dob     = $(document.body),
			arrnum  = parseInt(($(document).width() - 32)/params.swidth),
			pageId  = 1,
			wallarr = [];
		
		params.pondo.css('padding-left',( dob.width() - (arrnum)*params.swidth )/2 - 16);
		params.pandin = params.pondo.children();
		
		/* 墙的列 */
		for( var i=0; i<arrnum; i++ ){
			// 生成列
			params.pandin.prepend('<div class="colum" style="width:'+params.swidth+'px;"></div>');
			// 初始化数组
			wallarr.push(0);
		}
		var colum  = params.pandin.find('.colum'),
			minNum = -1;
		
		/* 开始Ajax */
		var ajaxType = true; // 控制是否允许下一次Ajax触发
		function doAjax( pageId , colSize ){
			$.ajax({
				type    : 'GET',
				url     : params.url,
				data    : 'pageId=' + pageId + '&colSize=' + params.size,
				success : function(data){
					data = eval('(' + data + ')');
					if( data.error == 'true' ) trace( data.msg )
					else{
						if( data == '' && pageId == 1 ){
							$('.ifnoimage').css('display','block');
						}
						var i = 0,
							interval = setInterval(function(){
								if( i<data.length ){
									doAnimate(data[i]);
									i++;
								}
								else{
									ajaxType = true;
									clearInterval( interval );
								}
							},120);
					}
				}
			});
		}
		function doAnimate( data ){
			minNum = getMin(wallarr); // 返回数组中最小值的下标
			
			var $html = doHTML( data );
			
			colum.eq(minNum).append($html);
			if( !$.browser.msie )
				$html.fadeOut(0).fadeIn('fast');
			
			var oHeight = colum.eq(minNum).outerHeight();
			
			// 初始加载时会有拿不到image高度的问题
			if( $html.find('[iheight]').height() == 0 ){
				oHeight += Number( $html.find('[iheight]').attr('iheight') );
			}

			wallarr.splice( minNum , 1 , oHeight );
			doHover($html);
		}
		function doHTML( data ){
			var html  = '<div class="one" nid="' + data.noteId + '" uid="' + data.userId + '">';
				html += ' <div class="position_re">';
				html += '  <div class="topmenu">';
				html += '   <a href="javascript:;" name="like" title="喜欢" class="onelike"></a>';
				html += '   <a href="javascript:;" name="fav" title="收藏" class="onefav"></a>';
				html += '  </div>',
				html += ' </div>';
				html += ' <div class="onecover">';
				html += ' <div class="onecoverin">';
				html += '  <div class="oneimage"><img title="' + data.userName + '" name="ja" href="' + iknowing_domain + '/note/' + data.noteId + '.html" iheight="' + data.height + '" src="' + data.picUrl + '"></div>';
				html += '  <a class="onetitle" href="' + iknowing_domain + '/note/' + data.noteId + '.html" title="' + data.title + '">' + data.title + '</a>';
				html += '  <div class="onedes">' + data.desc + '</div>';
				html += ' </div>';
				html += ' </div>';
				html += ' <div class="oneshandow"></div>';
				html += '';
				html += '</div>'
			return $(html);
		}
		
		function doHover(o){
			o.hover(
				function(){
					$(this).find('.topmenu').css('display','block');
				},
				function(){
					$(this).find('.topmenu').css('display','none');
				}
			);
		}
		
		doAjax(pageId);
		win.scroll(function(){
			if( ajaxType && minNum != -1 && win.scrollTop() + win.height() >= colum.eq(minNum).outerHeight() - win.height() ){
				ajaxType = false;
				pageId++;
				doAjax(pageId);
			}
		});
	},
	imstips : function( params ){
		var $interval = setInterval(doInterval,30000);
		function doInterval(){
			$.ajax({
				url      : iknowing_domain + '/msg/getNewMsgCount.html',
				type     : 'GET',
				dataType : 'text',
				success  : function( data ){
					data=eval('('+data+')');
					if( data.status == 'true' ){
						var $object = $('.imstips');
						$object.load(iknowing_domain+'/msg/getNewMsg.html',function(){
							$object.fadeIn('fast');
						});
					}
					else{
						$object.fadeOut('fast');
					}
				}
			});
		}
		doInterval();
		
		$('.imstipsclose').die('click').live('click',function(){
			$('.imstips').fadeOut('fast');
			clearInterval($interval);
		});
	},
	chooseall : function( params ){
		/*
		 * pondo : 绑定对象
		 */
		$(this).click(function(){
			var $this = $(this);
			$this.attr('checked') == 'checked' ?
				params.pondo.find('input:checkbox').attr('checked',true):
				params.pondo.find('input:checkbox').attr('checked',false);
		});
	},
	aload : function(o,href){
		var $this = $(this),
			$href,
		$href = href ? href : $this.attr('href');
		$this.click(function(e){
			o.load($href);
			e.stopPropagation();
			return false;
		});
	},
	setHomepage : function( params ){
		/*
		 * url : site address
		 */
		$(this).click(function(){
			if( $.browser.msie ){
				document.body.style.behavior='url(#default#homepage)';
				document.body.setHomePage('http://'+params.url);
			}
			else{
				if( window.netscape ){
					try{
						netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
					}
					catch(e){
						alert('该操作被浏览器拒绝!如果想启用该功能,请在地址栏内输入about:config,然后将项 signed.applets.codebase_principal_support 值该为true');
					}
					var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
					prefs.setCharPref('browser.startup.homepage','http://'+params.url);
				}
				else{
					alert('您的浏览器不支持此操作,请手动设置!');
				}
			}
		});
	},
	addFav : function( params ){
		/*
		 * name : site name
		 * url  : site address
		 */
		$(this).click(function(){
			if( $.browser.msie ){
				try{
					window.external.addFavorite('http://'+params.url,params.name);
				}
				catch(e){
					alert('添加失败!请使用 Ctrl+D 手动添加!');
				}
			}
			else if( window.sidebar ){
				window.sidebar.addPanel(params.name,'http://'+params.url,'');
			}
			else{
				alert('您的浏览器不支持此操作,请手动设置!');
			}
		});
	}
});

var trace = function( msg , fn , time ){
	var icss  = '<style type="text/css">';
		//alert
		icss += '.alert{';
		icss += 'width:240px; height:120px; line-height:120px;';
		icss += 'text-align:center;';
		icss += 'margin:-60px 0 0 -120px;';
		icss += 'top:50%; left:50%;';
		icss += 'z-index:999999;';
		icss += 'border:7px solid #ababab;';
		msie6() ?
			icss += 'position:absolute;':
			icss += 'position:fixed;';
		icss += 'background:white;'
		icss += '}';
		//mask
		icss += '.amask{';
		msie6() ?
			icss += 'height:' + $(document).height() + 'px;' :
			icss += 'height:100%;';
		msie6() ?
			icss += 'position:absolute;' :
			icss += 'position:fixed;';
		icss += 'width:100%;top:0;left:0;z-index:888888;';
		icss += 'background:black;';
		icss += '}';
		icss += '</style>';
		icss  = $(icss);
	
	var mask  = $('<div class="amask alpha0"></div>'),
		html = $('<div class="alert">'+msg+'</div>').hide(0);
	
	$(document.body).prepend( icss )
					.prepend( mask )
					.prepend( html );
	
	mask.animate({
		'opacity':0.5
		},'fast');
	html.fadeOut(0).fadeIn('fast');
	
	window.document.onkeydown = function(e){
		var e = e || window.event;
		if( e.keyCode == 27 )
			aniact();
	}
	
	if( !time ) var time = 2000
	var tot = setTimeout(aniact,time);
	
	function aniact(){
		mask.fadeOut('fast',function(){
			mask.remove();
			icss.remove();
		});
		html.fadeOut('fast',function(){
			html.remove();
			if( fn ) fn();
		});
		clearTimeout( tot );
	}
}

var confirm = function( msg , fn ){
	var icss  = '<style type="text/css">';
		//alert
		icss += '.alert{';
		icss += 'width:240px; height:128px; line-height:80px;';
		icss += 'text-align:center;';
		icss += 'margin:-60px 0 0 -120px;';
		icss += 'padding-top:20px;';
		icss += 'top:50%; left:50%;';
		icss += 'z-index:999999;';
		icss += 'border:7px solid #ababab;';
		msie6() ?
			icss += 'position:absolute;':
			icss += 'position:fixed;';
		icss += 'background:white;'
		icss += '}';
		//imen
		icss += '.imen{';
		icss += 'padding:10px 0;';
		icss += '';
		icss += '';
		icss += '';
		icss += '}';
		icss += '.imen a{';
		icss += 'margin:0 5px;';
		icss += '}';
		//mask
		icss += '.amask{';
		msie6() ?
			icss += 'height:' + $(document).height() + 'px;' :
			icss += 'height:100%;';
		msie6() ?
			icss += 'position:absolute;' :
			icss += 'position:fixed;';
		icss += 'width:100%;top:0;left:0;z-index:888888;';
		icss += 'background:black;';
		icss += '}';
		icss += '</style>';
		icss  = $(icss);
	
	var mask = $('<div class="amask alpha0"></div>'),
		iyes = $('<a class="button_green" href="javascript:;">确认</a>'),
		ino  = $('<a class="button_gray" href="javascript:;">取消</a>'),
		imen = $('<div class="imen"></div>').prepend(ino).prepend(iyes),
		html = $('<div class="alert">'+msg+'</div>').append(imen).hide(0),
		type = false;
	
	$(document.body).prepend( icss )
					.prepend( mask )
					.prepend( html );
	
	mask.animate({
		'opacity':0.5
		},'fast');
	html.fadeOut(0).fadeIn('fast');

	window.document.onkeydown = function(e){
		var e = e || window.event;
		if( e.keyCode == 27 )
			act();
	}

	iyes.click(function(){
		type = true;
		fn ? act( fn ) : act();
	});
	
	ino.click(function(){
		type = false;
		act();
	});
	
	function act( fn ){
		mask.fadeOut('fast',function(){
			mask.remove();
			icss.remove();
		});
		html.fadeOut('fast',function(){
			html.remove();
			if( fn ) fn();
		});
	}
}

function getMax( arr ){
	var maxIndex = 0;
	for( var i=0 ; i<arr.length ; i++ ){
		for( var j=1; j<arr.length ; j++ ){
			if( arr[j] > arr[i] ){
				i=j;
				maxIndex = i;
				continue;
			}
		}
		break;
	}
	return maxIndex;
}

function getMin( arr ){
	var minIndex = 0;
	for( var i=0 ; i<arr.length ; i++ ){
		for( var j=1; j<arr.length ; j++ ){
			if( arr[j] < arr[i] ){
				i=j;
				minIndex = i;
				continue;
			}
		}
		break;
	}
	return minIndex;
}

function throwPicker( o ){
	o.die('focusin').live('focusin',function(){
		o.datetimepicker({
			ampm            : false,      //上午下午是否显示
			timeFormat      : 'hh:mm',    //时间模式
			stepHour        : 1,          //拖动时间时的间隔
			stepMinute      : 5,          //拖动分钟时的间隔
			dateFormat      : 'yy-mm-dd', //日期格式设定
			dayNamesMin     : ['日','一','二','三','四','五','六'],
			monthNamesShort : ['1','2','3','4','5','6','7','8','9','10','11','12'],
			prevText        : '上一月',
			nextText        : '下一月',
			closeText       : '关闭',
			currentText     : '今天',
			changeYear      : true,       //年份下拉
			changeMonth     : true,       //月份下拉
			yearRange       : '-100:20',  //年份下拉选择
			showHour        : true,       //是否显示小时，默认是true
			showMinute      : true        //是否显示分钟，默认是true
		});
		if( msie6() )
			$('#ui-datepicker-div').height(336);
	});
}

function throwPickerNo( o ){
	o.die('focusin').live('focusin',function(){
		o.datepicker({
			inline: true,
			dateFormat      : 'yy-mm-dd',  //日期格式设定
			dayNamesMin     : ['日','一','二','三','四','五','六'],
			monthNamesShort : ['1','2','3','4','5','6','7','8','9','10','11','12'],
			prevText        : '上一月',
			nextText        : '下一月',
			closeText       : '关闭',
			currentText     : '今天',
			changeYear      : true,        //年份下拉
			changeMonth     : true,        //月份下拉
			yearRange       : '-100:20'    //年份下拉选择
		});
		if( msie6() )
			$('#ui-datepicker-div').height(214);
	});
}

function toDate( str ){
	var dat     = str.split(' ')[0].split('-'),
		newDate = new Date;
	return newDate.setFullYear(dat[0],dat[1],dat[2]);
}

function fresh( plus ){
	if( !plus ) plus = '';
	window.location.href = window.location.href + plus;
}

function numPlus( n ){
	var num = Number( n.text() );
	num++;
	n.text( num );
}
function numMenas( n ){
	var num = Number( n.text() );
	num--;
	n.text( num );
}

function setCookie(name,value) //两个参数，一个是cookie的名子，一个是值
{
	var Days = 30; //此 cookie 将被保存 30 天
	var exp = new Date(); //new Date('December 31, 9998');
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + '='+ escape (value) + ';expires=' + exp.toGMTString() + ';path=/';
}

function getCookie(name) //取cookies函数       
{
	var arr = document.cookie.match(new RegExp('(^| )'+name+'=([^;]*)(;|$)'));
	if(arr != null) return unescape(arr[2]); return null;
}

function delCookie(name) //删除cookie
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + '='+cval+';expires='+exp.toGMTString();
}
