 
// Rotate the home page case study thumbnail out this often
var daysBetweenNewCaseStudyThumbnails = 1000;

// If we want to force a certain case study to show up, we need to add an offset of days to the current day
var daysOffset = 2259;

var section_num = -1;
var sub_section_num = -1;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
	
// A list of all bio thumbnails
var bioThumbList = new Array("Amy_on.jpg", "Brian_on.jpg", "Bruce_on.jpg", "Chris_on.jpg", "David_C_on.jpg", "David_H_on.jpg", "Freda_on.jpg", "Jason_on.jpg", "Jeff_on.jpg", "Jenny_on.jpg", "Joel_on.jpg", "Kelly_on.jpg", "Kristin_on.jpg", "Leslie_on.jpg", "Melanie_on.jpg", "Mike_on.jpg", "Rainey_on.jpg", "Rick_on.jpg", "Sean_on.jpg", "Tito_on.jpg", "Todd_on.jpg", "Tyler_on.jpg");

// A list of all case study thumbnails
var caseStudies = new Array("reserve", "gtg", "infomotor", "drinkwell", "nvv", "kettle", "ready", "cisco", "serena", "madden", "sun", "beacon", "pci", "mountain", "digitec", "clinique", "citrix");

var caseStudyThumbList = new Array("CS_reserve_on.jpg", "CS_gtg_on.jpg", "CS_infomotor_on.jpg", "CS_Drinkwell_on.jpg", "CS_NVV_on.jpg", "CS_kettle_on.jpg", "CS_Ready_on.jpg", "CS_cisco_on.jpg", "CS_Serena_on.jpg", "CS_madden_on.jpg", "CS_Sun_on.jpg", "CS_beacon_on.jpg", "CS_pci_on.jpg", "CS_Mountain_on.jpg", "CS_Digitec_on.jpg", "CS_clinique_on.jpg", "CS_citrix_on.jpg");

var caseStudyInfo = new Array();
for(client in caseStudies)
{
	caseStudyInfo[caseStudies[client]] = new Array();
	caseStudyInfo[caseStudies[client]]["thumb"] = "CS_" + caseStudies[client] + "_on.jpg"
}

caseStudyInfo["reserve"]["description"] = "Groove 11 distilled the essence of the <a href='reserve.html'>Reserve Brands</a> into narratives designed to drive quality and discerning taste into the market, and in turn to activate distributors, trade partners, and consumers.";



function header_init()
{			 
	var sub0 = new Fx.Slide('sub_menu_0');	
	var sub1 = new Fx.Slide('sub_menu_1');	
	var sub2 = new Fx.Slide('sub_menu_2');	
	var sub3 = new Fx.Slide('sub_menu_3');	
	var sub4 = new Fx.Slide('sub_menu_4');	
	sub0.hide();
	sub1.hide();
	sub2.hide();
	sub3.hide();
	sub4.hide();
	
	// IE shows the menus briefly onload, so we had to initially hide them.
	//  At this point, however, they are tucked away so we can make them visible them again.
	$("top_nav").style.visibility = 'visible';

	// We're using two different methods for showing the sub menus.
	// One for 'trident' (IE) and one for the others
	if(isIE)
	{
		$$(".sub_menu").each(
			function(obj) {
				// obj.style.visibility = 'visible';
					obj.style.display = 'inline';
			}
		);
	}
	else
	{
		$$(".sub_menu").each(
			function(obj) {
				obj.style.visibility = 'visible';
			}
		);
	}
	
	$('nav_0').addEvent('mouseenter', function(e){
		sub0.pause();
		sub0.slideIn();
	});

	$('nav_0').addEvent('mouseleave', function(e){
		e.stop();
		sub0.pause();
		sub0.slideOut();
	});
	
	$('sub_menu_0').addEvent('mouseenter', function(e){
		sub0.pause();
		sub0.slideIn();
	});

	$('sub_menu_0').addEvent('mouseleave', function(e){
		e.stop();
		sub0.pause();
		sub0.slideOut();
	});
	
	$('nav_1').addEvent('mouseenter', function(e){
		sub1.pause();
		sub1.slideIn();
	});

	$('nav_1').addEvent('mouseleave', function(e){
		e.stop();
		sub1.pause();
		sub1.slideOut();
	});
	
	$('sub_menu_1').addEvent('mouseenter', function(e){
		sub1.pause();
		sub1.slideIn();
	});

	$('sub_menu_1').addEvent('mouseleave', function(e){
		e.stop();
		sub1.pause();
		sub1.slideOut();
	});
	
	$('nav_2').addEvent('mouseenter', function(e){
		sub2.pause();
		sub2.slideIn();
	});

	$('nav_2').addEvent('mouseleave', function(e){
		e.stop();
		sub2.pause();
		sub2.slideOut();
	});
	
	$('sub_menu_2').addEvent('mouseenter', function(e){
		sub2.pause();
		sub2.slideIn();
	});

	$('sub_menu_2').addEvent('mouseleave', function(e){
		e.stop();
		sub2.pause();
		sub2.slideOut();
	});
	
	$('nav_3').addEvent('mouseenter', function(e){
		sub3.pause();
		sub3.slideIn();
	});

	$('nav_3').addEvent('mouseleave', function(e){
		e.stop();
		sub3.pause();
		sub3.slideOut();
	});
	
	$('sub_menu_3').addEvent('mouseenter', function(e){
		sub3.pause();
		sub3.slideIn();
	});

	$('sub_menu_3').addEvent('mouseleave', function(e){
		e.stop();
		sub3.pause();
		sub3.slideOut();
	});
	
	$('nav_4').addEvent('mouseenter', function(e){
		sub4.pause();
		sub4.slideIn();
	});

	$('nav_4').addEvent('mouseleave', function(e){
		e.stop();
		sub4.pause();
		sub4.slideOut();
	});
	
	$('sub_menu_4').addEvent('mouseenter', function(e){
		sub4.pause();
		sub4.slideIn();
	});

	$('sub_menu_4').addEvent('mouseleave', function(e){
		e.stop();
		sub4.pause();
		sub4.slideOut();
	});
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
	node = document;
  if (tag == null)
	tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
	if (pattern.test(els[i].className) ) {
	  classElements[j] = els[i];
	  j++;
	}
  }
  return classElements;
}



