var faq = {
	initialize : function() { 
		this.faqs = $$('.faqs')	
		this.faqs.each(function(dl){ this.createAcoordion(dl) }.bind(this));
	},
	createAcoordion: function(faqDL) {
		this.stretchers = $ES('dd', faqDL);
		this.stretchers.each(function(item){ item.setStyles({'height': '0', 'overflow': 'hidden'}) });
		
		//window.onload = function(){ //safari cannot get style if window isnt fully loaded
		this.togglers = $ES('dt', faqDL);
		this.togglers.each(function(toggler, i){ toggler.defaultColor = toggler.getStyle('background-color');	});
		
		this.myAccordion = new Fx.MultipleOpenAccordion(
		this.togglers, 
		this.stretchers, 
			{
				opacity: false, 
				start: 'none', 
				accordObj:faqDL, 
				transition: Fx.Transitions.sineInOut, 
				onActive: function(toggler, i){ toggler.addClass('open');}, 
				onBackground: function(toggler, i){ toggler.removeClass('open');	}, 
				openAllTrigger:true
			}
		);
	}
};

function $set(val, defaultVal){
			if(typeof val == "undefined" || val == null) val = defaultVal;
			return val;
		};	
	
Fx.MultipleOpenAccordion = Fx.Elements.extend({
	extendOptions: function(options){
		// only expand all when it's the FAQ category list
		if ($('faq-list-content') != false) {
			this.allowMultiple = false;
			this.startOverride = 'open-first';
		} else {
			this.allowMultiple = true;
			this.startOverride = 'none';
		} // end else faq

		Object.extend(this.options, Object.extend({
			openAll: false,
			allowMultipleOpen: this.allowMultiple,
			firstElementsOpen: [0],
			start: 'open-first',
			fixedHeight: false,
			fixedWidth: false,
			alwaysHide: true,
			wait: false,
			onActive: Class.empty,
			onBackground: Class.empty,
			height: true,
			opacity: true,
			width: false,
			openAllTrigger:false,
			accordObj: ''
		}, options || {}));
	},
	initialize: function(togglers, elements, options){
		
		this.parent(elements, options);
		this.extendOptions(options);
		this.previousClick = 'nan';
		this.elementsVisible = [];
		togglers.each(function(tog, i){
			$(tog).addEvent('click', function(){this.toggleSection(i)}.bind(this));
		}, this);
		this.togglers = togglers;
		this.h = {}; 
		this.w = {};
		this.o = {};
		this.now = [];
		this.elements.each(function(el, i){
			this.now[i] = {};
			if(this.options.openAll && this.options.allowMultipleOpen) $(el).setStyles({'overflow': 'hidden'});
			else $(el).setStyles({'height': 0, 'overflow': 'hidden'});
		}, this);
		if(!this.options.openAll || !this.options.allowMultipleOpen) {
			// this is the override for the FAQ section
			if (this.startOverride) {
				this.options.start = this.startOverride;
			}
			switch(this.options.start){
				case 'none': break;
				case 'first-open': this.showSection(this.options.firstElementsOpen[0]); break;
				case 'open-first': this.toggleSection(this.options.firstElementsOpen[0]); break;
			}
		}
		if (this.options.openAll && this.options.allowMultipleOpen) {
			this.showAll();
		} else if (this.options.allowMultipleOpen) {
			//this.openSections(this.options.firstElementsOpen);
		}
		if(this.options.openAllTrigger){
			var showAll = this.options.accordObj.getParent().getElement('.showAll')
			var showNone = this.options.accordObj.getParent().getElement('.showNone')
			if (showAll) {
			showAll.addEvent('click', function(){this.toggleAllOpen();}.bind(this));
			showNone.addEvent('click', function(){this.toggleAllClose();}.bind(this));
			}
		}
	},
	hideThis: function(i){ //sets up the effects for hiding an element
		this.elementsVisible[i] = false;
		if (this.options.height) this.h = {'height': [this.elements[i].offsetHeight, 0]};
		if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, 0]};
		if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || 1, 0]};
	},

	showThis: function(i){ //sets up the effects for showing an element
		this.elementsVisible[i] = true;
		if (this.options.height) this.h = {'height': [this.elements[i].offsetHeight, this.options.fixedHeight || this.elements[i].scrollHeight]};
		if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, this.options.fixedWidth || this.elements[i].scrollWidth]};
		if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || 0, 1]};
	},
	toggleSection: function(iToToggle){
		if(iToToggle != this.previousClick || this.options.alwaysHide || this.options.allowMultipleOpen) {
			this.previousClick = iToToggle;
			var objObjs = {};
			var err = false;
			var madeInactive = false;
			this.elements.each(function(el, i){
				var update = false;
				this.now[i] = this.now[i] || {};
				if(i==iToToggle){
					if (this.elementsVisible[i] && (this.options.allowMultipleOpen || this.options.alwaysHide)){
						if(!(this.options.wait && this.timer)) {
							update = true;
							this.hideThis(i);
						} else {
							this.previousClick = 'nan';
							err = true;
						}
					} else if(!this.elementsVisible[i]){
						if(!(this.options.wait && this.timer)) {
							update = true;
							this.showThis(i);
						} else {
							this.previousClick = 'nan';
							err = true;
						}
					}
				} else if(this.elementsVisible[i] && !this.options.allowMultipleOpen) {
					if(!(this.options.wait && this.timer)) {
						update = true;
						this.hideThis(i);
					} else {
						this.previousClick = 'nan';
						err = true;
					}
				} 
				if(update) objObjs[i] = Object.extend(this.h, Object.extend(this.o, this.w));
			}, this);
			if (err) return;
			if (!madeInactive) this.options.onActive.call(this, this.togglers[iToToggle], iToToggle);
			this.togglers.each(function(tog, i){
				if (!this.elementsVisible[i]) this.options.onBackground.call(this, tog, i);
			}, this);
			return this.custom(objObjs);
		}
	},
	showSection: function(i, useFx){
		if($set(useFx, false)) {
			if (!this.elementsVisible[i]) this.toggleSection(i);
		} else {
			this.setSectionStyle(i,$(this.elements[i]).scrollWidth, $(this.elements[i]).scrollHeight, 1);
			this.elementsVisible[i] = true;
			return true;
		}
	},
	hideSection: function(i, useFx){
		if($set(useFx, false)) {
			if (this.elementsVisible[i]) this.toggleSection(i);
		} else {
			this.setSectionStyle(i,0,0,0);
			this.elementsVisible[i] = false;
			return true;
		}
	},
	setSectionStyle: function(i,w,h,o){ 
			if (this.options.opacity) $(this.elements[i]).setOpacity(o);
			if (this.options.height) $(this.elements[i]).setStyle('height',h+'px');
			if (this.options.width) $(this.elements[i]).setStyle('width',w+'px');
	},
	showAll: function(){
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
					this.showSection(idx, false);
			}, this);
		}
	},
	toggleAllOpen: function(){
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
					this.showSection(idx, false);
					this.options.onActive.call(el, this.togglers[idx], idx);
			}, this);
		}
	},
	hideAll: function(useFx){
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
				this.hideSection(idx, false);
			}, this);
		}
	},
	toggleAllClose: function(useFx){
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
				this.hideSection(idx, false);
				this.options.onBackground.call(el,  this.togglers[idx], idx);
			}, this);
		}
	},	
	openSections: function(array) {
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
				if(array.test(idx)) this.showSection(idx, false);
				else this.hideSection(idx, false);
			}, this);
		}
	}
});


function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	
	// otherwise, update 'characters left' counter
	else
		document.getElementById(cntfield).innerHTML =  maxlimit - field.value.length +' characters left';
};

function clearField(field) {
	if (field.value == 'Type your question here'){
		field.value = '';
	} 
};

function utter(q,i){
document.location="/owner-services/customer-support/ask-ford-faqs?qtype=qn&inputtext="+q;
};

setLoaded('faq');
