// the tab element
var qTab = null;

$(document).ready(
	function() {
		initPrimaryNav();
		qTab = $('#active-tab');

		// locate the default tab
		defaultTab = $('#primary-nav a.active');

		// if a tab is defaulted, show it
		if( defaultTab.size() > 0 ) {
			showTab(defaultTab);
		// otherwise, set defaultTab to null
		} else {
			defaultTab = null;
		}

		$('a.ifbyphone').click(
			function() {
				// 400 x 430
				 window.open( this.href, 'Clickto', 'width=330,height=310,toolbar=no,location=no, menubar=no, scrollbars=no, copyhistory=no,resizable=no');
				 return false;
			}
		);

		initSearchInput();
		initBigTarget();
	}
);

function initBigTarget() {
	$('.big-target').each(
		function() {
			var target = $(this);
			var url = $(target.find('a').get(0)).attr('href');
			target.click(
				function() {
					window.location.href = url;
				}
			)
		}
	);
}

function initSearchInput() {
	$('#query').focus(
		function() { 
			if( this.value == this.defaultValue ) {
				this.value = '';
			}
		}
	).blur(
		function() {
			if( this.value == '' ) { 
				this.value = this.defaultValue;
			}
		}
	);
}

function initPrimaryNav() {
	$('#primary-nav a').not('.active').mouseover(
		function() { showTab(this) }
	).mouseout(
		function() { hideTab(this) }
	);
}

function showTab(item) {
	var qItem = $(item);
	
	var nLeft = qItem.parent().get(0).offsetLeft;

	// i dont get it.  firefox will occassionally report nLeft as 48px.  it's only on insurance.  theres offset i can't account for
	if( nLeft == 48 ) {
		nLeft = 0;
	}
	
	if ( qItem.attr('id') == 't-supplemental') {
		//sWidth = '100px';
		nLeft -= 9;
	}
	
	qTab.css( {display: 'block', left: nLeft + 'px'} );

}

function hideTab() {
	// either restore the default tab state
	if( defaultTab != null ) {
		showTab(defaultTab);
	// or hide it all together
	} else {
		qTab.css( {left: '-1000px'} );
	}
}


/**
 *  @author:    Danny Ng (http://www.dannytalk.com)
 *  @date:      21/09/08
 *  @notes:     Free to use and distribute for non-commercial use without altering this notice. Would appreciate a link back.
 */
function getCumulativeOffset(el) {
	var x = 0;
	var y = 0;
	var cur = (el) ? el : this;
	do {
		if (cur.nodeName.toLowerCase != 'td') {
			x += cur.offsetLeft;
			y += cur.offsetTop;
		}
	}
	while ((cur = cur.offsetParent) && cur.nodeName.toLowerCase() != 'body');	

	return { x: x, y: y };
}