
	var ulWidth;
	var ulHeight;
	var scrollElem = jQuery('html');
	
	var preload = function(images) 
	{
		if (document.images) {
			var imageArray = new Array();
			imageArray = images.split(',');
			var imageObjArray = new Array()
			for(var i=0; i < imageArray.length; i++) {
				//document.write('<img src="' + imageArray[i] + '" />');// Write to page (uncomment to check images)
				imageObjArray[i] = new Image();
				imageObjArray[i].src = imageArray[i];
			}
		}
	}
	
	var showMore = function(object, toggle)
	{
		if(toggle)
		{
			jQuery(object).animate({width: '95%'}, 'slow', function()
			{
				jQuery(object).find('.showMoreDiv').toggle(true);
			});
		}
		else
		{
			//jQuery(object).css({width: ulWidth, height: ulHeight});
			jQuery(object).find('.showMoreDiv').toggle(toggle);
		}
		
		jQuery(object).toggleClass('open', toggle);
		jQuery(object).find('.showMoreImg').toggle(toggle);
		jQuery(object).find('.showMoreThumb').toggle((!toggle));
	}
	
	var hideAllMore = function()
	{
		jQuery('ul.project').each(function()
		{
			showMore(this, false);
		});
	}
	
	var openPress = function(object, response)
	{
		var  html  = '<li class="showMoreLi text">'+
			'<img src="/press/images/'+ response.image[0].name +'" /><h2>' + response.name  + '<br /><span class="position"> '+  '' /*response.buildingType*/ +'</span></h2><time datetime="' + response.date + ' pubdate">' + response.date + '</time><p>' + response.text + '</p><a href="/press/' + response.link + '" target="_blank" class="btn_downloadKit">DOWNLOAD PDF</a>'+
		'</li>'
	
		/*var rotoSize = getGallerySize(520, 503, response.image.length, false);
		
		html += '<li class="showMoreLi image">'+
			'<div style="display: none; width:' + rotoSize + 'px; overflow: hidden; position: relative; visibility: visible;" id="carousel" class="roto">'+
				'<button  class="prev">prev</button><button class="next">Next</button>'+
				'<div style="position: relative; padding: 0px; margin: 0px;" class="rotoFrame">';
	
		for(var i = 0; i < response.image.length; i++)
		{
			var idNum = i + 1;
			html += '<span style="display: block; float: left;" id="c' + idNum + '"><img class="galleryImages" src="images/press/' +  response.image[i].name + '.png" alt="" /></span>';
		}

		html += '</div></div></li>';*/
		
		var isImageSmall = getGallerySize(520, 503, response.image.length, true);
		openPoject(object, html, 250, response.image.length, isImageSmall, false); 
	}
	var openTeam = function(object, response)
	{
		var rotoSize = getGallerySize(348, 840, response.team.length, false);
		
		var html = '<li class="showMoreLi first"><h2>' + response.team[0].position + '</h2></li>'+
		'<li class="showMoreLi">'+
			'<div style="display: none; width:' + rotoSize + 'px; overflow: hidden; position: relative; visibility: visible;" id="carousel" class="roto">'+
				'<button class="prev">prev</button><button class="next">Next</button>'+
				'<div style="position: relative; padding: 0px; margin: 0px;" class="rotoFrame">';
				
				
		for(var i = 0; i < response.team.length; i++)
		{
			var idNum = i + 1;
			html += '<span style="display: block; float: left;" id="c' + idNum + '">'+
				'<ul style="height:450px; width: 840px;">'+
					'<li class="showMoreLi image"><img class="galleryImages" width="270px" src="images/team/' + response.team[i].image + '.jpg"  /></li>'+
					'<li class="showMoreLi text"><h2>' + response.team[i].name + '<br /><span class="position">' + response.team[i].position + '</span></h2><p>' + response.team[i].text + '</p></li>'+
				'</ul>'+
			'</span>';
		}
		
		html += '</div></div></li>';
		
		var isImageSmall  = getGallerySize(348, 840, response.team.length, true);
		openPoject(object, html, 450, response.team.length, isImageSmall, true); 
	}
	
	var openWork = function(object, response)
	{	
		var rotoSize = getGallerySize(669, 714, response.image.length, false);
		var html = '<li class="showMoreLi projectContainer clearfix">'+
			'<div class="projectInfo left">'+
				'<h1>' +  response.name + '</h1><span>' +  response.type + '</span>'+
				'<ul class="actionLinks">'+
					((response.description.length > 1 ) ? '<li class="tab selected" id="description"><a onclick="openTab(\'description\')">Description</a></li>' : '')+
					((response.spec.length > 1 ) ? '<li class="tab" id="spec"><a onclick="openTab(\'spec\')">Specs</a></li>': '')+
					((response.projectKit.length > 1 ) ? '<li class="tab" id="projectKit"><a onclick="openTab(\'projectKit\')">Press Kit</a></li>': '')+
				'</ul><!-- actionLinks -->'+
				'<div class="tabBox" id="descriptionBox">' + response.description + '</div>'+
				'<div class="tabBox" style="display:none" id="specBox">' + response.spec + '</div>'+
				'<div class="tabBox" style="display:none" id="projectKitBox">' + response.projectKit + '</div>'+
				'<a onclick="hideAllProjects(true)" class="backButton">BACK &nbsp;| </a> <a class="fsButton" onclick="fullScreen(\'work_' + response.id + '\')" > FULL SCREEN</a>'+
				'<ul class="socialLinks">'+
					'<li class="facebook"><a>Facebook</a>'+
						'<ul style="display:none">'+
							'<li class="btn_fb"><fb:like href="http://www.soma-architects.com/index.php?page=work&orderBy=date&workId=' + response.id + '" width="300" show_faces="false" colorscheme="dark"></fb:like></li>'+
						'</ul>'+
					'</li>'+
					'<li class="twitter"><a>Twitter</a>'+
						'<ul style="display:none">'+
							'<li><iframe allowtransparency="false" frameborder="0" scrolling="no" src="//platform.twitter.com/widgets/tweet_button.html?url=' + encodeURIComponent('http://www.soma-architects.com/index.php?page=work&orderBy=date&workId=' + response.id) + '&via=SomaArchitects" style="width:130px; height:20px; background-color: #333333;"></iframe><li>'+
						'</ul>'+
					'</li>'+
					'<li class="email"><a>Email</a>'+
						'<ul style="display:none">'+
							'<input class="contact" type="hidden" name="id" value="' + response.id + '" />'+
							'<li id="name_input"><input value="NAME" class="contact" type="text" name="name" /></li>'+
							'<li><input value="EMAIL" class="contact" type="text" name="email" /></li>'+
							'<li id="query_box"><textarea resize="no" value="QUERY" class="contact" name="query"></textarea></li>'+
							'<li id="submit" onclick="sendContact()">SUBMIT</li>'+
						'</ul>'+
					'</li>'+
				'</ul><!-- socialLinks -->'+
			'</div><!-- projectInfo -->'+
			'<div style="display: none; width:' + rotoSize + 'px; overflow: hidden; position: relative; visibility: visible;" id="carousel" class="roto">'+
				'<button class="prev">prev</button><button class="next">Next</button>'+
				'<div style="position: relative; padding: 0px; margin: 0px;" class="rotoFrame">';
				
		var imageString = '';
		var firstLoop = true;	
		
		for(var i = 0; i < response.image.length; i++)
		{
			var idNum = i + 1;
			imageString += (!firstLoop) ? ',' : '';
			imageString += 'images/work/' +  response.image[i].name + '.jpg,images/work/large/' +  response.image[i].name + '.jpg';
			html += '<span style="display: block; float: left;" id="c' + idNum + '"><img class="galleryImages" src="images/work/' +  response.image[i].name + '.jpg" alt="" /></span>';
			firstLoop = false;
		}
	
		preload(imageString);
		
		html += '</div></div></li>';
		
		var isImageSmall = getGallerySize(669, 714, response.image.length, true);
		openPoject(object, html, 450, response.image.length, isImageSmall, true);
	}
	
	var fullScreen = function(id)
	{
		var param = new Object;
		param['id'] = id;
		
		jQuery.ajax(
		{
			url: 'index.php?page=getWorkAjax',
			type: 'POST',
			data: param,
			dataType: 'json',
			success: function(response)
			{
				var buttons = '<button class="prev">prev</button>	<button class="next imageNext">Next</button>';
				
				var html =  buttons + '<div class="largeSlideShow right">';
				
				for(var i = 0; i < response.image.length; i++)
				{
					html += '<img class="imageNext" src="images/work/large/' +  response.image[i].name + '.jpg" alt="" />';
				}
				
				html += '</div>';
				
				Shadowbox.open(
				{
					content: html,
					player: "html",
					height: 750,
					width: 1450,
					options: 
					{
						handleOversize: "none",
						overlayOpacity: 0.9,
						onFinish: function()
						{
							jQuery('.largeSlideShow').cycle({
								fx:     'fade', 
								speed:  'fast', 
								timeout: 0, 
								next:   '.imageNext', 
								prev:   '.prev' 
							});
						}
					}
				});
			}
		});	
	}
	var openPoject = function(object, html, heightPx, imageNum, isImageSmall, loadBar)
	{
		var widthPx = document.body.clientWidth - 69;
		jQuery(object).toggleClass('open', true);
		jQuery(object).animate({width: widthPx, height: heightPx}, '400', function()
		{
			jQuery(object).append(html);
			
			jQuery('.btn_fb').each(function()
			{
				FB.XFBML.parse( this );
			});
			
			if(loadBar)
			{
				jQuery(object).append('<img id="loading" src="images/ajax-loader.gif" />');
				setTimeout(function(){openGallery(imageNum, isImageSmall)}, 2000);
			}
			else
			{
				openGallery(imageNum, isImageSmall);
			}
		});
		
		jQuery(object).children('li').hide();
	}
	
	var hideButton = function(imageNum, isImageSmall)
	{
		if(imageNum == 1 || isImageSmall)
		{
			jQuery('.next').hide();
		}
	}
	
	var close = function()
	{
		jQuery('.showMoreLi').remove();
		jQuery('.project').each(function()
		{
			jQuery(this).css({height:77});
			jQuery(this).removeClass('open');
			jQuery(this).children('li').show();
		});
	}
	
	var hideAllProjects = function(time)
	{
		(typeof time != 'undefined') ? window.setTimeout(close, 10) : close();
	}
	
	var getInfo = function(object, ajax, callBack)
	{
		var param = new Object;
		param['id'] = jQuery(object).attr('id');
		
		jQuery.ajax(
		{
			url: 'index.php?page=' + ajax,
			type: 'POST',
			data: param,
			dataType: 'json',
			success: function(response)
			{
				(typeof response == 'object' && typeof callBack == 'function') ? callBack(object, response) : ''; 				
			}
		});
	}
	
	var getGallerySize = function(startLenght, imageSize, imageCount, isImageSmaller)
	{
		var result;
		
		if((window.innerWidth - startLenght) > (imageCount * imageSize))
		{
			result = (isImageSmaller) ? true : imageCount * imageSize; 
		}
		else
		{
			result = (isImageSmaller) ? false : window.innerWidth - startLenght;
		}
		
		return result;
	}
	
	var openGallery = function(imageNum, isImageSmall)
	{
		jQuery('#loading').remove();
		jQuery("#carousel").roto({ snap: true });
		jQuery(".roto").css("visibility", "visible");
		hideButton(imageNum, isImageSmall);
		jQuery("#carousel").show();
	}
	
	var openTab = function(tabName)
	{
		jQuery('.tab').each(function()
		{
			jQuery(this).toggleClass('selected', (jQuery(this).attr('id') == tabName));
		});
		
		jQuery('.tabBox').each(function()
		{
			jQuery(this).toggle((jQuery(this).attr('id') == tabName + 'Box'));
		});
	}
	
	var animateScroll = function(target)
	{
		jQuery('html,body').clearQueue();
		jQuery('html,body').animate({scrollTop: target}, 600, 'linear');
	}
	
	jQuery(document).ready(function()
	{
		ulWidth = jQuery('.project').css('width');
		ulHeight = jQuery('.project').css('height');
		
		jQuery('#news .project').click(function()
		{
			if(!jQuery(this).hasClass('open'))
			{
				hideAllMore();
				showMore(this, 'slow');
			}
		});
		
		jQuery('#press-kit .project').click(function()
		{
			if(!jQuery(this).hasClass('open'))
			{
				hideAllProjects();
				getInfo(this, 'getPressAjax', openPress);
			}
		});
		
		jQuery('#team .project').click(function()
		{
			if(!jQuery(this).hasClass('open'))
			{
				hideAllProjects();
				getInfo(this, 'getTeamAjax', openTeam);
			}
		});
		
		jQuery('#work .project').click(function()
		{
			if(!jQuery(this).hasClass('open'))
			{
				hideAllProjects();
				getInfo(this, 'getWorkAjax', openWork);
			}
		});
		
		jQuery(document).keydown(function(e)
		{
			(e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 37 || e.keyCode == 39) ? e.preventDefault() : '';
			(e.keyCode == 38) ? animateScroll(document.body.scrollTop - 95) : '';
			(e.keyCode == 40) ? animateScroll(document.body.scrollTop + 95) : '';
			
			if(e.keyCode == 37)
			{
				jQuery('.prev').trigger('click');
			}
			
			if(e.keyCode == 39)
			{
				jQuery('.next').trigger('click');
			}
		});
	});
	
	var sendContact = function()
	{
		var valid = true;
		var param = new Object;
		
		jQuery('.contact').each(function()
		{
			if(jQuery(this).val() != '')
			{
				jQuery(this).removeAttr('style');
				param[jQuery(this).attr('name')] = jQuery(this).val();
			}
			else
			{
				valid = false
				jQuery(this).attr('style', 'background-color: #FF0000;');
			}
		});
		
		if(valid)
		{
			jQuery.ajax({
				url: 'index.php?page=sendContactAjax',
				type: 'POST',
				data: param,
				dataType: 'html',
				success: function(response)
				{
					jQuery('ul.socialLinks li ul').hide()
					alert(response);
				}
			});
		}
		else
		{
			alert('You have not entered all the required fields');
		}
	}
	
	jQuery(document).on("click", "ul.socialLinks li a", function()
	{
		var current = jQuery(this).parent('li');
		
		jQuery('ul.socialLinks li').each(function()
		{
			if(!jQuery(current).is(this))
			{
				jQuery(this).children('ul').hide();
			}
		});
		
		jQuery(current).children('ul').fadeToggle();
	});
	
	var contactArray = new Array('NAME', 'EMAIL');
	
	jQuery(document).on("click", ".contact", function()
	{
		if(jQuery.inArray(jQuery(this).val(),contactArray) > -1)
		{
			jQuery(this).val('');
		}
	});
	
	jQuery(document).on("click", "#work .project", function()
	{
		if(!jQuery(this).hasClass('open'))
		{
			hideAllProjects();
			getInfo(this, 'getWorkAjax', openWork);
		}
	});

