/* Required for IE6 Dropdowns: http://www.htmldog.com/articles/suckerfish/dropdowns/ */
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	
	var sfEls = document.getElementById("workSubNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	
	if($('projectDropDown')){
		var sfEl = document.getElementById("projectDropDown");

		sfEl.onmouseover=function() {
			this.className+=" sfhover";
		}
		
		sfEl.onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

var personFilterOnLoad = null;
var personBioOnLoad = null;
var personBioSectionOnLoad = null;
var peopleSection = null;
window.addEvent('domready', function() {	
	if($('peopleControls')){
		
		peopleSection = true;
		
		// swfaddress
		var hash = location.href.indexOf('#');
	    if(hash != -1 || hash == location.href.length - 1) {
			var swfaddressPath = SWFAddress.getPath().split("/");

			if(swfaddressPath[1]){
				personFilterOnLoad = swfaddressPath[1];
			}
			
			if(swfaddressPath[2]){
				personBioOnLoad = swfaddressPath[2];
				initBio(personBioOnLoad);
			}
			
			if(swfaddressPath[3]){
				personBioSectionOnLoad = swfaddressPath[3];
			}
		}
		
		setupPeopleControls();
	}
	
	if($('secondaryLogo')){setupLogoEasterEgg();}
	if($$('a[rel]').length){setupExternalLinks();}
	if($$('.peopletip').length){setupPeopleTips();}
	if($$('.projecttip').length){setupProjectTips();}
	if($$('.tooltip').length){setupToolTips();}
	if($('projDescription')){setupScrollBar($('projDescription'));}
	if($('jobList')){setupScrollBar($('jobList'));}
	if($('jobDescription')){setupScrollBar($('jobDescription'));}
	if($('autoload')){initBio($('autoload').getProperty('title'));}

	if ($('emailSignup')){signup();}
	if ($('html_content')) {
		$('html_content').setOpacity(0);
		$('b_overview').setOpacity(0);
		st = document.getElementById('subtitle').innerHTML;
		cy = document.getElementById('copy').innerHTML;
		add_scroll();
		//SWFAddress.setId('non_flash_content');
		SWFAddress.addId('non_flash_content');
		SWFAddress.onChange = function() {
			//alert('CALLED: SWFAddress.onChange = function() { change_section(); }');
			change_section();
		}
		swfalinks();
		preload_image_titles();
		
		var swfaddressPath = SWFAddress.getPath().split("#");
		if(swfaddressPath){
			var l = swfaddressPath[0].replace('/','').replace('/','').trim();
			SWFAddress.setValue(l);
		}
	}
});
function swfalinks() {
	$$('a.swfa').each(function(link){
		link.onclick = function() {
			var swfaddressPath = this.href.split("#");
			var l = swfaddressPath[1].replace('/','').replace('/','');
			SWFAddress.setValue(l);
			return false;
		}
	});
}
function update_breadcrumbs(section,title) {
	var home = '<li><a href="/">Home</a></li>';
	if (title == 'Our Methods') {
		var methods = '';
	} else {
		var methods = '<li><a href="/our-methods/#/" class="swfa">Our Methods</a></li>';
	}
	var current = '<li class="active">'+title+'</li>';
	$$('#breadcrumbs ul').setHTML(home+methods+current);
	swfalinks();
}
function change_section() {
	var el = $('html_content');
	var swfaddressPath = SWFAddress.getPath().split("#");
	var section = swfaddressPath[0].replace('/','').replace('/','').trim();
		var bfx = new Fx.Style($('b_overview'), 'opacity', {duration: 500, wait: false});
		var fx = new Fx.Style(el, 'opacity', {duration: 500, wait: false});
		if (section == 'overview' || section == '') bfx.start(0);
		var fadeoutfx = new Fx.Style(el, 'opacity', {duration: 500, wait: false, onComplete:function() {

			var url = '/our-methods/json.php?section='+section;
			var req = new Ajax(url,{
				method: 'get',
				onComplete:function(x) {
					if (section == 'overview' || section == '') {
						set_content(section,'Our Methods',st,cy,'/inc/i/text-our-methods.gif');
					} else {
						(function(){bfx.start(1);}).delay(1000);
						var myJsonObj = jsonParse(x);
						set_content(section,myJsonObj.title,myJsonObj.subtitle,myJsonObj.copy,myJsonObj.image);
					}
					(function(){fx.start(1);}).delay(1000);
				}
			}).request();
		}});
		fadeoutfx.start(0);
}
function preload_image_titles() {
	var url = '/our-methods/json.php?preload=true';
	var req = new Ajax(url,{
		method: 'get',
		onComplete:function(x) {
			var myJsonObj = jsonParse(x);
			var div = new Element ('div',{'id':'img_preload','style':'display:none;'}).injectInside($('contentContainer'));
			$('img_preload').setHTML(myJsonObj.images);
		}
	}).request();
}
function set_content(section,title,subtitle,copy,image) {
	$('title').setHTML('<img src="'+image+'" alt="'+title+'" />');	
	$('subtitle').setHTML(subtitle);
	$('copy').setHTML(copy);
	add_scroll();
	update_breadcrumbs(section,title);
}
function add_scroll() {
	// over state
	$('knob').addEvent('mouseover', function(event){ this.setStyle('background-position','0px -20px'); });
	$('knob').addEvent('mouseout', function(event){ this.setStyle('background-position','0px 0px'); });
	
	var top = $('subtitle').getCoordinates().height+$('title').getCoordinates().height+32;
	var new_h = $('html_content').getCoordinates().height-top;
	var h = $('copy').getSize().size.y;
	
	$('scrollable').setStyle('height',new_h); // set height of scrollable content
	
	if (h > (new_h-5)) {
		$('scrollbar').setStyle('display','block');
		$('knob').setStyle('display','block');
		new Slider($('scrollbar'), $('knob'), {
			range: [0],	// Minimum value
			mode:'vertical',
			offset:-10,
			steps:h-(new_h-20),
			onChange: function(value){
				$('copy').setStyle('margin-top', -(value));
			}
		}).set(0);
	} else {
		$('scrollbar').setStyle('display','none');
		$('knob').setStyle('display','none');
	}
}

function setupLogoEasterEgg(){
	$('logoLink').addEvent('mouseover', function(){
		$('primaryLogo').addClass('over');
		$('secondaryLogo').addClass('easter');
	});
	
	$('logoLink').addEvent('mouseout', function(){
		$('primaryLogo').removeClass('over');
		$('secondaryLogo').removeClass('easter');
	});
}

function setupExternalLinks(){
	$$('a[rel]').each(function(link){
		if(link.rel == "external"){
			link.target = "_blank";
		}
	});
}

function setupToolTips(){
	var Tips2 = new Tips($$('.tooltip'), {
		maxTitleChars: '300',
		offsets: {'x': 0, 'y': -25},
		initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
	});
	
	if(window.ie){
		$$('.tooltip').each(function(link){
			link.alt = "";
		});
		
		$$('#thumbnails img').each(function(link){
			link.alt = "";
		});
	}
}

function setupProjectTips(){
	var Tips3 = new Tips($$('.projecttip'), {
		maxTitleChars: '300',
		className: 'project',
		initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
	});
	
	if(window.ie){
		$$('.projecttip').each(function(link){
			link.alt = "";
		});
	}
}

function setupPeopleTips(){
	var Tips3 = new Tips($$('.peopletip'), {
		className: 'people',
		maxTitleChars: '300',
		initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
	});
	
	var easterEggTip = new Tips($('eastereggtip'), {
		className: 'easter',
		initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
	});
}

function setupScrollBar(content){

	// check if it needs a scroll
	if($(content).getSize().scrollSize.y > $(content).getSize().size.y){
		// close the copy and nest it within a div with the id 'scrollableCopy'
		scrollableCopy = $(content).clone()
		scrollableCopy.setProperty('id',content.id+'_scrollableCopy');
		scrollableCopy.addClass('scrollableCopy');
		scrollableCopy.setStyles({
		   border: 0,
		   height: "auto",
		   overflow: "visible"
		});
		
		if(content.id == "jobList" && window.webkit){
			scrollableCopy.setStyle("width",515);
		}
		
		$(content).empty();
		scrollableCopy.injectInside(content);

		// create scroll bar and inject it after the copy container div
		newHeight = $(content).getSize().size.y - 4;
		
		if(window.ie){
			if($(content).hasClass('categoryExpertise')){
				newTop = -$(content).getSize().size.y + 5;
				newHeight -= 6;
			}else{
				newTop = -$(content).getSize().size.y - 9;
			}
		}else{
			if($(content).hasClass('categoryExpertise')){
				newTop = -$(content).getSize().size.y + 5;
				newHeight -= 6;
			}else{
				newTop = -$(content).getSize().size.y + 2;
			}
			
			if($('peoplelist')){
				newTop += 2;
			}
			
			if(window.webkit){
				if($('jobList')){
					newTop += 1;
				}else{
					if(!$('peoplelist')){
						newTop -= 12;
					}
				}
			}
		}
		
		scrollBar = new Element('div', {'id': content.id+'_scrollbar', 'class': "scrollbar" ,'styles': {'height': newHeight, 'margin-top': newTop}});
		scrollKnob = new Element('div', {
									'id': content.id+'_knob',
									'class' : "knob"
								});
		
		scrollKnob.injectInside(scrollBar);
		scrollBar.injectAfter(content);
		$(content.id+'_scrollableCopy').setStyle('top',0);

		scrollKnob.copy = content.id+'_scrollableCopy';
		
		ticked = false;
		// set up slide
		var mySlide = new Slider(scrollBar, scrollKnob, {
			steps: 200,
			mode: 'vertical',
			onTick: function(step){
				ticked = true;
				this.knob.effect('top', {duration: 600}).start(step);
			},
			onComplete: function(step){
				ticked = false;
			},
			onChange: function(step){
				// change top of content
				elHeight = $(this.knob.copy).getSize().size.y - $(content).getSize().size.y + 50;
					
				// devide by two because there are 200 steps
				percent = (step/2)/100;
				newTop = Math.round(-(elHeight * percent));
				//$('knob').setHTML("ticked: " + ticked);
				
				if(ticked){
					$(this.knob.copy).effect('top', {duration: 600}).start(newTop);
				}else{
					$(this.knob.copy).setStyle('top', newTop);
				}
			}
		}).set(0);
		
		if(window.ie6){
			$(scrollKnob).addEvent('mouseenter', function(event){
				this.addClass('hover');
			});
			
			$(scrollKnob).addEvent('mouseleave', function(event){
				this.removeClass('hover');
			});
		}
	}
}

/****************************
	PROJET PAGE
****************************/
var currentPhoto = 1;

function swapPhotos(newPhoto){

	if(newPhoto != currentPhoto){
		// disable current thumb & photo
		$('thumb_'+currentPhoto).removeClass('active');
		var div = $('photo_'+currentPhoto);
		new Fx.Style(div, 'opacity', {duration: 600} ).start(0);
		
		currentPhoto = newPhoto;
		
		// turn on new current thumb & photo
		$('thumb_'+currentPhoto).addClass('active');
		var div = $('photo_'+currentPhoto).setStyles({
			opacity: 0,
			display:'block'
		});
		new Fx.Style(div, 'opacity', {duration: 600} ).start(1);
	}
}

/****************************
	MOODALBOX PAGES
****************************/

function initClientList(url){
	MOOdalBox.open( // case matters
		url, // the link URL
		"", // the caption (link's title) - can be blank
		"722 406" // width and height of the box - can be left blank
		);
}

function initProjQuotes(url){
	MOOdalBox.open( // case matters
		url, // the link URL
		"", // the caption (link's title) - can be blank
		"598 248" // width and height of the box - can be left blank
		);
}

function initVideo(url,w,h){
	MOOdalBox.open( // case matters
		url, // the link URL
		"", // the caption (link's title) - can be blank
		w+" "+(parseInt(h) + 57) // width and height of the box - can be left blank
		);
}

var currentQuote = 1;
function prevQuote(){
	newInactive = currentQuote;
	newQuote = currentQuote - 1;
	
	if(currentQuote - 1 > 0 && $('quote_'+newQuote)){
		$('quote_'+currentQuote).addClass('inactive');
		currentQuote--;
		$('quote_'+currentQuote).removeClass('inactive');
	}
	
	return false;
}

function nextQuote(){
	newInactive = currentQuote;
	newQuote = currentQuote + 1;
	
	if(currentQuote + 1 < 4 && $('quote_'+newQuote)){
		$('quote_'+currentQuote).addClass('inactive');
		currentQuote++;
		$('quote_'+currentQuote).removeClass('inactive');
	}
	
	return false;
}

/****************************
	OUR PEOPLE GRID/LIST CONTROLS
****************************/

var currentPeopleFilter = "all";
var currentPerson = null;

function setupPeopleControls(){

	$$('#peopleControls input').each(function(el){
		el.addEvent(
			'click',
			function(){
				if(this.value == "all"){
					toggleAllFilters(this.checked);
				}else{
					toggleFilter(this.value, this.checked);
				}
				
				// swfaddress
				currentPeopleFilter = this.title;
				
				if(personFilterOnLoad){
					personFilterOnLoad = null;
				}else{
					SWFAddress.setValue(this.title);
				}
			}
		);
	});
	
	// Check anchor links to see if we need to init any onload
	initFilter = null;
	
	if(personFilterOnLoad){
		initFilter = "#" + personFilterOnLoad;
	}else if(window.location.hash){
		initFilter = window.location.hash;
	}
	
	if(initFilter){
	
		switch(initFilter){
			
			case "#leadership":
					$('tag_1').click();
				break;
			case "#architectural":
					$('tag_2').click();
				break;
			case "#interior":
					$('tag_3').click();
				break;
			case "#landscape":
					$('tag_4').click();
				break;
			case "#sustainable":
					$('tag_5').click();
				break;
			case "#strategic":
					$('tag_6').click();
				break;
			case "#health":
					$('tag_7').click();
				break;
			case "#learning":
					$('tag_8').click();
				break;
			case "#work":
					$('tag_9').click();
				break;
			case "#play":
					$('tag_10').click();
				break;
		
		}
	}
}

var offOpacity = .3;

function toggleAllFilters(onOff){
/*	
	// Uncheck each box regardless
	for(var i = 1; i < 11; i++){
		$('tag_' + i).checked = false;
		
		// Clear Active Filters repository
		//$('peopleControls').removeClass('tag_' + i);
	}
	*/
	// Turn On/Off Each Person
	$$('#peoplelist li, #peoplelist img').each(function(el){
		if(onOff){
			//$(el).setOpacity(1);
			new Fx.Style(el, 'opacity', {duration: 600}).start(1);
		}else{
			//$(el).setOpacity(offOpacity);
			new Fx.Style(el, 'opacity', {duration: 600}).start(offOpacity);
		}
	});
}

function toggleFilter(filter, onOff){
	
	// turn off all regardless
	$('allInput').checked = false;

	totalFiltersOff = 0;

	for(var i = 1; i < 11; i++){
		if(!$('tag_' + i).checked){
			totalFiltersOff++
		}
	}
	
	if(totalFiltersOff == 10){
		// all filters are off, so show everyone
		$('allInput').checked = true;
		toggleAllFilters($('allInput').checked);
	}else{
		$$('#peoplelist li, #peoplelist img').each(function(el){
	
			if($(el).hasClass(filter) && onOff){
				//$(el).setOpacity(1);
				new Fx.Style(el, 'opacity', {duration: 600}).start(1);
			}else{
			
				// check to make sure it doesn't have any of the other active filter			
				var active = false;
				
				for(var i = 1; i < 11; i++){
					if($('tag_' + i).checked){
						if($(el).hasClass('tag_' + i)){
							active = true;
						}
					}
				}
				
				if(active){
					//$(el).setOpacity(1);
					new Fx.Style(el, 'opacity', {duration: 600}).start(1);
				}else{
					//$(el).setOpacity(offOpacity);
					new Fx.Style(el, 'opacity', {duration: 600}).start(offOpacity);
				}
			}
		});	
	}
}

function initBio(url){

	currentPerson = url;
	
	// swfaddress
	if(personBioOnLoad){
		personBioOnLoad = null;
	}else if(currentPeopleFilter){
		SWFAddress.setValue(currentPeopleFilter + "/" + currentPerson);
	}else{
		SWFAddress.setValue(currentPerson);
	}
	
	MOOdalBox.open( // case matters
		"/our-people/bio/"+url, // the link URL
		"", // the caption (link's title) - can be blank
		"722 406" // width and height of the box - can be left blank
		);
}

function setupBio(){
	setupScrollBar($('bioDescription'));
	
	if(personBioSectionOnLoad){
		elToDisplay = personBioSectionOnLoad;
		personBioSectionOnLoad = null;
	}else{
		elToDisplay = -1;
	}
	
	if($('expertiseCategories')){
		var accordion = new Accordion('h5', '.category', {
			opacity: false,
			alwaysHide: true,
			show: elToDisplay.toInt(),
			onActive: function(toggler, element){
				SWFAddress.setValue(currentPeopleFilter + "/" + currentPerson + "/" + toggler.getProperty('index'));
				toggler.addClass('active');
				if($('quote_' + toggler.id)){
					$('quote_' + toggler.id).addClass('activeQuote');
				}
			},
		 
			onBackground: function(toggler, element){
				toggler.removeClass('active');
				if($('quote_' + toggler.id)){
					$('quote_' + toggler.id).removeClass('activeQuote');
				}
			}
		}, $('expertiseCategories'));
		
		// Set up scroll bars
		$$('.categoryExpertise').each(function(el){
			setupScrollBar($(el.id));
		});
	}
	
}

function signup() {
	var visible = false;
	
	$('emailSignup').onclick = function() {
		$('emailForm').setOpacity(1);
		if (visible == false) {
			$('emailForm').setStyle('display','block');
			visible = true;
		} else {
			$('emailForm').setStyle('display','none');
			visible = false;
		}	
		return false;
	}
	
	// clear 'Enter email address' when user clicks box
	$('emailCM').onclick = function() { this.value = ''; }
	
	// submit to campaign monitor
	$('emailSubmit').onclick = function() {
		var emailaddress = $('emailCM').value;
		var req = new Ajax('/inc/p/campaignmonitor.php',{
			data: {email:emailaddress},
			method: 'post',
			onComplete:function() {
				$('emailForm').setHTML('<h3>Thank You</h3>');
				window.setTimeout(function() {
					//$('emailForm').setStyle('display','none');
					new Fx.Style('emailForm', 'opacity', {duration: 600}).start(0);
					window.setTimeout(function() {
						$('emailForm').setHTML('<input type="text" name="email" value="Enter your email address" id="emailCM" /><br /><a id="emailSubmit" href="#">Submit</a>');
					},650);
					visible = false;
				},1000);
			}
		}).request();
		
		return false;
	}
	
}

function lightboxCallFromFlash(imagepath) {
	Lightbox.show(imagepath);
	/*
		MOOdalBox.open( // case matters
			"/our-people/bio/", // the link URL
			"", // the caption (link's title) - can be blank
			"722 406" // width and height of the box - can be left blank
		);
	*/
}
