$(document).ready(function() {
	if ($('table.search').length != 0 ) {
		var foo = new tableObj();     
	};   
		
	if ($('.toolTip').length != 0) {
		htip();	
	}		 
	
	linkHandler();
	//googleLink();
			     	
});

var tableObj = function(){
	//create object rowLighter method
	this.rowLighter = function  () {
		$('tr.contentOverview').hover( 		
			function () {    		   
				$(this).addClass('rowSelected');
			} ,
			function () {
				$(this).removeClass('rowSelected');
			}
		);   
	}, //<--separate with comma   
	
	//create object celHover method
	this.celHover = function () {
		$('table tr.contentOverview td.first').hover(
			function  () {
				$(this).toggleClass('hover');
			},
			function  () {
				$(this).toggleClass('hover');
			}
		);		
	},

	//create object tableExpand method
	this.tableExpand = function () {  
		//switch indicator on td.first to open
		$('tr.contentOverview td.first').hover(  
			function () {    			
				$(this).children('a').addClass('selected');
			} ,
			function () {
				$(this).children('a').removeClass('selected');
			}    	
		);            
			
		//toggle class on click toggle open/close on next tr.detail    
		$('tr.contentOverview td.first').click(
			function  () {
				$(this).parent('tr.contentOverview').toggleClass('open').next('tr.contentDetail').toggleClass('hide');			
				return false;
			});    
		
		//enable closeAll link	
		$('a.closeAll').click(
			function  () {
				$('tr.contentOverview').each(function () {
				   $(this).removeClass('open').next('tr.contentDetail').addClass('hide'); 
				})    
			return false;
			});
		
	}
		//init : function(){
		this.rowLighter();  
		this.celHover();
		this.tableExpand();
	//},
}

/* htip tooltip function */
function htip() {
	$('.toolTip, .htip').bind('mouseover',function(){
		//vars gather data for tooltip, and contruct markup
		var targ = $(this).attr('href') + "Content";
		var tipContent = $(targ).html();
		
		//calculate position and offset of htip
		var offsetLeft = $(this).width()/1.7;
		var offsetTop = $(this).height()/1.2;
		var position = $(this).offset();
		var tipLeft = position.left + offsetLeft;
		var tipTop = position.top + offsetTop;
		
	    
		//assemble tooltip and its position
		var htip = "<div class=\"htip\" style=\"position: absolute; z-index:999; left:" + tipLeft + "px; top:" + tipTop + "px;\">" + tipContent + "</div>";			
		
		//remove all open tooltips first
		if ($('.htip')){
			//$('div.htip').addClass('removeMe');
			//$('div.htip.removeMe').remove();
		};
		
		//render tooltip to page
		$('body').append(htip);
		$('.htip').fadeIn(200);
				
		//set timeout delay for mouseout
		var delay = 300;	
		//declare local var timeout. if this not declared, clearTimeout will not work
		var timeout;	
		
		$('.toolTip, .htip').mouseout(function(){
			//add class to identify which tooltip to remove
			$('div.htip').addClass('removeMe');
			//declare vars						
			var tooltip;
			var htip;
			
			htip = $('.htip');
					
			timeout = setTimeout(function (){								
				$('.htip.removeMe').fadeOut(100, function(){															
					$(this).remove();						
				});								
			}, delay);		
			
			$(this).mouseover(function(){
				clearTimeout(timeout);
			});
			htip.mouseover(function(){		
				clearTimeout(timeout);
			});
					
		});//end mousout function	
			
		//kill the click on 'a'
		$(this).click(function(){
			return false;
		})
		
	});			
} 
      
//this function opens external links in another window/tab and adds google tracking code for external links.
function linkHandler() {
	$('a').click(function(){
	
		if(typeof($(this).attr('href'))!='undefined'){
			var href = $(this).attr('href');
			if(href.match(/^http/)){						
				try {
					pageTracker._trackPageview('/outgoing/' + href);			
					window.open(href); 
					return false;
				}
				catch(err) {
					return false;
				}
			}
			if(href.match(/.pdf$/)){						
				try {				   		
					window.open(href); 
					return false;
				}
				catch(err) {
					return false;
				}
			}
		}
	});
	
}
