//placeholder占位
//$('input[placeholder]').defaultValue();

String.prototype.replaceAll = function(s1,s2){
	return this.replace(new RegExp(s1,"gm"),s2);
}
/**
 * 字符串 format
 * "测试字符串{0}，测{1}试".strFormat("第一个占位值","第二个占位值")
 */
String.prototype.strFormat=function(){
    if(arguments.length==0) return this;
    for(var s=this,i=0;i<arguments.length;i++){
        s=s.replace(new RegExp("\\{"+i+"\\}","g"),arguments[i]);
    }
    return s;
}
//定义map
function Map() {
    this.container = {};
}
//将key-value放入map中
Map.prototype.put = function(key,value){
    try{
        if(key!=null && key != "")
            this.container[key] = value;
    }catch(e){
        return e;
    }
};
//根据key从map中取出对应的value
Map.prototype.get = function(key){
    try{
        return this.container[key];
    }catch(e){
        return e;
    }
};
//判断map中是否包含指定的key
Map.prototype.containsKey=function(key){
    try{
        for(var p in this.container){
            if(this.p==key)
                return true;
        }
        return false;
    }catch(e){
        return e;
    }
}
//获取map的大小
Map.prototype.size=function(){
    return this.keyArray().length;
}
//返回map中的key值数组
Map.prototype.keyArray=function(){
    var keys=new Array();
    for(var p in this.container)  {
        keys.push(p);
    }
    return keys;
}
//判断map中是否包含指定的value
Map.prototype.containsValue = function(value){
    try{
        for(var p in this.container) {
            if(this.container[p] === value)
                return true;
        }
        return false;
    }catch(e){
        return e;
    }
};

//删除map中指定的key
Map.prototype.remove = function(key){
    try{
        delete this.container[key];
    }catch(e){
        return e;
    }
};
//清空map
Map.prototype.clear = function(){
    try{
        delete this.container;
        this.container = {};
    }catch(e){
        return e;
    }
};

function getOffset(e) {
	var target = e.target;
	if (target.offsetLeft == undefined) {
		target = target.parentNode;
	}
	var pageCoord = getPageCoord(target);
	var eventCoord = {
		x: window.pageXOffset + e.clientX,
		y: window.pageYOffset + e.clientY
	};
	var offset = {
		offsetX: eventCoord.x - pageCoord.x,
		offsetY: eventCoord.y - pageCoord.y
	};
	return offset;
}
function getPageCoord(element) {
	var coord = {
		x: 0,
		y: 0
	};
	while (element) {
		coord.x += element.offsetLeft;
		coord.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coord;
}
function viewAnimate(){
	$("body").on("hover",".view,.lp-view-item",function(e){
		var _this  = $(this),
		_desc  = _this.find(".view-shadow").stop(true).show(),
		width  = _this.width(), 
		height = _this.height(),
		left   = (e.offsetX == undefined) ? getOffset(e).offsetX : e.offsetX,
		top    = (e.offsetY == undefined) ? getOffset(e).offsetY : e.offsetY, 
		right  = width - left, 
		bottom = height - top,
		rect   = {},
		_min   = Math.min(left, top, right, bottom),
		_out   = e.type == "mouseleave", 
		spos   = {};
		rect[left] = function (epos){
			spos = {"left": -width, "top": 0};
			if(_out){
				_desc.animate(spos, "fast",function(){
					removeElseMask(_desc);
				});
			}else{
				_desc.css(spos).animate(epos, "fast");
			}
		};
		rect[top] = function (epos) { 
			spos = {"top": -height, "left": 0};
			if(_out){
				_desc.animate(spos, "fast",function(){
					removeElseMask(_desc)
				});
			}else{
				_desc.css(spos).animate(epos, "fast"); 
			}
		};
		rect[right] = function (epos){ 
			spos = {"left": left,"top": 0};
			if(_out){
				_desc.animate(spos, "fast",function(){
					removeElseMask(_desc);
				});
			}else{
				_desc.css(spos).animate(epos, "fast"); 
			}
		};
		rect[bottom] = function (epos){
			spos = {"top": height, "left": 0};
			if(_out){
				_desc.animate(spos, "fast",function(){
					removeElseMask(_desc);
				});
			}else{
				_desc.css(spos).animate(epos, "fast");
			}
		};
		rect[_min]({"left":0, "top":0});
	});
}
function removeElseMask(domItem){
	domItem.hide();
}
viewAnimate();		


//表格隔行换色
$(".table-public").find("tbody tr:even").addClass("tr-alt");
/*滚动条*/
var $scrollPane,paneApi;
try{
	$scrollPane=$('.scroll-pane').jScrollPane();
	paneApi=$scrollPane.data('jsp');
}catch(e){}
$(window).bind(
	'resize',
	function()
	{
		if(paneApi){
			if ($.browser.msie) {
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(
						function()
						{
							paneApi.reinitialise();
							throttleTimeout = null;
						},
						50
					);
				}
			} else {
				paneApi.reinitialise();
			}
		}	
	}
);
/*header部分下拉列表 个人信息  中英文切换*/
$('.user-select,.lang-select').click(function(event) {
	$(this).find(".dk-select-options").slideToggle(100);
	$(this).toggleClass("current");
	$(".school-list-wrapper").slideUp(100);
	event.stopPropagation();
});

