//Preload images here.
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
	  var cacheImage = document.createElement('img');
	  cacheImage.src = arguments[i];
	  cache.push(cacheImage);
	}
  };
})(jQuery);

$.preLoadImages("/wp-content/themes/custom/gr/nav/divider_on.gif", "/wp-content/themes/custom/gr/nav/divider_on.gif");
$.preLoadImages("/wp-content/themes/custom/gr/tooltip-bubble.png", "/wp-content/themes/custom/gr/tooltip-bubble.png");
$.preLoadImages("/wp-content/themes/custom/gr/nav/reserve-management_on.gif", "/wp-content/themes/custom/gr/nav/reserve-management_on.gif");
$.preLoadImages("/wp-content/themes/custom/gr/nav/our-firm_on.gif", "/wp-content/themes/custom/gr/nav/our-firm_on.gif");
$.preLoadImages("/wp-content/themes/custom/gr/nav/news-blog.gif", "/wp-content/themes/custom/gr/nav/news-blog.gif");
$.preLoadImages("/wp-content/themes/custom/gr/nav/our-people_on.gif", "/wp-content/themes/custom/gr/nav/our-people_on.gif");
$.preLoadImages("/wp-content/themes/custom/gr/nav/wealth-management_on.gif", "/wp-content/themes/custom/gr/nav/wealth-management_on.gif");
$.preLoadImages("/wp-content/themes/custom/gr/nav/why-campion_on.gif", "/wp-content/themes/custom/gr/nav/why-campion_on.gif");
$.preLoadImages("/wp-content/themes/custom/gr/homepage-feature/overlay-fade.png", "/wp-content/themes/custom/gr/homepage-feature/overlay-fade.png");

function highlightNavItem(){
	
	if (typeof(arguments[0]) != 'number'){
		//first, get id number of this nav link
		var className = $(this).attr('class');
		var navIdNum = className.substring(className.length - 1) * 1;
		var navIdName = $(this).attr('id');
		
		var previousIdNum = navIdNum - 1;
		var nextIdNum = navIdNum + 1;
		navIdName = navIdName.substring(4, navIdName.length);
	}
	//If the arguement is a number, run the querys with that number as the identifier.
	else{
		var navIdNum = arguments[0];
		var navIdName = arguments[1];
		
		var previousIdNum = navIdNum - 1;
		var nextIdNum = navIdNum + 1;
	}
	
	//Hack for IE. Hide all menues before showing the new one.
	//$('ul.drop-down-menu').css('display', 'hidden');
	
	//change image of selected nav link
	$('img.nav-item-' + navIdNum).attr('src', '/wp-content/themes/custom/gr/nav/' + navIdName + '_on.gif');
	$('img.nav-item-' + navIdNum).siblings('.nav-divider').attr('src', '/wp-content/themes/custom/gr/nav/divider.gif');
}


function unhighlightNavItem(){
	
	//If the argument passed is not a number then we can use jquery's THIS.
	if (typeof(arguments[0]) != 'number'){
		//first, get id number of this nav link
		var className = $(this).attr('class');
		var navIdNum = className.substring(className.length - 1) * 1;
		var navIdName = $(this).attr('id');
		
				
		var previousIdNum = navIdNum - 1;
		var nextIdNum = navIdNum + 1;
		navIdName = navIdName.substring(4, navIdName.length);
	}
	//If the arguement is a number, run the querys with that number as the identifier.
	else{
		var navIdNum = arguments[0];
		var navIdName = arguments[1];
		
		var previousIdNum = navIdNum - 1;
		var nextIdNum = navIdNum + 1;
	}

	//change image of selected nav link
	$('img.nav-item-' + navIdNum).attr('src', '/wp-content/themes/custom/gr/nav/' + navIdName + '.gif');


	//Change divider line images 

	//change image of divider with the same id num
	$('img.nav-divider').attr('src', '/wp-content/themes/custom/gr/nav/divider.gif');

	$('ul#primary-nav img.permaHighlightDivider').attr('src', '/wp-content/themes/custom/gr/nav/divider_on.gif');
	$('ul#primary-nav li.permaHighlight').prev().children().attr('src', '/wp-content/themes/custom/gr/nav/divider_on.gif');
}
function highlightParentNav(){
	
	//	alert($(this).parent().attr('class'));
	var parentClass = $(this).parent().attr('class');
	parentClass = parentClass.replace( "menu-", "" );
	parentClass = parentClass.replace( "-container", "" );

	var listId = $('li.' + parentClass).attr('id');

	navIdNum = listId.substring(listId.length - 1) * 1;

	highlightNavItem(navIdNum, parentClass);
}
function unhighlightParentNav(){
	
	//	alert($(this).parent().attr('class'));
	var parentClass = $(this).parent().attr('class');
	parentClass = parentClass.replace( "menu-", "" );
	parentClass = parentClass.replace( "-container", "" );

	var listId = $('li.' + parentClass).attr('id');

	navIdNum = listId.substring(listId.length - 1) * 1;

	unhighlightNavItem(navIdNum, parentClass);
}

