$(function() {
	$sort_list = $('#sort-list');
	$cloned_list = $('#sort-list').clone().removeAttr('id').addClass('cloned-list').css('display', 'none');
	$cloned_list.appendTo('.deliverables-list');
	$sort_results = $cloned_list.find('li');
	
	// QuickSand sort
	$('.sort-links a').live('click', function(e) {
		if($(this).hasClass('active') == false) {
			var sort_by = $(this).attr('class').split('-');
			
			if(sort_by[1] == 'all') {
				$sort_results = $cloned_list.find('li');
			} else {
				$sort_results = $cloned_list.find('li[data-type*="' + sort_by[1] + '"]');
			}
			
			$('.sort-links .active').removeClass('active');
			$(this).addClass('active');
			
			$sort_list.quicksand($sort_results, {
				adjustHeight: 'dynamic',
				attribute: 'data-type',
				duration: 800,
				easing: 'swing',
				useScaling: false,
				enhancement: function() {
					$('#sort-list .overlay').each(function() {
						$(this).css('opacity', 0);
					});
				}
			}, function() {
				var li_idx = 1;
				$('#sort-list li').each(function() {
					if(li_idx > 4) {
						li_idx = 1;
					}
					$(this).attr('rel', li_idx);
					li_idx++;
				});
				$('#sort-list .overlay').each(function() {
					var h = $(this).outerHeight();
					$(this).css({'top': '-' + h + 'px', 'opacity' : 1});
				});
				set_footer_height();
			});
		}
		e.preventDefault();
	});
	
	$('.detail a').live('click', function(e) {
		$('.sort-links .active').removeClass('active');
		//var sort_by = $(this).text().replace('/', '-').replace(', ', '-').replace(' ', '-').replace('& ', '').toLowerCase();
		var sort_by = $(this).attr('ref');
		if($(this).hasClass('l-client')) {
			var data_type = 'data-client';
		}
		if($(this).hasClass('l-type')) {
			var data_type = 'data-type';
			//var sort_link = sort_by.split('-');
		    //$('.sort-links .sort-' + sort_link[0]).addClass('active');
		    $('.sort-links .sort-' + sort_by).addClass('active');
		}
		if($(this).hasClass('l-industry')) {
			var data_type = 'data-industry';
		}
		
		$sort_results = $cloned_list.find('li[' + data_type + '*="' + sort_by + '"]');
		
		var bla = data_type.split('-');		
		$sort_list.quicksand($sort_results, {
			adjustHeight: 'dynamic',
			attribute: 'data-type',
			duration: 800,
			easing: 'swing',
			useScaling: false,
			enhancement: function() {
				$('#sort-list .overlay').each(function() {
					$(this).css('opacity', 0);
				});
			}
		}, function() {
			var li_idx = 1;
			$('#sort-list li').each(function() {
				if(li_idx > 4) {
					li_idx = 1;
				}
				$(this).attr('rel', li_idx);
				li_idx++;
			});
			$('#sort-list .overlay').each(function() {
				var h = $(this).outerHeight();
				$(this).css({'top': '-' + h + 'px', 'opacity' : 1});
			});
			set_footer_height();
		});
		e.preventDefault();
	});
	
	// Load more Projects in List
	$('.btn-load-more').live('click', function() {
		var projects_url = $(this).attr('href');
		load_more_projects(projects_url);
		return false;
	});
	
	// Nav Dropdowns
	$('#nav > ul > li').hover(function() {
		$(this).find('ul').hide();
		$(this).find('a:eq(0)').addClass('hover');
		$(this).find('.dd').show(0, function() {
			$(this).find('ul').slideDown(600, 'easeInOutExpo');
		});
	}, function() {
		$(this).find('.dd, .dd ul').hide();
		$(this).find('.hover').removeClass('hover');
	});
	
	// Nav Contact Link
	$('#nav-contact a.notext').click(function() {
		var form_offset = $('#contact-form').offset().top;
		$('html, body').animate({
			scrollTop : form_offset
		}, {
			duration: form_offset*0.6,
			easing: 'easeInOutSine',
			queue: false,
			complete: function() {
				$('#contact-form .blink:eq(0)').focus();
			}
		});
		return false;
	});
	
	$.validator.addMethod("notEqual", function(value, element, param) {
      return this.optional(element) || value != param;
    }, "Required");
    $.validator.addMethod("phone", function(phone_number, element) {
	var digits = "0123456789";
	var phoneNumberDelimiters = "()- ext.";
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	var minDigitsInIPhoneNumber = 10;
	s=stripCharsInBag(phone_number,validWorldPhoneChars);
	return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
	}, "Please enter a valid phone number");

	
	// Contact Form Validation
	$('#contact-form').validate({
        onfocusout: false,
        focusInvalid: false,
        onkeyup: false,
        onkeydown: false,
	    rules: {
		    name: {
		        notEqual: "Name"
		    },
		   email: function(value, element) {
			    return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(element.value);
			},
		    phone: {
		    	notEqual: "Phone number"
		    },
		    message: {
		    	notEqual: "Tell us about it"
		    }
	    },
        errorPlacement: function(error, element) {
        	element.next().slideDown();
        },
        submitHandler: function(form) {
        	$('.error:visible').each(function() {
        		$(this).slideUp();
        	});
        	$('#contact-form').fadeOut(function() {
        		$('.form-success').fadeIn();
        	});
        	$(form).ajaxSubmit();
        }
    });
	
	// Reveal contact form
	$('.form-success p a').click(function() {
		$('.form-success').fadeOut(function() {
			$('#contact-form').fadeIn();
		});
		return false;
	});
	
	// Blink fields
	$('.blink').focus(function() {
		if(this.value == this.title) {
			this.value = ''
		}
		if($(this).parents('#contact-form').length && $(this).next().is(':visible')) {
			$(this).next().slideUp();
		}
	}).blur(function() {
		if(this.value == '') {
			this.value = this.title
		}
	});
	
	// Footer fading image
	$('.img-office').hover(function() {
		$(this).find('span').stop(true,true).fadeIn(300);
	}, function() {
		$(this).find('span').stop(true,true).fadeOut(200);
	});
	
	// Featured project anchors
	$('.scroll-link').live('click', function() {
		var hr = $(this).attr('href');
		if($(hr).length > 0) {
			var item_offset = $(hr).offset().top - 30;
			$('html, body').animate({
				scrollTop: item_offset
			}, {
				duration: 1000,
				easing: 'easeInOutSine',
				queue: false
			});
		}
		return false;
	});
	
	// Deliverables Thumb hover
	$('.deliverables-list .thumb').live('mouseenter', function() {
		if($(this).hasClass('active') == false) {
			$(this).parents('li:eq(0)').addClass('hover');
			$(this).find('.overlay').animate({
				top : 0
			}, {
				queue : false,
				easing : 'easeInOutSine'
			});
		}
	}).live('mouseleave', function() {
		$(this).parents('li:eq(0)').removeClass('hover');
		$(this).find('.overlay').animate({
			top : '-' + $(this).find('.overlay').outerHeight() + 'px'
		}, {
			queue : false,
			easing : 'easeInOutSine'
		});
	});
	
	// Deliverables Details
	$('.deliverables-list .thumb').live('click', function() {
		if($(this).hasClass('active')) {
			return false;
		} else {
			$(this).addClass('new-clicked');
			show_project_info();
			return false;
		}
	});
	$('.deliverables-list .close').live('click', function() {
		hide_project_info();
	});
	
	// Featured projects prev & next
	//l_prev = false;
	//l_next = false;
	//$('.prev-project').live('click', function() {
	//	l_prev = true;
	//	var project_url = $(this).attr('href');
	//	console.log("prev function call");
	//	load_featured_project(project_url);
	//	return false;
	//});
    //
	//$('.next-project').live('click', function() {
	//	l_next = true;
	//	var project_url = $(this).attr('href');
	//	console.log("next function call");
	//	load_featured_project(project_url);
	//	return false;
	//});
	
	$(window).resize(function() {
		set_footer_height();
		if($('.project-nav').length > 0) {
			$('.project-nav-fixed').css('left', $('#main .center').offset().left+10);
		}
	});
	
	// IE6 PNG FIX
	if($.browser.msie && $.browser.version == 6) {
		DD_belatedPNG.fix('#logo a, #footer .socials .facebook, #footer .socials .twitter, #nav, #nav a.notext, #nav .dd-m, #sub-nav, .btn-launch, .btn-more, .btn-send, #content .deliverables-list .shadow-m, .side-link, .side-link .shadow, .prev-profile .icon, .next-profile .icon, .ico-carrot, .ico-no1, .ico-d, .ico-tool, .ico-wheat, .featured-clients td img, .prev-project .icon, .next-project .icon, #content .deliverables-list .thumb .icon');
	}
});