$('.user-select').find(".dk-option").click(function(event) {
	$(this).siblings().removeClass("dk-option-selected");
	$(this).addClass("dk-option-selected");
	_optionsHide();
	$(this).parents(".my-select").removeClass("current");
	event.stopPropagation();
});

$(document).click(function(target) {
	var _length = $(target).parents(".my-select").length;
	if (_length <= 0) {
		_optionsHide();
	}
});


function _optionsHide() {
	$(".my-select .dk-select-options").slideUp(100);
	$(".my-select").removeClass("current");
}

$('.lang-select').find(".dk-option").click(function(event) {
	$(this).siblings().removeClass("dk-option-selected");
	$(this).addClass("dk-option-selected");
	_html = $(this).html();
	$(this).parents(".lang-select").find(".dk-selected").html(_html);
	_optionsHide();
	$(this).parents(".my-select").removeClass("current");
	event.stopPropagation();
});

/*fix效果*/
function fixAnimate($obj,initTop,fixTop){
	var _left=$obj.offset().left;
	var _sTop=$(window).scrollTop();
	var _sLeft=$(window).scrollLeft();
	if(initTop<=_sTop&&_sLeft==0){		
		$obj.css({
			position:"fixed",
			left:_left-_sLeft,
			top:fixTop
		});	
	}else{
		$obj.css({
			position:"absolute",
			left:'auto',
			top:0
		});
	}
}
 /*题目闪动效果
 */
function shake(ele, cls, times,time) {
	var i = 0,
		t = false,
		o = ele.attr("class") + " ",
		c = "",
		times = times || 2;
	if (t) return;
	t = setInterval(function () {
		i++;
		c = i % 2 ? o + cls : o;
		ele.attr("class", c);
		if (i == 2 * times) {
			clearInterval(t);
			ele.removeClass(cls);
		}
	},
	time);
};

/*左右滚动的最大幅度*/
var scrollSize=1;
function _getMaxScrollWidth(){
	var $container=$(".tabs-wrap");
	var wrapWidth = $container.outerWidth();
	var tabsWidth = 0;
	$('ul li',$container).each(function(){
		tabsWidth += $(this).outerWidth(true);
	});
	return tabsWidth-wrapWidth;
}
//初始判断，如果tab个数少，则不显示左右按钮
function executeLearnTab(){
	(function(){
		var $tab=$(".learn-tab").find(".tabs").children("li");
		var liSize=$tab.size();
		if(_getMaxScrollWidth()>0&&liSize>5){
			$(".learn-tab").addClass("tab-scroll").find(".scroll-left").addClass("scroll-no");
		}else{
			$(".learn-tab").find(".btn-scroll").hide();
			var totalWidth=$(".learn-tab").outerWidth();
			var avgWidth=(totalWidth/liSize)-1;
			$tab.find(".tab-inner").width(avgWidth);
			$(".learn-tab .tab-active:first-child").find(".tab-inner").width(avgWidth+1);
		}
	})();
	function _setProperties($btn){
		var $container=$(".tabs-wrap");
		var $ul=$container.find("ul");
		var scrollWidth=$ul.find("li").outerWidth(true)*scrollSize;
		var startPos=$container.scrollLeft();
		var btnStr=$btn.attr("class").split(/\s+/)[1];
		if(btnStr=="scroll-right"){
			if((startPos+scrollWidth)>=_getMaxScrollWidth()){
				$btn.addClass("scroll-no");
			}
			var pos=Math.min(startPos+scrollWidth,_getMaxScrollWidth());
		}
		if(btnStr=="scroll-left"){
			if((startPos-scrollWidth)<0){
				$btn.addClass("scroll-no");
			}
			var pos=Math.max(startPos-scrollWidth,0);
		}
		
		$container.stop(true,true).animate({scrollLeft:pos}, 200);
	}
	$(".learn-tab").find(".scroll-right").click(function(){
		$(this).siblings(".scroll-left").removeClass("scroll-no");
		_setProperties($(this));
	});
	$(".learn-tab").find(".scroll-left").click(function(){
		$(this).siblings(".scroll-right").removeClass("scroll-no");
		_setProperties($(this));
	});
}
executeLearnTab();

