/* 
	Developed by:
		Duane Comeaux
	Created:
		03-12-2009
	Last modified:
		10-15-2009
	Requires:
		jQuery 1.3.2 or newer
		jQuery History plugin
*/

var dataSrc = '';
var page = '';
var activate_gallery = false;
var recentHash = "";
var thumbH = 64;	// Includes margins
var thumbs_displayed = 5;
var descrp_array = new Array();
var heading_array = new Array();
var lg_image_path = 'images/projects/';
var page_name = '';
var current_hash = '';

function changeTitle()
{
	var hash = window.location.hash;
	//var page = hash.split('-');
	
	//alert(hash);
	if( /company/.test(hash) )
		document.title = 'Macgregor Associates Architects - Company';
	if( /contact/.test(hash) )
		document.title = 'Macgregor Associates Architects - Contact Us';
	if( /services/.test(hash) )
		document.title = 'Macgregor Associates Architects - Services';
	if( /community/.test(hash) )
		document.title = 'Macgregor Associates Architects - Community';
	if( /careers/.test(hash) )
		document.title = 'Macgregor Associates Architects - Careers';
	if( /projects/.test(hash) )
		document.title = 'Macgregor Associates Architects - Projects';
	if( /home/.test(hash) )
		document.title = 'Macgregor Associates Architects';
	if( hash == '' )
		document.title = 'Macgregor Associates Architects';
	if( hash == ' ' )
		document.title = 'Macgregor Associates Architects';
}

$(document).ready(function()
{  
	changeTitle();
	$.history.init(pageLoad);
   /* $("a[rel='history']").click(function(){
    	heading_array = new Array();
        $.history.load(this.href.replace(/^.*#/, ''));
        return false;
    });
    */
});

function SO()
{
	 swfobject.embedSWF("flash/maa_flash.swf", "homeFlashContent", "750", "500", "9.0.0", "flash/expressInstall.swf");
}

