﻿var YXD = {};

YXD.slideMenuInit = function(num){
	$('#slideMenu li').eq(num).addClass('on');
	$('#slideMenu li').each(function(i){
		if($(this).hasClass('on')){
			var _t = $(this).position().top - 2;
			$('#menuSlider').css({top:_t});
			$('#menuSlider').fadeIn(100);
		}
	});
}

YXD.slideMenu = function(){
	var menu_li = $('#slideMenu li');
	var menu_a = $('#slideMenu a');
	var menu_slider = $('#menuSlider');
	var timer;	
	menu_li.mouseenter(function(){
		var _this = $(this);
		timer = setTimeout(function(){
			var _t = _this.position().top - 2;
			menu_li.removeClass('on');
			menu_slider.fadeIn(100);
			menu_slider.animate(
				{top:_t},200,function(){
					_this.addClass('on');
					//_this.find('a').css({zoom:1});
				});
			
		},250);
	});
	
	menu_li.mouseleave(function(){
		clearTimeout(timer);
	});
}

YXD.topbtn = function(){
	var topBtn = $('#slide-topbtn');
	var mainContent = $('.mid-bd');
	var _win_h, _body_h, _left, _scrolltop;
	var _main_h = mainContent.height();
	
	topBtn.click(function(){$('html,body').animate({scrollTop: '0'}, 500);return false;});
	
	function setPos(){
		_body_h = $(document).height();
		_win_h = $(window).height();
		_scrolltop = document.documentElement.scrollTop;
		_left = mainContent.offset().left + 554;
		
		if(_scrolltop > 200){
			_setTop = _scrolltop + _win_h - 120;
			topBtn.css({left:_left, top:_setTop});
			topBtn.show();
		}
		if((_body_h - _scrolltop - _win_h) < 180){
			_setTop = _body_h - 380;
			topBtn.css({left:_left, top:_setTop});
			topBtn.show();
		}
		
		
		
	}
	
	setPos();
	
	$(window).resize(function(){
		setPos();
	});
	
	var _timer;
	$(window).scroll(function(){
		topBtn.hide();
		_timer = setTimeout(setPos,500);
	});
	
	if(_timer){
		clearTimeout(_timer);
	}

}

function Slider(ctrls, pics, config){
	this.ctrls = ctrls;
	this.pics = pics;
	this.config = {
		speed:1500,//轮播速度
		fadeSpeed:1500,//渐显速度
		auto:true,//是否启用自动播放
		xmlurl:null,//配置图片地址、链接的xml文件地址
		effect:true,//是否开启效果（未实现）
		ctrlClass:'',//当前按钮的样式名
		index:0,//初始化显示第n张图片
		wrap:'',//图片容器
		ctrlClass:''//图片容器的class 
	}
	for(var i in config){
		this.config[i] = config[i];
	}
	
	if(this.ctrls.length <= 1){
		this.ctrls.hide();
		return;
	}
	
	
	var _this = this;
	if(_this.config.xmlurl != null){this.loadxml(_this)}
	this.init(_this);
}

Slider.prototype = {
	loadxml:function(_this){
		$.ajax({
			type:'GET',
			url:_this.config.xmlurl,
			async:false,
			cache:false,
			timeout:2000,
			error:function(XMLHttpRequest, textStatus, errorThrown){
				_this.ctrls.hide();
				return false;
			},
			success:function(resp){
				var respics = $(resp).find('items pic');
				_this.pics.find('a').each(function(i){
					$(this).html('<img src="' + $(respics).eq(i).attr('href') + '" />');
				});
			}
		});
	},
	
	init:function(_this){
		
		this.pics.hide();
		this.ctrls.removeClass(this.config.ctrlClass);
		this.pics.eq(this.config.index).show();
		this.ctrls.eq(this.config.index).addClass(this.config.ctrlClass);
		
		function temp(){
			_this.autoSlide();
		}
		
		if(this.config.auto){
			var timer = setInterval(temp,_this.config.speed);
		}
		
		this.timer2 = null;
		
		this.ctrls.each(function(i){
			$(this).mouseenter(function(){
				if(timer){
					clearInterval(timer);
				}
				if($(this).hasClass(_this.config.ctrlClass)){
					return false;
				}
				__this = $(this);
				_this.timer2 = setTimeout(function(){
						_this.ctrls.removeClass(_this.config.ctrlClass);
						$(__this).addClass(_this.config.ctrlClass)
						_this.pics.hide();
						_this.pics.eq(i).fadeIn(_this.config.fadeSpeed);
						if(_this.config.wrap != ''){
							var rg = new RegExp(_this.config.wrapClass + '\\d');
							_this.config.wrap.get(0).className = _this.config.wrap.get(0).className.replace(rg, '');
							_this.config.wrap.addClass(_this.config.wrapClass + i);
						}
					},200)
				
			})
			.mouseleave(function(){
				if(_this.config.auto){
					timer = setInterval(temp,_this.config.speed);
				}
				clearTimeout(_this.timer2);
			});
		});
	},
	
	autoSlide:function(){
		var rg = new RegExp(this.config.wrapClass + '\\d');
		if(this.config.index == this.pics.length - 1){
			this.pics.eq(this.config.index).hide();
			this.pics.eq(0).fadeIn(this.config.fadeSpeed);
			this.ctrls.eq(this.config.index).removeClass(this.config.ctrlClass);
			this.ctrls.eq(0).addClass(this.config.ctrlClass);
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.removeClass(this.config.wrapClass + this.config.index);
			}
			this.config.index = 0;
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.addClass(this.config.wrapClass + this.config.index);
			}
		}
		else{
			this.ctrls.eq(this.config.index).removeClass(this.config.ctrlClass);
			this.ctrls.eq(this.config.index + 1).addClass(this.config.ctrlClass);
			this.pics.eq(this.config.index).hide();
			this.pics.eq(this.config.index + 1).fadeIn(this.config.fadeSpeed);
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.removeClass(this.config.wrapClass + this.config.index);
			}
			
			this.config.index++;
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.addClass(this.config.wrapClass + this.config.index);
			}
			
		}
		return this.config.index;
	}
}

$(document).ready(function(){
	YXD.topbtn();
})