 (function($) {
	$.fn.accordion = function(o) {
		return this.each(function() {			
			new $.accordion(this,o);
			
		});
	}
	
	$.accordion = function(ele, o) {
		var options = $.extend({ 
			easing: "easeOutExpo",
			alwaysOpen: true,
			duration: 500
		},o);
		
		var self = this;
		var p = ele;
		var current = undefined;
		var bAnim = false;
		var contentHeight = 0;
		var toShow = undefined;
		var toHide = undefined;
		
		var onMouseOver = function() {
			$(this).parent().addClass("hover");
		};
		var onMouseOut = function() {
			$(this).parent().removeClass("hover");
		};
		
		var onMouseClick = function() {
			//if (current == this) { return true; }
			if (bAnim == true) { return false; }
			
			bAnim = true;
			
			var c = undefined;
			var e = undefined;
			var ca = "hide";
			var ea = "show"
			
			toShow = undefined;
			toHide = undefined;
			
			prt = $(this).parent();
			
			if ($(prt).is(".active") && options.alwaysOpen) {
				bAnim = false;
				return false;
			}
			
			if ($(prt).is(".active")) {
				$(prt).removeClass("active");
				toHide = $(".item-content",prt);
			} else {
				toHide = $(".active",p);
				if (toHide.length < 1) {
					toHide = undefined;
				} else {
					$(toHide).removeClass("active");
					toHide = $(".item-content",toHide);
				}
				
				$(prt).addClass("active");
				toShow = prt;
				toShow = $(".item-content",toShow);
			}
			
			if (toShow != undefined) {
				toShow.stop();
				h = toShow.height();
				if (h != contentHeight)  {
					ea = "+=" + (contentHeight - h);
				} else {
					ea = "show";
				}
			}
			
			if (toHide != undefined) {
				toHide.stop();
				h = toHide.height();
				
				if (h != contentHeight)  {
					ca = "-=" + h;
				} else {
					ca = "hide";
				}
			}
				
			if (toHide != undefined) {
				var height = toHide.height();
				if (toShow) {
					toShow.css({ height: "0px", overflow: 'hidden' }).show();
				}
				toHide.animate({height:"hide"},{
					duration:options.duration,
					queue:false,
					easing:options.easing,
					step: function(n){
						if (toShow != undefined) {
							h = Math.ceil(height - n);
							toShow.height(h);
						}
					},
					complete:function() {
						bAnim = false;
					}
				});
			} else if (toShow != undefined) {
				toShow.animate({height:ea},{duration:options.duration,queue:false,easing:options.easing,complete:function(){
					bAnim = false;
				}});
			}
			
			return false;
		};
		
		var init = function() {
			$(p).addClass("accordion");
			
			maxHeight = 0;
			headHeight = 0;
			itemsCount = 0;
			
			items = new Array();
			totalTitleHeight = 0;
			i=0;
			$(">li", p).each(function() {
				$(this).addClass("accordion-item");
				
				head = $(">a:eq(0)",this);
				content = $(">div:eq(0)",this);
				
				$(head).addClass("item-header");
				$(content).addClass("item-content");
				
				h1 = $(head).height();
				if (headHeight < h1) {
					headHeight = h1;
				}
				
				
				h2 = $(content).height();
				if (maxHeight < h2) {
					maxHeight = h2;
				}
				
				items[i] = {
					'height' : h1+h2,
					'title' : head,
					'content' : content
				}
				
				i++;
			});
			
			itemCount = i;
			contentHeight = maxHeight;
			
			for(i=0;i<itemCount;i++) {
				$(items[i].content).css({height:contentHeight+"px"});
			}
			
			$(".item-content",p).hide();
			$("li:eq(0)>.item-content",p).show();
			
			maxHeight = $(p).height() + 3;
			
			$(p).css({height:maxHeight+"px",overflow:"hidden"});
			
			$(".item-content",p).hide();
			
			if ($("li.active",p).size() > 0) {
				$("li.active",p).show();
			} else if (options.alwaysOpen) {
				$("li:eq(0)",p).addClass("active");
				$("li:eq(0)>.item-content",p).show();
			}
			
			$(".item-header", p).bind("click", onMouseClick);
			$(".item-header", p).bind("mouseover", onMouseOver);
			$(".item-header", p).bind("mouseout", onMouseOut);
		};
		
		init();
	}
})($);