$(window).scroll(function() {
	if($('body').hasClass('projects') && $('.project-nav').length> 0) {
		var nav_offset = $('#content .project-nav').offset();
		var scroll = $(document).scrollTop();
		if($.browser.msie && $.browser.version == 6) {
			
		} else {
			if(scroll >= 737) {
				$('#content .main-project-image').addClass('main-image-padded');
				$('#content .project-nav').addClass('project-nav-fixed').css('left', nav_offset.left);
				if($.browser.msie && $.browser.version == 6) {
					$('.project-nav').css('top', $(window).height()-scroll);
				}
			} else { 
				$('#content .project-nav').removeClass('project-nav-fixed').attr('style', '');
				$('#content .main-project-image').removeClass('main-image-padded');
			}
		}
	}
});

$(window).load(function() {
	centered_headings();
	if ($('body').hasClass('home')) {
		var content_shown = 0;
		$('body').css('overflow', 'hidden');
		if($(window).height < 854) {
			var main_height = 854;
		} else {
			var main_height = $(window).height();
		}
		$('#main').css('height', main_height);
		$('.image-fader').fadeIn(1200);
		$('#logo').delay(400).fadeIn(1000);
		$(document).mousemove(function() {
			if(content_shown == 0) {
				content_shown = 1;
				$('#nav').fadeIn(1000);
				$('.tagline').fadeIn(1000, function() {
					if($('#main').hasClass('slide-footer')) {
						$('#main').animate({
							'height' : '534px'
						}, {
							complete : function() {
								set_footer_height();
							}
						});
					}
					if($('#main').hasClass('fade-footer')) {
						$('#footer').hide();
						$('#main').css('height', '534px');
						set_footer_height();
						$('#footer').fadeIn();
					}
				});
			}
			$('body').css('overflow', 'visible');
			
		});
	};
	if($('body').hasClass('projects')) {
		$('.thumb .overlay').each(function() {
			var h = $(this).outerHeight();
			$(this).css({'top': '-' + h + 'px', 'opacity' : 1});
		});
		if($('#sort-list').length) {
			$('#sort-list').css('height', $('#content').innerHeight()-$('.sort-links').outerHeight()-$('.row-bottom').height()-60);
		}
	}
});

