//function to preload images
function MM_preloadImages() { 
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//images to be rotated on homepage
var homepage_pics = new Array();
homepage_pics[0] = 'images/bg_home_machupichu.jpg';
homepage_pics[1] = 'images/bg_home_pyramids.jpg';
homepage_pics[2] = 'images/bg_home_aqueduct.jpg';
homepage_pics[3] = 'images/bg_home_stonehenge.jpg';

//images to be rotated on detailpages
var detailpage_pics = new Array();
detailpage_pics[0] = 'images/bg_detail_machupichu.jpg';
detailpage_pics[1] = 'images/bg_detail_pyramids.jpg';
detailpage_pics[2] = 'images/bg_detail_aqueduct.jpg';
detailpage_pics[3] = 'images/bg_detail_stonehenge.jpg';

//function to preload homepage images
function preloadHomepageImages(){
	var imagesToLoad = "";
	for(var i=0;i<homepage_pics.length;i++){
		if(i>0){
			imagesToLoad += ","
		}
		imagesToLoad += "'"+homepage_pics[i]+"'";
	}	
	MM_preloadImages(imagesToLoad);
}

//function to rotate pics on homepage
var pic_counter = 0;
function runHomepageSlideshow(){   		
   	//set background
	document.body.style.backgroundImage = 'url("images/bg_home_aqueduct_new.jpg")';
	//set pic counter into session cookie so same theme is maintained through session
   	setCookie('pic_counter_cookie', pic_counter);
   	//increment counter
   	pic_counter++;
   	if (pic_counter > (homepage_pics.length-1)){
   		pic_counter = 0;
   	}     	
   	setTimeout('runHomepageSlideshow()', 30000)
}

//function to set the homepage background based on the time
function setInitialHomepageBackground(){
	preloadHomepageImages();
	pic_counter = getPicCounter();
}

//function to set the detail page background based on the current counter
function setDetailpageBackground(page){
	if(page == 'products'){
		document.body.background = 'images/bg_detail_stonehenge_new.jpg';
	} else if(page == 'services'){
		document.body.background = 'images/bg_detail_machupichu_new.jpg';
	} else {
		document.body.background = 'images/bg_detail_aqueduct_new.jpg';
	}
	/* old code for switching the background. may be removed
	var pic_counter_session = getCookie('pic_counter_cookie');
	if(pic_counter_session == null){
		pic_counter_session = getPicCounter();
	}
	document.body.background = detailpage_pics[pic_counter_session];
	*/
}

//function to return a number between 0 and 3 based on the time
function getPicCounter(){
	var currentdate = new Date();
	var theseconds = currentdate.getSeconds();
	var tempcounter = 0;
	if(theseconds >= 15 && theseconds < 30){
		tempcounter = 1;
	} else if(theseconds >= 30 && theseconds < 45){
		tempcounter = 2;
	} else if(theseconds >=45){
		tempcounter = 3;
	}
	return tempcounter;
}

//function to update the style class of an item
function updateStyle(item, newstyle){
	item.className = newstyle;
}

//function to update the SRC of an item
function updateImageSrc(item, src){
	var it = document.getElementById(item);
	it.src = "images/buttons/" + item + src + ".gif";
}

//function to update the style class of a menu item
function updateMenuItemStyle(menuitem){
	var unselected = 'MENU_unselected';
	var selected = 'MENU_selected';
	if(menuitem.className == unselected){
		menuitem.className = selected;
		document.getElementById("link_"+menuitem.id).className = selected;
		document.getElementById("arrow_"+menuitem.id).style.visibility = "visible";
	} else {
		menuitem.className = unselected;
		document.getElementById("link_"+menuitem.id).className = unselected;
		document.getElementById("arrow_"+menuitem.id).style.visibility = "hidden";
	}
}

var detailsectioncount = 0;

//function to hide/show detail page sections
function displaySectionByName(name){
	var item = document.getElementById(name);
	displaySection(item);
}

//function to hide/show detail page sections
function displaySection(item){
	var detailsection = document.getElementById(item.id+"_detail");
	var arrow = document.getElementById(item.id+"_arrow");
	var more = document.getElementById(item.id+"_more");
	if(detailsection.style.display == "inline"){
		detailsection.style.display = "none";
		arrow.src = "images/gfx_arrowright.gif";
		if(more){
			more.style.display = "inline";
		}
		detailsectioncount--;
	} else {
		detailsection.style.display = "inline";
		arrow.src = "images/gfx_arrowdown.gif";
		if(more){
			more.style.display = "none";
		}
		detailsectioncount++;
	}
	setFooterLocation();
} 

//function to properly position the footer div height based on page content
function setFooterLocation(){
	var docheight = document.getElementById("DP_maincontent").offsetHeight;
	var footer = document.getElementById("footer_default");
	if(docheight > 625){
		footer.style.top = docheight+110;
	} else {
		footer.style.top = 725;
	}
}

//set the appropriate toplevel menu to selected
function setToplevelMenu(menu){
	document.getElementById(menu+"_topmenu").className = "TOPMENU_selected";
}

//set the breadcrumb menu choice that should be selected
function setBreadcrumbs(selection){
	var selected_style = "BC_selected";
	document.getElementById(selection+"_bc").className = selected_style;
	document.getElementById(selection+"_bcbox").src = "images/gfx_box.gif";
}

//set sidebar style for active page
function setSidebarStyle(page){
//	document.getElementById("SB_"+page).className = "SidebarContentGrey";
//	document.getElementById("sidebar_"+page).className = "FONT_headline_sbgrey";
//	document.getElementById("sidebar_"+page+"_pic").style.display = "none"; 
//	if(document.getElementById("sidebar_"+page+"_link1")){
//		document.getElementById("sidebar_"+page+"_link1").className = "SidebarContentGrey";
//	}
//	if(document.getElementById("sidebar_"+page+"_link2")){
//		document.getElementById("sidebar_"+page+"_link2").className = "SidebarContentGrey";
//	}
//	if(document.getElementById("sidebar_"+page+"_link3")){
//		document.getElementById("sidebar_"+page+"_link3").className = "SidebarContentGrey";
//	}
}

//set sidebar style for active page - THIS VERSION HAS NEWS ITEMS INDIVIDUALLY GREY
//function setSidebarStyle(page){	
//	document.getElementById("sidebar_"+page).className = "FONT_headline_sbgrey";
//	document.getElementById("sidebar_"+page+"_pic").style.display = "none"; 
//	if('news' != page){
//		document.getElementById("SB_"+page).className = "SidebarContentGrey";
//		if(document.getElementById("sidebar_"+page+"_link1")){
//			document.getElementById("sidebar_"+page+"_link1").className = "SidebarContentGrey";
//		}
//		if(document.getElementById("sidebar_"+page+"_link2")){
//			document.getElementById("sidebar_"+page+"_link2").className = "SidebarContentGrey";
//		}
//	}
//}

//set news items to selected or not
//function setNewsItemSidebar(itemno){
//	document.getElementById("sidebar_news").className = "FONT_headline_sbgrey";
//	document.getElementById("sidebar_news_pic").style.display = "none"; 
//	if(document.getElementById("sidebar_news_link"+itemno)){
//		document.getElementById("sidebar_news_link"+itemno).className = "SidebarContentGrey";
//		document.getElementById("sidebar_news_date"+itemno).className = "FONT_newsdategrey";
//	}
//}

//function to process a client login
function login(){
	alert('This feature is not currently available.');
}

var windowcount = 0;
//open a window with no toolbar or scrollbars to display a screenshot
function openCleanWindow(img){
	var url = img.src.substring(0,img.src.length-4)+'_full.gif';
	window.open(url,""+windowcount,"toolbar=no,scrollbars=no,menubar=no,status=no,directories=no");
	windowcount++;
}

//menu functionality ------------------------------------------------------------------------------------
var productsMenuTimer = null;
var servicesMenuTimer = null;
var expertiseMenuTimer = null;
var partnersMenuTimer = null;
var aboutusMenuTimer = null;

function launchMenu(menu, show){
	//set toggle for showing or hiding the menu
	var toggle = "inline";
	if(!show){
		toggle = "none";
	} else {
		cancelTimedHideMenu(menu);
	}
	document.getElementById(menu+"_base").style.display = toggle;	
	document.getElementById(menu+"_menu").style.display = toggle;
}

function timedHideMenu(menu){
	eval(menu+"MenuTimer = setTimeout(\"launchMenu('\"+menu+\"', false)\",75);");
	
	/* replaced by the above eval statement -- val, oct 7, 2009
	if('products' == menu){
		productsMenuTimer = setTimeout("launchMenu('"+menu+"', false)",75);
	}
	if('sports' == menu){
		sportsMenuTimer = setTimeout("launchMenu('"+menu+"', false)",75);
	}
	if('expertise' == menu){
		expertiseMenuTimer = setTimeout("launchMenu('"+menu+"', false)",75);
	}
	if('clients' == menu){
		clientsMenuTimer = setTimeout("launchMenu('"+menu+"', false)",75);
	}*/
}

function cancelTimedHideMenu(menu){
	eval("if ("+menu+"MenuTimer) clearTimeout("+menu+"MenuTimer); "+menu+"MenuTimer = null;");
	
	/* replaced by the above eval statement -- val, oct 7, 2009
	if('products' == menu){
		if (productsMenuTimer) clearTimeout(productsMenuTimer);
			productsMenuTimer = null;
	}
	if('sports' == menu){
		if (sportsMenuTimer) clearTimeout(sportsMenuTimer);
			sportsMenuTimer = null;
	}
	if('expertise' == menu){
		if (expertiseMenuTimer) clearTimeout(expertiseMenuTimer);
			expertiseMenuTimer = null;
	}
	if('clients' == menu){
		if (clientsMenuTimer) clearTimeout(clientsMenuTimer);
			clientsMenuTimer = null;
	}
	*/
}

function getCookie(name){
	var cname = name + "=";
    var dc = document.cookie;
    if (dc.length > 0){
    	begin = dc.indexOf(cname);
        if (begin != -1){
        	begin += cname.length;
            end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
           	return unescape(dc.substring(begin, end));
        }
    }
    return null;
}

function setCookie(name, value){
	document.cookie = name + "=" + escape(value);
}

//function to check for valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
   //search email text for regular exp matches
   if (strEmail.search(validRegExp) == -1) return false;
   return true; 
}

