// JavaScript Document

var offices = new Array()

function parseXML (xmlObj, thefunction){

	offices = new Array();

	officeNode = xmlObj.getElementsByTagName("office");
	for (x = 0; x< officeNode.length; x++){
		office = new Object();
		office.abbreviations = new Object();
		office.location = new Object();	
		office.altlocation = new Object();	

// top level info about this office:
		office.name = getVal(officeNode[x].getElementsByTagName("name")[0]);
		office.jobs = getVal(officeNode[x].getElementsByTagName("joblink")[0]);	
		office.opened = getVal(officeNode[x].getElementsByTagName("opened")[0]);
		office.associates = getVal(officeNode[x].getElementsByTagName("associates")[0]);	
// end top level info
	
// address information for this office:	
		loc = officeNode[x].getElementsByTagName("location")[0];
		office.location.address = getVal(loc.getElementsByTagName("address")[0]);
		office.location.city = getVal(loc.getElementsByTagName("city")[0]);
		office.location.state = getVal(loc.getElementsByTagName("state")[0]);
		office.location.postalcode = getVal(loc.getElementsByTagName("postalcode")[0]);
		office.location.country = getVal(loc.getElementsByTagName("country")[0]);
		office.location.maplink = getVal(loc.getElementsByTagName("maplink")[0]);
// end address information	

// address information for this office:	
		altloc = officeNode[x].getElementsByTagName("altlocation")[0];
		if (altloc) {
			office.altlocation.address = getVal(altloc.getElementsByTagName("address")[0]);
			office.altlocation.city = getVal(altloc.getElementsByTagName("city")[0]);
			office.altlocation.state = getVal(altloc.getElementsByTagName("state")[0]);
			office.altlocation.postalcode = getVal(altloc.getElementsByTagName("postalcode")[0]);
			office.altlocation.country = getVal(altloc.getElementsByTagName("country")[0]);
			office.altlocation.maplink = getVal(altloc.getElementsByTagName("altmaplink")[0]);
			office.abbreviations.altcity = getAttr(altloc.getElementsByTagName("city")[0], "abbr");
			office.abbreviations.altstate = getAttr(altloc.getElementsByTagName("state")[0], "abbr");	
			office.abbreviations.altcountry = getAttr(altloc.getElementsByTagName("country")[0], "abbr");				
		}
// end address information	


// abbreviations for this office
		office.abbreviations.city = getAttr(loc.getElementsByTagName("city")[0], "abbr");
		office.abbreviations.state = getAttr(loc.getElementsByTagName("state")[0], "abbr");	
		office.abbreviations.country = getAttr(loc.getElementsByTagName("country")[0], "abbr");	
		office.abbreviations.office = getAttr(officeNode[x].getElementsByTagName("name")[0], "abbr");
// end abbreviations

// images for this office
		img = officeNode[x].getElementsByTagName("images")[0];
		img = img.getElementsByTagName('image');
		allImages = new Array();
		for (i=0; i<img.length; i++){
			thisImg = new Object();
			thisImg.theType = getAttr(img[i], "type");
			thisImg.theCaption = getAttr(img[i], "caption");
			thisImg.theSrc = getAttr(img[i], "src");
			thisImg.theThumb = getAttr(img[i], "thumb");
			allImages.push(thisImg);
			if (thisImg.theType == "main"){office.mainimg = thisImg.theSrc;}
		}
		office.images = allImages;
// end image build		

		offices.push(office);  // add this office to our array.
	}

// next steps:		
	if (window.location.href.toLowerCase().indexOf('office.html') > -1){
		fillOffice();  // show an individual office if we're on an office page
	} else if (window.location.href.toLowerCase().indexOf('office_locations.html') > -1){
		fillOfficeList();  // show an individual office if we're on an office page		
	} else {
		fillGrid(); // fill the grid if we're on the main page
	}	
}


function fillGrid(){
	var r = '';
	r += '<table cellpadding="0" cellspacing="0" border="0" width="100%" id="officestable">';
	for (e=0; e<offices.length; e++){
		imgstyle=' class="rimage"';				
		tdstyle='';
		if (e % 2 ==0){
			imgstyle = '';
			if (e != 0){r+= '</tr>';}
			tdstyle+=' class="rborder"';
			r+= '<tr>';
		}
		thisquote = '';
		thestate = offices[e].abbreviations.state != ''?', ' + offices[e].abbreviations.state:'';
		r+= '<td' + tdstyle +' valign="middle"><a href="/careers/working_here/offices/office.html?id=' + offices[e].abbreviations.office + '"><img src="/_img/offices/' + offices[e].mainimg +'" alt="' + offices[e].name + '" align="left"' + imgstyle + '></a><p style="margin-top:29px;"><a href="/careers/working_here/offices/office.html?id=' + offices[e].abbreviations.office + '">'+ offices[e].name +  '</a></p>'
//		r+= '<p style="margin-top:8px;">Open since: ' + offices[e].opened + '<br />Associates: ' + offices[e].associates + '</p>'
		r+= '</td>';
	}
	r += '</table>';
	document.getElementById('officegrid').innerHTML = r;
}

