/* main js file for dms site */

var currNav = ''; /* current state of the nav */
var currPerson = ''; /* on the people page, which person is in view? */
var currPView = '';

var currImage = 1;
var totalImages = 1;

var projectTypes = ['allProjects','residential', 'commercial', 'mixed', 'green'];

function openNav(whichOne) {

	if(whichOne == 'people'){ // open the people nav
		
		active(null,'projects');
		$("#sPeople").show('blind',{  direction: "vertical" },'fast');
		if(currNav != ''){
			$("#sProjects").hide('blind',{  direction: "vertical" },'fast');
		}
		currNav = 'people';	
		$('#people').addClass("subNavActive");
		$('#projects').removeClass("subNavActive");
	
	} else if(whichOne == 'projects') { // open the projects nav
		
		active(null,'projects');
		$("#sProjects").show('blind',{  direction: "vertical" },'fast');
		if(currNav != ''){
			$("#sPeople").hide('blind',{  direction: "vertical" },'fast');
		}
		currNav = 'projects';
		$('#projects').addClass("subNavActive");
		$('#people').removeClass("subNavActive");
		
	} else { // close them both
		currNav = '';
		active(null,'projects');
		active(null,'people');
		$("#sProjects").hide('blind',{  direction: "vertical" },'fast');
		$("#sPeople").hide('blind',{  direction: "vertical" },'fast');	
		$('#people').removeClass("subNavActive");
		$('#projects').removeClass("subNavActive");	
	}
}


function active(whichOne, context){ // user has clicked on something in the main nav
	$('#people').removeClass("subNavActive");
	$('#projects').removeClass("subNavActive");	
	$('#green').removeClass("subNavActive");
	if(context == 'people'){
		$('#sPeople').find('li').removeClass('subNavActive'); // remove red from nav item
	
		if(currPerson != ''){
			// clean up people
			$('#peoplePages').find('div').fadeOut('fast');	
		
		} else {
			// clean up intro people page
			$("#peoplePeople").fadeOut('fast');	
			$("#peopleT1").fadeOut('fast');	
			$("#peopleT2").fadeOut('fast');	
			$("#tSelector").fadeOut('fast');
			$("#glassesHolder").fadeOut('fast');	
			$("#glassesCopy").fadeOut('fast');
			hideGlasses();
		}
		
		// find out which one was clicked
		var tempString = whichOne.context.id;
		tempString = tempString.substr(1);
		currPerson = tempString;
		
		SWFAddress.setValue(currPerson);
		_gaq.push(['_trackPageview', '/people/' + currPerson]); 
		
			
	} else if(context == 'projects') {
		$('#sProjects').find('li').removeClass('subNavActive');
		
		if(currPView != ''){
			// clean all projects
			$("#residential").fadeOut('fast');	
			$("#commercial").fadeOut('fast');	
			$("#green").fadeOut('fast');
			$("#mixed").fadeOut('fast');
			$("#allProjects").fadeOut('fast');
			
		} 
		
		if(whichOne != null){
			var tempString = whichOne.attr("id");
			tempString = tempString.substr(2);
			currPView = tempString;
			
			SWFAddress.setValue(projectTypes[currPView]);
			_gaq.push(['_trackPageview', '/projects/' + projectTypes[currPView]]); 
		
			
		
		}
	}

	$(whichOne).addClass("subNavActive"); // if possible, set the clicked item to red
}


function sendMailTo(name, company, domain) {
// dynamically hide email addresses
      locationstring = 'mai' + 'lto:' + name + '@' + company + '.' + domain;
      window.location.replace(locationstring);
}

function cleanTurtle(){
// cleans up the turtleneck ui
	$('#b0').removeClass('tSquareOn');
	$('#b1').removeClass('tSquareOn');
	$('#b2').removeClass('tSquareOn');
	
	if(activeTurtle == '0'){
		setTimeout(function(){$('#peopleT2').fadeOut('slow');}, 500);
	}
	
	if(activeTurtle == '1'){
		setTimeout(function(){$('#peopleT1').fadeOut('slow');}, 500);
	}
	
}

