// JavaScript Document

$(function(){
	new System();
});

/**
 * 系统
 */
var System = function(){
	this.dom = this.getDom();
	this.timer;
	this.init();
}
System.prototype = {
	init: function(){
		var _this = this;
	
		this.changeNav();
		
		this.dom.qqBtn.hover(
			function(){ _this.dom.qqImg.stop().show('fast'); },
			function(){ _this.dom.qqImg.stop().hide('fast'); }
		);
		
		this.dom.wxBtn.hover(
			function(){ _this.dom.wxImg.stop().show('fast'); },
			function(){ _this.dom.wxImg.stop().hide('fast'); }
		);
		
		this.dom.sBtn.click(function(){
			if(_this.dom.sKw.val() == '')
				window.open('http://www.baidu.com/s?wd=site:www.shifangyuan.org');
			else
				window.open('http://www.baidu.com/s?wd=' + _this.dom.sKw.val() + ' site:www.shifangyuan.org');
		});
	},
	changeNav: function(){
		var _this = this;
		
		this.dom.nav.hover(
			function(){
				var self = $(this);
				_this.timer = setTimeout(function(){
					self.find('b').show().prev().addClass('a');
					self.find('div').slideDown('fast');
				}, 600);
			},
			function(){
				clearTimeout(_this.timer);
				$(this).find('b').hide().prev().removeClass('a');
				$(this).find('div').stop().slideUp('fast');
			}
		);
	},
	getDom: function(){
		return {
			nav		: $('#nav .left > li'),
			qqBtn	: $('#show_qq_btn'),
			wxBtn	: $('#show_wx_btn'),
			qqImg	: $('#qq_img'),
			wxImg	: $('#wx_img'),
			sKw		: $('#search_kw'),
			sBtn	: $('#search_btn')
		}
	}
}

/**
 * BANNER
 */
var Banner = function(speed){
	this.dom = this.getDom();
	this.p;
	this.timer;
	this.speed = speed;
	this.init();
}
Banner.prototype = {
	init: function(){
		this.format();
	},
	format: function(){
		var _this = this;
		var bannerBtn;
		bannerBtn = HTML.btns;
		for(var i = 0; i < this.dom.imgs.length; i++){
			bannerBtn += HTML.btnc;
		}
		bannerBtn += HTML.btne;
		this.dom.banner.append(bannerBtn);
		bannerBtn = this.dom.banner.find('span');
		this.dom.imgs.hide().css('z-index', '0');
		this.dom.imgs.eq(0).show().css('z-index', '1');
		bannerBtn.removeClass('s');
		bannerBtn.eq(0).addClass('s');
		this.p = 0;
		this.itv();
		bannerBtn.each(function(i){
			$(this).mouseover(function(){
				_this.clearItv();
				_this.play(i);
				_this.itv();
			});
		});
	},
	play: function(p){
		var bannerBtn = this.dom.banner.find('span');
		if(p == this.p)
			return false;
		
		this.dom.imgs.eq(this.p).fadeOut('slow').css('z-index', '0');
		this.dom.imgs.eq(p).fadeIn('slow').css('z-index', '1');
		
		bannerBtn.eq(this.p).removeClass('s');
		bannerBtn.eq(p).addClass('s');
		
		this.p = p;
	},
	autoPlay: function(){
		var bannerBtn = this.dom.banner.find('span');
		var sel = this.p;
		this.p == (this.dom.imgs.length - 1) ? this.p = 0 : this.p++;
		
		this.dom.imgs.eq(sel).fadeOut('slow').css('z-index', '0');
		this.dom.imgs.eq(this.p).fadeIn('slow').css('z-index', '1');
		
		bannerBtn.eq(sel).removeClass('s');
		bannerBtn.eq(this.p).addClass('s');
	},
	itv: function(){
		var _this = this;
		this.timer = setInterval(function(){
			_this.autoPlay();
		}, this.speed);
	},
	clearItv: function(){
		clearInterval(this.timer);
	},
	getDom: function(){
		return {
			banner	:	$('#banner'),
			imgs	:	$('#banner img')
		}
	}
}

/**
 * 风采
 */
var Slide = function(){
	this.dom = this.getDom();
	this.p = 0;
	this.listNum = [];
	this.f = [0, 0, 0, 0];
	this.init();
}
Slide.prototype = {
	init: function(){
		var _this = this;
		
		this.dom.list.each(function(i){
			_this.listNum[i] = $(this).find('a').length;
		});
	
		this.changeColumn();
		
		this.dom.leftBar.click(function(){
			_this.changeList(true);
		});
		
		this.dom.rightBar.click(function(){
			_this.changeList(false);
		});
	},
	changeColumn: function(){
		var _this = this;
		var tabs = this.dom.tabs;
		var imgWrap = this.dom.imgWrap;
		var index, oft;
		
		tabs.click(function(){
			_this.p = index = $(this).index();
			oft = -(index * 152);
			tabs.removeClass('li').eq(index).addClass('li');
			imgWrap.stop().animate({marginTop: oft + 'px'}, 'normal');
		});
	},
	changeList: function(b){
		var maxP = (this.listNum[this.p] - 4) < 0 ? 0 : this.listNum[this.p] - 4;
		var oft;
		
		if(b){
			if(this.f[this.p] > 0)
				this.f[this.p]--;
			else
				return false;
		}else{
			if(this.f[this.p] < maxP)
				this.f[this.p]++;
			else
				return false;
		}
		
		oft = -(this.f[this.p] * 220);
		
		this.dom.list.eq(this.p).stop().animate({marginLeft: oft + 'px'}, 'normal');
	},
	getDom: function(){
		return {
			tabs		: $('#slide .tab li'),
			leftBar		: $('#slide .leftbar'),
			rightBar	: $('#slide .rightbar'),
			imgWrap		: $('#slide .imgwrap'),
			list		: $('#slide .list')
		}
	}
}

var _p = 0;

/**
 * 获取图片列表
 */
function getData(cid, b){
	if(b)
		_p--;
	else
		_p++;
	if(_p <= 1)
		_p = 1;
	
	$.ajax({
		type: 'get',
		url: '?m=imgs&a=getdata&cid=' + cid + '&p=' + _p + '&' + Math.random(),
		success: function(retVal){
			if(retVal == ''){
				_p--;
				return false;
			}
			
			$('#imgListWrap').html(retVal).find('img').css('opacity', 0.5).animate({opacity: 1}, 'slow');
		}
	});
}

var HTML = {
	btns	:	'<ul>',
	btnc	:	'<span>&nbsp;</span>',
	btne	:	'</ul>'
}