function fillOfficeList(){
	var r = '';
	fullList = Math.round((offices.length+1)/3)
	startAt = 1
	for (e=0; e<offices.length; e++){
		if (e % fullList == 0 && e!=0  && startAt != 3){
			document.getElementById('list' + startAt).innerHTML = r;			
			r = '';
			startAt++;
		}
		thecity = offices[e].location.city
		thestate = offices[e].abbreviations.state;
		thecountry = offices[e].abbreviations.country != 'US' && offices[e].location.country != offices[e].location.city?offices[e].location.country:'';
		if ((thecity != '' && thestate != '') || (thecity != '' && thestate == '' && thecountry != '')) {thecity += ', ';}
		//thestate = offices[e].abbreviations.state != ''?', ' + offices[e].abbreviations.state:'';
		thepostalcode = offices[e].location.postalcode != ''? ' ' + offices[e].location.postalcode:'';
		
		altaddress = '';
		extrabr = '';
		altcity = offices[e].altlocation.city
		altstate = offices[e].abbreviations.altstate;
		altcountry = offices[e].abbreviations.altcountry != 'US'?offices[e].altlocation.country:'';
		if ((altcity != '' && altstate != '') || (altcity != '' && altstate == '' && altcountry != '') ) {altcity += ', ';}
		//if (altcountry != '' && altstate != '') {altstate += ', ';}		
		altpostalcode = offices[e].altlocation.postalcode != ''? ' ' + offices[e].altlocation.postalcode:'';		
		officeS = ''
		if (offices[e].altlocation.address) {
			altaddress = '<br />' + offices[e].altlocation.address + ', ' + altcity + altstate + altpostalcode + '&nbsp;' + altcountry ;
			extrabr = '<br />';
			officeS = 's';
		}
		r+= '<a href="/careers/working_here/offices/office.html?id=' + offices[e].abbreviations.office + '" onmouseover="document.getElementById(\'locationDetails\').innerHTML = \'<strong>' + offices[e].name + ' office' + officeS + '</strong> ' + extrabr + offices[e].location.address + ', ' + thecity + thestate + thepostalcode + ' ' + thecountry + altaddress + '\'">' + offices[e].name + '</a>'
	}
	document.getElementById('list3').innerHTML = r;
}

function fillOffice(){
	for (i=0; i<offices.length; i++){
		if (offices[i].abbreviations.office.toLowerCase() == id.toLowerCase()){
			thisoffice = offices[i];
			document.getElementById('officeList').innerHTML += '<p><span style="color:#c4863d;font-weight:bold;">' + offices[i].name + '</span></p>';
		} else {
			document.getElementById('officeList').innerHTML += '<p><strong><a href="/careers/working_here/offices/office.html?id=' + offices[i].abbreviations.office + '">' + offices[i].name + '</a></strong></p>';
		}
	}
	var r = '';
	thumbImages = new Array();
	officeName = thisoffice.name;
	for (i = 0; i < thisoffice.images.length; i++){
		ourimage = thisoffice.images[i];
		if (ourimage.theType == 'page'){
			thisImage = new Object();
			thisImage.src = ourimage.theSrc;
			thisImage.thumb = ourimage.theThumb;
			thisImage.caption = ourimage.theCaption;
			thumbImages.push(thisImage);
		}
	}	
	
	thestate = thisoffice.abbreviations.state != ''? ', ' + thisoffice.abbreviations.state:'';
	thecountry = thisoffice.abbreviations.country != 'US' && thisoffice.abbreviations.country != ''? ', ' + thisoffice.location.country:'';
	thisaddress = thisoffice.location.address + '<br />' + thisoffice.location.city + thestate + thecountry + '  ' + thisoffice.location.postalcode;
	if (thisoffice.location.maplink) { thisaddress += '<br /><a href="' + thisoffice.location.maplink + '" target="_blank">view map</a>';}
	altaddress = '';
	altstate = thisoffice.abbreviations.altstate != ''? ', ' + thisoffice.abbreviations.altstate:'';
	altcountry = thisoffice.abbreviations.altcountry != 'US' && thisoffice.abbreviations.altcountry != ''? ', ' + thisoffice.altlocation.country:'';
	if (thisoffice.altlocation.address) {altaddress = '<br /><br />' + thisoffice.altlocation.address + '<br />' + thisoffice.altlocation.city + altstate + altcountry + '  ' + thisoffice.altlocation.postalcode;}
	if (thisoffice.altlocation.maplink) { altaddress += '<br /><a href="' + thisoffice.altlocation.maplink + '" target="_blank">view map</a>';}

	newslide(0);
	document.getElementById('slideDetailName').innerHTML = thisoffice.name;
	document.getElementById('slideDetailAddress').innerHTML =  thisaddress + altaddress;
	document.getElementById('slideDetailOffice').innerHTML =  'Local employer since: ' + thisoffice.opened + '<br />Associates: ' + thisoffice.associates;
	thejoblink =  thisoffice.jobs;
	document.getElementById('officeTitle').src = '/_img/office_title_' + thisoffice.abbreviations.office + '.gif';	
	for (m = 0; m< thumbImages.length; m++){
		document.getElementById('thumb' + m).style.display = '';		
		document.getElementById('thumb' + m).src = '/_img/offices/' + thumbImages[m].thumb;	
	}
	if (thumbImages.length <= 1) {document.getElementById('slideThumbs').style.display = 'none';}
	
}