function resetProjects() {
	$("#greenCopy").fadeOut('fast');		
	$("#residential").fadeOut('fast');	
	$("#commercial").fadeOut('fast');	
	$("#green").fadeOut('fast');
	$("#mixed").fadeOut('fast');
	$("#allProjects").fadeIn('slow');
	
	if(thisPage == 'projects'){
		currNav = 'projects';
		$("#projectsCopy").fadeIn('fast');
		$('#projects').addClass("subNavActive");
	} else {
		currNav = 'green';
		$("#greenCopy").fadeIn('fast');
		$('#projects').removeClass("subNavActive");
	}
	

	$('#sProjects').find('li').removeClass("subNavActive");

	$('#detailCopy').fadeOut('fast');
	$('#detailLarge').fadeOut('fast');
	$('#goBack').fadeOut('fast');
	$('#picNav').fadeOut('fast');
	$('#detailSmall').fadeOut('fast');
	currImage = 1;
	SWFAddress.setValue('/');
	
}

function resetPeople(){
// fires if the user has viewed a 'person' page, then clicked back to the main people page

	$('#peoplePages').find('div').fadeOut('fast');		
	$('#sPeople').find('li').removeClass("subNavActive");
	
	currNav = 'people';	
	currPerson = '';
	$('#people').addClass("subNavActive");

	// clean up intro people page
	$("#peoplePeople").fadeIn('slow');	
	$("#tSelector").fadeIn('slow');
	$("#glassesHolder").fadeIn('slow');	
	$("#glassesCopy").fadeIn('slow');
	
	// reset the turtleneck state
	$('#b0').removeClass('tSquareOn');
	$('#b1').removeClass('tSquareOn');
	$('#b2').addClass('tSquareOn');
	
	showGlasses();
	
	SWFAddress.setValue('/');
	
}

function tActive(whichOne){
// lets the user change out the turtleneck on the people page
	
	if('#' + whichOne.context.id == $('#t0').selector){
		if(activeTurtle == '0'){
			return;
		} else {
			cleanTurtle();
			activeTurtle = '0';
			$('#b0').addClass('tSquareOn');	
			$('#peopleT2').fadeIn('fast');
		}
	
	} else if ('#' + whichOne.context.id == $('#t1').selector){
		if(activeTurtle == '1'){
			return;
		} else {
			cleanTurtle();
			activeTurtle = '1';
			$('#b1').addClass('tSquareOn');	
			$('#peopleT1').fadeIn('fast');
		}
	
	
	} else if ('#' + whichOne.context.id == $('#t2').selector){
		if(activeTurtle == '2'){
			return;
		} else {
			cleanTurtle();
			activeTurtle = '2';
			
			$('#b2').addClass('tSquareOn');	
		}
	}
	
	_gaq.push(['_trackPageview', '/people/turtleneck']); 
	
}


function handleArrows(whichIndex, totalProjects){
	// handles user interaction with the project detail thumbnails and nav arrows
	totalProjects = parseInt(totalProjects);

	if(whichIndex == 1){
		$('#leftArrow').animate({opacity:.2}, 'fast');	
		$('#leftArrow').css('cursor','default');
	} else {
		$('#leftArrow').animate({opacity:1}, 'fast');
		$('#leftArrow').css('cursor','pointer');	
	}
	
	if(whichIndex == totalProjects){
		$('#rightArrow').animate({opacity:.2}, 'fast');	
		$('#rightArrow').css('cursor','default');	
	} else {
		$('#rightArrow').animate({opacity:1}, 'fast');	
		$('#rightArrow').css('cursor','pointer');	
	}
	
	_gaq.push(['_trackPageview', '/projects/arrows']); 
}


function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

var resetGreen = function(){
	
}