/*左右滚动工具*/
(function($) {       
	$.fn.scrollToolbar = function(options) { 
		var defaults={size:1}
		var opts = $.extend({},defaults, options); 
		return this.each(function(){
			/*获取最大滚动值*/
			var $this=$(this);
			var $container=$this.find(".scroll-wrap");
			function getMaxScrollWidth(){	
				var wrapWidth = $container.outerWidth();
				var tabsWidth = 0;
				$('.scroll-item',$container).each(function(){
					tabsWidth += $(this).outerWidth(true);
				});
				return tabsWidth-wrapWidth;
			}
			function setProperties($btn){
				var scrollWidth=$container.find(".scroll-item").outerWidth(true)*opts.size;
				var startPos=$container.scrollLeft();
				var btnStr=$btn.attr("class").split(/\s+/)[1];
				if(btnStr=="btn-next"){
					if((startPos+scrollWidth)>=getMaxScrollWidth()){
						$btn.addClass("scroll-disabled");
					}
					var pos=Math.min(startPos+scrollWidth,getMaxScrollWidth());
				}
				if(btnStr=="btn-prev"){
					if((startPos-scrollWidth)<0){
						$btn.addClass("scroll-disabled");
					}
					var pos=Math.max(startPos-scrollWidth,0);
				}
				$container.stop(true,true).animate({scrollLeft:pos}, 200);
			}
			if(getMaxScrollWidth()<0){
				$this.find(".btn-tool").hide();
			}else{
				$this.addClass("scroll-true").find(".btn-prev").addClass("scroll-disabled");
				
				$this.find(".btn-next").click(function(){
					$(this).siblings(".btn-prev").removeClass("scroll-disabled");
					setProperties($(this));
				});
				$this.find(".btn-prev").click(function(){
					$(this).siblings(".btn-next").removeClass("scroll-disabled");
					setProperties($(this));
				});
			}
		}); 
	};     
})(jQuery);      

/*头像圆角*/
function formatAvatar(elem){
	if(window.PIE){
		$(elem).each(function() {
			PIE.attach(this);
		});
	}
}
formatAvatar(".user-max");
formatAvatar(".user-min");
formatAvatar(".user-header");
formatAvatar(".t-img img");
formatAvatar(".img-show");
formatAvatar(".user-img");

/**/
(function($) {
	$.fn.scrollFix = function(options) {
		var defaults = {
			initTop : 0,
			effect:false,
			outerDom:null,//相对外围元素
			relative:null,//浮动dom 宽度相对元素（jQuery Object）
			className:'fix-default'
		};
		var opts = $.extend({}, defaults, options);
		var $tempDom=$("<div class='placeholderDom'>&nbsp;</div>");
		return this.each(function() {
			var $this = $(this),compareLeft=$this.position().left;
			$this.removeAttr("style");
			var offsetTop = $this.offset().top;
			var offsetParentTop=$this.parent().offset().top;
			var _tempDom=$this.parent().find(".placeholderDom");
			if(_tempDom.length>0){
				$tempDom=_tempDom;
			}
			//设置临时元素位置属性
			$tempDom.css({
				"float":$this.css("float"),
				"position":$this.css("position"),
				"top":$this.css("top"), "left":$this.css("left"), "right":$this.css("right")
			});
			$(window).scroll(function() {
				_setProperty($this);	
			});
			$(window).resize(function(){
				$this.removeAttr("style");
				$this.removeClass("fixed-effect").removeClass(opts.className);
				compareLeft=$this.position().left;
				offsetTop = $this.offset().top;
				_setProperty($this);
			});

			_setProperty($this);
			
			/*设置property start*/
			function _setProperty($elem) {
				var _offsetLeft = $elem.parent().offset().left,offsetLeft=_offsetLeft+compareLeft;
				var height =$elem.outerHeight();
				var parentHeight;
				if(opts.outerDom){
					parentHeight=opts.outerDom.outerHeight();
				}else{
					parentHeight= $elem.parent().outerHeight();
					if($elem.parent()[0].tagName=="BODY"){
						parentHeight+=$(window).scrollTop();
					}
				}
				var scrollTop = $(window).scrollTop();
				var scrollLeft = $(window).scrollLeft();
				if (scrollTop > (offsetTop - opts.initTop) && offsetLeft >=0 && scrollLeft <= 0) {
					var compareTop = height - (parentHeight - scrollTop+offsetParentTop);
					$tempDom.css("height",$elem.outerHeight());
					if(!$elem.next(".placeholderDom").length>0){
						$elem.after($tempDom);
					}
					if(opts.relative){
						$elem.css({
							"width":opts.relative.outerWidth()
						});
					}
					if (compareTop < 0) {
						$elem.css({
							position : "fixed",
							left : offsetLeft,
							top : opts.initTop,
							zIndex:20
						});
						if(opts.effect){
							$elem.addClass("fixed-effect");
						}
					} else {
						$elem.css({
							position : "fixed",
							left : offsetLeft,
							top : -compareTop,
							zIndex:20
						});
						if(opts.effect){
							$elem.addClass("fixed-effect");
						}
					}
					if(opts.className){
						$elem.addClass(opts.className);
					}
				} else {
					$elem.css({
						position : "static"
					});
					$elem.removeClass("fixed-effect");
					$elem.removeClass(opts.className);
					$elem.removeAttr("style");
					$elem.next(".placeholderDom").remove();
				}
			}
			/*设置property end*/

			$.fn.updateScrollFix=function(){
				
				_setProperty($this);
				
			}
		});
	};
})(jQuery);

				