/*
jQuery scripts for Loewy Accordion (for Services page)
- Sukiman, suki@webexcellence.net, http://www.webexcellence.net/
*/
(function($) {
	$.loewy = $.loewy || {};
	
	$.fn.loewyAccordion = function(o) {
		return this.each(function() {			
			new $.loewy.Accordion(this,o);
			
		});
	}
	
	$.loewy.Accordion = function(ele, o) {
		var options = $.extend({ 
			easeShow: "easeOutExpo",
			easeHide: "easeOutExpo",
			duration: 400
		},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).addClass("navTitle_hover")
		};
		var onMouseOut = function() {
			$(".navTitle_hover",p).removeClass("navTitle_hover");
			$(this).find("a").css("text-decoration","none");
		};
		
		var onMouseClick = function() {
			//if (current == this) { return true; }
			if (bAnim == true) { return true; }
			
			bAnim = true;
			
			var c = undefined;
			var e = undefined;
			var ca = "hide";
			var ea = "show"
			
			toShow = undefined;
			toHide = undefined;
			
			if ($(this).is(".navTitle_current")) {
				$(this).removeClass("navTitle_current");
				toHide = $(this).next(".navContent");
			} else {
				toHide = $(".navTitle_current",p).next(".navContent");
				if (toHide.size() < 1) {
					toHide = undefined;
				}
				
				$(".navTitle_current",p).removeClass("navTitle_current");
				
				$(this).addClass("navTitle_current");
				toShow = $(this).next(".navContent");
			}
			
			if (toShow != undefined) {
				
				toShow.stop();
				h = toShow.outerHeight();
				if (h != contentHeight)  {
					ea = "+=" + (contentHeight - h);
				} else {
					ea = "show";
				}
			}
			
			if (toHide != undefined) {
				toHide.stop();
				h = toHide.outerHeight();
				
				if (h != contentHeight)  {
					ca = "-=" + h;
				} else {
					ca = "hide";
				}
				
				
			}
				
			if (toHide != undefined) { 
				var height = toHide.outerHeight();
				if (toShow) {
					toShow.css({ height: 0, overflow: 'hidden' }).show();
				}
				toHide.animate({height:"hide"},{
					duration:options.duration,
					queue:false,
					easing:options.easeHide,
					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.easeShow,complete:function(){
					bAnim = false;
				}});
			}
			
			return false;
		};
		
		var init = function() {
			maxHeight = $(p).height();
			itemsCount = $(".navItem", p).length;
			
			items = new Array();
			totalTitleHeight = 0;
			i=0;
			$(".navItem", p).each(function() {
				h = $(".navTitle", this).height();
				
				items[i] = {
					'height' : h,
					'title' : $(".navTitle", this),
					'content' : $(".navContent", this)
				}
				totalTitleHeight+=h;
				i++;
			});
			
			contentHeight = maxHeight - totalTitleHeight;
			$(".navContent",p).each(function(){
				$(this).css("display", "none");
			});
			
			$(".navTitle", p).bind("click", onMouseClick);
			$(".navTitle", p).bind("mouseover", onMouseOver);
			$(".navTitle", p).bind("mouseout", onMouseOut);
		};
		
		init();
	}
})($);