
var FAQ = (function(){
	function constructor(){
		this.list = $('#faqs-list li');
		this.Alist = [];
		this.list.each(
			(function(e){return function(i,li){
				e.Alist.push(new Q($(li),e));
			}})(this)
		);
	}
	
	constructor.prototype.closeAll = function(){
		for(A in this.Alist){
			this.Alist[A].toggleA(0);
		}
	}
	
  	return constructor;
})();


var Q = (function(){
	function constructor(_element,_controller){
		this.p = 0;
		this.li = _element;
		this.controller = _controller;
		this.q = this.li.children('a');
		this.a = this.li.children('div');
		this.qHeight = this.q.outerHeight();
		this.aHeight = this.a.outerHeight();
		this.li.css('background-position','0 '+this.qHeight+'px');
		this.children = this.a.children();
		
		this.a.css('height','0');
		this.children.css('position','relative').css('top',-this.aHeight*1.5+'px');
		
		this.q.click((function(e){return function(){
			return e.toggleA(e.li.hasClass('active')?0:1);
		}})(this));
	}
	
	constructor.prototype.position = function(pos){
		this.a.css('height',pos*this.aHeight);
		this.children.css('top',(1-pos)*-this.aHeight*1.5+'px');
		this.li.css('background-position','0 '+(1-pos)*this.qHeight+'px');
	}
	
	constructor.prototype.toggleA = function(end){
		if(end){
			this.controller.closeAll();
			this.li.addClass('active');
		} else {
			this.li.removeClass('active');	
		}
		$(this).stop().animate({p:end},{
			duration:600,
			easing:'easeInOutQuad',
			step:(function(e){return function(p){e.position(p);return false;}})(this)
		});
		return false;
	};
	
  	return constructor;
})();


$(document).ready(function() {

	if('FAQ' in window){var fq = new FAQ();}
	
	var bing = 0;
	
		
	
	$(".nav a").live("click",function(e) { 
		bing = 1;
		e.preventDefault();
		$(".left-nav ul li a").css("color","");
		$(this).css("color","#8c7b58");
		$(".left-nav ul li a").css("backgroundPosition","");
		$(this).css("backgroundPosition","0px -65px");
		$("body").stop(true,true).scrollTo("#container #"+$(this).attr("class"),{speed:1000,easing:"easeInOutQuad",queue:false,offset:-131,onAfter:function() {
			bing = 0;
		}});
	});
	
	$(window).scroll(function() {
		if(bing == 0) {
		$(".item").each(function() {
			if($(window).scrollTop()+131 >= $(this).position().top && $(window).scrollTop()+131 < $(this).position().top+$(this).height()+131) {
				$(".left-nav ul li a").css({"backgroundPosition":"","color":""});
				$(".left-nav ul li ."+$(this).attr("id")).css({"backgroundPosition":"0px -65px","color":"#8c7b58"});
			}
		});
		}
	});
	
	
	
});

window.onload = function(){
	var hash = document.location.hash;//reposition anchor tag correctly with the header
	if(hash && hash.indexOf('/') == -1 && $(hash).length){
		$("body").stop(true,true).scrollTo($(hash),{speed:0,offset:-131});
	}
}
