/*Plugins By Heavensn*/
function MaxMenu(i){
	//过多菜单项变成下拉菜单开始
	var $MenuNum = i; //设置最大菜单项 
	var $menu = $('#menu>ul');
	var $MoreLi = $menu.find('> li:gt('+($MenuNum-2)+')');
	var $MoreLiNum = $menu.find('> li:gt('+($MenuNum-1)+')').length;
	if($MoreLiNum>0){
		$('<li id="MoreMenu" class="iHover"><a href="javascript:void(0);">更多</a><ul></ul></li>').appendTo('#menu>ul');
		var $MoreMenu = $('#MoreMenu>ul');
		$MoreLi.appendTo($MoreMenu);	
	}
	//过多菜单项变成下拉菜单结束
	
	$('#menu>ul>li:last').addClass('last');
	
	//下拉菜单滤镜效果开始
	$menu.find('ul').parent().each(function(){												
			$(this).hover(
				function(){
					$(this).find('>ul').stop(true, true);
					$(this).find('>ul').slideDown(200);
					return false; //阻止冒泡
				},
				function(){
					$(this).find('>ul').slideUp(300);
				}
			);
	});
	//下拉菜单滤镜效果结束
	
	$(".iHover").hover(
		function(){
			$(this).addClass("iShow");
		},function(){
			$(this).removeClass("iShow");
		}
	);
};

/**
 * 图片尺寸自适应
 * @param {String} src
 * 更新页面载入完毕再执行、修正小图问题、修正不带宽度高度参数的图片问题、最大高宽自动读取、调用一次可全局应用
 */

jQuery.fn.fixInBoxComplete = function(src)
{
    var _imgs = jQuery(this);
	//判断浏览器类型及页面是否载入完毕
	var appname = navigator.appName.toLowerCase();
	if (appname.indexOf("netscape") == -1) {  //IE、Opera
		document.onreadystatechange = function() {
			if (document.readyState == "complete") {		
				imageSize();
			}
		}
	} else { //Firefox、Chrome
		window.onload = function() {
			imageSize();
		}
	}
	function imageSize(){
		_imgs.each(function(){
			var _img=jQuery(this);
			var _src = src?src:_img.attr("src");
			var maxWidth = jQuery(this).parent().width(); //从父元素中自动获取最大宽度
			var maxHeight = jQuery(this).parent().height();	//从父元素中自动获取最大高度
			if (_src.indexOf("accessoryId=")>0 && _src.indexOf("height=")==-1){
				_img.attr("src", function(){
					return _src+"&width="+maxWidth+"&height="+maxHeight;
				});
				
			}
			var imageSizeFixerFull = new ImageSizeFixerFull(_img,maxWidth,maxHeight);
			imageSizeFixerFull.resize();
			
		});
	}
    return true;
}
var ImageSizeFixerFull = function(imageEl,maxWidth,maxHeight){
	this.resize = function(){
		var _cop  = maxWidth/maxHeight; // 最优宽高比例
		// 原始尺寸
		var _width;
		var _height;
		var _marginTop; // 内填充上
		var _marginLeft; // 内填充左
		// 修改尺寸
		var mWidth;
		var mHeight;
		// 比例
		var _copSource;
		var _copChange; // 缩略比例
		var _cImg = imageEl;// fixed:单页多图片自动缩略不均衡
		_width = _cImg.attr("width");
		_height = _cImg.attr("height");
		_marginTop = parseInt(_cImg.css("margin-top"),10) || 0;
		if (_width > maxWidth || _height > maxHeight) // 需要缩略的条件
		{
			_copSource = _width/_height; // 当前宽高比例
			// 按照比例缩略
			if (_copSource > _cop){ // 当前图片较宽
				// 将宽度缩略到限制的最宽
				mWidth = maxWidth;
				_copChange = mWidth/_width;
				mHeight = _height*_copChange;
			}else{ // 比例相符
				mHeight = maxHeight;
				_copChange = mHeight/_height;
				mWidth = _width*_copChange;
			}
			_marginTop = (maxHeight-mHeight)/2;
			_cImg.attr("width",mWidth);
			_cImg.attr("height",mHeight);
			_cImg.css("margin-top",_marginTop);
		}else { //宽高都小于最大宽高时候
			_marginTop = (maxHeight-_height)/2;
			_cImg.attr("width",_width);
			_cImg.attr("height",_height);
			_cImg.css("margin-top",_marginTop);
		}
	}
}

