window.addEvent('domready', function() { 
	if($('home_flash')){
		swfobject.addDomLoadEvent(initWindowResize);
	}else{
		initWindowResize();
	}
	
	if($('select_another_project')){setupSelectAnotherProjectDropDown();}
	if($('slide_nav')){setupSlideNav();}
	
	setupNavRollOvers();
	
	if ($('projectSlides')  && $('next')) { projectSlideshow(); }
	if ($('sitesearchsubmit')) { placeholderSubmit(); }
	if ($('currentJobs')) { jobOpenings(); }
	if ($('specialty')) { specialties(); }
});

// this is only for showing the client the templates
function placeholderSubmit() {
	$('sitesearchsubmit').onclick = function() {
		window.location = 'search-results.php';
		return false;
	}
}

function jobOpenings() {
	hideJobs();
	
	$$('#currentJobs li a').each(function(el,index) {
		el.onclick = function() {
			$('jobListings').setStyle('display','none');
			$$('#jobSpecific div.job')[index].setStyle('display','block');
			return false;
		}
	});
	
	$$('a.back').each(function(el,index) {
		el.onclick = function() {
			$('jobListings').setStyle('display','block');
			hideJobs();
			return false;
		}
	});

}
function hideJobs() {
	$$('#jobSpecific div.job').each(function(el,index) {
		el.setStyle('display','none');
	});
}

function initWindowResize(){
	setupTopographyOnResize();
	swffit.addResizeEvent(setupTopographyOnResize); 
}

function setupTopographyOnResize(){
	if($('topography')){
		
		var size = swffit.setSize();

		$('topography').setStyle("width",size.width);
		$('topography').setStyle("height",size.height);

		
		$('header').setStyle("width",size.width);
		$('header').setStyle("height",100);
		
		var win_size = $('topography').getSize();
		var win_width = win_size.x;

		var win_height = win_size.y;
		var left = Math.floor((win_width * .5)) - 84;
		var width = win_width - left;
		
		$('mainNav').setStyle("left", left);
		$('mainNav').setStyle("width", width + 20);
		$('mainNav').setStyle("height", win_height + 20);
		
		$('tertNav').setStyle("left",left);

		if(!Browser.Engine.trident){
			header_width = $('header').getSize().x;
	
			$$('.stretchy').each(function(item, index){
				_x = item.getPosition().x;
	
				if(size.width <= 980){
					item.setStyle("width","100%");
				}else{
					item.setStyle("width",header_width - _x);
				}
			});
		}
	}
}

function setupNavRollOvers(){
	$$('#mainNav li.main_nav_item').each(function(item, index){
		$(item).addEvents({
			'mouseover': function(){
				var size = swffit.setSize();
				$('header').setStyle("height",size.height);
				if(!window.XMLHttpRequest) {
					$('header').setStyle("height",700);
				}
				
				$('mainNav').addClass('list_over');
				this.addClass('nav_over');
			},
			'mouseout': function(e){
				$('mainNav').removeClass('list_over');
				this.removeClass('nav_over');
				$('header').setStyle("height",100);
			}
		});

	});
}

function fadeInNav(){
	$('header').set('opacity',0);
	$('header').fade(1);
}

function setupSelectAnotherProjectDropDown(){
	$('select_another_project').addEvents({
		'mouseover': function(){
			$('select_another_project').addClass('over');
		},
		'mouseout': function(e){
			$('select_another_project').removeClass('over');
		}
	});
}

function setupSlideNav(){
	$$('#slide_nav li a').each(function(item, index){
    	var shim = new Element('span', {'class': 'shim'});
		shim.inject(item, 'top');
		shim.setStyle('opacity',.35);
		
		item.addEvent('click',function(e){
			show_slide(this);
		});
	});
	
	$$('#slides a').each(function(item, index){
    	if(index > 0){
	    	item.setStyle('opacity',0);
    		item.removeClass('hide');
    	}
	});
}

function show_slide(el){
	
	var el_index = 0;
	
	$$('#slide_nav li a').each(function(item, index){
		if(item == el){
			el_index = index;
			item.addClass('active');
		}else{
			item.removeClass('active');
		}
	});
	
	$$('#slides a').each(function(item, index){
    	if(index == el_index){
    		item.fade('in');
    	}else{
    		item.fade('out');
    	}
	});
}

function specialties() {
	$$('#specialty li').each(function(el,index) {
		el.onclick = function() {
			this.toggleClass('expand');
		}
	});
}

function projectSlideshow() {
	
	var currentIndex = 0;
	
	$$('#projectSlides li').each(function(el,index) {
		if (!el.hasClass('active')) {
			el.set('opacity',0);
		}
	});
	
	$('next').onclick = function() {
				
		if (currentIndex >= ($$('#projectSlides li').length-1)) {
			currentIndex = 0;
		} else {
			currentIndex++;
		}
		
		$$('#projectSlides li').each(function(el,index) {
			if (el.hasClass('active')) {
				el.removeClass('active');
				el.fade('out');
			}
		});
		
		$$('#projectSlides li')[currentIndex].addClass('active');
		$$('#projectSlides li')[currentIndex].fade('in');
		
		return false;
		
	}
}