jQuery(function($) {
	/* IE6提示升级 */
	if( msie6() ){
		$('#upbrowser').show(0);
	}
	
	var dc      = $(document.body),
		wc      = $('#windows_content'),
		tree    = $('#tree'),
		site    = $('#site'),
		content = $('#content');
	
	var leftside  = $('.leftside'),
		rightside = $('.rightside');
	
	var osframe = $('#iframe');
		osframe.css('background-color','#fafdff');
	
	/* 调整主体部分高度 -- All Browser */
	var wc_size = function(o){
		o.height( dc.height() - 74 );
	}
	wc_size(wc);
	$(window).resize(function(){
		wc_size(wc);
	});
	
	/* Javascript 生成展开收缩箭头 */
	var sideswitch = $('<div class="sideswitch"></div>'),
		leftswitch = $('<a href="javascript:;" class="sideswitch_left" type="on"></a>'),
		rightswitch = $('<a href="javascript:;" class="sideswitch_right" type="on"></a>');
	
	switch( content.attr('class') ){
		case 'content' :
			sideswitch.prepend( leftswitch ).prepend( rightswitch );
			dc.prepend( sideswitch );
			break;
		case 'incontent' :
			sideswitch.prepend( rightswitch );
			dc.prepend( sideswitch );
			break;
		case 'ancontent' :
			sideswitch.prepend( leftswitch );
			dc.prepend( sideswitch );
			break;
		default : break;
	}
	
	/* 树 -- 收起.展开 */
	tree.find('a.arr').click(function(){
		var $this = $(this),
			$fath = $this.parent(),
			$bind = $fath.next(),
			$sped = 'normal',
			$type = $this.attr('name');
		
		switch( $type ){
			case 'on' :
				$bind.slideUp($sped,function(){
					$fath.css('background-position','0 -34px');
					$this.attr('name','off');
				});
				break;
			case 'off' :
				$bind.slideDown($sped,function(){
					$fath.css('background-position','0 0');
					$this.attr('name','on');
				});
				break;
			default : trace('type error!!'); break;
		}

	});
	
	$('#search_all').defaultTxT('请输入搜索内容');
	$('#tags_text').defaultTxT('添加标签,回车确认');
	$('#ose_title').defaultTxT('请输入标题');
	$('#inv_title').defaultTxT('欢迎加入海知网');
	$('#member_search').defaultTxT('请输入成员名');
	$('#do_search').defaultTxT('请输入搜索内容');
	
	/* 社区管理员 -- 搜索 */
	$('#member_submit').click(function(event){
		var $val = $('#member_search').val();
		if( $val == '' || $val == '请输入成员名' ){
			trace('请输入成员名');
			event = event || window.event;
			event.cancelBubble = true;
			event.stopPropagation();
			return false;
		}
	});
	
	/* OS顶部鼠标滑过时 */
	var foodhtml = '';
		foodhtml += '<div class="st_food">';
		foodhtml += '<div>';
		foodhtml += '<a target="_new" href="'+iknowing_domain+'/download/go.html">客户端下载</a>';
		foodhtml += '<a href="'+$('#userpage').attr('href')+'">个人主页</a>';
		foodhtml += '<a href="'+iknowing_domain+'/setting/index.html">设置</a>';
		foodhtml += '<a href="'+iknowing_domain+'/friend/index.html">通讯录</a>';
		foodhtml += '<a href="'+iknowing_domain+'/group/my.html">小组</a>';
		foodhtml += '<a href="'+iknowing_domain+'/activity/index.html">活动</a>';
		foodhtml += '<a href="'+iknowing_domain+'/logout.html">退出</a>';
		foodhtml += '</div>';
		foodhtml += '</div>';
		foodhtml = $(foodhtml);
	$('.st_name').mouseenter(function(){
		var $this = $(this);
		dc.prepend(foodhtml);
		foodhtml.css({
			'top' : $this.offset().top + 4,
			'left': $this.offset().left - 43
		}).mouseleave(function(){
			foodhtml.remove();
		});
	});
	
	/* 列表单项鼠标动作 */
	listhover( content.find('.note_list li') );
	function listhover(o){
		o.hover(
			function(){
				var $this = $(this),
					$menu = $this.find('.til .menu'),
					$del  = $this.find('.til .del'),
					$shid = $this.find('.til .shield');
				$menu.css('display','block');
				$del.css('display','block');
				$shid.css('display','block');
			},
			function(){
				var $this = $(this),
					$menu = $this.find('.til .menu'),
					$del  = $this.find('.til .del'),
					$shid = $this.find('.til .shield');
				$menu.css('display','none');
				$del.css('display','none');
				$shid.css('display','none');
			}
		);
	}
	
	/* 边栏内容切换 */
	site_change( site.find('.site_con_til span a') , site.find('.site_con_cot') );
	site_change( $('.userdynamic_menu a') , $('.userdynamic') );
	function site_change( o , t ){
		o.click(function(){
			var $this  = $(this),
				$name  = $this.attr('name'),
				$speed = 'normal';
			
			if( $name == 'all' ){
				t.find('ul').hide(0).slideDown($speed);
			}
			else{
				t.find('ul').hide(0);
				t.find('ul[name='+$name+']').slideDown($speed);
			}
		});
	}
	
	/* 打开关闭边栏 newPage */
	$(function(){
		doAct( leftswitch  , leftside  , 180 , 'left'  , 'normal');
		doAct( rightswitch , rightside , 280 , 'right' , 'normal');
		function doAct( o , u , w , f , s ){
			/*
			 * o : 触发事件对象
			 * u : 绑定动画对象
			 * w : 展开宽度
			 * f : 方向
			 * s : 速度
			 */
			o.click(function(){
				var $type = o.attr('type');
				
				switch( $type ){
					case 'on':
						switch( f ){
							case 'left' :
								o.animate({
									'left' : 100
								},s,function(){ o.css('background-position','right top') });
								if( msie6() ){
									content.animate({
										'width':content.width() + ( w + 40 - 7 ),
										'left' : 100 + 7
									},s);
								}
								else{
									content.animate({
										'left' : 100 + 7
									},s);
								}
							break;
							case 'right' :
								o.animate({
									'right' : 100
								},s,function(){ o.css('background-position','left top') });
								if( msie6() ){
									content.animate({
										'width' : content.width() + ( w + 40 - 7 ),
										'right' : 100 + 7
									},s);
								}
								else{
									content.animate({
										'right' : 100 + 7
									},s);
								}
							break;
							default : trace('f error!!'); break;
						}
						
						u.animate({
							'width' : 0
						},s).hide(0);
						o.attr('type','off');
					break;
					case 'off':
						switch( f ){
							case 'left' :
								o.animate({
									'left' : w + 120
								},s,function(){ o.css('background-position','left top') });
								if( msie6() ){
									content.animate({
										'width': content.width() - ( w + 40 - 7 ),
										'left' : w + 140
									});
								}
								else{
									content.animate({
										'left' : w + 140
									});
								}
							break;
							case 'right' :
								o.animate({
									'right' : w + 120
								},s,function(){ o.css('background-position','right top') });
								if( msie6() ){
									content.animate({
										'width' : content.width() - ( w + 40 - 7 ),
										'right' : w + 140
									},s);
								}
								else{
									content.animate({
										'right' : w + 140
									},s);
								}
							break;
							default : trace('f error!!'); break;
						}
						u.show(0).animate({
							'width' : w
						},s);
						o.attr('type','on');
					break;
					default : trace('type error!!'); break;
				}
				if( msie6() ){
					$('.container').height( content.height() );
				}
			});
		}
	});
	
	/* 判断小于1000宽度的屏幕 */
	$(function(){
		if( $(document.body).width() <= 1080 && $('.content_ie6').length < 1 ){
			var o = rightswitch,
				u = rightside,
				w = 280,
				f = 'right',
				s = 'normal';
			
				o.animate({
					'right' : 100
				},s,function(){ o.css('background-position','left top') });
				if( msie6() ){
					content.animate({
						'width' : content.width() + ( w + 40 - 7 ),
						'right' : 100 + 7
					},s);
				}
				else{
					content.animate({
						'right' : 100 + 7
					},s);
				}
				
				u.animate({
					'width' : 0
				},s,function(){
					if( msie6() ){
						$('.container').height( content.height() );
					}
					o.attr('type','off');
				}).hide(0);
		}
	});
	
	/* 打开关闭边栏 myOS */
	$(function(){
		var $sico = site.find('.site_cover'),
			$cntr = site.find('.site_content'),
			$type,
			$norW = 280,
			$minW = 8;
		site.find('.site_slidmenu a').click(function(){
			var $this = $(this);
				$type = $cntr.attr('type');
			doAct({
				s    : $sico,
				o    : $this,
				type : $type,
				c    : $cntr
			});
		});
		site.find('.close').click(function(){
			doAct({
				s    : $sico,
				type : 'on',
				c    : $cntr
			});
		});
		
		var doAct = function( params ){
			if( !params.o )
				params.o = site.find('.site_slidmenu a');
			switch( params.type ){
				case 'on':
					params.s.animate({
						'width' : $minW
					});
					params.c.animate({
						'width' : 0
					},function(){
						params.o.removeClass().addClass('off');
						params.c.attr('type','off')
					});
					if( msie6() ){
						content.animate({
							'width' : '+='+$norW
						});
						osframe.animate({
							'width' : '+='+$norW
						});
					}
					break;
				case 'off':
					params.s.animate({
						'width' : $norW + $minW
					});
					params.c.animate({
						'width' : $norW
					},function(){
						params.o.removeClass().addClass('on');
						params.c.attr('type','on');
					});
					if( msie6() ){
						content.animate({
							'width' : '-='+$norW
						});
						osframe.animate({
							'width' : '-='+$norW
						});
					}
					break;
				default:trace('type error!!'); break;
			};
		}
	});
	
	/* 文件域 */
	$('#gfile').die('change').live('change',function(){
		var $this = $(this),
			$o = $('.cosfile');
		$o.val( $this.val() ).focus();
	});
	$('#cfile').die('change').live('change',function(){
		var $this = $(this),
			$o = $('.cosfile');
		$o.val( $this.val() ).focus();
	});
	$('#ofile').die('change').live('change',function(){
		var $this = $(this),
			$o = $('.cosfile');
		$o.val( $this.val() ).focus();
	});
	$('#hfile').die('change').live('change',function(){
		var $this = $(this),
			$o = $('.cosfile');
		$o.val( $this.val() ).focus();
	});
	$('#afile').die('change').live('change',function(){
		var $this = $(this),
			$o = $('.cosfile');
		$o.val( $this.val() ).focus();
	});
	
	/* Tags Remove */
	$('#tags b').die('click').live('click',function(){
		var $object = $(this).parent();
		$object.fadeOut('normal',function(){
			$object.remove();
		});
	});
	
	/* Msg & Frd ListHover */
	if( msie6() ){
		$('ul[name=listhover] li').hover(
			function(){
				var $this = $(this);
				$this.css('background-color','#dee7ee');
				$this.find('div.listclose').css('display','block');
				$('ul[name=listhover]').scroll(function(){
					$this.find('div.listclose').css('display','none');
				});
			},
			function(){
				var $this = $(this);
				$this.css('background-color','');
				$this.find('div.listclose').css('display','none');
			}
		);
	}
	
	/* Msg Board */
	function msgCompatible(){
		
		$('.content_scroll').height( content.height() - 46 );
		$('#ose_title').width( content.width() - 42 );
		
		$('.msg_list_content ul').height( $('.win_mc').height() - 68 );
		$('.msg_main_talk_list ul').height( $('.win_mc').height() - 218 );
		
		$('.frd_list_content ul').height( $('.win_mc').height() - 75 );
		$('.frd_info_list ul').height( $('.win_mc').height() - 142 );
		$('.frd_search_content').height( $('.win_mc').height() - 50 );
		$('.frd_dosearchit').height( $('.win_mc').height() - 50 );
		
		$('.set_list_content').height( $('.win_mc').height() - 40 );
		$('.set_info_list').height( $('.win_mc').height() - 38 );
		
		$('#friend_search_text').width( $('#friend_search_text').parent().width() - 53 );
		//friend_search_text
		//friend_search_btn
		
	}
	msgCompatible();
	$(window).resize(function(){
		msgCompatible();
	});
	
	/* Win Book */
	function mid( o ){
		o.css({
			'top'  : ( dc.height() - o.height() ) / 2,
			'left' : ( dc.width() - o.width() ) / 2
		});
	}
	mid( $('.winbook') );
	$(window).resize(function(){
		mid( $('.winbook') );
	});

	/* 返回顶部 */
	$('#backtop').die('click').live('click',function(){
		$(document).scrollTop(0);
	});
	
	/* Index Slog */
	$(function(){
		var interval = setInterval(function(){
			var $object = $('#index_slog'),
				$one    = $object.find('li').eq(0),
				$height = $one.height();
				
			$object.stop().animate({
				'top' : -$height
			},'normal',function(){
				$one.appendTo($object);
				$object.css('top',0);
			});
		},5000);
	});
	
	/* 附件图标 */
	$('#attach a').each(function(){
		var $this = $(this),
			$arr  = $this.html().split('.'),
			$type = $arr[$arr.length-1],
			$icon = 'attach';
		switch($type){
			case 'txt'   : $icon = 'txt';   break;
			case 'word'  : $icon = 'word';  break;
			case 'excel' : $icon = 'excel'; break;
			case 'chm'   : $icon = 'chm';   break;
			case 'pdf'   : $icon = 'pdf';   break;
			case 'ppt'   : $icon = 'ppt';   break;
			case 'rar'   : $icon = 'rar';   break;
			case 'jpg'   : $icon = 'jpg';   break;
			case 'gif'   : $icon = 'gif';   break;
			case 'png'   : $icon = 'png';   break;
			case 'psd'   : $icon = 'psd';   break;
			case 'ai'    : $icon = 'ai';    break;
			default : $icon = 'attach';  break;
		}
		$this.css('background-image','url("'+static_domain+'/image/'+$icon+'.jpg")');
	});
	
	/* 登录后返回评论 */
	$(function(){
		$object = $('[name=loginback]');
		$object.attr('href',$object.attr('href')+'?reurl='+window.location.href);
	});
	
	/* 点击复制原地址 */
	$('#source').click(function(){
		var $object = $('.popresource .popresourceinput'),
			$speed  = 'fast';
		$object.fadeIn($speed,function(){
			$object.select().focus().focusout(function(){
				$object.fadeOut($speed);
			});
		});
		/*
		var $source = $('#copysource'),
			$souval = $source.val(),
			$range;
		$source.select();
		if( $.browser.msie ){
			range = $source[0].createTextRange();
			range.execCommand('Copy');
			alert('直接按Ctrl+V即可粘帖原地址');
		}
		else{
			alert('按Ctrl+C即可复制原地址');
		}
		
		if( window.clipboardData ){
			window.clipboardData.clearData();
			window.clipboardData.setData('Text',$source);
		}
		else if( window.netscape ){
			try{
				netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
			}
			catch(e){
				alert('请在地址栏内输入about:config,将signed.applets.codebase_principal_support设置为true之后重试即可!');
				return false;
			}
		}
		else{
			trace('您的浏览器不支持此功能');
		}
		*/
	});
	
	/* Choose All */
	$('[name=chooseall]').chooseall({
		'pondo' : $('#mmi')
	});
	
	/* Domain Logo */
	$(function(){
		if( iknowing_domain.indexOf('iknowing.com') == -1 ){
			var $domain = window.location.href.split('http://')[1].split('/')[0];
			$('[name=domainlogo]').css('background-image','url('+static_domain+'/image/'+$domain+'.jpg)');
		}
	});
	
	/* Community Logo */
	$(function(){
		var local = window.location.href,
			reg   = /\d+/g;
		if( local.indexOf('community') != -1 ){
			local = reg.exec(local.split('community')[1]);
			$('#comlogo').attr('src',static_logo+'/community/'+local+'.jpg');
		}
	});
	
	/* Set Home and Add Fav */
	$(function(){
		var	$object = $('#bar'),
			$minw   = 68,
			$lenw   = 9 * 48,
			$speed  = 'normal',
			$name   = '海知网',
			$group  = '87471837',
			$weibo  = '2202330857';

		switch( getCookie('bar') ){
			case 'all' :;
				break;
			case 'half' :
				$object.find('.bar_top').slideUp(0);
				break;
			case 'never' :
				$object.find('.bar_top').slideUp(0);
				$object.find('.menu').css({
					'width'   : 0
				});
				$object.find('.menu').next().removeClass().addClass('bar_ss_off');
				$object.css('width',$minw);
			default:; break;
		}
		
		switch( iknowing_domain ){
			case 'http://www.iknowing.com/iknowing' :
				$name  = '海知网';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			case 'http://www.pkush.org/iknowing' :
				$name = '北大上海校友会';
				$group = '96213681';
				$weibo = 'pkushanghai';
				break;
			case 'http://shequ.shkpzx.com/iknowing' :
				$name = '上海科普作家网';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			case 'http://i.iknowing.com/iknowing' :
				$name = '海知网';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			case 'http://shequ.ccharity.org/iknowing' :
				$name = '美丽心灵';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			case 'http://spsp.iknowing.com/iknowing' :
				$name = '浦东软件园';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			case 'http://shequ.xingbaby.org/iknowing' :
				$name = '星Baby';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			case 'http://shequ.eseegirl.com/iknowing' :
				$name = 'esee girl';
				$group = '87471837';
				$weibo = '2202330857';
				break;
			default : break;
		}
		
		var $href = window.location.href.match(/http:\/\/([^\/]+)\//i);
		$('.bar_community').attr('href',$href[0]);
		
		$object.find('.sethome').html('将'+$name+'设为首页');
		$object.find('.addfav').html('将'+$name+'添加至收藏夹');
		$object.find('.sina').attr('href','http://weibo.com/'+$weibo).html('到微博关注'+$name);
		$object.find('.qq').attr('href','http://qun.qq.com/#jointhegroup/gid/'+$group).html($name+'QQ交流群');
		
		$object.find('.bar_backtop').die('click').live('click',function(){
			var interval = setInterval(function(){
				var $top = $(document).scrollTop();
				if( $top <= 5 ){
					$(document).scrollTop(0);
					clearInterval( interval );
				}
				else{
					$(document).scrollTop( $top/2 );
				}
			},16);
		});
		
		$object.find('.bar_low').die('click').live('click',function(){
			$object.find('.bar_top').stop().slideUp($speed);
			setCookie('bar','half');
		});
		
		$object.find('.bar_ss_on').die('click').live('click',function(){
			var $this = $(this);
			$object.find('.bar_top').stop().slideUp($speed,function(){
				$object.find('.menu').stop().animate({
					'width' : 0
				},$speed,function(){
					$(this).hide(0);
				});
				$object.stop().animate({
					'width' : $minw
				},$speed,function(){
					$this.removeClass().addClass('bar_ss_off');
					setCookie('bar','never');
				});
			});
		});
		$object.find('.bar_ss_off').die('click').live('click',function(){
			var $this = $(this);
			$object.find('.menu').show(0).stop().animate({ 'width' : $lenw },$speed,function(){ $this.removeClass().addClass('bar_ss_on'); });
			$object.stop().animate({ 'width' : $lenw + $minw },$speed,function(){
				$object.find('.bar_top').stop().slideDown($speed,function(){ setCookie('bar','all'); });
			});
		});
		
		$.fn.treeScroll();
		
		$('[name=sethomepage]').setHomepage({
			'url' : iknowing_domain
		});
		$('[name=addfavorite]').addFav({
			'name' : $name,
			'url'  : iknowing_domain
		});
	});
});
