// Global JavaScripts

/* Function to add routines to page load event */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* Just add class="rollover" to the img tag and make sure your images are named whatever.gif and whatever-over.gif */
/* You can use any kind of images and name them what you like.  The "-over" is the part that matters */
function initrollovers() {
	if (!document.getElementById) return;
	var aPreLoad = new Array();
	var sTempsrc;
	var aImages = $("img,input");
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'rollover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '-over'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				sTempsrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			aImages[i].onmouseout = function() {
				if (!sTempsrc) sTempsrc = this.getAttribute('src').replace('-over'+ftype, ftype);
				this.setAttribute('src', sTempsrc);
			}
		}
	}
} 
addLoadEvent(initrollovers);


//Home Page Feature & Promotions
/* Remove BC's automated image links */

function FeatureRemoveLinks() {
	$('#promoRotator div .promoImage a').each(function() {
		$(this).removeAttr('href');
	});
	
	$('#promos-container div #promoLink').each(function() {
		var writePath = $(this).attr('href');		
		$(this).next().attr('href',writePath);
	});	
}

		







// Back to List
/*Present the Back to List on detail pages when called and hiden by default to conserve templates*/
// Only on client list pages, located within page for bio template (leadership)

function RemoveBackToList() {
	//if it is there
	if ($('#clientList-nav').length > 0) {
		var file = $.url.attr('file');
		var section = $.url.segment(1);	
		if (file != null) {
			if (file != "default.htm") {
				$('#clientList-nav').css('display','inline');
			}
		}
	}
}



// Active state on secondaryNav
/*determine the section then style the appropriate nav to conserve templates*/

function SecondaryNavActive() {
	//if it is there
	if ($('#secondaryNav').length > 0) {
		var section = $.url.segment(1);
		if (section != null) {
			var currentNav = $("#secondaryNav a[href*=" + section + "]");
			$(currentNav).addClass("active");
		}
		
	}
}


// Active state on secondaryNav Leadership
/*determine the section then style the appropriate nav to conserve templates*/

function SecondaryNavActiveLeadership() {
	//if it is there
	if ($('#secondaryNav-people').length > 0) {
		var section = $.url.segment(1);
		if (section != null) {
			var currentNav = $("#secondaryNav-people a[href*=" + section + "]");
			$(currentNav).addClass("active");
		}
		
	}
}

// Active state on leadership jQuery

function jQueryBios() {
	if ($('#mycarousel').length > 0) {
		var file = $.url.attr('file');	
		var section = $.url.segment(2);
		
		if (section != null) {
			var currentBio = $("#mycarousel li a[href*=" + section + "]");
			$(currentBio).parent().addClass("selected");

		}

	}
	
	var container = $('#mycarousel');
	var startPlace = $('li',container).index($('.selected',container));
	startPlace = startPlace + 1;
    $('#mycarousel').jcarousel({
		start: startPlace
	});

}



// contact inquiries - show and hide divs based on form select