var links = {
	enable: function()
		{
			
			var parentObj = this;			
			$("a[rel='history']").bind('click', function(){
				var hash = this.href.replace(/^.*#/, '');
				heading_array = new Array();
				$.history.load(hash);
				parentObj.disable();
				return false;
			});
		},
	disable: function()
		{
			$("a[rel='history']").unbind('click');
		}
};

function pageLoad(hash){
	
	descrp_array = new Array();
	page_name = hash;
	$('#testimonial').hide();
			
	if( /projects-/.test(hash) ){
		activate_gallery = true;
	}else{
		activate_gallery = false;
	}
	
	links.enable();
	
	resetLinkStates();	
	setLinkStates(hash);
	switchTestimonial(hash);
	
	switch(hash){
		case ''	:	$('#testimonial').show();	
		$('.company-options').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow');
		getURL(hash);	rotateBanner('home');	break;
		
		case 'home'	:	$('#testimonial').show();	
		//hash = '';
		$('.company-options').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow');
		getURL(hash);	rotateBanner('home');	break;
		
		case 'company'	:	$('#testimonial').show();
		$('.company-options').show('slow'); 
		$('.company-options-services').hide('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow');  showLoading(true); 
		getURL(hash);	rotateBanner('company');	break;
		
		case 'company-services'	:	$('#testimonial').show();
		$('.company-options').show('slow'); 
		//$('.company-options-services').show('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true); 
		getURL(hash);		rotateBanner('company');		break;
		
	/*	case 'company-services-master-planning'	:	
		$('.company-options').show('slow');	
		$('.company-options-services').show('slow'); 
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true); 
		getURL(hash);	rotateBanner('company');	break;
		
		case 'company-services-architecture'	:	
		$('.company-options').show('slow');	
		$('.company-options-services').show('slow'); 
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); activate_gallery = true; showLoading(true); 
		getURL(hash);	rotateBanner('company');	break;
		case 'company-services-interiors'	:	
		$('.company-options').show('slow');	
		$('.company-options-services').show('slow'); 
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true); 
		getURL(hash);	rotateBanner('company');	break;
	*/
		case 'company-careers'	:	
		$('.company-options').show('slow'); 
		$('.company-options-services').hide('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true); 
		getURL(hash);		rotateBanner('company');		break;
		
		case 'company-community'	:	$('#testimonial').show();
		$('.company-options').show('slow'); 
		$('.company-options-services').hide('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true);  
		getURL(hash);		rotateBanner('company');		break;
		
		case 'contacts'	:	
		$('.company-options').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.projects-options').hide('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true); 
		getURL(hash);	rotateBanner('contacts');	break;
		
		case 'projects'	:	$('#testimonial').show();
		$('.projects-options').show('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');	
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); showLoading(true); 
		getURL(hash);	rotateBanner('projects');	break;
		
		case 'projects-distribution'	:	
		$('.projects-options').show('slow'); 
		$('.projects-options-distribution').show('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow'); 
		activate_gallery = true; showLoading(true); getURL('projects-distribution-spec-distribution'); rotateBanner('projects');	break;
		
		case 'projects-office'	:	
		$('.projects-options').show('slow'); 
		$('.projects-options-office').show('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');	
		activate_gallery = true; showLoading(true); getURL('projects-office-office'); rotateBanner('projects');	break;
		
		case 'projects-interiors'	:	
		$('.projects-options').show('slow');
		$('.projects-options-interiors').show('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');
		activate_gallery = false; showLoading(true); getURL('projects-interiors'); rotateBanner('projects');	break;
		
		case 'projects-manufacturing'	:	
		$('.projects-options').show('slow');
		$('.projects-options-manufacturing').show('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');
		activate_gallery = false; showLoading(true); getURL('projects-manufacturing'); rotateBanner('projects');	break;
	}
	if( /projects-distribution-/.test(hash) ){
		$('.projects-options').show('slow'); 
		$('.projects-options-distribution').show('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');
		showLoading(true); activate_gallery = true; getURL(hash);	rotateBanner('projects');
	}
	if( /projects-office-/.test(hash) ){ 
		$('.projects-options').show('slow');
		$('.projects-options-office').show('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');
		showLoading(true); activate_gallery = true; getURL(hash);	rotateBanner('projects');
	}
	if( /projects-interiors-/.test(hash) ){ 
		$('.projects-options').show('slow');
		$('.projects-options-interiors').show('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-manufacturing').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');
		showLoading(true); activate_gallery = true; getURL(hash);	rotateBanner('projects');
	}
	if( /projects-manufacturing-/.test(hash) ){
		$('.projects-options').show('slow');
		$('.projects-options-manufacturing').show('slow'); 
		$('.projects-options-distribution').hide('slow'); 
		$('.projects-options-office').hide('slow'); 
		$('.projects-options-interiors').hide('slow'); 
		$('.company-options-services').hide('slow');	
		$('.company-options').hide('slow');
		showLoading(true); activate_gallery = true; getURL(hash);	rotateBanner('projects');
	}
}

function switchTestimonial(p){
	switch(p){
		case '': var html = '<p>"Macgregor Associates Architects have been and continue to be essential to IDI\'s success. The firm\'s high-quality design and attention to detail have time and again provided IDI a strategic edge, producing Class-A facilities that provide IDI a competitive edge. Macgregor Associates\' commitment to service is unsurpassed and has never failed to impress us and our customers. In my 20 years of experience with the firm, Macgregor Associates has always exceeded expectations."</p><br><br><p><span class="green bold">Greg Gregory</span><br><span>Founder</span><br><span class="orange">IDI</span></p>';
		break;
		
		case 'home': var html = '<p>"Macgregor Associates Architects have been and continue to be essential to IDI\'s success. The firm\'s high-quality design and attention to detail have time and again provided IDI a strategic edge, producing Class-A facilities that provide IDI a competitive edge. Macgregor Associates\' commitment to service is unsurpassed and has never failed to impress us and our customers. In my 20 years of experience with the firm, Macgregor Associates has always exceeded expectations."</p><br><br><p><span class="green bold">Greg Gregory</span><br><span>Founder</span><br><span class="orange">IDI</span></p>';
		break;
		
		case 'company': var html = '<p>"MAA has continued to provide world class architectural design work and critical space planning counsel for our rapidly growing Division. Their ability to respond quickly with solid design deliverables, at a fair price, has made them an integral value added vendor. I would highly recommend that you meet with MAA before you finalize your design team or design plans."</p><br><br><p><span class="green bold">John Gabilondo</span><br><span class="orange" >Mitsubishi</span></p>';
		break;
		
		case 'company-services' : var html = '<p>"Macgregor Associates understands the developer\'s competing challenges. They repeatedly deliver user friendly designs that respect the project budget."</p><br><br><p><span class="green bold">Vince Smith</span><br><span>President</span><br><span class="orange" >Corinthian Properties, LLC</span></p>';
		break;
		
		case 'company-community' : var html = '<p>"This building reflects the pride residents take in their work, which produces quality service to Baddour Custom Packaging customers. It is a place of learning-social- and vocational-skill building-for residents, a place where the mission of The Baddour Center is fulfilled."</p><br><br><p><span class="green bold">Parke Pepper</span><br><span>Executive Director</span><br><span class="orange" >The Baddour Center</span></p>';
		break;
		
		case 'projects' : var html = '<p>"Their designs accomplished our goals and were both built on time and in budget. In both projects, there were no surprises, not one! We interviewed a number of firms; Macgregor Associates was head and shoulders above the rest."</p><br><br><p><span class="green bold">David A. Walker</span><br><span>Sr Executive VP</span><br><span class="orange" >Pier 1 Imports</span></p>';
		break;
		
		default: var html = '';
	}
	$('#testimonial').html('<div class="content-header" ><h3>Testimonial</h3></div>' + html);
}

function rotateBanner(hash){
	$('#header-image').attr('src', 'images/maa-header-' + hash + '.jpg');
}

function getURL(new_page){
	if( new_page == '' || new_page == 'home'){
		new_page = 'index';
	}
	current_hash = new_page;
	
	var textDocument = $.ajax(
	{
		type		:	'GET',
		url			:	'pages/' + new_page + '.txt',
		dataType	: 	'text',
		data		:	'sent=true',
		success		:	handleData,
		cache		:	true,
		async		:	true,
		error		:	handleError,
		complete	:	handleComplete,
		dataFilter	:	handleFilter,
		timeout		:	30000
	});
}

function handleError(XHR, error, except)
{
	$('#loader')
		.html('<div class="info">Oops! There was an error with your request.<br />Please try again. If the problem persists, contact the site administrator.</div>');
}

function handleComplete(XHR, status)
{
	//alert('Completed: ' + status);
}

function handleFilter(data, type)
{
	return data;
}

function handleData(data, status)
{	
	$('#replace-content')
		.css({textAlign: 'left'})
		.html(data);
	
	if( $('#main-flash').length )
	{
		SO();
	}
	
	showLoading(false);
	
	links.enable();
	
	if( activate_gallery == true ){
		activateGallery();
	}
	
	changeTitle();
}

function handleDescriptions(data, status)
{	
	var image = data.getElementsByTagName('image');
	$(image).each(function(index)
	{
		descrp_array.push($(this).text());
		heading_array.push($(this).attr('heading'));
	});
}

function activateGallery(){
	$('.thumb')
		.css({opacity: 0.8});
	var xmlDocument = $.ajax(
	{
		type		:	'GET',
		url			:	'pages/' + page_name + '.xml',
		dataType	: 	'xml',
		data		:	'sent=true',
		success		:	handleDescriptions,
		cache		:	true,
		async		:	true,
		error		:	handleError,
		complete	:	handleComplete,
		dataFilter	:	handleFilter,
		timeout		:	30000
	});
	
	//	Count the number of thumbs
	var counter = 0;
	var thumbs_position = 0;
	
	$('.thumb')
		.each(function(index)
		{
			counter++;
		});
		
	if( counter <= thumbs_displayed ){
		$('#arrow-bottom')
				.html('<img src="images/arrow-blank.jpg" width="42" height="7" />');
		$('#arrow-top')
			.html('<img src="images/arrow-blank.jpg" width="42" height="7" />');
	}
	$('#arrow-top')
		.html('<img src="images/arrow-blank.jpg" width="42" height="7" />')
		.addClass('disabled');
		
	var thumbs_total_height = counter * thumbH;
		
	$('#arrow-top')
		.mouseover(function()
		{
			
		})
		.click(function()
		{
			moveThumbs(1);
		})
		.mouseout(function()
		{
			
		});
		
	$('#arrow-bottom')
		.bind('mouseover', function()
		{
		
		})
		.bind('click', function()
		{
			moveThumbs(-1);
		})
		.bind('mouseout', function()
		{
		
		});
		
	$('.thumb')
		.filter('div')
			.bind('mouseover', function()
			{
				if( !$(this).hasClass('selected') ){
					$(this)
						.css({opacity: 1.0});
				}
			})
			.bind('click', function()
			{
				showImageLoading(true);
				$('.thumb')
					.removeClass('selected')
					.css({opacity: 0.8});
				$(this)
					.addClass('selected')
					.css({opacity: 1.0});
					
				var item = $(this).attr('rel');
				var item_array = item.split('-');
				var num = item_array.pop();
				var descrp = descrp_array[num - 1];
				var head = heading_array[num-1];
				
				while( descrp.indexOf('{newline}') != -1 ){
					descrp = descrp.replace('{newline}','<br />');
				}
				$('#image-description')
					.html('<p class="bold plus-one">' + head  + '</p><br><p>' + descrp + '</p>');
				
				var display_image = lg_image_path + item_array.join('_') + '/'  + num + item_array.join('_') + '.jpg';
				
				$('#display')
				.find('img:last')
					.fadeOut('fast');
					
				var img = new Image();
				$(img)
					.load(function()
					{
						$('#display')
						.find('img:last')
						.fadeIn('slow')
						.attr('src', img.src);
							
						showImageLoading(false);
					})
					.error(function()
					{
						$('#display')
						.find('img:last')
						.fadeIn('slow')
						.attr('src', 'images/error.jpg');
					})
					.attr('src',display_image);
				
			})
			.bind('mouseout', function()
			{
				if( !$(this).hasClass('selected') ){
					$(this)
						.css({opacity: 0.8});
				}
			});
			
	function moveThumbs(direction){
		if( counter <= thumbs_displayed )
			return;
			
		if( direction == -1 ){
			if( 2*thumbs_position < thumbs_total_height ){
				$('#arrow-bottom')
					.html('<a href="javascript:void(0);"><img src="images/arrow-bottom.jpg" width="42" height="7" /></a>')
					.removeClass('disabled');
					
				$('#arrow-top')
					.html('<a href="javascript:void(0);"><img src="images/arrow-top.jpg" width="42" height="7" /></a>')
					.removeClass('disabled');
					
				thumbs_position += thumbH * thumbs_displayed;
				$('#thumbs')
					.animate(
					{
						'top': '-=' + (thumbH * thumbs_displayed) + 'px'
					}, 1000, 'swing');
					
				if( 2*thumbs_position > thumbs_total_height ){
					$('#arrow-bottom')
						.html('<img src="images/arrow-blank.jpg" width="42" height="7" />')
						.addClass('diasbled');
				}
			}else{
				//	disable the arrow
				$('#arrow-bottom')
					.html('<img src="images/arrow-blank.jpg" width="42" height="7" />')
					.addClass('diasbled');
			}			
		}
		
		if( direction == 1 ){
			
			if( 2*thumbs_position > 0 ){
				$('#arrow-top')
					.html('<a href="javascript:void(0);"><img src="images/arrow-top.jpg" width="42" height="7" /></a>')
					.removeClass('disabled');
					
				$('#arrow-bottom')
					.html('<a href="javascript:void(0);"><img src="images/arrow-bottom.jpg" width="42" height="7" /></a>')
					.removeClass('disabled');
					
				thumbs_position -= thumbH * thumbs_displayed;
				$('#thumbs')
					.animate(
					{
						'top': '+=' + (thumbH * thumbs_displayed) + 'px'
					}, 1000, 'swing');
					
				if( thumbs_position == 0 ){
					$('#arrow-top')
					.html('<img src="images/arrow-blank.jpg" width="42" height="7" />')
					.addClass('disabled');
				}
			}else{
				$('#arrow-top')
					.html('<img src="images/arrow-blank.jpg" width="42" height="7" />')
					.addClass('disabled');
			}
		}
	}
}

function resetLinkStates(){
	// Main links
	$('#home')
		.removeAttr('style');
	$('#company')
		.removeAttr('style');
	$('#projects')
		.removeAttr('style');
	$('#contacts')
		.removeAttr('style');
	//	Sub Links
	$('#company-services')
		.removeAttr('style');
	$('#company-careers')
		.removeAttr('style');
	$('#company-community')
		.removeAttr('style');
	$('#projects-distribution')
		.removeAttr('style');
	$('#projects-office')
		.removeAttr('style');
	$('#projects-interiors')
		.removeAttr('style');
	$('#projects-manufacturing')
		.removeAttr('style');	
}

function setLinkStates(hash){
	//	Main Links
	if( hash == '' ){
		$('#home')
			.css({color: '#999'});
	}
	if( /company/.test(hash) ){
		$('#company')
			.css({color: '#999'});
	}
	if( /home/.test(hash) ){
		$('#home')
			.css({color: '#999'});
	}
	if( /projects/.test(hash) ){
		$('#projects')
			.css({color: '#999'});
	}
	if( /contacts/.test(hash) ){
		$('#contacts')
			.css({color: '#999'});
	}
	//	Sub Links
	if( /company-services/.test(hash) ){
		$('#company-services')
			.css({color: '#9a4b00'});
	}
	if( /company-careers/.test(hash) ){
		$('#company-careers')
			.css({color: '#9a4b00'});
	}
	if( /company-community/.test(hash) ){
		$('#company-community')
			.css({color: '#9a4b00'});
	}
	if( /projects-distribution/.test(hash) ){
		$('#projects-distribution')
			.css({color: '#9a4b00'});
	}else if( /projects-office/.test(hash) ){
		$('#projects-office')
			.css({color: '#9a4b00'});
	}else if( /projects-interiors/.test(hash) ){
		$('#projects-interiors')
			.css({color: '#9a4b00'});
	}else if( /projects-manufacturing/.test(hash) ){
		$('#projects-manufacturing')
			.css({color: '#9a4b00'});
	}
}

function showLoading(a){
	if(a){
		$('#content')
			.prepend('<div id="loading"><img src="images/ajax-loader.gif" /><br>loading...please wait!</div>');
			
		$('#loading')
			
			.css({
				position: 'absolute',
				width: '300px',
				height: '30px',
				borderTop: '2px solid #00626D',
				borderBottom: '2px solid #00626D',
				background: '#fff',
				paddingTop: '5px',
				paddingBottom: '5px',
				left: Math.round(($('#content').width() - 300)/2) + 'px',
				top: Math.round(($('#content').height() - 30)/2) + 'px',
				textAlign: 'center',
				display: 'block',
				zIndex: 10
			});
	}else{
		$('#loading')
		.each(function()
		{
			$(this)
			.hide('fast', function()
			{
				$(this).remove();
			});
		});
	}
}

function showImageLoading(a){
	if(a){
		$('#display')
			.prepend('<div id="loading"><img src="images/ajax-loader-2.gif" /></div>');
			
		$('#loading')
			.css({
				position: 'absolute',
				left: Math.round(($('#display').width() - 100)/2) + 'px',
				top: Math.round(($('#display').height() - 100)/2) + 'px',
				textAlign: 'center',
				display: 'block',
				zIndex: 100
			});
	}else{
		$('#loading')
		.each(function()
		{
			$(this)
			.hide('fast', function()
			{
				$(this).remove();
			});
		});
	}
}