//Global object
if(!(document._w)){_w = {}}
(function($){
	$.fn.extend({
		drawerDown : function(speed) {
			var speed = speed || 500;
			if(''==this.attr('drawerdown')){
				this.css({marginTop: this.height() * -1 ,display:'block'})
					.animate({opacity:0.5},{queue:false,duration:0});
			}
			this.css({ display:'block'})
				.animate({marginTop: this.height() * -1 ,opacity:0.5},{queue:false,duration:0});
			this.animate({opacity:1,marginTop:0},{queue:false,duration:speed});
			this.attr('drawerdown','1');
			return this;
		},
		drawerUp : function(speed) {
			var speed = speed || 500;
			this.animate({opacity:0,marginTop:this.height() * -1},{queue:false,duration:speed,complete:function(){$(this).animate({opacity:1})}});
			this.attr('drawerdown','0');
			return this;
		},
		drawerToggle : function(speed) {
			var speed = speed || 500;
			if(this.attr('drawerdown') == '1') {
				return this.drawerUp(speed);
			} else {
				return this.drawerDown(speed);
			}
		}
	})
	var calcAccord = function(baseclass){
		this.baseclass = baseclass || 'section';
		this.handle = '.'+baseclass+'head';
		this.drawer = '.'+baseclass+'drawer';
		this.content = '.'+baseclass;
	};
	calcAccord.prototype = {
		init : function(){
			var handle = this.handle;
			var content = this.content;
			var drawer = this.drawer;
			$(content).not(content+'-open').drawerUp();
			$(content+'-open').attr('drawerDown', '1');
			$(handle).click(function(){
				var handleclass = handle.split('.')[1];
				if($(this).hasClass(handleclass+'-open')){
					$(this).addClass(handleclass+'-close').removeClass(handleclass+'-open');
				}else if($(this).hasClass(handleclass+'-close')){
					$(this).removeClass(handleclass+'-close').addClass(handleclass+'-open');
				}else if($(this).hasClass(handleclass+'-first-open')){
					$(this).addClass(handleclass+'-first-close').removeClass(handleclass+'-first-open');
				}else {
					$(this).removeClass(handleclass+'-first-close').addClass(handleclass+'-first-open');
				}
				$(this).next(drawer).children(content).drawerToggle();
				return false;
			})
			return this;
		},
		adjustHeight : function() {
			$(this).drawerUp()
		}
	}
	$(function(){
		var t;
		t = new calcAccord('section').init();
	})
})(jQuery);