function inquiriesForms() {

		$('#careers').hide();
		$('#pressMedia').hide();
		$("#formSelectGeneralInquires").change(function(){
			if(this.value == 'nothing'){
				$("#inquiry").show(); 
				$("#careers").hide(); 
				$("#pressMedia").hide();
				$("#formSelectGeneralInquires").val("GeneralInquires").attr("selected", "selected");
				}
			else if(this.value == 'GeneralInquires'){
				$("#inquiry").show(); 
				$("#careers").hide(); 
				$("#pressMedia").hide();
				$("#formSelectGeneralInquires").val("GeneralInquires").attr("selected", "selected");
				}
			else if(this.value == 'Careers'){
				$("#inquiry").hide(); 
				$("#careers").show(); 
				$("#pressMedia").hide();
				$("#formSelectCareers").val("Careers").attr("selected", "selected");
				}
			else if(this.value == 'PressMedia'){
				$("#inquiry").hide(); 
				$("#careers").hide(); 
				$("#pressMedia").show();
				$("#formSelectPressMedia").val("PressMedia").attr("selected", "selected");
				}
	});
		$("#formSelectCareers").change(function(){
			if(this.value == 'nothing'){
				$("#inquiry").show(); 
				$("#careers").hide(); 
				$("#pressMedia").hide();
				$("#formSelectGeneralInquires").val("GeneralInquires").attr("selected", "selected");
				}
			else if(this.value == 'GeneralInquires'){
				$("#inquiry").show(); 
				$("#careers").hide(); 
				$("#pressMedia").hide();
				$("#formSelectGeneralInquires").val("GeneralInquires").attr("selected", "selected");
				}
			else if(this.value == 'Careers'){
				$("#inquiry").hide(); 
				$("#careers").show(); 
				$("#pressMedia").hide();
				$("#formSelectCareers").val("Careers").attr("selected", "selected");
				}
			else if(this.value == 'PressMedia'){
				$("#inquiry").hide(); 
				$("#careers").hide(); 
				$("#pressMedia").show();
				$("#formSelectPressMedia").val("PressMedia").attr("selected", "selected");
				}			
	});
		$("#formSelectPressMedia").change(function(){
			if(this.value == 'nothing'){
				$("#inquiry").show(); 
				$("#careers").hide(); 
				$("#pressMedia").hide();
				$("#formSelectGeneralInquires").val("GeneralInquires").attr("selected", "selected");
				}
			else if(this.value == 'GeneralInquires'){
				$("#inquiry").show(); 
				$("#careers").hide(); 
				$("#pressMedia").hide();
				$("#formSelectGeneralInquires").val("GeneralInquires").attr("selected", "selected");
				}
			else if(this.value == 'Careers'){
				$("#inquiry").hide(); 
				$("#careers").show(); 
				$("#pressMedia").hide();
				$("#formSelectCareers").val("Careers").attr("selected", "selected");
				}
			else if(this.value == 'PressMedia'){
				$("#inquiry").hide(); 
				$("#careers").hide(); 
				$("#pressMedia").show();
				$("#formSelectPressMedia").val("PressMedia").attr("selected", "selected");
				}		
	});
}




// Browser Detection
function BrowserCheck() {
	var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    if($.browser.msie){
        $('body').addClass('msie');
        $('body').addClass('msie' + $.browser.version.substring(0,1));
    }
  
    if($.browser.chrome){
        $('body').addClass('chrome');
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('chrome' + userAgent);
        $.browser.safari = false;
    }
    
    if($.browser.safari){
        $('body').addClass('safari');
        userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('safari' + userAgent);
    }
    
    if($.browser.mozilla){
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
            $('body').addClass('firefox');
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,1);
            $('body').addClass('firefox' + userAgent);
        }
        else{
            $('body').addClass('mozilla');
        }
    }

    if($.browser.opera){
        $('body').addClass('opera');
    }
}



function PhotoGallery(){
	if ($("#PhotoGalleryDisplay").length > 0) {

		// Remove elements with no image
		$('#PhotoGalleryDisplay li div:empty').each(function() {
			$(this).parent().remove();
		});

		// Remove unnecessary BC links
		$('#PhotoGalleryDisplay li div a').each(function() {
			$(this).removeAttr('href');
		});

		// Variable for gallery		
		var gallery = $("#PhotoGalleryDisplay");
		// Find number of images in gallery
		var numImages = gallery.find("li").length;
		
		gallery.find("li").each(function() {
			// Add Captions from alt tags
			var image = $(this).find("img");
			var caption = image.attr("alt");
			if (caption != "") {
				$(this).find("div").after("<div class=\"caption\">" + caption + "</div>");
			}
		});


		// Build gallery navigation
		var galleryNav = '<div id="GalleryNav">';
		for (i=1; i<=numImages; i++) {
			galleryNav += '<a id="GalleryNav' + i + '" href="#">' + i + '</a>';
		}
        galleryNav += '</div>';
        
		// Add the gallery navigation in after the gallery itself
		gallery.append(galleryNav);
		
		$('#PhotoGalleryDisplay').css({'display' : 'block'});
		$("#PhotoGalleryDisplay").jcarousel({
	        scroll: 1,
			animation: 600,
			auto: 5,
			wrap: 'last',
			initCallback: carousel_initCallback,
			itemVisibleInCallback: carousel_itemVisibleInCallback,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });
	}
}

// Photo Gallery Support Functions
function carousel_itemVisibleInCallback(carousel, item, idx, state) {
	$('#GalleryNav a').removeClass("current");
	$('#GalleryNav'+idx).addClass("current");
}
function carousel_initCallback(carousel) {
	$('#GalleryNav a').bind('click', function() {
		carousel.scroll($.jcarousel.intval($(this).text()));
		carousel.stopAuto();
		carousel.startAuto();
		$('#GalleryNav a').removeClass("current");
		$(this).addClass("current");
		return false;
	});
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

