// add os/browser specific things
$(function(){ // (executes when the dom is ready)
	if ((/Win/.test(navigator.platform)) && (!$.browser.safari) && ($.browser.msie)) {
		$('#navigation-list').addClass("windows");
	};
});

// email page
function email_page() {
	maillink = "mailto:?subject= I saw this on Cameron-Brooks.com...";
	maillink += "&body=I thought you might be interested in this page on Cameron-Brooks.com.  You can view the page here: " + location.href;
	maillink += "%0D%0A%0D%0AFor over 41 years, Cameron-Brooks has been helping Junior Officers make a successful military transition into the business world--and transforming their lives in the process. We specialize in placing future leaders with America's top companies."; 
	location.href = maillink;
}
// print and email functionality, and home page image rotations
$(function(){ // (executes when the dom is ready)
	$(".email-page").click(function(){
		email_page();
	});
	$(".print-page").click(function(){
		window.print();
	});
	rotating_images = $(".rotating-image").length;
	random_image_num = Math.floor(Math.random()*rotating_images);
	$(".rotating-image").hide();
	$(".rotating-image").eq(random_image_num).show();
	$(".rotating-image").eq(random_image_num).find("img").show();
});


// ** Main navigation rollover functions **
// **                                    **
$(function(){ // (executes when the dom is ready)
	// variables for our mouseouts and mouseactions
	var hasOrginalActiveNav = false;
	var originalActiveNav;
	var originalActiveNavLink;
	var cuedOverEffect;
	var cuedOutEffect;
	var cuedRestoreEffect;
	var rolloverDelay = 200;
	
	// search for nav-something or snav-something in the body tag
	// this tells us if a link should be active by default
	// (this is because each of our nav links uses an id, e.g. nav-home, so we define
	//  active links by simply declaring this id in the body tag of each page as a class name)
	$("body[@class*=nav-]").each(function(i) {
		var bodyClasses = $(this).attr("class").split(" ");
		var regex1 = /\bnav-\b/;
		var regex2 = /\bsnav-\b/;
		$.each(bodyClasses, function(i, n) {
			if (regex1.test(n) || regex2.test(n)) {
				// add class "active" to the first link in the li#nav-something or li#snav-something
				$('#'+n+'>a').addClass("active");
				// see if we're dealing with a sub nav, if so it uses class instead of id
				if (regex2.test(n)) {
					$('.'+n+'>a').addClass("active");
				};
			};
		 });
	});
	
	// functions which carryout the over/out effects
	$.navMouseOver = function(hoveredElement) {
		var hoveredElementID;
		clearTimeout(cuedRestoreEffect);
		// grab the id of the elment
		$("#navigation-list>li").each(function(i) {
			if (hoveredElement == this) hoveredElementID = i;
		});
		if ($(hoveredElement).is('.hover')) {
			clearTimeout(cuedOverEffect);
			clearTimeout(cuedOutEffect);
		} else {
			cuedOverEffect = setTimeout('$.navMouseOverEffect("' + hoveredElementID + '")',rolloverDelay);
		};
	};
	$.navMouseOut = function(hoveredElement) {		
		clearTimeout(cuedRestoreEffect);
		clearTimeout(cuedOverEffect);
		clearTimeout(cuedOutEffect);
		// only cue the mouse out effect if this element is not our original nav element
		// (or we'd just turn it off only to turn it on again when we call our restore effect)
		// NOTE: commented out since we don't want any "always on" states in our nav
		/*if (!$(originalActiveNav).is(".hover")) {*/
			cuedOutEffect = setTimeout('$.navMouseOutEffect()',rolloverDelay);
		/*};*/
	};
	$.navMouseOverEffect = function(hoveredElementID) {
		var hoveredElement = $("#navigation-list>li")[hoveredElementID];
		$(hoveredElement).addClass("hover");
		if (hasOrginalActiveNav) {
			if ($("#navigation-list li").index(hoveredElement)!=$("#navigation-list li").index($(originalActiveNav)[0])) {
				$(originalActiveNavLink).removeClass("active");
			};
		};
		$("#navigation-list>li").not(hoveredElement).each(function(i) {
			$(this).removeClass("hover");
			$(this).removeClass("active");
		});
	};
	$.navMouseOutEffect = function() {
		$("#navigation-list>li").removeClass("hover");
		if (hasOrginalActiveNav) cuedRestoreEffect = setTimeout('$.navRestoreOriginalState()',20);
	};
	$.navRestoreOriginalState = function() {
		var restore = true;
		// make sure no other navs are on the "hover" state before we restore
		$("#navigation-list>li.hover").each(function(i) { restore = false; });
		if (restore) {
			$(originalActiveNav).addClass("active");
			$(originalActiveNavLink).addClass("active");
		};		
	};
	
	// if a navigation link has the class of "active" then set it's parent LI to "active" as well
	// we also keep a reference of the link and parent LI so we can turn on/off the "active" state
	// while we mouseover other links in the navigation
	$("#navigation-list>li>a.active").each(function(i) {
		hasOrginalActiveNav = true;
		$(this).parent().addClass("active");
		originalActiveNavLink = $(this);		
		originalActiveNav = $(this).parent();
	});
	
	// finally, add mouseover/out functions to each nav element
	$("#navigation-list>li").each(function(i){
		$(this).mouseover(function() {
			$.navMouseOver(this);
		});
		$(this).mouseout(function() {
			$.navMouseOut(this);
		});
	});
	
});

function songPreview(mp3_file) {
	document.write(' <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="17" height="17" style="position: relative;bottom: -3px;"><param name="movie" value="musicplayer.swf?b_fgcolor=7a110f&song_url='+mp3_file+'.mp3" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="musicplayer.swf?b_fgcolor=7a110f&song_url='+mp3_file+'.mp3" width="17" height="17"><!--<![endif]--><img src="musicplayer.gif" width="17" height="17" alt="" /><!--[if !IE]>--></object><!--<![endif]--></object><!--[if IE]>&nbsp;<![endif]-->');
};

function songPreview2(mp3_file) {
	document.write(' <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="57" height="15" style="position: relative;bottom: -3px;"><param name="movie" value="musicplayer2.swf?b_fgcolor=7a110f&song_url='+mp3_file+'.mp3" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="musicplayer2.swf?b_fgcolor=7a110f&song_url='+mp3_file+'.mp3" width="57" height="15"><!--<![endif]--><img src="musicplayer.gif" width="57" height="15" alt="" /><!--[if !IE]>--></object><!--<![endif]--></object><!--[if IE]>&nbsp;<![endif]-->');
};

function songPreview3(mp3_file,song_title) {
	document.write(' <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="500" height="15" style="position: relative;bottom: -3px;"><param name="movie" value="musicplayer2.swf?b_fgcolor=7a110f&player_title='+song_title+'&song_title='+song_title+'&song_url='+mp3_file+'.mp3" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="musicplayer2.swf?b_fgcolor=7a110f&player_title='+song_title+'&song_title='+song_title+'&song_url='+mp3_file+'.mp3" width="500" height="15"><!--<![endif]--><img src="musicplayer.gif" width="500" height="15" alt="" /><!--[if !IE]>--></object><!--<![endif]--></object><!--[if IE]>&nbsp;<![endif]-->');
};