jQuery.fn.equalize_height = function() {
	var h = 0;
	var collection = jQuery(this);	
	collection.each(function() {
		if (jQuery(this).height() > h) {
			h = jQuery(this).height();
		}
	});
	collection.each(function() {
		jQuery(this).height(h);
	});
}

jQuery(function($) {
	
	$('.not-a-link, .not-a-link a').click(function() {
		return false;
	});
	
	$('#navigation > ul > li.current-menu-item, #navigation > ul > li.current-menu-parent').addClass('hover').find('ul:eq(0)').show();
	$('#navigation > ul > li > ul > li.current-page-ancestor, #navigation > ul > li > ul > li.current-menu-parent').addClass('current_page_item').parent().show().parent().addClass('hover');
	
	if ($('body').hasClass('single-project') || $('body').hasClass('industry-taxonomy')) {
		var li = $('#navigation > ul > li:eq(0)');
		li.addClass('hover');
		li.find('ul:eq(0)').show();
	}
	
	$('#navigation > ul > li > a').click(function() {
		if ($(this).parent().find('ul').length) {
			window.location.href = $(this).parent().find('ul a:eq(0)').attr('href');
			return false;	
		}
	});
	
	$('#search .blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).css('color','#fff').val('');
		}
		$(this).parents('#search').addClass('active');
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).css('color','#999').val($(this).attr('title'));
			if ($(this).parents('.layout-orange').length) {$(this).css('color','#000')};
		}
		$(this).parents('#search').removeClass('active');
	});
	$('.blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	if ($('#slider').length) {
		$("#slider ul").jcarousel({
			auto: 4,
			scroll: 1,
			wrap: 'both',
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: mycarousel_itemVisibleInCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
	    });
	};
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		DD_belatedPNG.fix('#logo h1 a, #search .btn-go, #slider .caption, #slider .slider-navigation a, .title-bar, .post .image .caption, .overlay, .grid li span');
	};
	
	$('.grid a').click(function() {
		var th = $(this);
		if (!$('.grid').is('#all-projects-grid') && !$('.grid').is('#press-list')) {
			var x=0, y=0;
			
			if ($('.grid').is('#client-logos') || $('.grid').is('#team-list')) {
				$('.popup').hide();
				$(th.attr('href')).show();
			}
			
			$('.overlay').css('height', $(document).height()+'px').fadeIn();
					
			if ($('.grid').is('#team-list')) {
				x = $('#content').offset().left;
				y = $('#content').offset().top;
				$('.popup').css({'left': x+'px', 'top': y+'px'});
			} else {
				x = $(this).offset().left;
				y = $(this).offset().top;
				$('.popup').css({'left': x+'px', 'top': y+'px'});
			};
			return false;	
		}
	});
	
	$('.side-team-member-list ul ul a').click(function() {
		var href = $(this).attr('href');
		if (href != '#') {
			$('.popup').hide();
			$(href).show();	
			var x = $('#content').offset().left;
			var y = $('#content').offset().top;
			$('.overlay').css('height', $(document).height()+'px').fadeIn().find('.popup').css({'left': x+'px', 'top': y+'px'});
		}
		return false;
	});
	
	$('.clients-list a').click(function() {
		x = $('#content').offset().left + $('.clients-list li:eq(0)').width();
		y = $('#content').offset().top;
		
		$('.popup').hide();
		$($(this).attr('href')).show();
		
		$('.overlay').css('height', $(document).height()+'px').fadeIn().find('.popup').css({'left': x+'px', 'top': y+'px'});
		return false;
	});
	
	$('.popup .close').live('click',function() {
		$(this).parents('.overlay').fadeOut();
		return false;
	});
	
	$('.overlay').live('click',function(e) {
		if (!$(e.target).is('a')) {
			if ($(e.target).hasClass('popup') || $(e.target).parents('.popup').length) {
				return false;
			};
			$(this).fadeOut();
			return false;
		}
	});
	
	$('.awards a').hover(
		function() {
			var href = $(this).attr('href');
			$('.awards p').hide();
			$('.awards a').removeClass('active');
			$(this).addClass('active');
			$(href).fadeIn();
		},
		function() {}
	);
	$('.awards a').click(function() { return false; });
	
	$('.blog-sidebar .widget:last').addClass('widget-last');
	
	$('.lineup').equalize_height();
	$('.lineup-2').equalize_height();
	
	//Moving comment textarea above the other fields
	if ($('#respond .fields').length) {
		var comment_area = $('#respond textarea').clone();
		$('#respond textarea').remove();
		$(comment_area).insertBefore($('#respond .fields'));	
	}
	
	if (!$('body').hasClass('blog') && !$('body').hasClass('single-post') && !$('body').hasClass('archive')) {
		if ($('#main').hasClass('layout-white')) {
			$('body').addClass('white');
		}
	}
	
	var loading = false;

	$('#project-nav a.industry-link').click(function() {
    var url = $(this).attr('href').replace(/industry\/([^\/]*)\/{0,1}$/, "work/all-projects/?industry=$1");
	  window.location = url;
		return false;
	});

  function getUrlParameters() {
    var map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
      map[key] = value;
    });
    return map;
  }
	var industry = getUrlParameters()['industry'];

	function connect_project_grid_links() {
	  $('#all-projects-grid a.project-link').click(function () {
      var url = $(this).attr('href');
	    if(industry) {
	      url += "?industry=" + industry;
	    }
	    window.location = url;
	    return false;
	  });
	}
	connect_project_grid_links();

	$('#projects-nav a').click(function() {
		if ($(this).hasClass('active') || $('#all-projects-grid li:animated').length || loading) {
			return false;
		}
		loading = true;
		$('#projects-nav a.active').removeClass('active');
		$(this).addClass('active');
		
		$('#all-projects-grid li').fadeTo("medium", 0);

		var url = $(this).attr('href');
		
		$.ajax({  
			type: "GET",  
			url: url,
			success: function(response) { 
		    industry = url.replace(/^.*industry\/([^\/]*)\/{0,1}$/, "$1");
		    if(industry === url) industry = null;

				var html = $(response).find('#all-projects-grid').html();
				$('#all-projects-grid').html(html);
				$('#all-projects-grid li').hide().fadeTo("medium", 1);

				connect_project_grid_links();
				loading = false;
			}
		});
		return false;
	});
	
	$('.close-stf').click(function() {
		$('#send-to-friend').fadeOut();
		return false;
	});
	
	$('.open-stf').click(function() {
		if ($('#send-to-friend').length && $('#send-to-friend input').length) {
			$('#send-to-friend').fadeIn();
		}
		return false;
	});
	
	$('#send-to-friend').live('submit', function() {
		var th = $(this);
		var email_regex = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
		
		var to_email = $(this).find('input[name="to_email"]');
		if (!to_email.val().match(email_regex)) {
			alert('Please input the receiver\'s correct email');
			to_email.focus();
			return false;
		}
		
		var from_email = $(this).find('input[name="from_email"]');
		if (!from_email.val().match(email_regex)) {
			alert('Please input your correct email');
			from_email.focus();
			return false;
		}
		
		var your_message = $(this).find('textarea[name="your_message"]')
		if (!your_message.val() || your_message.val() == '' || your_message.val() == your_message.attr('title')) {
			alert('Please input your correct message');
			your_message.focus();
			return false;
		}
		
		$.ajax({  
			type: "POST",  
			url: $(this).attr('action'),
			data: "send_form=yes&to_email=" + to_email.val() + "&from_email=" + from_email.val() + '&your_message=' + your_message.val(),
			success: function(response) { 
				th.html('<p style="text-align: center; padding: 20px 0;">' + response + '</p>');
				setTimeout(function() {
					$('#send-to-friend').fadeOut();
				}, 1000);
			}
		});
		return false;
	});
	
	$('.open-ap-popup').click(function() {
		var href = $(this).attr('href');
		if ($(href).is(':visible')) {
			$(href).fadeOut();
			$(this).text($(this).text().replace('Hide', 'See'));
		} else {
			$(href).fadeIn();
			$(this).text($(this).text().replace('See', 'Hide'));
		}
		return false;
	});
	
});

function mycarousel_initCallback(carousel) {
	jQuery("#slider .slider-controls a").click(function(){
    	var x = parseInt(jQuery(this).text());
    	carousel.scroll(x);
    	return false;
    });
    
	jQuery('#slider .slider-navigation a.next').click(function() {
		carousel.next();
		return false;
	});
	
	jQuery('#slider .slider-navigation a.prev').click(function() {
		carousel.prev();
		return false;
	});
};

function mycarousel_itemVisibleInCallback(carousel, li, pos, state) {
	jQuery("#slider .slider-controls a").removeClass('active');
	jQuery("#slider .slider-controls a").eq(pos-1).addClass('active');
};