function pageLoaded() {
	set_footer_height();
	wrap_nav_dropdowns();
	if($('#sub-nav').length>0) {
		center_subnav();
	}
	if($('body').hasClass('projects')) {
		$('.deliverables-list > ul').each(function() {
			var li_idx = 1;
			$(this).find('li').each(function() {
				if(li_idx > 4) {
					li_idx = 1;
				}
				$(this).attr('rel', li_idx);
				li_idx++;
			});
			
		});
		$('.thumb .overlay').each(function() {
			$(this).css('opacity', 0);
		});
		setDetailsHeight();
		wrap_project_dds();
		wrap_sort_links();
		apply_sort_types();
	}
};

function set_footer_height() {
	var all_h = parseInt($(window).height());
	if($('body').hasClass('home')) {
		var cnt_h = parseInt($('#main').height() + 333);
	} else {
		var cnt_h = parseInt($('#header').height() + $('#main').height() + 333);
	}
	if (all_h >= cnt_h) {
		$('#footer').css('padding-bottom', all_h-cnt_h);
	} else {
		$('#footer').css('padding-bottom', '');
	};
}

function wrap_nav_dropdowns() {
	$('#nav li ul').each(function() {
		var dd_content = $(this).html();
		$(this).parent().append('<div class="dd"><div class="dd-t"></div><div class="dd-m"><div class="cl"></div><div class="dd-i left"><ul></ul></div><div class="cl"></div></div><div class="dd-b"></div></div>');
		$(this).parent().find('.dd-i ul').append(dd_content);
		$(this).remove();
	});
}