//function to check for valid phone number
function isValidPhoneNumber(s){ 
	if(s.length < 7) return false;
	var goodChars = "0123456789()-+ext. ";
	var i = 0;
	for (i =0; i <= s.length -1; i++) {
		if (goodChars.indexOf(s.charAt(i)) == -1){			
			return false;
		} 
	} 
	return true;
}

/*
	AJAX Submission
*/
function retrieveURL(url) {
	var req;
    if (window.XMLHttpRequest) { // Non-IE browsers
	    req = new XMLHttpRequest(); 
		try {
	    	req.open("GET", url, true);
	   	} catch (e) {
	    	alert(e);
	    }
	    req.send(null);
    } else if (window.ActiveXObject) { // IE    
		req = new ActiveXObject("Microsoft.XMLHTTP");
      	if (req) {
        	req.open("GET", url, true);
        	req.send();
      	}
    }
} 

//function to log a download
function logDownload(filename){
	retrieveURL('cgi-bin/downloadlogger.pl?filename='+filename);
}

//toggle the social network links 
function toggleSocialNetworkLinks(item, color){
	var the_path = 'images/logo_'+item+'.png';
	if(color){
		the_path = 'images/logo_'+item+'_color.png';
	}
	document.getElementById('sn'+item).src = the_path;
}