function clearDefaultFieldValues(){
	
	var defaultValue = $(this).val();
	
	if (defaultValue == 'Name' ||
		defaultValue == 'City' ||
		defaultValue == 'Email Address' ||
		defaultValue == 'Your comment or question' ||
		defaultValue == 'Search'){
			$(this).val('');
		}
}

function highlightFeatureItem(keyNum){

	//change background color of this div, this will create the extra bar of color over the text
	$('div#link-text-' + keyNum).css('background-image', 'url(/wp-content/themes/custom/gr/homepage-feature/highlight-box-main.png)');
	$('div#link-text-' + keyNum).css('background-repeat', 'repeat-x');
	$('div#link-text-' + keyNum + ' a').css('color', '#fff');
	
	//add left diagonal corner to this list item
	$('div#left-corner-' + keyNum).css('background-image', 'url(/wp-content/themes/custom/gr/homepage-feature/highlight-box-left.png)');
	$('div#left-corner-' + keyNum).css('background-position', 'top right');
	
	//add right diagonal corner to this list item
	$('div#right-corner-' + keyNum).css('background-image', 'url(/wp-content/themes/custom/gr/homepage-feature/highlight-box-right.png)');
	$('div#right-corner-' + keyNum).css('background-position', 'top right');
}

function unhighlightFeatureItem(keyNum){

	//change background color of this div, this will create the extra bar of color over the text
	$('div#link-text-' + keyNum).css('background-image', 'none');
	$('div#link-text-' + keyNum + ' a').css('color', '#58595b');
	
	//add left diagonal corner to this list item
	$('div#left-corner-' + keyNum).css('background-image', 'none');
	
	//add right diagonal corner to this list item
	$('div#right-corner-' + keyNum).css('background-image', 'none');
}

function manualTransitionSlide(){
	
	//remove the mouseover event from this all tabs until transition is done
	$('.highlight-link-text').unbind();
	
	//the user has engaged so kill the automatic cycling
	if (timeoutSlideshow) clearInterval(timeoutSlideshow);
			
	//get key for selected slide
	var thisNav = $(this).attr('id');
	var keyNum = thisNav.substring(thisNav.length - 1);
	
	unhighlightFeatureItem(currentSlide);
	highlightFeatureItem(keyNum);
	
	//alert("currentSlide is: " + currentSlide + " // next slide is: " + keyNum);
	
	//Fade out the current slide and fade in the next one
	$('div#highlight-image-' + currentSlide).fadeOut(100, function(){
		$('div#highlight-image-' + currentSlide).hide();
		$('div#highlight-image-' + keyNum).fadeIn(175, function(){
	
			//restore all mouse events
			//$('.highlight-link-text').mouseover(manualTransitionSlide);
	
			//unbind the event from the newly displayed slide
			//$('#link-text-' + keyNum).unbind('mouseover', manualTransitionSlide);
			
			$('.highlight-link-text').not('#link-text-' + keyNum).mouseover(manualTransitionSlide);
		});
	});
	
	//the chosen slide has now been displayed. Update currentSlide
	currentSlide = keyNum;
}


function autoTransitionSlide(slide){
	
	highlightFeatureItem(slide);
	unhighlightFeatureItem(slide-1);
	
	//Fade out the current slide and fade in the next one
	$('div#highlight-image-' + (slide - 1)).fadeOut(100, function(){
		$('div#highlight-image-' + (slide - 1)).hide();
		$('div#highlight-image-' + slide).fadeIn(175);
	});
	
	//bind the mouseover event back to the previous item.
	$('#link-text-' + (slide - 1)).bind('mouseover', manualTransitionSlide);
	var totalSlides = $('.highlight-image').length;
	if (slide == totalSlides) clearInterval(timeoutSlideshow);
}