function wrap_sort_links() {
	var container_width = $('#content').width();
	$('.sort-links ul').each(function() {
		$(this).find('li').each(function() {
			$(this).find('a').wrapInner('<span />');
	    });
	});
	$('.row-bottom').append('<span class="border-l"></span><span class="border-r"></span>').find('a').wrapInner('<span><span></span></span>');
	var link_width = $('.btn-load-more').width();
	var border_width = Math.round((container_width-link_width)/2 -12);
	$('.row-bottom .border-l, .row-bottom .border-r').animate({
		width: border_width
	});
}

function apply_sort_types() {
	$('.deliverables-list ul').each(function() {
		var temp = {};
		
		$(this).find('li').each(function() {
			if($(this).find('.detail').length > 0) {
				//var sort_client = $(this).find('.l-client').text().replace(', ', '-').replace(' ', '-').replace('/', '-').replace('& ', '').toLowerCase();
				//var sort_industry = $(this).find('.l-industry').text().replace(' ', '-').replace('/', '-').replace('& ', '').toLowerCase();
				var sort_client = $(this).find('.l-client').attr('ref');
				var sort_industry = $(this).find('.l-industry').attr('ref');
				sort_type = [];
				for(d=0;d<$(this).find('.l-type').length;d++) {
					var sort_type_txt = $(this).find('.l-type').eq(d).attr('ref').replace(' ', '-').replace('/', '-').replace('& ', '').toLowerCase();
					sort_type.push(sort_type_txt);
				}
				
				if(typeof(temp[sort_type]) == "undefined") temp[sort_type] = 0;
				if(typeof(temp[sort_industry]) == "undefined") temp[sort_industry] = 0;
				if(typeof(temp[sort_client]) == "undefined") temp[sort_client] = 0;
				
				
				$(this).attr('data-client', sort_client + '-' + temp[sort_client]);
				$(this).attr('data-industry', sort_industry + '-' + temp[sort_industry]);
				$(this).append('<span class="hidden" style="display: none;"></span>');
				var new_sort_type = [];
				for(i=0;i<sort_type.length;i++) {
					if(i<sort_type.length-1) {
						$(this).find('.hidden').append(sort_type[i] + '-' + temp[sort_type] + '-');
					} else {
						$(this).find('.hidden').append(sort_type[i] + '-' + temp[sort_type]);
					}
				}
				var all_sorts = $(this).find('.hidden').text();
				$(this).attr('data-type', all_sorts);
				$(this).find('.hidden').remove();
				
				temp[sort_type] ++;
				temp[sort_client] ++;
				temp[sort_industry] ++;
					
			}
		});
	});
}

function center_subnav() {
	var subnav_length = $('#sub-nav li').length;
	if(subnav_length%2 == 1) {
		var middle_item = Math.round(subnav_length/2)-1;
		var subnav_left = $('#sub-nav').offset().left;
		var middle_item_link = $('#sub-nav li').eq(middle_item).find("a");
		var middle_item_left = middle_item_link.offset().left-subnav_left + middle_item_link.width()/2;
		var subnav_padding = $("#sub-nav").width()/2 - middle_item_left;
		$("#sub-nav ul").css('padding-left', subnav_padding);
	} else {
		var item_w = 0;
		$('#sub-nav li').each(function() {
			item_w += $(this).outerWidth();
		});
		$('#sub-nav ul').css('padding-left', Math.round(($('#sub-nav').width()-item_w)/2));
	}
}

function centered_headings() {
	$('.section h4, .page-section h4').each(function() {
		var heading_width = $(this).width();
		$(this).wrapInner('<span class="heading-wrap" />');
		var text_width = $(this).find('.heading-wrap').width()+24;
		var border_width = Math.round((heading_width-text_width)/2);
		$(this).append('<span class="border-l"></span><span class="border-r"></span>');
		$(this).find('.border-l, .border-r').animate({'width': border_width});
	});
}

