function AsTabs() {}
AsTabs.prototype = {
	delay: 5,
	slideTime: 500,
	delta: function(progress) {
		if (progress < 0.5) {
			return this.basicDelta(2 * progress) / 2;
		}
		else {
			return (2 - this.basicDelta(2 * (1 - progress))) / 2;
		}
	},
	basicDelta: function(progress) {
		return Math.pow(progress,6);
	},
	init: function(content) {
		this.content = content;
		this.cItems = as.w(this.content.childNodes).filter(function() {return this.tagName && this.tagName.toLowerCase() == "li"});
		
		
		this.generateContents();
		this.decorate();
		this.addEventListeners();
		this.slideIn(0);
	},
	addEventListeners: function() {
		as.w("a",this.contents).click(function(e,index) {
			e.preventDefault();
			this.slideIn(index);
		},this);
	},
	generateContents: function() {
		var contents = as.before("<ul class='as-tabs-contents'></ul>",this.content);
		as.w("div.title h3",this.content).each(function () {
			as.append("<li><a href='#'>"+this.innerHTML+"</a></li>",contents);
		})
		this.contents = contents;
		this.contentsItem = as.w("li",this.contents);
	},
	decorate: function() {
		this.cItems.each(function() {this.style.height = "0px"});
	},
	slideIn: function(newIndex) {
		this.contentsItem.removeClass("active");
		this.contentsItem.set[newIndex].className += " active";
		if (this.currentIndex != undefined) {
			this.slide({
				ast: new Date(),
				element: this.cItems.set[this.currentIndex],
				from: this.cItems.set[this.currentIndex].offsetHeight,
				to: 0,
				param: "height",
				addition: "px"
			});
		}
		this.cItems.set[newIndex].style.display = "block";
		this.slide({
			ast: new Date(),
			element: this.cItems.set[newIndex],
			from: 0,
			to: as.$$("div.tabs-item",this.cItems.set[newIndex]).offsetHeight,
			param: "height",
			addition: "px"
		});
		this.currentIndex = newIndex;
	},
	slide: function(data) {
		var slideTime = data.slideTime || this.slideTime
		var progress = (new Date() - data.ast)/slideTime;
		var delta = this.delta(progress);
		data.element.style[data.param] = data.from + (data.to - data.from)*delta + data.addition;
		
		if (progress >= 1) {
			data.element.style[data.param] = data.to + data.addition;
			try {
				this.afterSlide();
				data.callback();
			}
			catch(e) {}
		}
		else {
			setTimeout(as.bind(function() {
				this.slide(data);
			},this),this.delay);
		}
	},
	afterSlide: function() {
		var c = this.currentIndex;
		this.cItems.each(function(index) {if (c != index) {this.style.display = "none"}});
	}
}

function asTabsInit() {
	as.w("ul.as-tabs-content").construct(AsTabs);
}
as.ready(asTabsInit);