$(document).ready(function() {
    //init nav
	
	$("#sProjects").fadeOut(0);
	$("#sPeople").fadeOut(0);
	
	$('#projects').click(function() {
  		if(currNav != 'projects'){	
 			window.location.href = 'projects.php';
 		} else {
			_gaq.push(['_trackPageview', '/projects/reset']); 
			resetProjects();	
		}
 	});
 	
 	$('#people').click(function() {
  		if(currNav != 'people'){
  			window.location.href = 'people.php';
 		} else {
			_gaq.push(['_trackPageview', '/people/reset']); 
 			resetPeople();
 		}
 	});
	
	
	$('#greenNav').click(function() {
  		if(currNav != 'green'){
  			window.location.href = 'green.php';
 		} else {
			_gaq.push(['_trackPageview', '/green/reset']); 
 			resetProjects();
 		}
 	});
 	
	// add click events to each of the sub menu items in 'people' 	
 	$('#sPeople').find('li').click(function() {
		active($(this), 'people');
 	});
 	
 	// add click events to each of the sub menu items in 'projects' 	
 	$('#sProjects').find('li').click(function() {
		active($(this), 'projects');
 	});
 	
 	if(thisPage == 'people'){
    	openNav('people');

    	activeTurtle = '2';
    	
		$('#tSelector').find('li').click(function() {
			tActive($(this));
 		});
 		
 		$('#b2').addClass('tSquareOn');	
 		    	
    	if(SWFAddress.getValue() != '/'){
			tempString = SWFAddress.getPath().substr(1);
			currPerson = tempString;
    	}
		
		// clean up glasses for ie
		
		if($.browser.msie){
			for(g=0; g<18; g++){
				var ieGlasses = '<img src="i/peopleImages/glasses/' + g + 'ie.png" />';
				$('#container').find('#g'+g).html(ieGlasses);
			}
		}
    }
 	

	if(thisPage == 'projects'){
		openNav('projects');
    	
    	$('#goBack').click(function() {     	
    		SWFAddress.setValue(SWFAddress.getPath());
    	})
    
		$('.pTable').find('.tableElement').mouseover(function () {
			$(this).addClass('tableElementOver');
			 $(this).animate({opacity: .5}, 'slow');	
		});
	
		$('.pTable').find('.tableElement').mouseout(function () {
			$(this).removeClass('tableElementOver');
			$(this).animate({opacity: 1}, 'fast');	
		});
		
		$('.pTable').find('.pText').click(function () {
			// user clicked on a thumbnail. show the detail view of the project
			newId = $(this).context.id.substring(7); // we only care about the number
			newSwfAddy = SWFAddress.getPath();
			newSwfAddy += '?detail=' + newId;
			SWFAddress.setValue(newSwfAddy);
			_gaq.push(['_trackPageview', '/projects/' + newSwfAddy ]); 

		});
		
		
		if(SWFAddress.getValue() != '/'){
			tempString = SWFAddress.getPath().substring(1);
			currPView = tempString;
    	}

	}
	
	
	if(thisPage == 'green'){
    	//openNav('projects');
    
		$('#greenNav').addClass("subNavActive");
		
    	$('#goBack').click(function() {     	
    		SWFAddress.setValue(SWFAddress.getPath());
    	})
    
		$('.pTable').find('.tableElement').mouseover(function () {
			$(this).addClass('tableElementOver');
			$(this).animate({opacity: .5}, 'slow');	
		});
	
		$('.pTable').find('.tableElement').mouseout(function () {
			$(this).removeClass('tableElementOver');
			 $(this).animate({opacity: 1}, 'fast');	
		});
		
		$('.pTable').find('.pText').click(function () {
			// user clicked on a thumbnail. show the detail view of the project
			newId = $(this).context.id.substring(7); // we only care about the number
			newSwfAddy = SWFAddress.getPath();
			newSwfAddy += '?detail=' + newId;
			SWFAddress.setValue(newSwfAddy);
			_gaq.push(['_trackPageview', '/green/' + newSwfAddy ]); 

		});
		
		
		if(SWFAddress.getValue() != '/'){
			tempString = SWFAddress.getPath().substring(1);
			currPView = tempString;
    	}
	}
	
	
	
	$(document).pngFix(); 
	
	// swfaddress stuff
	var handleChange = function(event) {
		if(thisPage == 'people'){
		// handle people page updates
		
			if(SWFAddress.getPath() != '/'){
				$('#peoplePages').find('div').fadeOut('fast');		
				$('#sPeople').find('li').removeClass("subNavActive");
	
				$("#peoplePeople").fadeOut('fast');	
				$("#peopleT1").fadeOut('fast');	
				$("#peopleT2").fadeOut('fast');	
				$("#tSelector").fadeOut('fast');
				$("#glassesHolder").fadeOut('fast');	
				$("#glassesCopy").fadeOut('fast');
				$('#people').removeClass("subNavActive");
				hideGlasses();
				
				tempString = SWFAddress.getPath().substr(1);
				$('#p' + tempString).fadeIn('slow');	
				$('#p' + tempString).find('div').fadeIn('slow');
				$('#s' + tempString).addClass("subNavActive");	
				currPerson = tempString;
				SWFAddress.setTitle('DMS Architects - People - ' + peopleArray[tempString].name);
			
			
			} else {
				resetPeople();
				SWFAddress.setTitle('DMS Architects - People');
			}
			
		
		} else {
		
		// handle project page updates
		if(SWFAddress.getQueryString() == undefined){
			if(SWFAddress.getPath() != '/'){
	    		// we're on a detail view, so we need to move some stuff around
	    		$('#detailCopy').fadeOut('fast');
	    		$('#detailLarge').fadeOut('fast');
	    		$('#goBack').fadeOut('fast');
				$('#picNav').fadeOut('fast');
				$("#greenCopy").fadeOut('fast');		
				$("#residential").fadeOut('fast');	
				$("#commercial").fadeOut('fast');	
				$("#green").fadeOut('fast');
				$("#mixed").fadeOut('fast');
				$("#allProjects").fadeOut('fast');
				$("#projectsCopy").fadeOut('fast');
				$('#sProjects').find('li').removeClass("subNavActive");
				$('#projects').removeClass("subNavActive");
				$('#detailSmall').fadeOut('fast');
				
			
					
				tempString = SWFAddress.getPath().substring(1);
				$('#'+tempString).fadeIn('slow');	
				
				if(thisPage == "projects"){				
					SWFAddress.setTitle('DMS Architects - Projects - ' + toTitleCase(tempString));
				} else {
					SWFAddress.setTitle('DMS Architects - Green - ' + toTitleCase(tempString));
				}
				
				theIndex = '';
				
				for(z = 1; z<projectTypes.length; z++){
					if(tempString == projectTypes[z]){
						theIndex = z;
					}
				}
				
				
				
				if(tempString == 'green'){ // green section has its own copy
					$("#greenCopy").fadeIn('slow');
				} else {
					$("#projectsCopy").fadeIn('slow');
					$('#pr' + theIndex).addClass("subNavActive");	
				}
				currPView = tempString;
    		} else {
    			resetProjects();
    			if(thisPage == "projects"){				
					SWFAddress.setTitle('DMS Architects - Projects');
				} else {
					SWFAddress.setTitle('DMS Architects - Green');
				}
			}		
	   		
   		} else {
   		   		
   			whichProject = SWFAddress.getQueryString().substring(7);
			
			if(thisPage == "projects"){				
					SWFAddress.setTitle('DMS Architects - Projects - ' + projectArray[whichProject].name);
				} else {
					SWFAddress.setTitle('DMS Architects - Green - ' + projectArray[whichProject].name);
			}
			
			var imagePath = "";
			
			if(thisPage == "projects"){		
				imagePath = "i/projectImages/";
			} else {
				imagePath = "i/greenImages/";
			}
   			
   			
   			$("#allProjects").fadeOut('fast');
			$("#residential").fadeOut('fast');	
			$("#commercial").fadeOut('fast');	
			$("#green").fadeOut('fast');
			$("#mixed").fadeOut('fast');
   			$('#detailCopy').fadeIn('slow');
   			$('#detailLarge').fadeIn('slow');
   			$('#goBack').fadeIn('slow');
			$('#picNav').fadeIn('slow');
			$('#detailSmall').fadeIn('slow');
			
			smallImages = '';
			for(s=0; s<projectArray[whichProject].detailImageCount; s++){

			smallImages += '<div class="thumbWrapper"><img id="t' + s + '" src=" ' + imagePath + 'thumb/th_' + projectArray[whichProject].detailImagePrefix + '_' + s + '.jpg" class="thumb" /></div>';
						
				
					
			}
			
			$('#picNumber').html('1/' + projectArray[whichProject].detailImageCount);
			handleArrows(1,projectArray[whichProject].detailImageCount);
			
			currImage = 1;
			totalImages = parseInt(projectArray[whichProject].detailImageCount);
			
   			// dump in the copy for the project
			$('#detailCopy').html('<b>'+projectArray[whichProject].name + '</b><br /><br />' + projectArray[whichProject].copy);	 
			// set up the first large image
			
			$('#detailLarge').html('<img src="' + imagePath + 'details/' + projectArray[whichProject].detailImagePrefix + '_0.jpg" id="bigImage" />');  
			
			
			// dump in the thumbnail images into the small image div
			$('#detailSmall').html(smallImages);
			
			// make the first item 'hot'
			$("#detailSmall").find("div:first-child").find("img").addClass('thumbOn');
			 
			// nice rollover effects for the thumbnail images
			$("#detailSmall").find("img").mouseover(function(){
				 $(this).animate({opacity: .5}, 'slow');		
			});
			
			$("#detailSmall").find("img").mouseout(function(){
				  $(this).animate({opacity: 1}, 'fast');			 
			});
			
			$('#rightArrow').unbind('click');
			$('#leftArrow').unbind('click');
			
			$('#rightArrow').click(function(){
				if(currImage < totalImages){
					$('#detailLarge').html('<img src="' + imagePath + 'details/' + projectArray[whichProject].detailImagePrefix + '_' +  currImage + '.jpg" id="bigImage" />');
					$("#detailSmall").find("img").removeClass('thumbOn');
					$("#detailSmall").find("#t"+currImage).addClass('thumbOn');
					currImage += 1;		
					$('#picNumber').html(currImage + '/' + projectArray[whichProject].detailImageCount);
					handleArrows(currImage, projectArray[whichProject].detailImageCount);			
				} else {
					return;	
				}
			});
			
			$('#leftArrow').click(function(){
				if(currImage > 1){
					currImage -= 2;	
					$('#detailLarge').html('<img src="' + imagePath + 'details/' + projectArray[whichProject].detailImagePrefix + '_' +  currImage + '.jpg" id="bigImage" />');
					$("#detailSmall").find("img").removeClass('thumbOn');
					$("#detailSmall").find("#t"+currImage).addClass('thumbOn');
					
					currImage += 1;		
					$('#picNumber').html(currImage + '/' + projectArray[whichProject].detailImageCount);
					handleArrows(currImage, projectArray[whichProject].detailImageCount);	
							
				} else {
					return;	
				}
			});
			
			
			
			$("#detailSmall").find("img").click(function(){
				$("#detailSmall").find("img").removeClass('thumbOn');
				$(this).addClass('thumbOn');
				
				var imgClick = $(this).context.id.substring(1);
				var displayClick = parseInt($(this).context.id.substring(1)) + 1;
				$('#detailLarge').html('<img src="' + imagePath + 'details/' + projectArray[whichProject].detailImagePrefix + '_' +  imgClick + '.jpg" id="bigImage" />');
				
				$('#picNumber').html(displayClick + '/' + projectArray[whichProject].detailImageCount);
				handleArrows(displayClick, projectArray[whichProject].detailImageCount);
				currImage = displayClick;
				_gaq.push(['_trackPageview', '/projects/thumbnailClick' ]); 

				
			});
			


			if(SWFAddress.getPath() != '/'){
				tempString = SWFAddress.getPath().substring(1);
				theIndex = '';
			
				for(z = 1; z<projectTypes.length; z++){
					if(tempString == projectTypes[z]){
						theIndex = z;
					}
				}
				$('#projects').removeClass("subNavActive");
				
				if(thisPage == "projects"){				
					$('#pr' + theIndex).addClass("subNavActive");
				} 
					
					
   				}
		
   			}
		
		}

	}
	
	if((thisPage == 'projects')||(thisPage == 'people')||(thisPage == 'green')){
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);	
	}
	
 	
 	// debug. disable for production
	$(document).ajaxError(function(){
    	if (window.console && window.console.error) {
    	    console.error(arguments);
    	}
	});
});