function heading_right_border() {
	var heading_width = $('.profile-entry h4').width();
	var heading_text_width = $('.profile-entry h4 .text').innerWidth()+12;
	var border_width = Math.round(heading_width-heading_text_width-20);
	$('.profile-entry h4 .border').animate({'width': border_width});
}

function wrap_project_dds() {
	$('.deliverables-list .shadows').each(function() {
		var pt = $(this).html();
		$(this).hide().removeClass('shadows').html('').append('<div class="shadow-t"></div><div class="shadow-m"><div class="cl"></div><div class="shadow-i left"></div><div class="cl"></div></div><div class="shadow-b"></div>').find('.shadow-i').append(pt);
	});
}

function show_project_info() {
	$('.new-clicked .overlay').animate({
		'top' : '-' + $('.new-clicked .overlay').outerHeight() + 'px'
	}, {
		queue : false,
		duration : 300,
		easing : 'easeInOutSine',
		complete : function() {
			$('.new-clicked').addClass('active');
		}
	});
	if($('#sort-list').length) {
		$('#sort-list').attr('style', '');
	}
	
	if($('.visible').length > 0) {
		
		var is_bottom = $('.clicked').offset().top < $('.new-clicked').offset().top;
		
		var t = $(window).scrollTop() - $('.visible').height();
		if(is_bottom)
			$('html, body').animate({scrollTop : t}, {duration : 700, queue : false });
		
		$('.visible .shadow-i').slideUp(700, function() {
			$('.visible').removeClass('visible');
			$('.deliverables-list li.cl').remove();
			
			$(this).parents('li:eq(0)').find('.active').removeClass('clicked').removeClass('active');
			$('.new-clicked').parents('li:eq(0)').find('.shadow-t').addClass('shadow-t' + $('.new-clicked').parents('li:eq(0)').attr('rel'));
			if($('.new-clicked').parents('li:eq(0)').attr('rel') == 1) {
				var margin_l = '-172px';
				var margin_r = '-892px';
				if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
					$('.new-clicked').parents('li:eq(0)').next().next().next().after('<li class="cl">&nbsp;</li>');
				} else {
					$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
				}
			}
			if($('.new-clicked').parents('li:eq(0)').attr('rel') == 2) {
				var margin_l = '-412px';
				var margin_r = '-652px';
				if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
					$('.new-clicked').parents('li:eq(0)').next().next().after('<li class="cl">&nbsp;</li>');
				} else {
					$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
				}
			}
			if($('.new-clicked').parents('li:eq(0)').attr('rel') == 3) {
				var margin_l = '-652px';
				var margin_r = '-412px';
				if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
					$('.new-clicked').parents('li:eq(0)').next().after('<li class="cl">&nbsp;</li>');
				} else {
					$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
				}
			}
			if($('.new-clicked').parents('li:eq(0)').attr('rel') == 4) {
				var margin_l = '-892px';
				var margin_r = '-172px';
				if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
					$('.new-clicked').parents('li:eq(0)').after('<li class="cl">&nbsp;</li>');
				} else {
					$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
				}
			}
			$('#content').css('overflow', 'visible');
			$('.new-clicked').parents('li:eq(0)').find('.shadow-i').hide();
			$('.new-clicked').parents('li:eq(0)').find('.project-info').css({
				marginRight : margin_r,
				marginLeft : margin_l,
				'opacity' : 1
			});
			$('.project-info:visible').hide();
			$('.new-clicked').parents('li:eq(0)').find('.project-info').show().find('.shadow-i').slideDown(function() {
				$(this).parents('.project-info:eq(0)').addClass('visible');
				$('#sort-list').css('height', $('#content').innerHeight()-$('.sort-links').outerHeight()-$('.row-bottom').height()-60);
			});		
			$('.new-clicked').removeClass('new-clicked').addClass('clicked');		
			
			$('html, body').animate({
				scrollTop : $('.clicked').offset().top-20
			}, {
				duration : 200,
				queue : false
			});
		});
		
		
		
	} else {
		$('.new-clicked').parents('li:eq(0)').find('.shadow-t').addClass('shadow-t' + $('.new-clicked').parents('li:eq(0)').attr('rel'));
		
		if($('.new-clicked').parents('li:eq(0)').attr('rel') == 1) {
			var margin_l = '-172px';
			var margin_r = '-892px';
			if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
				$('.new-clicked').parents('li:eq(0)').next().next().next().after('<li class="cl">&nbsp;</li>');
			} else {
				$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
			}
		}
		if($('.new-clicked').parents('li:eq(0)').attr('rel') == 2) {
			var margin_l = '-412px';
			var margin_r = '-652px';
			if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
				$('.new-clicked').parents('li:eq(0)').next().next().after('<li class="cl">&nbsp;</li>');
			} else {
				$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
			}
		}
		if($('.new-clicked').parents('li:eq(0)').attr('rel') == 3) {
			var margin_l = '-652px';
			var margin_r = '-412px';
			if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
				$('.new-clicked').parents('li:eq(0)').next().after('<li class="cl">&nbsp;</li>');
			} else {
				$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
			}
		}
		if($('.new-clicked').parents('li:eq(0)').attr('rel') == 4) {
			var margin_l = '-892px';
			var margin_r = '-172px';
			if($('.new-clicked').parents('ul:eq(0)').find('li').length > 4) {
				$('.new-clicked').parents('li:eq(0)').after('<li class="cl">&nbsp;</li>');
			} else {
				$('.new-clicked').parents('ul:eq(0)').find('li:last').after('<li class="cl"></li>');
			}
		}
		$('#content').css('overflow', 'visible');
		if($.browser.msie && $.browser.version < 8) {
			$('.new-clicked').parents('li:eq(0)').find('.project-info').css({
				marginRight : margin_r,
				marginLeft : margin_l
			}).show().find('.shadow-i').hide().slideDown(function() {
				$(this).parents('.project-info:eq(0)').addClass('visible');
				$('#sort-list').css('height', $('#content').innerHeight()-$('.sort-links').outerHeight()-$('.row-bottom').height()-60);
			});
		} else {
			$('.new-clicked').parents('li:eq(0)').find('.project-info').css({
				marginRight : margin_r,
				marginLeft : margin_l,
				'opacity' : 0
			}).slideDown(function() {
				$(this).animate({
					'opacity' : 1
				}, {
					complete : function() {
						$('.new-clicked').parents('li:eq(0)').find('.shadow-i').slideDown(function() {
							$(this).parents('.project-info:eq(0)').addClass('visible');
							$('#sort-list').css('height', $('#content').innerHeight()-$('.sort-links').outerHeight()-$('.row-bottom').height()-60);
							$('.new-clicked').removeClass('new-clicked').addClass('clicked');
						});
					}
				});
			});
			$('html, body').animate({
				scrollTop : $('.new-clicked').offset().top-20
			}, {
				queue : false
			});
			
		}
	}
}

