$(function() {
	$(window).resize(function() {
		$('div:last').height($(window).height() - $('div:last').offset().top);
	});
	$(window).resize();
	
	$('body').click(function(e) {
		if($(e.target).closest('#footer').length == 0 && currentForm != false) {
			expandForm(currentForm);
		}
	});
	
	$(".texture").css('min-height', $(window).height());
	
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
	{
	     $("#footer").css("position", "static");
	     $("#footer").css("bottom", "auto");
	     $("#footer").css("padding-top", "1px");
	     $('.middle').css('margin-top', '68px');
	};
});
$(function(){
    $(window).resize(function(){
        var h = $(window).height();
        var w = $(window).width();
        
    });
});
var currentForm = false;
var inProgress = false;

function expandForm(id)
{
	if (inProgress == true)
	{
		return false;
	}

	if (currentForm != false)
	{
		if (currentForm == id)
		{
			inProgress = true;
			$('#footer').animate({
				bottom: 0
			}, 250, function() {
				$('#footer .inner #' + id).fadeToggle(200);
				$('#footer .inner').height('0px');
				currentForm = false;
				inProgress = false;
			});

			return false;
		}

		else
		{
			$('#footer .inner #' + currentForm).hide();
			$('#footer .inner #' + id).show();
			currentForm = id;
			return false;
		}
	}

	inProgress = true;
	$('#footer .inner').height('400px');
	$('#footer .inner #' + id).fadeToggle(200);
	$('#footer').animate({
		bottom: 300
	}, 250, function() {
		inProgress = false;
		currentForm = id;
	});
	
	return false;
}

function showProjectDetails(id)
{
	$('#details-' + id).css('display', 'block');
}

function hideProjectDetails(id)
{
	$('#details-' + id).css('display', 'none');
}

function submitForm(type)
{
	$.ajax({
			type: 'POST',
			url: BASE_URL + 'contact/' + type,
			dataType: 'json',
			data: $('#' + type + 'Form :input'),
			beforeSend: function(){
				$('#errorMsg').remove();
				$('#' + type + 'Form').before('<p id="formLoading"><img src="' + STATIC_URL + 'images/ajax-loader.gif" alt="Loading" /></p>');
				$('#' + type + 'Form input').attr('disabled', 'disabled');
			},
			success: function(data){
				$('#formLoading').remove();
				
				if (data.success == true)
				{
					$('#' + type + 'Form').html('<p class="success">Thank you for your submission. We will get back to you shortly.</p>');
				}
				
				else
				{
					$('#' + type + 'Form input').attr('disabled', '');
					var errorMsg = '<ul>';
					
					for (var key in data.errors)
					{
						if (data.errors.hasOwnProperty(key))
						{
							errorMsg += '<li>' + data.errors[key] + '</li>';
						}
					}
					
					errorMsg += '</ul>';
					
					$('#' + type + 'Form').before('<div id="errorMsg" class="clearfix">' + errorMsg + '</div>');
				}
			}
		});
}

function showComments(id)
{
	$('#Comments' + id).toggle('slow');
}

function addComment(id)
{
	$.ajax({
			type: 'POST',
			url: BASE_URL + 'news/add',
			dataType: 'json',
			data: $('#CommentForm' + id + ' :input'),
			beforeSend: function(){
				$('#newsErrorMsg').remove();
				$('#CommentForm' + id).before('<p id="newsFormLoading"><img src="' + STATIC_URL + 'images/ajax-loader-news.gif" alt="Loading" /></p>');
				$('#CommentForm' + id + ' input').attr('disabled', 'disabled');
			},
			success: function(data){
				$('#newsFormLoading').remove();
				
				if (typeof(data.success) != 'undefined')
				{
					$('#CommentForm' + id + ' input').removeAttr('disabled');
					$('#CommentForm' + id + ' textarea[name$="comment"]').val('');
					$('#CommentForm' + id + ' input[name$="name"]').val('Name');
					$('#CommentForm' + id + ' input[name$="email"]').val('Email');
					$('#CommentHeader' + id).before('<div class="comment">' + data.success + '</div>');
					$('#CommentForm' + id).before('<p class="success">Your comment has been added.</p>');
				}
				
				else
				{
					$('#CommentForm' + id + ' input').attr('disabled', '');
					var errorMsg = '<ul>';
					
					for (var key in data.errors)
					{
						if (data.errors.hasOwnProperty(key))
						{
							errorMsg += '<li>' + data.errors[key] + '</li>';
						}
					}
					
					errorMsg += '</ul>';
					
					$('#CommentForm' + id).before('<div id="newsErrorMsg" class="clearfix">' + errorMsg + '</div>');
				}
			}
		});
}

$(document).ready(function() {
	/** RFP Fields **/
	$('#rfp_name').focusin(function(){
		if ($(this).val() == 'Name')
		{
			$(this).val('');
		}
	})
	$('#rfp_name').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Name');
		}
	})
	
	$('#rfp_email').focusin(function(){
		if ($(this).val() == 'Email')
		{
			$(this).val('');
		}
	})
	$('#rfp_email').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Email');
		}
	})
	
	$('#rfp_comments').focusin(function(){
		if ($(this).val() == 'Tell Us About This Project')
		{
			$(this).val('');
		}
	})
	$('#rfp_comments').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Tell Us About This Project');
		}
	})
	
	/** Request a Meeting Fields **/
	$('#meeting_name').focusin(function(){
		if ($(this).val() == 'Name')
		{
			$(this).val('');
		}
	})
	$('#meeting_name').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Name');
		}
	})
	
	$('#meeting_email').focusin(function(){
		if ($(this).val() == 'Email')
		{
			$(this).val('');
		}
	})
	$('#meeting_email').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Email');
		}
	})
	
	$('#meeting_comments').focusin(function(){
		if ($(this).val() == 'Tell Us What\'s On Your Mind')
		{
			$(this).val('');
		}
	})
	$('#meeting_comments').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Tell Us What\'s On Your Mind');
		}
	})
	
	/** Resume fields **/
	$('#resume_name').focusin(function(){
		if ($(this).val() == 'Name')
		{
			$(this).val('');
		}
	})
	$('#resume_name').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Name');
		}
	})
	
	$('#resume_email').focusin(function(){
		if ($(this).val() == 'Email')
		{
			$(this).val('');
		}
	})
	$('#resume_email').focusout(function(){
		if ($(this).val() == '')
		{
			$(this).val('Email');
		}
	})
});

function filterResults(type)
{
	window.location = BASE_URL + 'work/?' + type + '=' + $('#' + type + ' option:selected').val();
}