//渐变出场
jQuery.fn.ListShow = function(ShowTime,AnimateTime,last,obj){ //参数分别为：显示速度、渐变动画速度、每排最后的元素索引、对象元素
	var List = jQuery(this);
	var LiLength = List.find(obj).length;
	var LiPage = 0;
	if(last>0){
		List.find(obj+':nth-child('+last+'n+'+last+')').addClass('last');
	}
	
	List.find(obj).css({
		visibility:'hidden'
	});

	Interval = setInterval(iShow,ShowTime);

	function iShow(){
		var n = LiPage-1;
		if(LiPage>LiLength){
			clearInterval(Interval);
		}
		LiPageClickold(List.find(obj+':eq('+(n)+')'),1);	
		LiPage++;
	}
	
	function LiPageClickold(obj,diaphaneity){
		obj.css({
			visibility:'visible',
			opacity: "0"
		});
		obj.animate({
			opacity: diaphaneity
		},AnimateTime);
	}
}

//Title信息提示框
jQuery.fn.TitleInfoBox = function(SiteName,SiteInfo,obj,opacity,TextLast){ //参数分别为：友情链接时网站名称标识、友情链接时网站简介标识、元素对象、背景透明度、友情链接时文字连接是否后置
	var LinkList = jQuery(this);
	var Info;
	var ImgAlt;
	if(TextLast){
		LinkList.find(obj+'.textLink').appendTo(LinkList);
	}
	LinkList.find(obj).each(function(){
		$(this).find('a').mouseover(function(e){
				Info = $(this).attr("title");
				if(Info!=null && Info!="" ){
					ImgAlt = $(this).find('img:first').attr("alt");
					$('body').append('<div id="InfoBox"></div>');
					if(SiteName === "" && SiteInfo ==="" ) {
						$(this).attr("title","");
						$(this).find('img').attr("alt","");
						$('#InfoBox').html(Info);
					}else {
						var InfoArr = new Array();
						InfoArr = Info.split("|");
						$(this).attr("title","");
						$(this).find('img').attr("alt","");
						$('#InfoBox').html('<span class="title">'+SiteName+'：</span>'+InfoArr[0]+'<br /><span class="title">'+SiteInfo+'：</span>'+InfoArr[1]);
					}
					$('#InfoBox').css({
						display: 'block',
						top: (e.pageY+20),
						left: (e.pageX+10),
						opacity: '0'
					}).animate({
						opacity: opacity
					},500);
				}
		}).mousemove(function(e){
			$('#InfoBox').css({
				top: (e.pageY+20),
				left: (e.pageX+10)
			});
		}).mouseout(function(e){
			$('#InfoBox').remove();
			if(Info!=null && Info!=""){
				$(this).attr("title",Info);
			}
			if(ImgAlt!=null && Info!=""){
				$(this).attr("alt",ImgAlt);
			}
		});
	});
}

//留言切换
var GuestbookHeight;
$(function(){
	GuestbookHeight = $('.GuestBookList').height();
});
function GuestBookList(e){
	var BookList = $('.GuestBookList');
	var BookForm = $('.GuestBookForm');
	BookList.stop(true,true);
	if(e==='yes'){
		$('.BookFormBtn').removeClass('iShow');
		$('.BookListBtn').addClass('iShow');
		BookList.animate({
			height: GuestbookHeight,
			opacity:"1"
		},700);
	}else {
		$('.BookListBtn').removeClass('iShow');
		$('.BookFormBtn').addClass('iShow');
		BookList.animate({
			height: '0',
			opacity:"0"
		},700);
	}
}

//友情链接切换
var LinkListHeight;
$(function(){
	LinkListHeight = $('.LinkListBox').height();
});
function LinkList(e){
	var LinkList = $('.LinkListBox');
	var LinkForm = $('.LinkFormBox');
	if(e==='yes'){
		$('.FormBtn').removeClass('iShow');
		$('.ListBtn').addClass('iShow');
		LinkList.animate({
			height: LinkListHeight,
			opacity: "1"
		},700);
		LinkForm.animate({
			height: '0',
			opacity: "0"
		},700)
	}else {
		$('.FormBtn').addClass('iShow');
		$('.ListBtn').removeClass('iShow');
		LinkForm.animate({
			height: '360',
			opacity: "1"
		},700)
		LinkList.animate({
			height: '0',
			opacity: "0"
		},700)
	}
}