function hide_project_info() {
	$('#content, #sort-list').css('height', '');
	$('.visible .shadow-i').slideUp(function() {
		$(this).parents('.project-info:eq(0)').animate({
			opacity : 0
		}, {
			complete : function() {
				$('.visible').parents('li:eq(0)').find('.clicked').removeClass('clicked').removeClass('active');
				$('.visible').slideUp(function() {
					$(this).removeClass('visible');
					$('.deliverables-list li.cl').remove();
					$('#sort-list').css('height', $('#content').innerHeight()-$('.sort-links').outerHeight()-$('.row-bottom').height()-60);
				});
			}
		});
	});
}

function load_featured_project(project_url) {
	if(l_prev) {
		var slide_pos = '-620px';
		var new_slide_pos = '620px';
		l_prev = false;
	}
	if(l_next) {
		var slide_pos = '620px';
		var new_slide_pos = '-620px';
		l_next = false;
	}
	var cont_height = $('#content .project-area').outerHeight();
	$('#main').append('<div class="loaded-project" style="position: absolute; width: 940px; top: -10000px; left: -10000px;"></div>');
	$.ajax({
		url: project_url,
		type: "GET",
		success: function(project) {
			$('.loaded-project').append(project).find('.wrapper').show(0, function() {
				$('.deliverables-list > ul').each(function() {
					var li_idx = 1;
					$(this).find('li').each(function() {
						if(li_idx > 4) {
							li_idx = 1;
						}
						$(this).attr('rel', li_idx);
						li_idx++;
					});
				});
			});
				
			$('#content .prev-project').each(function() {
				$(this).attr('href', $('.loaded-project .prev-project').attr('href')).find('.text').fadeOut(function() {
					$(this).text($('.loaded-project .prev-project:eq(0) .text').text()).fadeIn();
				});
			});
			$('#content .next-project').each(function() {
				$(this).attr('href', $('.loaded-project .next-project').attr('href')).find('.text').fadeOut(function() {
					$(this).text($('.loaded-project .next-project:eq(0) .text').text()).fadeIn();
				});
			});
			$('#content .project-area').css('height', cont_height).find('.wrapper').fadeOut(700, function() {
				$(this).html('');
				$('#content .projects-head h2').append('<span style="left: ' + slide_pos + ';">' + $('.loaded-project .projects-head h2 span').text() + '</span>');
				$('#content .projects-foot h2').append('<span style="left: ' + slide_pos + ';">' + $('.loaded-project .projects-foot h2 span').text() + '</span>');
				$('#content .projects-head h2 span:eq(0)').animate({
					'left' : new_slide_pos
				});
				$('#content .projects-head h2 span:eq(1)').animate({
					'left' : 0
				}, {
					complete: function() {
						$('#content .projects-head h2 span:eq(0)').remove();
					}
				});
				$('#content .projects-foot h2 span:eq(0)').animate({
					'left' : new_slide_pos
				});
				$('#content .projects-foot h2 span:eq(1)').animate({
					'left' : 0
				}, {
					complete: function() {
						$('#content .projects-foot h2 span:eq(0)').remove();
					}
				});
				
				wrap_project_dds();
				$('#content .project-area .wrapper').append($('.loaded-project .wrapper').html());
				
				$('.loaded-project').remove();
				
				$('#content .wrapper .thumb .overlay').each(function() {
					$(this).css('opacity', 0);
				});
				
				var new_cont_height = $('.project-area .wrapper').height();
				
				$('.project-area').animate({
					'height' : new_cont_height
				}, {
					complete : function() {
						var proj_nav_l = $('#content').offset().left;
						if($(window).scrollTop() > 800) {
							$('.project-nav').addClass('project-nav-fixed').css('left', proj_nav_l);
							$('html, body').animate({
								scrollTop: $('.projects-head').offset().top
							}, {
								duration: 2000
							});
						}
						$('#content .project-area .wrapper').fadeIn(function() {
							$('#content .wrapper .thumb .overlay').each(function() {
								var h = $(this).outerHeight();
								$(this).css({
									'top' : '-' + h + 'px',
									'opacity' : 1
								});
							});
							$('.project-area').css('height', '');
							centered_headings();
						});
					}
				});
			});
		}
	});
}