function turn_image_off(_img_ref)
{
	// Flip the image from 'off' to 'on'
	try{
		btnImage = _img_ref.src.replace("_over", "_on")
		_img_ref.src = btnImage;
	}
	catch (err) {
	}
}

function turn_image_on(_img_ref)
{
	try{
		// Flip the image from 'off' to 'on'
		btnImage = _img_ref.src.replace("_on", "_over")
		_img_ref.src = btnImage;
	}
	catch (err) {
	}
}
	
// Swap out the bio info for another person's info
function new_bio(_name)
{
	// Remove the pound sign from the name, if necessary
	if(_name.indexOf("#") >= 0)
	{
		_name = _name.substr(0,_name.indexOf("#"));
	}

	$('bio_name').innerHTML = $('bio_name_' + _name).innerHTML;
	$('bio_title').innerHTML = $('bio_title_' + _name).innerHTML;
	$('bio_copy').innerHTML = "";
	$('bio_copy').innerHTML = $('bio_copy_' + _name).innerHTML;

	setTimeout('$("bio_pic").style.backgroundImage = "url(images/bio_' + _name + '.jpg)"', 0);

	load_bio_movie(_name);

	if(_name == "bruce" || _name == "tito" || _name == "jason" || _name == "todd")
	{
		$('bio_video_holder').setStyle('visibility', 'hidden');
	}
	else
	{
		$('bio_video_holder').setStyle('visibility', 'visible');
	}
	
	return false;
}

function load_bio_movie(_name)
{
	setTimeout('thisMovie("foundation").loadNewVideo("_flv/video_' + _name + '.flv")',400);
}


function thisMovie(movieName)
{
	return (isIE) ? window[movieName] : document[movieName];
}

function turnMuteOn()
{
	btnImage = $('mute_button_img').src.replace("_on", "_off");
	thisMovie("foundation").muteOn();

	$('mute_button_img').src = btnImage;
}

function turnMuteOff()
{
	btnImage = $('mute_button_img').src.replace("_off", "_on");
	thisMovie("foundation").muteOff();

	$('mute_button_img').src = btnImage;
}

function mute_button_click()
{	
	// Toggle the cookie value
	sound_value = ((typeof sound_value == "undefined") || (sound_value == "on")) ? 'off' : 'on';
	Cookie.write('sound_value', sound_value, {duration: 365*10});	

	// Toggle the appearance of the button and set the volume
	if(sound_value == "off")
	{
		turnMuteOn();
	}
	else
	{
		turnMuteOff();
	}
}

// The Flash piece will call this function to determine if
//	it should mute itself or not 
function get_sound_status()
{
	return sound_value;
}


