window.addEvent('domready', function() {

	// Value picker rollover
	if($('valuepicker')) {
		var vpopen = function() {
			var picker = this;
			if(this.get('tag') == 'a') {
				picker = this.getParent('li');
			}
			$$('#valuepicker li').removeClass('selected');
			picker.addClass('selected');
		};
		$$('#valuepicker li').addEvent('mouseenter', vpopen);
		$$('#valuepicker li a').addEvent('focus', vpopen);
	}
	
	// Bloglist rollover
	if($$('.bloglist').length > 0) {
		var bfocus = function() {
			var row = this;
			if(this.get('tag') == 'a') {
				row = this.getParent('tr');
			}
			row.addClass('selected');
		};
		var bblur = function() {
			var row = this;
			if(this.get('tag') == 'a') {
				row = this.getParent('tr');
			}
			row.removeClass('selected');
		};
		$$('.bloglist tr.tablerow').addEvent('mouseenter', bfocus);
		$$('.bloglist tr.tablerow a').addEvent('focus', bfocus);
		$$('.bloglist tr.tablerow').addEvent('mouseleave', bblur);
		$$('.bloglist tr.tablerow a').addEvent('blur', bblur);
		$$('#bloggerlist tr.tablerow').addEvent('click', function() {
			window.location = this.getElement('a').getProperty('href');
		});
	}
	
	// Employee gallery rollover
	if($('employeestories')) {
		var egfocus = function() {
			var row = this;
			if(this.get('tag') == 'a') {
				row = this.getParent('li.employee');
			}
			$('introtext').setStyles({position: 'absolute', left: '-9999px'});
			$$('#employeestories.gallery li.employee').removeClass('selected');
			row.addClass('selected');
		};
		$$('#employeestories.gallery li.employee').addEvent('mouseenter', egfocus);
		$$('#employeestories.gallery div.gallerydetails a').addEvent('focus', egfocus);
		$$('#employeestories.gallery li.first').fireEvent('mouseenter');
	}
	
	// Location map rollover
	if($('locationmap')) {
		var mapImages = new Asset.images([
			'/images/locationmap.gif',
			'/images/locationmap-ireland.gif',
			'/images/locationmap-scotland.gif',
			'/images/locationmap-wales.gif',
			'/images/locationmap-northwest.gif',
			'/images/locationmap-northeast.gif',
			'/images/locationmap-midlands.gif',
			'/images/locationmap-eastanglia.gif',
			'/images/locationmap-southwest.gif',
			'/images/locationmap-london.gif',
			'/images/locationmap-southeast.gif'
		]);
		
		var mapImageClass = $('locationmapimage').get('class');
		
		var infoToggle = function(overClass) {
			$$('#locationinfo li').setStyle('display','none');
			$$('#locationinfo .'+overClass).setStyle('display','block');
		}
		infoToggle(mapImageClass);
		
		var mapfocus = function() {
			var shape = this;
			$('locationmapimage').set('src', '/images/'+shape.get('id')+'.gif');
			infoToggle(shape.get('id'));
		}
		var mapleave = function() {
			$('locationmapimage').set('src', '/images/'+mapImageClass+'.gif');
			infoToggle(mapImageClass);
		}
		var mapblur = function() {
			// check if no areas have focus before reverting the image
			var mapfocus = false;
			$each($$('#locationmap area'), function (shape) {
				if(shape.focus) {
					mapfocus = true;
				}
			});
			if(mapfocus) {
				$('locationmapimage').set('src', '/images/'+mapImageClass+'.gif');
			}
			infoToggle(mapImageClass);
		}
		// firefox calls blur on mousedown on image map areas so its event must be stopped
		var mapmousedown = function(e) {
			e.stop();
		}
		$$('#locationmap area','#locationmap-anylocation').addEvents({'mouseenter': mapfocus, 'focus': mapfocus});
		$$('#locationmap area','#locationmap-anylocation').addEvents({'mouseleave': mapleave});
		$$('#locationmap area','#locationmap-anylocation').addEvents({'blur': mapblur});
		$$('#locationmap area','#locationmap-anylocation').addEvents({'mousedown': mapmousedown});
	}
	
	// flash banner on homepage
	if($('template').hasClass('homepage')) {
		if(swfobject.hasFlashPlayerVersion('6.0.0')) {
			$$('#menu1','#menu2').setStyle('opacity','0');
			$$('#menu1','#menu2').setStyle('left','0px');
		} else {
			$$('#menu1','#menu2').setStyle('left','0px');
			$('menubackground').setStyle('backgroundImage', 'url(/images/menu-bg-homepage.jpg)');
		}
		swfobject.embedSWF('/flash/banner-homepage.swf', 'menubackgroundflash', '960', '331', '6.0.0', false, false, {wmode: 'transparent'}, false);
		
	}

	// flash banners on top pages
/*	if(swfobject.hasFlashPlayerVersion('9.0.0')) {
		if($('template').hasClass('ourstory')) {
			swfobject.embedSWF('/flash/banner-our-story.swf', 'menubackgroundflash', '960', '234', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
		if($('template').hasClass('newtoconstruction')) {
			swfobject.embedSWF('/flash/banner-construction.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
		if($('template').hasClass('locationmap')) {
			swfobject.embedSWF('/flash/banner-location.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
		if($('template').hasClass('whoisdoingwhatatwates')) {
			swfobject.embedSWF('/flash/banner-employee.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
		if($('template').hasClass('contact')) {
			swfobject.embedSWF('/flash/banner-contact.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
		if($('template').hasClass('jobsearch')) {
			swfobject.embedSWF('/flash/banner-job-search.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
		if($('template').hasClass('ourdna')) {
			swfobject.embedSWF('/flash/banner-dna.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
		}
	} else {
		var templateClasses = $('template').className.split(" ");
		var templateName = templateClasses[2];

		// set letters to default location
		$('letter1').setStyles('.nojs .'+templateName+' #letter1');
		$('letter2').setStyles('.nojs .'+templateName+' #letter2');
		$('letter3').setStyles('.nojs .'+templateName+' #letter3');
		$('letter4').setStyles('.nojs .'+templateName+' #letter4');
		$('letter5').setStyles('.nojs .'+templateName+' #letter5');
	}*/
	
	// flash banners on values pages
	if($('template').hasClass('winningformula')) {
		swfobject.embedSWF('/flash/banner-winning-formula.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
	}
	if($('template').hasClass('worthwhilework')) {
		swfobject.embedSWF('/flash/banner-worthwhile-work.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
	}
	if($('template').hasClass('respect')) {
		swfobject.embedSWF('/flash/banner-respect.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
	}
	if($('template').hasClass('development')) {
		swfobject.embedSWF('/flash/banner-development.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
	}
	if($('template').hasClass('reward')) {
		swfobject.embedSWF('/flash/banner-reward.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
	}
	if($('template').hasClass('flexibility')) {
		swfobject.embedSWF('/flash/banner-flexibility.swf', 'menubackgroundflash', '960', '212', '9.0.0', false, false, {wmode: 'transparent'}, false);
	}
});

// function to fade in menus on homepage (fired by flash)
function fadeIn() {
	$$('#menu1','#menu2').set('tween',{duration: 1000});
	$$('#menu1','#menu2').tween('opacity',1);
}
