var Home = {};

Home.proItem = {
	over: function(el){
		var li = $(el).upWithMe('li');
		var p = li.last();
		p.anim().to('top', -1).ease(Como.Anim.ease.end).duration(200).go();
	},

	out: function(el){
		var li = $(el).upWithMe('li');
		var p = li.last();
		p.anim().to('top', -107).ease(Como.Anim.ease.begin).duration(200).go();
	}
};

Home.headItem = Class.create({
	initialize: function(opt){
		this.tabt = $(opt.tabt);
		this.tabc = $(opt.tabc);
		this.cur = opt.cur || 0;
		this.isAniming = false;
		this.size = this.tabt.length;
		this._bind();
	},

	setInterval: function(){
		this.clearInterval();
		this._bindInterval = window.setInterval(this._bindIntervalFun.bind(this), 5000);
	},

	clearInterval: function(){
		if(!this._bindInterval){
			window.clearInterval(this._bindInterval);
			this._bindInterval = null;
		}
	},

	_bindIntervalFun: function(){
		this.setCur(this.cur + 1);
	},

	_bind: function(){
		this.tabt.on('click', function(e){
			if(this.isAniming)return;
			var span = Como.Event.element(e);
			var key = span.attr('key').toInt();
			if(key == this.cur)return;
			this.setCur(key);
		}.bind(this));
	},

	setCur: function(n){
		if(this.cur == n) return;
		if(n >= this.size) n = 0;
		if(n < 0) n = 0;
		this.isAniming = true;
		var curEl = $(this.tabc.get(this.cur));
		var nextEl = $(this.tabc.get(n));

		var curSpan = $(this.tabt.get(this.cur));
		var nextSpan = $(this.tabt.get(n));
		curSpan.removeClass('on');
		nextSpan.addClass('on');
		if(this.cur > n){
			curEl.anim().from('left', 0).to('left', 860).to('opacity', 0).ease(Como.Anim.ease.begin).ondone(function(){
				curEl.removeAttr('style').hide();
				if(!Como.Browser.ie)curEl.css('opacity', 1);
			}).go();
			nextEl.anim().show().from('left',-860).to('left', 0).ease(Como.Anim.ease.begin).ondone(function(){
				nextEl.removeAttr('style');
				this.isAniming = false;
			}.bind(this)).go();
		} else {   //向左
			curEl.anim().from('left', 0).to('left', -860).to('opacity', 0).ease(Como.Anim.ease.begin).ondone(function(){
				curEl.removeAttr('style').hide();
				if(!Como.Browser.ie)curEl.css('opacity', 1);
			}).go();
			nextEl.anim().show().from('left',860).to('left', 0).ease(Como.Anim.ease.begin).ondone(function(){
				nextEl.removeAttr('style');
				this.isAniming = false;
			}.bind(this)).go();
		}
		this.cur = n;
	}
});

Como.onloadHandler(function(){
	if(window._page == 'home'){
		var a = new Home.headItem({
				tabt: '#chead .cheadm span',
				tabc: '#chead .chead-item',
				cur: 0
			});
	}
});
