$(document).ready(function() {
	
	var cookieDuration = 60;
	
	//Gallery
	
	//shows the thumbnail overlays, if javascript is disabled, the overlays will be disabled
	$('.overlay').css('opacity', .15);
	$('.overlay').hide();
	
	$('.thumbnailImgContainer').live('mouseover',
		function(){
			$(this).find('.overlay').show();
			$(this).parent().find('.thumbInfoContainer').children().children().css('color', '#636363');
		});
	
	$('.thumbnailImgContainer').live('mouseout',
		function(){
			$(this).find('.overlay').hide();
			$(this).parent().find('.thumbInfoContainer').children().children().css('color', '#000000');
	});
	
	$('.thumbGalleryLink').live('mouseover',
		function(){
			$(this).parent().parent().parent().find('.overlay').show();
			$(this).parent().parent().children().children().css('color', '#636363');
			
	});
	
	$('.thumbGalleryLink').live('mouseout',
		function(){
			$(this).parent().parent().parent().find('.overlay').hide();
			$(this).parent().parent().children().children().css('color', '#000000');
	});
	
	var moreLink = getCookie('more_link');
	if(moreLink == ''){
		moreLink = 'false';
	}
	
	//show the category links when the more button is clicked
	$('#moreLink').click(function(){
		if($(this).hasClass('activeGalleryLink')){
			setCookie('more_link', 'false', cookieDuration);
			setCookie('refresh', true, cookieDuration);
			$('#moreCategories').fadeOut();
			$(this).removeClass('activeGalleryLink');
		}
		else{
			setCookie('more_link', true, cookieDuration);
			setCookie('refresh', 'true', cookieDuration);
			$('#moreCategories').fadeIn();
			$(this).addClass('activeGalleryLink');
		}
		return false;
	});
	
	//CMS
	
	$('.slideLink').click(function(){
		return false;
	});
	
	//shows loader gif on the Add Photos page when the save button is clicked
	$('.loaderBtn').click(function(){
		$('.loaderImg').show();
	});
	
	//shows the project buttons in project index on rollover
	$('.indexImage').hover(
		function(){
			$(this).find('.projectBtns').fadeIn('fast');
		},
		function(){
			$(this).find('.projectBtns').fadeOut('fast');
		}
	);
	
	//shows the image buttons in sort_photos view on rollover
	$('.sortPhotoFormContainer').hover(
		function(){
			$(this).find('.projectBtns').fadeIn('fast');
			$(this).find('.thumbCategories').fadeOut('fast');
		},
		function(){
			$(this).find('.projectBtns').fadeOut('fast');
			$(this).find('.thumbCategories').fadeIn('fast');
		}
	);
	
	$('.editPhotoImgContainer').hover(
		function(){
			$(this).find('.editPhotoProjectBtns').fadeIn('fast');
		},
		function(){
			$(this).find('.editPhotoProjectBtns').fadeOut('fast');
		}
	);
	

	//makes inactive projects slightly transparent in the project index view
	$('.inactiveProject').css('opacity', .4);
	$('.numPhotos').css('opacity', .7);
	
	//makes the thumb category boxes on the sort photos page slightly transparent
	$('.thumbCategoryBox').css('opacity', .7);
	$('.cmsTextCaption').css('opacity', .8);
	
	//controls the add attachment and add caption buttons on the edit photo page
	$('.editPhotoBtn').toggle(function(){
		$(this).addClass('openPhotoMenu');
		$(this).removeClass('closedPhotoMenu');
	}, function(){
		$(this).removeClass('openPhotoMenu');
		$(this).addClass('closedPhotoMenu');
	});
	
	$('.attachmentBtn').toggle(function(){
		$(this).siblings('.attachmentForm').show();
	}, function(){
		$(this).siblings('.attachmentForm').hide();
	});
	
	$('.captionBtn').toggle(function(){
		$(this).siblings('.captionForm').show();
	}, function(){
		$(this).siblings('.captionForm').hide();
	});
	
	//charCount plugin for CMS inputs
	$('#projectTitleInput').charCount({
		allowed: 30,
		cssExceeded: 'charOver'
	});
	
	$('#projectDateInput').charCount({
		allowed: 4,
		cssExceeded: 'charOver'
	});
	
	$('#projectTypeInput').charCount({
		allowed: 35,
		cssExceeded: 'charOver'
	});
	
	$('#projectDescInput').charCount({
		allowed: 900,
		cssExceeded: 'charOver',
		css: 'counter descCounter'
	});
	
	$('#projectPhotoInput').charCount({
		allowed: 40,
		cssExceeded: 'charOver'
	});
	
	$('#projectPhotoLinkInput').charCount({
		allowed: 300,
		cssExceeded: 'charOver'
	});
	
	$('#projectLinkNameInput').charCount({
		allowed: 40,
		cssExceeded: 'charOver'
	});
	
	$('#projectLinkInput').charCount({
		allowed: 300,
		cssExceeded: 'charOver'
	});
	
	$('.captionTitleInput').charCount({
		allowed: 30,
		cssExceeded: 'charOver'
	});
	
	$('.captionDescInput').charCount({
		allowed: 400,
		cssExceeded: 'charOver',
		css: 'counter captionCounter'
	});
	
	
	//Ajax functions for the gallery
	
	var type = getCookie('type');
	if(type == ''){
		type = 'all';
	}
	var view = getCookie('view');
	if(view == ''){
		view = 'thumbnail';
	}
	var infoType = getCookie('info_type');
	var id = getCookie('id');
	var returnView = getCookie('return_view');
	var returnType = getCookie('return');
	var noImagesMsg = false;
	
	$('#portfolioLink').click(function(){
		deleteCookie('refresh');
		deleteCookie('id');
		deleteCookie('view');
		deleteCookie('type');
		deleteCookie('return');
		deleteCookie('return_view');
		deleteCookie('info_type');	
	});
	
	function setCookies(){
		//alert('type: '+type+', view: '+view+', id: '+id+', return_view: '+returnView+', returnType: '+returnType);
		if(noImagesMsg == true){
			noImagesMsg = false;
			clearNoImagesMsg();
		}
		setCookie('refresh', true, cookieDuration);
		setCookie('id', id, cookieDuration);
		setCookie('type', type, cookieDuration);
		setCookie('view', view, cookieDuration);
		setCookie('return', returnType, cookieDuration);
		setCookie('return_view', returnView, cookieDuration);
		setCookie('info_type', infoType, cookieDuration);
	}
	
	
	$('.infoLink').click(function(){
		clearActiveLinks();
		$('#viewBtns').hide();
		$('#galleryBtns').hide();
		infoType = this.id;
		type = '';
		if(view != 'info'){
			view = 'info';
			$('#gallery').fadeOut('fast', function(){ $('#infoContainer').fadeIn('fast', function(){ scrollInfo(); }); });
		}
		else{
			scrollInfo();
		}
		$(this).addClass('activeInfoLink');
		setCookies();
		
		return false;
	});
	
	function scrollInfo(){
		if(infoType == 'who'){
			$.scrollTo('#whoContainer', 1000);
		}
		else if(infoType == 'publications'){
			$.scrollTo('#pubContainer', 1000);
		}
		else if(infoType == 'awards'){
			$.scrollTo('#awardsContainer', 1000);
		}
		else if(infoType == 'affiliations'){
			$.scrollTo('#affiliationsContainer', 1000);
		}
		else if(infoType == 'history'){
			$.scrollTo('#historyContainer', 1000);
		}
		else if(infoType == 'contact'){
			$.scrollTo('#contactContainer', 1000);
		}
		else if(infoType == 'services'){
			$.scrollTo('#servicesContainer', 1000);
		}
	}
	
	//category links
	$('.sortLink').click(function(){
		$('#galleryBtns').hide();
		clearActiveLinks();
		if(view != 'thumbnail' && view != 'grid'){
			view = 'thumbnail';
		}
		if(view == 'thumbnail'){
			$('#thumbnail').addClass('activeThumbView');
			$('#grid').removeClass('activeGridView');
		}
		if(view == 'grid'){
			$('#thumbnail').removeClass('activeThumbView');
			$('#grid').addClass('activeGridView');
		}
		$('#galleryContainer').fadeOut('fast');
		type = this.id;
		infoType = '';
		returnView = '';
		returnType = '';
		id = 0;
		$(this).addClass('activeCategory');
		setCookies();
		
		if(view == 'thumbnail'){
			$.ajax({
			   type: "GET",
			   url: forUrl('/includes/getThumbView.php'),
			   data: "type="+type,
			   dataType: 'xml',
			   success: procThumbView,
			   error: badData
			 });
		}
		if(view == 'grid'){
			$.ajax({
			   type: "GET",
			   url: forUrl('/includes/getGridView.php'),
			   data: "type="+type,
			   dataType: 'xml',
			   success: procGridView
			 });
		}
		
		return false;
	});
	
	$('#returnPortfolioLink').click(function(){
		$('#galleryBtns').hide();
		$('#thumbnail').addClass('activeThumbView');
		$('#grid').removeClass('activeGridView');
		view = 'thumbnail';
		type = 'all';
		infoType = '';
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getThumbView.php'),
		   data: "type=all",
		   dataType: 'xml',
		   success: procThumbView,
		   error: badData
		 });
		
		
		return false;
	});
	
	function clearActiveLinks(){
		$('#all').removeClass('activeCategory');
		$('#favorites').removeClass('activeCategory');
		$('#built').removeClass('activeCategory');
		$('#details').removeClass('activeCategory');
		$('#site').removeClass('activeCategory');
		$('#infill').removeClass('activeCategory');
		$('#green').removeClass('activeCategory');
		$('#exterior').removeClass('activeCategory');
		$('#interiors').removeClass('activeCategory');
		$('#stairs').removeClass('activeCategory');
		$('#porch').removeClass('activeCategory');
		$('#cabinets').removeClass('activeCategory');
		$('#windows').removeClass('activeCategory');
		$('#bathrooms').removeClass('activeCategory');
		$('#kitchens').removeClass('activeCategory');
		$('#services').removeClass('activeInfoLink');
		$('#who').removeClass('activeInfoLink');
		$('#awards').removeClass('activeInfoLink');
		$('#publications').removeClass('activeInfoLink');
		$('#affiliations').removeClass('activeInfoLink');
		$('#history').removeClass('activeInfoLink');
		$('#contact').removeClass('activeInfoLink');
		$('#commercial').removeClass('activeCategory');
	}
	
	//thumbnail veiw
	$('.thumbViewLink').click(function(){
		$('#galleryBtns').hide();
		$('#galleryContainer').fadeOut('fast');
		view = this.id;
		returnView = '';
		returnType = '';
		infoType = '';
		setCookies();
		if(view == 'thumbnail'){
			$('#thumbnail').addClass('activeThumbView');
			$('#grid').removeClass('activeGridView');
			$.ajax({
			   type: "GET",
			   url: forUrl('/includes/getThumbView.php'),
			   data: "type="+type,
			   dataType: 'xml',
			   success: procThumbView
			 });
		}
		if(view == 'grid'){
			$('#thumbnail').removeClass('activeThumbView');
			$('#grid').addClass('activeGridView');
			$.ajax({
			   type: "GET",
			   url: forUrl('/includes/getGridView.php'),
			   data: "type="+type,
			   dataType: 'xml',
			   success: procGridView
			 });
		}
		
		return false;
	});
	
	$('.gridLink').live('click', function(){
		$('#galleryContainer').fadeOut('fast');
		returnType = type;
		returnView = 'grid';
		view = 'gallery';
		infoType = '';
		id = $(this).parents('.gridImgContainer').attr('id');
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getGalleryView.php'),
		   data: "type="+type+"&id="+id,
		   dataType: 'xml',
		   success: procGalleryView
		 });
		 
		return false;
	});
	
	$('.gridOverlay').live('click', function(){
		$('#galleryContainer').fadeOut('fast');
		returnType = type;
		returnView = 'grid';
		view = 'gallery';
		infoType = '';
		id = $(this).parents('.gridImgContainer').attr('id');
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getGalleryView.php'),
		   data: "type="+type+"&id="+id,
		   dataType: 'xml',
		   success: procGalleryView
		 });
		 
		return false;
	});
	
	//thumbnail that links to a specific gallery
	$('.thumbLink').live('click', function(){
		$('#galleryContainer').fadeOut('fast');
		returnType = type;
		returnView = 'thumbnail';
		infoType = '';
		id = $(this).parents('.thumbImage').attr('id');
		view = 'gallery';
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getGalleryView.php'),
		   data: "type="+type+"&id="+id,
		   dataType: 'xml',
		   success: procGalleryView
		 });
		
		return false;
	});
	
	//thumbnail that links to a specific gallery
	$('.thumbOverlay').live('click', function(){
		$('#galleryContainer').fadeOut('fast');
		returnType = type;
		returnView = 'thumbnail';
		infoType = '';
		id = $(this).parents('.thumbImage').attr('id');
		view = 'gallery';
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getGalleryView.php'),
		   data: "type="+type+"&id="+id,
		   dataType: 'xml',
		   success: procGalleryView
		 });
		
		return false;
	});
	
	//info link that links to a specific gallery
	$('.infoGalleryLink').live('click', function(){
		clearActiveLinks();
		$('#all').addClass('activeCategory');
		$('#galleryContainer').fadeOut('fast');
		returnType = 'all';
		returnView = 'thumbnail';
		infoType = '';
		id = $(this).attr('name');
		type = 'all';
		view = 'gallery';
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getGalleryView.php'),
		   data: "type="+type+"&id="+id,
		   dataType: 'xml',
		   success: procGalleryView
		 });
		
		return false;
	});
	
	
	$('#returnThumbsLink').live('click', function(){
		if(returnView == 'thumbnail'){
			$('#thumbnail').addClass('activeThumbView');
			$('#grid').removeClass('activeGridView');
		}
		if(returnView == 'grid'){
			$('#thumbnail').removeClass('activeThumbView');
			$('#grid').addClass('activeGridView');
		}
		$('#galleryBtns').hide();
		$('#galleryContainer').fadeOut('fast');
		type = returnType;
		view = returnView;
		infoType = '';
		id = 0;
		setCookies();
		
		
		
		if(view == 'thumbnail'){
			$.ajax({
			   type: "GET",
			   url: forUrl('/includes/getThumbView.php'),
			   data: "type="+type,
			   dataType: 'xml',
			   success: procThumbView
			 });
		}
		if(view == 'grid'){
			$.ajax({
			   type: "GET",
			   url: forUrl('/includes/getGridView.php'),
			   data: "type="+type,
			   dataType: 'xml',
			   success: procGridView
			 });
		}
		
		return false;
	});
	
	$('#fullProjectLink').live('click', function(){
		$('#galleryContainer').fadeOut('fast');
		type = 'all';
		infoType = '';
		setCookies();
		
		$.ajax({
		   type: "GET",
		   url: forUrl('/includes/getGalleryView.php'),
		   data: "type="+type+"&id="+id,
		   dataType: 'xml',
		   success: procGalleryView
		 });
		
		return false;
	});
	
	
	function procThumbView(data, status){
		//alert('thumbs');
		$('#infoContainer').hide();
		$('#returnPortfolioLink').addClass('hideLink');
		$('#viewBtns').show();
		$('#gallery').show();
		$('#gallery').empty();
		var galleryWidth = data.getElementsByTagName("galleryWidth")[0].childNodes[0].nodeValue;
		var view = data.getElementsByTagName("view")[0].childNodes[0].nodeValue;
		var viewType = data.getElementsByTagName("viewType")[0].childNodes[0].nodeValue;
		var gallery = document.getElementById('gallery');
		gallery.style.width = galleryWidth+'px';
		var thumbnail = data.getElementsByTagName("thumbnail");
		if(thumbnail.length > 0){
			var content = '';
			for(x = 0;x < thumbnail.length;x++){
				var id = thumbnail[x].getElementsByTagName("id")[0].childNodes[0].nodeValue;
				if(id != 0){
					var title = thumbnail[x].getElementsByTagName("title")[0].childNodes[0].nodeValue;
					var projectType = thumbnail[x].getElementsByTagName("type")[0].childNodes[0].nodeValue;
					var date = thumbnail[x].getElementsByTagName("date")[0].childNodes[0].nodeValue;
					var image_path = thumbnail[x].getElementsByTagName("image_path")[0].childNodes[0].nodeValue;
					if(thumbnail[x].getElementsByTagName("award").length > 0){
						var award = thumbnail[x].getElementsByTagName("award")[0].childNodes[0].nodeValue;
					}
					
					content = content+'<div class="thumbImage" id="'+id+'">';
					content = content+'<div class="thumbnailImgContainer">';
					if(thumbnail[x].getElementsByTagName("award").length > 0 && award == 1){
						content = content+'<img src="'+forUrl('/img/award_banner.png')+'" class="awardBanner" />';
					}
					content = content+'<img src="'+forUrl('/img/ajax-loader.gif')+'" class="thumbLoader" />';
					content = content+'<div class="thumbOverlay overlay"></div>';
					content = content+'<a href="'+forUrl('/projects/display/Array/view:gallery/type:'+type+'id:'+id+'/return:'+type+'/return_view:thumbnail')+'" class="thumbLink">';
					content = content+'<img src="'+forUrl('/img/projects/thumbnail/'+image_path)+'" alt="" class="thumbnailImg" /></a></div>';
					content = content+'<div class="thumbInfoContainer">';
					content = content+'<h2 class="thumbInfoTitle"><a href="'+forUrl('/projects/display/Array/view:gallery/type:'+type+'id:'+id+'/return:'+viewType+'/return_view:thumbnail')+'" class="thumbGalleryLink thumbLink">'+title.toUpperCase()+'</a></h2>';
					content = content+'<h3 class="thumbInfoType"><a href="'+forUrl('/projects/display/Array/view:gallery/type:'+type+'id:'+id+'/return:'+viewType+'/return_view:thumbnail')+'" class="thumbGalleryLink thumbLink">'+projectType.toUpperCase()+'</a></h3>';
					content = content+'<h3 class="thumbInfoDate"><a href="'+forUrl('/projects/display/Array/view:gallery/type:'+type+'id:'+id+'/return:'+viewType+'/return_view:thumbnail')+'" class="thumbGalleryLink thumbLink">'+date.toUpperCase()+'</a></h3>';
					content = content+'</div></div>';
				}
			}
		}
		//alert(content);
		$('#gallery').append(content);
		$('.overlay').css('opacity', .15);
		$('.overlay').hide();
		$('#galleryContainer').fadeIn('fast', function(){ $.scrollTo('#gallery', 1000); });
	}
	
	function procGridView(data, status){
		//alert('grid');
		$('#infoContainer').hide();
		$('#returnPortfolioLink').addClass('hideLink');
		$('#viewBtns').show();
		$('#gallery').show();
		$('#gallery').empty();
		var thumbnail = data.getElementsByTagName("thumbnail");
		var galleryWidth = 0;
		var secondRowWidth = 0;
		var allImages = 0;
		if(thumbnail.length > 0){
			for(i = 0;i < thumbnail.length;i++){
				allImages = (allImages + parseInt(thumbnail[i].getElementsByTagName("grid_width")[0].childNodes[0].nodeValue)) + 3;
			}
		}
		var halfWidth = Math.ceil(allImages/2);
		
		if(thumbnail.length > 0){
			for(n = 0;n < thumbnail.length;n++){
				galleryWidth = (galleryWidth + parseInt(thumbnail[n].getElementsByTagName("grid_width")[0].childNodes[0].nodeValue)) + 3;
				if(galleryWidth > halfWidth){
					var breakNum = n;
					break;
				}
			}
			galleryWidth = galleryWidth + 3;
			
			for(v = 0;v < thumbnail.length;v++){
				if(v > n){
					secondRowWidth = (secondRowWidth + parseInt(thumbnail[v].getElementsByTagName("grid_width")[0].childNodes[0].nodeValue)) + 3
				}
			}
			
			var spacerWidth = (galleryWidth - secondRowWidth) - 6;
		}
		
		var gallery = document.getElementById('gallery');
		gallery.style.width = galleryWidth+'px';
		
		if(thumbnail.length > 0){		
			var view = data.getElementsByTagName("view")[0].childNodes[0].nodeValue;
			var viewType = data.getElementsByTagName("viewType")[0].childNodes[0].nodeValue;
			if(thumbnail.length > 0){
				var content = '';
				//alert(true);
				for(x = 0;x < thumbnail.length;x++){
					var id = thumbnail[x].getElementsByTagName("id")[0].childNodes[0].nodeValue;
					if(id != 0){
						var grid_width = thumbnail[x].getElementsByTagName("grid_width")[0].childNodes[0].nodeValue;
						var image_path = thumbnail[x].getElementsByTagName("image_path")[0].childNodes[0].nodeValue;
						
						content = content+'<div class="gridImgContainer" id="'+id+'" style="width:'+grid_width+'px">';
						content = content+'<div class="thumbnailImgContainer">';
						content = content+'<img src="'+forUrl('/img/ajax-loader.gif')+'" class="gridLoader" />';
						content = content+'<div class="gridOverlay overlay" style="width:'+grid_width+'px"></div>';
						content = content+'<a href="'+forUrl('/projects/display/Array/view:gallery/type:'+type+'id:'+id+'/return:'+viewType+'/return_view:grid')+'" class="gridLink">';
						content = content+'<img src="'+forUrl('/img/projects/grid/'+image_path)+'" alt="" class="gridImg" /></a></div>';
						content = content+'</div>';
					}
				}
			}
			
			if(spacerWidth > 0){
				content = content+'<div id="gridSpacer" style="width:'+spacerWidth+'px"></div>';
			}
			//alert(content);
		}
		else{
			gallery.style.width = '0px';
			noImagesMsg = true;
			$('#galleryContainer').append('<p id="noImagesMsg">No Images Available in this Category.</p>');
		}
		
		$('#gallery').append(content);
		$('.overlay').css('opacity', .15);
		$('.overlay').hide();
		$('#galleryContainer').fadeIn('fast', function(){ $.scrollTo('#gallery', 1000); });
	}
	
	function procGalleryView(data, status){
		//alert('gallery');
		$('#infoContainer').hide();
		$('#gallery').show();
		$('#gallery').empty();
		$('#viewBtns').hide();
		$('#galleryBtns').show();
		$('#returnThumbsLink').removeClass('hideLink');
		$('#returnThumbsLink').show();
		if(type == 'all' || type == 'built' || type == 'favorites'){
			$('#fullProjectLink').hide();
		}
		else{
			$('#fullProjectLink').removeClass('hideLink');
			$('#fullProjectLink').show();
		}
		var images = data.getElementsByTagName("image");
		if(type == 'all' || type == 'built' || type == 'favorites'){
			var galleryWidth = 360;
		}
		else{
			var galleryWidth = 0;
		}
		if(images.length > 0){
			for(i = 0;i < images.length;i++){
				galleryWidth = (galleryWidth + parseInt(images[i].getElementsByTagName("gallery_width")[0].childNodes[0].nodeValue)) + 3;
				if(images[i].getElementsByTagName("caption").length > 0){
					galleryWidth = galleryWidth + 200;
				}
			}
		}
		var gallery = document.getElementById('gallery');
		gallery.style.width = galleryWidth+'px';
		
		var content = '';
		if(images.length > 0){
			var viewType = data.getElementsByTagName("viewType")[0].childNodes[0].nodeValue;
			var title = data.getElementsByTagName("title")[0].childNodes[0].nodeValue;
			var award = data.getElementsByTagName("award")[0].childNodes[0].nodeValue;
			var projectType = data.getElementsByTagName("type")[0].childNodes[0].nodeValue;
			var date = data.getElementsByTagName("date")[0].childNodes[0].nodeValue;
			if(data.getElementsByTagName("description").length > 0){
				var description = data.getElementsByTagName("description")[0].childNodes[0].nodeValue;
			}
			if(data.getElementsByTagName("photo").length > 0){
				var photo = data.getElementsByTagName("photo")[0].childNodes[0].nodeValue;
			}
			var usePhotoLink = false;
			if(data.getElementsByTagName("photoLink").length > 0){
				usePhotoLink = true;
				var photoLink = data.getElementsByTagName("photoLink")[0].childNodes[0].nodeValue;
			}
			if(data.getElementsByTagName("link").length > 0 && data.getElementsByTagName("link_name").length > 0){
				var link = data.getElementsByTagName("link")[0].childNodes[0].nodeValue;
				var link_name = data.getElementsByTagName("link_name")[0].childNodes[0].nodeValue;
			}
			if(type == 'all' || type == 'built' || type == 'favorites'){
				content = content+'<div id="projectInfoContainer">';
				content = content+'<h2>'+title.toUpperCase()+'</h2>';
				content = content+'<h3 id="projectInfoType">'+projectType+'</h3>';
				content = content+'<h3 id="projectInfoDate">'+date+'</h3>';
				if(data.getElementsByTagName("description").length > 0){
					content = content+'<p id="projectInfoDesc">'+description.replace(/\n/g,'<br />')+'</p>'; 
				}
				if(data.getElementsByTagName("photo").length > 0){
					if(usePhotoLink == true){
						content = content+'<p id="projectInfoPhoto">PHOTOGRAPHY BY: <a target="_blank" href="'+photoLink+'"  class="projectInfoPhotoName">'+photo+'</a></p>';
					}
					else{
						content = content+'<p id="projectInfoPhoto">PHOTOGRAPHY BY: <span class="projectInfoPhotoName">'+photo+'</span></p>';
					}
				}
				if(data.getElementsByTagName("link_name").length > 0){
					if(data.getElementsByTagName("link").length > 0){
						content = content+'<p id="secondPhotographer"><a target="_blank" href="'+link+'" class="projectInfoPhotoName">'+link_name+'</a></p>';
					}
					else{
						content = content+'<p id="secondPhotographer"><span class="projectInfoPhotoName">'+link_name+'</span></p>';
					}
				}
				content = content+'</div>';
			}
		
		
			//alert(true);
			for(x = 0;x < images.length;x++){
				if(images[x].getElementsByTagName("image_path").length > 0){
					var image_path = images[x].getElementsByTagName("image_path")[0].childNodes[0].nodeValue;
				}
				var gallery_width = images[x].getElementsByTagName("gallery_width")[0].childNodes[0].nodeValue;
				if(images[x].getElementsByTagName("pdf").length > 0){
					var pdf = images[x].getElementsByTagName("pdf")[0].childNodes[0].nodeValue;
				}
				if(images[x].getElementsByTagName("quicktime").length > 0){
					var quicktime = images[x].getElementsByTagName("quicktime")[0].childNodes[0].nodeValue;
				}
				if(images[x].getElementsByTagName("caption").length > 0){
					var caption = images[x].getElementsByTagName("caption")[0].childNodes[0].nodeValue;
				}
				if(images[x].getElementsByTagName("caption_title").length > 0){
					var caption_title = images[x].getElementsByTagName("caption_title")[0].childNodes[0].nodeValue;
				}
				
				if(images[x].getElementsByTagName("image_path").length > 0){
					content = content+'<div class="galleryImage" style="width:'+gallery_width+'px">';
					content = content+'<img src="'+forUrl('/img/ajax-loader.gif')+'" class="imgLoader" />';
					content = content+'<img src="'+forUrl('/img/projects/gallery/'+image_path)+'" alt="" class="galleryImg" />';
					if(images[x].getElementsByTagName("pdf").length > 0){
						content = content+'<a target="_blank" href="'+forUrl('/uploads/pdf/'+pdf)+'" class="galleryPDFIcon"><img src="'+forUrl('/img/pdf.png')+'" alt="pdf"></a>';
					}
					if(images[x].getElementsByTagName("quicktime").length > 0){
						if(images[x].getElementsByTagName("image_path").length > 0){
							content = content+'<a target="_blank" href="'+forUrl('/uploads/quicktime/'+quicktime)+'" class="galleryQuickBothIcon"><img src="'+forUrl('/img/quicktime.png')+'" alt="movie"></a>';
						}
						else{
							content = content+'<a target="_blank" href="'+forUrl('/uploads/quicktime/'+quicktime)+'" class="galleryQuickIcon"><img src="'+forUrl('/img/quicktime.png')+'" alt="movie"></a>';
						}
					}
				content = content+'</div>';
				if(images[x].getElementsByTagName("caption").length > 0){
					content = content+'<div class="captionContainer">';
						content = content+'<img src="'+forUrl('/img/left_arrow_over.png')+'" alt="" class="captionArrow" />';
						if(images[x].getElementsByTagName("caption_title").length > 0){
							content = content+'<h3 class="captionTitle">'+caption_title+'</h3>';
						}
						else{
							content = content+'<h3 class="captionTitle"></h3>';
						}
						content = content+'<p class="captionText">'+caption.replace(/\n/g,'<br />')+'</p>';
						content = content+'</div>';
					}
				}
			}
		}
		else{
			noImagesMsg = true;
			$('#galleryContainer').append('<p id="noImagesMsg">No Images Available in this Category.</p>');
		}
		//alert(content);
		$('#gallery').append(content);
		$('#galleryContainer').fadeIn('fast');
		
	}
	
	function clearNoImagesMsg(){
		$('#noImagesMsg').remove();
	}
	
	function badData(xhr){
		alert('There was a problem');
		alert(xhr.status);                 //Eg: 404, 500
		alert(xhr.responseText);		   //like view source of the error page
	}
	
	function setCookie(name,value,minutes){
		var expires = '';
		
		if(minutes){
			var date = new Date();
			date.setTime(date.getTime() + (1000 * 60 * minutes));
			expires = '; expires=' + date.toGMTString();
		}
		
		document.cookie = name + '=' + value + expires +'; path=/';
	}
	
	function getCookie(c_name)
	{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
	}
	
	function deleteCookie(name, path, domain){
		if (getCookie( name )) document.cookie = name + "=" + 
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	
	function forUrl(url) {
		return $('base').attr('href')+url.substr(1);
	}

});