function load_more_projects(projects_url) {
	$.ajax({
		url: projects_url,
		type: "GET",
		success: function(projects) {
			$('#sort-list, .cloned-list:last').append(projects);
			apply_sort_types();
			var li_idx = 1;
			$('#sort-list li').each(function() {
				if(li_idx > 4) {
					li_idx = 1;
				}
				$(this).attr('rel', li_idx);
				li_idx++;
			});
			$('#sort-list .overlay').each(function() {
				var h = $(this).outerHeight();
				$(this).css('top', '-' + h + 'px');
			});
			wrap_project_dds();
			setDetailsHeight();
			$('#sort-list').css('height', $('#content').innerHeight()-$('.sort-links').outerHeight()-$('.row-bottom').height()-60);
		}
	});
}

function setDetailsHeight() {
	var d_height = 0
	if($('.detail').length > 0) {
		for(d=0;d<$('.detail').length;d++) {
			if($('.detail').eq(d).innerHeight() > d_height) {
				d_height = $('.detail').eq(d).innerHeight();
			}
		}
		$('.detail').each(function() {
			var pad_b = $(this).css('padding-bottom').replace('px', '');
			var pad_t = $(this).css('padding-top').replace('px', '');
			var hei = d_height - parseInt(pad_t) - parseInt(pad_b);
			$(this).css('height', hei);
		});
	}
}
function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
