﻿$(".txt-search").focus(function(){
	$(this).val("");
}).blur(function(){
	if($(this).val()==""){
		$(this).val("--请输入关键字--");
	}
});
(function(){
	$.fn.tabs = function(o){
		var o = $.extend({meth:"hover"}, o||{})
		return this.each(function(){
			var $menu = $(this).children(".tab-menu").find("li"), $cont = $(this).children(".tab-cont").children(".cont");
			$menu.each(function(i){
				if(o.meth == "click"){
					$(this).click(function(){ toggle(i) });				
				}else if(o.meth == "hover"){
					$(this).hover(function(){ toggle(i) });
				}
			});
			function toggle(i){
				$menu.removeClass("hover");
				$menu.eq(i).addClass("hover");
				$cont.removeClass("hover");
				$cont.eq(i).addClass("hover");
			}
		});
	}
	$.fn.picshow = function(o){
		var o = $.extend({
			 pause:5000,
			 fspd:500,
			 auto:true
		}, o||{});
		return this.each(function(){
			var $cont = $(".ps-cont > a", this), $title = $(".ps-title > h3", this), $nav = $(".ps-nav > a", this),
			len = $cont.length;
			var interval = null, index = 0, isOver = true;
			if(o.auto){$cont.hover(function(){clearInterval(interval);}, auto);}
			if(o.auto){
				$nav.each(function(i){
					$(this).hover(function(){
						clearInterval(interval);
						roll(i);
					}, auto);
				});
			}else{
				$nav.each(function(i){
					$(this).hover(function(){roll(i);});
				});
			}
			
			function auto(){
				clearInterval(interval);
				interval = setInterval(function(){ roll(index==(len-1) ? 0 : index+1);}, o.pause);
			}
			function roll(i){
				if(isOver){
					isOver = false;
					$cont.hide();
					$cont.eq(i).fadeIn(o.fspd, function(){ isOver = true; });
					$nav.removeClass("hover");
					$nav.eq(i).addClass("hover");
					$title.hide();
					$title.eq(i).show()
					index = i;
				}
			}
			//init
			$cont.eq(index).show();
			$nav.eq(index).addClass("hover");
			if(o.auto){auto();}
		});
	}
	$.fn.rollpic = function(o){
		var o = $.extend({
			pause:5000,
			nspd:1000,
			uspd:300,
			vnum:5,
			snum:1,
			start:0,
			isH:true,
			auto:true
		}, o||{});
		
		return this.each(function(){
			var $cont = $(".sp-cont", this), 
			$prev = $(".next", this), $next = $(".prev", this),
			$a = $cont.children("a"), len = $a.length, v=o.vnum;
			if(len<v){return false;}
			$cont.prepend($a.slice(len-v-1+1).clone(true)).append($a.slice(0,v).clone(true));
			o.start += v;
			var curr = o.start;
			
			var interval = null, a_dir = o.isH ? "marginLeft":"marginTop", c_dir = "left",
			aSize = o.isH ? $a.outerWidth(true) : $a.outerHeight(true), contDS = o.isH ? "width" : "height",
			itemLength = $cont.children("a").size();
			$cont.css(contDS, itemLength*aSize).css(a_dir, -(curr*aSize));
			
			var isOver = true;
			
			if(o.auto){
				$cont.hover(function(){
					clearInterval(interval);
				}, function(){
					if(c_dir == "left"){
						interval = setInterval(function(){ roll(curr+o.snum)}, o.pause);
					}else if(c_dir == "right"){
						interval = setInterval(function(){ roll(curr-o.snum)}, o.pause);
					}
				});
			}
			if($prev){
				$prev.click(function(){
					if(o.auto){clearInterval(interval)};
					if(isOver==true){roll(curr-o.snum, o.uspd);c_dir = "right";};
					if(o.auto){interval = setInterval(function(){ roll(curr-o.snum)}, o.pause)};
				});
			}
			if($next){
				$next.click(function(){
					if(o.auto){clearInterval(interval)};
					if(isOver==true){roll(curr+o.snum, o.uspd); c_dir = "left";};
					if(o.auto){interval = setInterval(function(){ roll(curr+o.snum)}, o.pause)};
				});
			}
			if(o.auto){ interval = setInterval(function(){ roll(curr+o.snum)}, o.pause);}
			function roll(to, spd){
				if(isOver){
					var spd = spd || o.nspd
					isOver = false;
					if(to<=o.start-v-1){
							$cont.css(a_dir, -((v+(len-v)+curr)*aSize)+"px");
							curr = (v+(len-v)+curr)-o.snum;
					}else if(to>=itemLength-v+1) {
							$cont.css(a_dir, -( (v-(itemLength-v-curr)) * aSize ) + "px" );
							curr = (v-(itemLength-v-curr))+o.snum;
					}else curr = to;
					
					$cont.animate(
							a_dir == "marginLeft" ? {"marginLeft": -(curr*aSize) } : {"marginTop": -(curr*aSize) } , spd, function(){isOver = true;}
					);
				}
				return false;
			};
			
		});
	}
})(jQuery);