function assignPermanentHighlight($dir){
	
	var nav;
	var image;
	
	if ($dir == 'our-firm'){ nav = 'our-firm'; image = nav; }
	else if ($dir == 'our-people' || $dir == 'bio'){ nav = 'our-people'; image = 'our-people'; }
	else if ($dir == 'wealth-management'){ nav = 'wealth-management'; image = nav; }
	else if ($dir == 'reserve-management'){ nav = 'reserve-management'; image = nav; }
	else if ($dir == 'why-campion'){ nav = 'why-campion'; image = nav; }
	else if ($dir == 'news-events'){ nav = 'news-events'; image = 'blog'; }
	else if ($dir == 'why-campion'){ nav = 'why-campion'; image = nav; }
	else if ($dir == 'blog'){ nav = 'blog'; image = 'blog'; }
	
	$('ul#primary-nav li.' + nav).addClass('permaHighlight');
		
	//change image of selected nav link
	$('ul#primary-nav li.permaHighlight img.nav-link').attr('src', '/wp-content/themes/custom/gr/nav/' + image + '_on.gif');
	
	//change image of divider with the same id num
	$('ul#primary-nav li.permaHighlight img.nav-divider').attr('src', '/wp-content/themes/custom/gr/nav/divider_on.gif');
	$('ul#primary-nav li.permaHighlight img.nav-divider').addClass('permaHighlightDivider');
	
	//change image of previous divider (previousid = currrentid-1)
	$('ul#primary-nav li.permaHighlight').prev().children().attr('src', '/wp-content/themes/custom/gr/nav/divider_on.gif');
	$('ul#primary-nav li.permaHighlight img.nav-divider').addClass('permaHighlightDivider');
		
	//change color of decorative top portion
	$('ul#primary-nav li.permaHighlight').css('background-color', '#667f68');
		
	//add diagonal corner to previous nav element
	$('ul#primary-nav li.permaHighlight').prev().css('background-image', 'url(/wp-content/themes/custom/gr/nav/corner-previous-item.gif)');
	$('ul#primary-nav li.permaHighlight').prev().css('background-position', 'top right');
		
	//add diagonal corner to next nav element
	$('ul#primary-nav li.permaHighlight').next().css('background-image', 'url(/wp-content/themes/custom/gr/nav/corner-next-item.gif)');
	$('ul#primary-nav li.permaHighlight').next().css('background-position', 'top left');
}


/* currentSlide is to begin keeping track of which slide is displaying in the 
	slide show.  Declare this  variable here so it will be global.  It will
	need to be accessed by multiple functions.
*/
var currentSlide = 1;

function createThickboxPDFLink( idNum ){
	
	//Add thickbox class to PDF links
	jQuery('#custom-widget-' + idNum + ' a').addClass('thickbox');
	var downloadLink = jQuery('#custom-widget-' + idNum + ' .update-text').html();
	downloadLink = encodeURIComponent(downloadLink);
	var downloadURL = jQuery('#custom-widget-' + idNum + ' a').attr('href');
	jQuery('#custom-widget-' + idNum + ' a').attr('href', '/vb_ajax.php?&download=' + downloadLink + '&KeepThis=true&TB_iframe=true&height=400&width=600&target=new');
	//Intercept download of PDF forms
	jQuery('#custom-widget-' + idNum + ' a').click( function(e){
		
		e.preventDefault();
	});	
}

$(document).ready(function(){

	//Highlight the main nav tab that cooresponds to this page
	assignPermanentHighlight(thisDir);
	
	$('img.nav-link').mouseover(highlightNavItem);
	$('ul.drop-down-menu').mouseover(highlightParentNav);
	
	$('img.nav-link').mouseout(unhighlightNavItem);
	$('ul.drop-down-menu').mouseout(unhighlightParentNav);
	
	//Remove events from the current page's nav tab
	//highlightNavItem(thisDir);
	$('li.permaHighlight img.nav-link').unbind('mouseover', highlightNavItem);
	$('li.permaHighlight img.nav-link').unbind('mouseout', unhighlightNavItem);
	$('li.permaHighlight ul.drop-down-menu').unbind('mouseout', unhighlightParentNav);

	//Clear all form fields when they are clicked on
	$('input[type=text]').focus(clearDefaultFieldValues);
	$('textarea').focus(clearDefaultFieldValues);
	
	/*Get coordinates and display the green tool tip bubble */
	/*
	$('div.highlight img.cfa-icon').mouseover(function(e){
		
		var xCoord = e.pageX;
			
		var tooltip = $('div#tooltip-bubble');
		tooltip.css('left', e.pageX - (tooltip.width()+25) );
		tooltip.css('top', e.pageY - (tooltip.height()+20) );
		
		$('div#tooltip-bubble').css('display', 'block');
	});
	*/
	/*Hide the green tool tip bubble */
	/*
	$('div.highlight img.cfa-icon').mouseout(function(){
		
		$('div#tooltip-bubble').css('display', 'none');
	});
	*/
	
	/* The following initialzes and starts the slideshow */
	//this variable keeps track of which slide is being shown.
	var nextSlide = currentSlide + 1;
	
	var callTransition = function(){ 

		nextSlide = ++currentSlide;
		//alert("timeout: " + timeoutSlideshow);
		autoTransitionSlide(nextSlide);
	};

	//call interval to begin slide show	
	timeoutSlideshow = setInterval(callTransition, 5500);

	highlightFeatureItem(currentSlide);

	//this control is for when the user selects a nav button
	$('.highlight-link-text').mouseover(manualTransitionSlide);
	$('#link-text-1').unbind('mouseover', manualTransitionSlide);
	$('#link-text-1').mouseover(function(){
		clearInterval(timeoutSlideshow);
	});
	//Stop the slideshow if the user rolls over the main image
	$('div.highlight-image').mouseover(function(){
	
		clearInterval(timeoutSlideshow);
	});
	
	createThickboxPDFLink( 1027 );
	createThickboxPDFLink( 1019 );



	$('#custom-widget-920,#custom-widget-917,#custom-widget-914').addClass('accordion');
	$('#custom-widget-920 h3:first-child,#custom-widget-917 h3:first-child,#custom-widget-914 h3:first-child').addClass('head');
	$('.accordion .head').click(function() {
	$(this).next().toggle('fast');
	$(this).parent().find('.plus_acordian').toggle();
	$(this).parent().find('.minus_acordian').toggle();
	return false;
	}).next().hide();
	$('.accordion').append("<span class='plus_acordian'>+</span>");
	$('.accordion').append("<span class='minus_acordian'>&#8211;</span>");
	$('.minus_acordian').hide();
		/*$(function() {
			$( "#tabs" ).tabs({
				event: "mouseover"
			});
		}); */
});



