// --------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------
// behaviours - manages display and hide of help overlay;
// depends: 	jQuery library;
// author:		mdja;
// --------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------


// -- define namespace;
if (typeof Barclaycard !== 'object') var Barclaycard={};

// -- page help overlay - control object;  
Barclaycard.help = function(el) {
	var root_el = el;
	var callout_el = null;
	var default_help_url = '#';
	var more_link_el = null;
	var timeout = null;
	var current_question = null;
	var disabled = false;
	
	this.onReady = function() {
		// get callout DIV;
		callout_el= $(root_el).find('.calloutContainer div.callout, .calloutContainer div.genericCallout')[0];
		if (!callout_el) return false;
		
		// get default help URL;	
		more_link_el = $(callout_el).find('div.more a')[0];
		if (more_link_el) default_help_url = more_link_el.href;
		
		// bind link events;
		$(root_el).find('a.question').mouseover(function() {showHelpFor(this);}).
			mouseout(function() {setToHide();}).
			focus(function() {showHelpFor(this);}).
			blur(function() {hide();});
			
		$(callout_el).mouseover(function() {persist();}).mouseout(function() {setToHide();}).bgiframe();
		
		$(root_el).find('a.question').click(function() {
			return false;
		});	
	
		/* bind accordion;
		jQuery('#sideBar ul.accordion').accordion({
			active: '.selected', 
			header: 'h3',
			//navigation: true,
			event: 'mouseover',
			animated: 'easeslidefast',
			autoHeight: false
			}).bind("change.ui-accordion", function() {enable();});
		*/
		// to disable while accordioning;
		//$('.inPageHelp ul.accordion h3').mouseover(function() {if (!($(this).hasClass('selected'))) disable();});
	
	};

	var disable = function(){
		hide();
		disabled = true;
	}

	var enable = function() {
		disabled = false;
	};
	var setToHide = function() {
		timeout = setTimeout(function() {hide();}, 200);
	};
	
	var hide = function() {
		$(callout_el).css({display: 'none'});
		if (current_question) $(current_question).removeClass('over');
		current_question = null;
	};
	
	var persist = function() {
		if (timeout) clearTimeout(timeout);
		timeout = null;
	};
	// shows help for question;
	var showHelpFor = function(q_el) {

		if (disabled) return;
		
		//el_index = $('.inPageHelp').index($(q_el).closest('.inPageHelp'));
		//callout_el = callout_el_array[el_index];
		
		persist();
		if (current_question) $(current_question).removeClass('over');
		
		var pos = $(q_el).position();
		callout_el.style.display = 'block';
		var ww = callout_el.offsetWidth;

		$(callout_el).css({display: 'block', top: pos.top-25, left: pos.left-ww - 20}).
		find('div.top div.calloutContent').empty().
							append('<p class="heading">' + q_el.innerHTML + '</p>').
							append($(q_el.parentNode).find('p.answer').clone()).end();

		if (more_link_el) {
			var more_links = $(q_el.parentNode).find('a.more');
			if (more_links.length>0) more_link_el.href = more_links[0].href;
			else more_link_el.href = default_help_url;
			
		}
		
		// set hover state;
		$(q_el).addClass('over');
		current_question = q_el;
	};
};



$(document).ready(function(){
	$('.inPageHelp').each(function() {(new Barclaycard.help(this)).onReady();});
});

// --------------------------------------------------------------------------------------------------------------------------------
// --- end of file --- 
// -------------------------------------------------------------------------------------------------------------------------------