// From the current URL, grab the query string
function getQuery()
{
	var queryStr = (location.href.lastIndexOf('?') < 0) ? "" : location.href.substr(location.href.lastIndexOf('?') + 1);
	return queryStr;
}

function getQueryVar(_var)
{
	
	if(getQuery().split(_var + "=")[1])
	{
		return getQuery().split(_var + "=")[1].split("&")[0];
	}
	else
	{
		return false;
	}
}

function write_flash(_swfPath, _swfToLoad, _sectionName) {
    var so1 = new SWFObject(_swfPath, "foundation", "980", "350", "8", "#FFFFFF");
	so1.addVariable("swf_to_load", _swfToLoad + "?section_name=" + _sectionName);
	if(_sectionName != "undefined") {so1.addVariable("section_name",_sectionName); }
	so1.addParam("wmode", "window");
    so1.addParam("quality", "high");
    so1.addParam("allowScriptAccess", "always"); 
	so1.write('flash_div');
}


// window.addEvent('domready', function() {
window.onload = function() {
	header_init();
	sound_value = Cookie.read('sound_value');

	// If there's a horizontal scroller, initialize it.
	if(typeof hScroll != "undefined"){
		h_scroll_init();
	}
	
	if(typeof sectionName == "undefined")
	{
		sectionName = "undefined";
	};

	// If there's a case study thumbnail, rotate it out every "daysBetweenNewCaseStudyThumbnails" days
	if($('featured_case_study'))
	{		
		// This is a rather unruly, but sensible, way to get a new image every 14 days
		// 1) Get the total number of days passed since 1/1/70
		var currentTime = new Date()
		var daysPassedSince1970 = Math.floor((currentTime.getTime() * .001) / 86400) + daysOffset; // 86400 = seconds in a day
		
		// There are so many blocks of days during which each thumbnail can be shown for 14 days
		var currentBlockOfDays = daysPassedSince1970 % (daysBetweenNewCaseStudyThumbnails * caseStudyThumbList.length);
		
		// Determine what the current case study thumbnail should be during this block of days
		var caseStudyNum = Math.floor(currentBlockOfDays / daysBetweenNewCaseStudyThumbnails);

		// For debugging: When will the next thumbnail be shown?
		// alert("new case study thumbnail (" + (caseStudyNum + 1) + "/" + caseStudyThumbList.length + ") in " + (((caseStudyNum + 1) * daysBetweenNewCaseStudyThumbnails) - currentBlockOfDays) + " days.");
		
		// Replace the thumbnail, link, and copy with the thumb and copy for today
		var caseRef = caseStudies[caseStudyNum];
		$('featured_case_study').getElement('.video_preview').setStyle('backgroundImage','url(images/thumbnails/case_studies/' + caseStudyInfo[caseRef]["thumb"] + ')');
		$('featured_case_study').getElement('.video_preview a').setProperty('href', caseRef + '.html');
		$('featured_case_study').getElement('.overview').set('html', caseStudyInfo[caseRef]["description"]);
	}

	// If there's a video thumbnail, set the bio image randomly
	if($('meet_the_team'))
	{
		var bioNum = Math.floor(Math.random() * bioThumbList.length);
		$('meet_the_team').getElement('.video_preview').setStyle('backgroundImage','url(images/thumbnails/bios/' + bioThumbList[bioNum] + ')');
	}

	// Don't even try to load the Flash if the user is viewing via an iPod/iPhone
	if(!Browser.Platform.ipod)
	{
		var newName = getQueryVar('name');
		
		// Only pages where swfToLoad is defined (i.e. not the bio page) will load a SWF here
		if(typeof swfToLoad != "undefined")
		{
			write_flash("shell.swf",swfToLoad,sectionName);
		}
		else if(typeof newName != "undefined")
		{
			// If a name has been passed to the page, display that person's bio
			if(typeof newName == "undefined" || !newName )
			{
				newName = 'rick';
			}

			var so1 = new SWFObject("FlvPlayer8.swf", "foundation", "326", "300", "8", "#FFFFFF");
			so1.addVariable("video_file", "_flv/video_" + newName + ".flv");
			so1.addParam("wmode", "transparent");
			so1.addParam("quality", "high");
			so1.addParam("allowScriptAccess", "always");    
	
			so1.write('bio_video_holder');
		
			setTimeout('new_bio("' +  newName + '");', 200);
		}
	}
	else
	{
		$('flash_div').setStyle('height','40px');
	}
};
