// Expandable Blocks
(function($){
	"use strict";
	window.expandables = window.expandables || [];
	$(document).ready(function(){
		$(".expandable-block").each(function(){			
			window.expandables = new expandable(this);
		});
	});
	var expandable = function(el) {
		if (el.expandable === undefined) {
			$.extend(this, {
				container: el,
				title: {},
				control: {},
				content: {},
				visible: false,
				initialized: false,
				animating: false,
				init: function(){
					if (!this.initialized) {
						var me = this;
						me.title = $(me.container).find(".expandable-title");
						me.content = $(me.container).find(".expandable-content");
						me.control = $(me.container).find(".expandable-control");
						me.content.hide();
						me.control.click(function(){
							me.toggle();
						});
						this.initialized = true;
					}
					return this.container;
				},
				show: function() {
					var me = this;
					if (!me.animating) {
						me.animating = true;
						$(me.container).removeClass('closed').addClass('open');
						me.content.slideDown(200,'linear',function(){
							me.visible = true;
							me.animating = false;
						});
					}
					return me;
				},
				hide: function() {
					var me = this;
					if (!me.animating) {
						me.animating = true;
						$(me.container).removeClass('open').addClass('closed');
						me.content.slideUp(200,'linear',function(){
							me.visible = false;
							me.animating = false;
						});
					}
					return me;
				},
				toggle: function (){
					if (this.visible) {
						this.hide();
					} else {
						this.show();
					}
					return this;
				}
			});
			el.expandable = this;
			this.init();
		}
		return el;
	};
}(jQuery));


