jQuery(function() {
	//we cannot set this as default as it is used by contact us popup already
	//Application.defaults.popup_arrContainerIds = "#popupFaq";				
	var POPUP_FAQ_ID = "#popupFaq";
	var POPUP_FAQ_CLOSE_ID = "#popupFaqClose";
	var LIST_FAQ_SELECTOR = "ul.listFaq";
	
	function getListFaqItems() {
		return jQuery(LIST_FAQ_SELECTOR + " > li");
	}
	
	jQuery(POPUP_FAQ_CLOSE_ID).click(function(){
		Application.disablePopup();
	});				
				
	jQuery("body").addClass("yui-skin-sam");	
	
	if (jQuery("#residualValueRates").length) displayData("FAQResidualValueData","residualValueRates");
	if (jQuery("#StatutoryBands").length) displayData("StatutoryBandsData","StatutoryBands");
	
	getListFaqItems().click(function() {
		
		var index = jQuery.inArray(this, getListFaqItems());
		selectFaqListItem(index);
		displayPopup();				 
	}).each(function(i) {
		jQuery(this).prepend('<span class="bullet"><span class="qNum">Q' + (i + 1) + '</span>.</span>');
	});
	
	jQuery("#popupFaqPrev").click(function(event) {
		event.preventDefault();
		var index = jQuery(POPUP_FAQ_ID).data("selectedIndex");
		if (isNaN(index) || index <= 0) {
			index = 0;
		} else {
			index--;
		}
		selectFaqListItem(index);
		displayPopup();
	});
	
	jQuery("#popupFaqNext").click(function(event) {
		event.preventDefault();
		var index = jQuery(POPUP_FAQ_ID).data("selectedIndex");
		var maxIndex = getListFaqItems().length - 1;
		if (isNaN(index) || index >= maxIndex) {
			index = maxIndex;
		} else {
			index++;
		}
		selectFaqListItem(index);
		displayPopup();
	});
	
	var trigger_scroll = 0, process_scroll;
	function defer_scroll(element) {
		var period = 500;
		var currentTime = (new Date()).getTime();
		if (currentTime - trigger_scroll > period) {
			jQuery.scrollTo(element, 400, {});
		} else {
			if (process_scroll) clearTimeout(process_scroll);
			process_scroll = setTimeout("defer_scroll(" + element + ")", period);
		}
		trigger_scroll = currentTime;
	}
	function selectFaqListItem(index) {
		getListFaqItems().each(function(i) {
			var li = jQuery(this);
			if (index == i) {
				li.find("span.faqTitle").addClass("selected");
				jQuery(POPUP_FAQ_ID).data("selectedIndex", index);
				//defer_scroll(li);
			} else {
				li.find("span.faqTitle").removeClass("selected");
			}
		});
	}
	
	function displayPopup() {
		var index = jQuery(POPUP_FAQ_ID).data("selectedIndex");
		var len = getListFaqItems().length;
		if (index == 0 || len == 0) {
			jQuery("#popupFaqPrev").hide();
		} else {
			jQuery("#popupFaqPrev").show();
		}
		if (len == 0 || index == len - 1) {
			jQuery("#popupFaqNext").hide();
		} else {
			jQuery("#popupFaqNext").show();
		}
		var body = jQuery("#popupFaq .body");
		body.html(getListFaqItems()[index].innerHTML).find("div.faqContent").css("display", "block").find(".contactUsLink").each(function() {
			Application.initContactUsEvent(this);	
		});
		body.find("span.bullet").remove();
		body.find("span.faqTitle").html(function(i, html) {
			return '<span class="enlarge">Q' + (index + 1) + '</span>. ' + html;
		});
		
		jQuery("#txtPageInfo").text("Question " + (index + 1) + " of " + len);

		Application.popup(POPUP_FAQ_ID);
	}
	
	function displayData(srcTable,displayArea){
		var output = document.getElementById(displayArea);
		var xml = getXhttp( );
		
		if (!xml){
				output.innerHTML = "<p class=\"error\">Could not obtain the latest Residual Value figures. Please contact ORIX via Contact Us page.</p>";
		}else{
			
			//prepare the submit string
			xml.open('POST', '/_out/'+srcTable+'.html',true);
			//make sure to add following header to dynamically generated page!!
			xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xml.send('');
	
			//wait for the response
			xml.onreadystatechange = function() {
				if (xml.readyState == 4){
					if(xml.status == 200){
						//show results
						output.innerHTML = xml.responseText;
					}else{
						//some error ocurred, display returned error text
						output.innerHTML = "<p class=\"error\">Could not obtain the latest Residual Value figures. Please contact ORIX via Contact Us page.</p>";		
					}
				}
			}//END onreadystatechange
		}
	}
	
});
