// the tab element
var qTab = null;

$(document).ready(
	function() {
		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;
			}
		);

        $('.colorbox').each(function(){
            var jThis = $(this),
                sWidth = jThis.attr('video-width'),
                sHeight = jThis.attr('video-height');

            if( !sWidth ) sWidth = '50%';
            if( !sHeight ) sHeight = '50%';

            jThis.colorbox({iframe: true, width: sWidth, height: sHeight});
        });


		initPrimaryNav();
		initSearchInput();
		initBigTarget();
		
		$('#pro-bono-video').dialog({ autoOpen: false, draggable: false, resizable: false, modal: true, width: 587, show: 'blind' })
		//$('#senior-video-1-video').dialog({ autoOpen: false, draggable: false, resizable: false, modal: true, width: 587, show: 'blind' })
		//$('#senior-video-2-video').dialog({ autoOpen: false, draggable: false, resizable: false, modal: true, width: 587, show: 'blind' })
		
		$('.tab-highlight').click(function() {
				 $('#tabs').addClass('highlights');
				 $('#description-content').hide();
				 $('#highlight-content').show();
				 return false;
			}
		);
		
		$('.tab-desc').click(function() {
				 $('#tabs').removeClass('highlights');
				 $('#highlight-content').hide();
				 $('#description-content').show();
				 return false;
			}
		);

        $('.the-boxes div').each(
            function(){
                var jThis = $(this),
                    jA = jThis.find('h3 a');
                jThis.click(
                    function(){
                        window.location.href = jA.attr('href');
                    }
                ).mouseover(
                    function(){
                        jThis.addClass('over')
                    }
                ).mouseout(
                    function(){
                        jThis.removeClass('over')
                    }
                );
            }
        );

		$('#splash-nav div.large-section').mouseover(function() {
				 $(this).addClass('over');
			}
		);
		
		$('#splash-nav div.large-section').mouseout(function() {
				 $(this).removeClass('over');
			}
		);
		
		$('#splash-nav div.large-section').click(function() {
				 window.location = $(this).attr('href');
				 return false;
			}
		);

		$('.large-section').click(function() {
				 window.location = $(this).attr('href');
				 return false;
			}
		);

		$('#pro-bono').click(function() {
				 $('#pro-bono-video').dialog('open')
			}
		);
		
		$('#domestic').click(function() {
				 $('#domestic-video').dialog('open')
			}
		);
		
//		$('#seniorone').click(function() {
//				 $('#senior-video-1-video').dialog('open')
//			}
//		);
		
//		$('#seniortwo').click(function() {
//				 $('#senior-video-2-video').dialog('open')
//			}
//		);
	}
);

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);
	
	// this is a bit fragile, so changing to concrete numbers.
	
	//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 == 559 ) {
	//	nLeft = 549;
	//}
	
	//if ( qItem.attr('id') == 't-supplemental') {
		//sWidth = '100px';
	//	nLeft -= 9;
	//}
	
	nLeft = qItem.attr('pos') * 110;    // 110 = width of a tab	
	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 };
}