function sidemenu(id){
	var $obj = $(id), sideMenuIndex = -1;
	$obj.children("li").each(function(i){
		if($(this).children("a").attr("href") == window.location.pathname){
			sideMenuIndex = i;
			$(this).addClass("hover");
		}
	});
	$obj.find("li:has(li) li").each(function(i){
		if($(this).children("a").attr("href") == window.location.pathname){
			sideMenuIndex = i;
			$(this).addClass("hover");
            $(this).parent("ul").parent("li").addClass("hover");
			$(this).parent("ul").show();
		}
	});

	$obj.find("li:has(li) > a").toggle(function(){
		$(this).parent().children("ul").slideDown();
	},function(){
		$(this).parent().children("ul").slideUp();
	})
}

function setNowLoc(obj, extPath){
	var href = window.location.href;
	var $obj = $(obj), $obj_li = $("li", $obj), extPath = extPath || null;
	var http_hostname = "http://" + window.location.hostname;
	if(href == http_hostname + "/" || href == http_hostname || href == "http://" + window.location.host + extPath){
		$obj_li.each(function(){
			var a_href = $(this).children("a").attr("href");
			if(a_href == http_hostname || a_href == "/" || a_href == extPath){
	 			$(this).addClass("now");
				return false;
			}
		});
		return false;
	}
	$obj_li.each(function(){
		var a_href = $(this).children("a").attr("href");
		if(href.indexOf(a_href) > -1 && a_href != "/" && a_href != extPath){
			$(this).addClass("now");
		}
	});
}
(function(){
	$.fn.scrollpic = function(o){
		o = $.extend({
						pause: 30,
						nstep: 1,
						ustep: 5,
						isH: true						
		}, o||{});
		
		return this.each(function(){
			var $box = $(".sp-box", this), $cont = $(".sp-cont", this), 
			$prev = $(".prev", this), $next = $(".next", this);
			$a = $cont.children("a"), len = $a.length;
			$cont.append($a.clone(true));
			var box = $box.get(0);
			
			var interval = null, cdir, a_dir = o.isH ? (['left', 'right']):(['top', 'bottom']),
			contDS = o.isH ? "width" : "height", aSize = o.isH ? $a.outerWidth(true) : $a.outerHeight(true), contS = len*aSize;
			$cont.css(contDS, 2*contS);
			
			$box.hover(function(){
				clearInterval(interval);
			}, function(){
				roll(cdir, o.nstep);
			});
			if($prev){
				$prev.hover(function(){
					roll(a_dir[1], o.ustep);
				}, function(){
					roll(a_dir[1], o.nstep);
				});
			}
			if($next){
				$next.hover(function(){
					roll(a_dir[0], o.ustep);
				}, function(){
					roll(a_dir[0], o.nstep);
				});
			}
			roll(a_dir[0], o.nstep);
			function roll(dir, step){
				clearInterval(interval);
				cdir = dir;
				interval = setInterval(function(){ _roll(cdir, step)}, o.pause);
			};
			function _roll(dir, step){
				switch(dir){
					case "left":
						(box.scrollLeft>=contS) ? (box.scrollLeft -= contS) : (box.scrollLeft += step);
						break;
					case "right":
						(box.scrollLeft<=0) ? (box.scrollLeft += contS) : (box.scrollLeft -= step);
						break;
					case "top":
						(box.scrollTop>=contS) ? (box.scrollTop -= contS) : (box.scrollTop += step);
						break;
					case "bottom":
						(box.scrollTop<=0) ? (box.scrollTop += contS) : (box.scrollTop -= step);
						break;
					default:
						return;
				}
			};
		});
	};
})(jQuery);


function setNowLoc(obj, extPath){
	var href = window.location.href;
	var $obj = $(obj), $obj_li = $("li", $obj), extPath = extPath || null;
	var http_hostname = "http://" + window.location.hostname;
	if(href == http_hostname + "/" || href == http_hostname || href == "http://" + window.location.host + extPath){
		$obj_li.each(function(){
			var a_href = $(this).children("a").attr("href");
			if(a_href == http_hostname || a_href == "/" || a_href == extPath){
	 			$(this).addClass("current");
				return false;
			}
		});
		return false;
	}
	$obj_li.each(function(){
		var a_href = $(this).children("a").attr("href");
		if(href.indexOf(a_href) > -1 && a_href != "/" && a_href != extPath){
			$(this).addClass("current");
		}
	});
}


function sidemenu(id){
	var $obj = $(id), sideMenuIndex = -1;
	$obj.children("li").each(function(i){
		if(window.location.pathname.indexOf($(this).children("a").attr("href"))>-1){
			sideMenuIndex = i;
			$(this).addClass("now");
		}
	});
}