function highlightServiceTab(thisSubDir){

	 $('div#main-content.subpage ul#tabs_nav li a').click(function() {
	  $(this).parent().parent().children('.current').removeClass('current');
	  $(this).parent().addClass('current');
	 });
		
	if (thisDir == "reserve_management"){ 
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(3)').css('max-width', '160px');
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(2)').css('max-width', '110px');
		}
	else if (thisDir == "wealth_management"){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(1)').css('max-width', '94px');
		$('div#main-content.subpage ul#tabs_nav li:eq(5)').css('max-width', '80px');
		$('div#main-content.subpage ul#tabs_nav li:eq(4)').css('max-width', '80px');
		$('div#main-content.subpage ul#tabs_nav li:eq(3)').css('max-width', '105px');
		$('div#main-content.subpage ul#tabs_nav li:eq(2)').css('max-width', '75px');
		}

	if (thisSubDir == 'expert-advisors-trusted-fiduciaries'){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(0)').addClass('current');
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(0)').addClass('current');
		}
	else if (thisSubDir == 'investment-management'){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(1)').addClass('current');
		}	
	else if (thisSubDir == 'investment-policy'){ 
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(1)').addClass('current');
		}	
	else if (thisSubDir == 'financial-planning'){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(2)').addClass('current');
		}
	
	else if (thisSubDir == 'performance-measurement'){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(3)').addClass('current');
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(2)').addClass('current');
		}
	else if (thisSubDir == 'investment-committee-education'){ 
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(3)').addClass('current');
		}
	
	else if (thisSubDir == 'investor-education'){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(4)').addClass('current');
		}
	
	else if (thisSubDir == 'custodial-services'){ 
		$('div#main-content.subpage ul#tabs_nav li:eq(5)').addClass('current');
		$('div#main-content.subpage ul#reserve_tabs_nav li:eq(4)').addClass('current');
		}
}


var tld_ = new Array()
tld_[0] = "com";
tld_[1] = "org";
tld_[2] = "net";
tld_[3] = "ws";
tld_[4] = "info";
tld_[10] = "co.uk";
tld_[11] = "org.uk";
tld_[12] = "gov.uk";
tld_[13] = "ac.uk";
var topDom_ = 13;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";

function addmail(name, dom, tl, params)
{
	var s = e(name,dom,tl);
	document.write('<a href="'+m_+s+params+'">'+s+'</a>');
}
function addmail2(name, dom, tl, params, display)
{	
	if(name=="tb"){
		name="terence.burns";
		}
	else if(name=="as"){
		name="aaron.shackelford";
		}
	else if(name=="ks"){
		name="kevin.schweiker";
		}
	else if(name=="an"){
		name="alexandra.nasif";
		}
	document.write('<a href="'+m_+e(name,dom,tl)+params+'">');
}
function e(name, dom, tl)
{
	var s = name+a_;
	if (tl!=-2)
	{
		s+= dom;
		if (tl>=0)
			s+= d_+tld_[tl];
	}
	else
		s+= swapper(dom);
	return s;
}
function swapper(d)
{
	var s = "";
	for (var i=0; i<d.length; i+=2)
		if (i+1==d.length)
			s+= d.charAt(i)
		else
			s+= d.charAt(i+1)+d.charAt(i);
	return s.replace(/\?/g,'.');
}


function silentErrorHandler() {return true;}
window.onerror=silentErrorHandler;

