$(document).ready(function() {

	// Layout fix
	fixLayout();
	$(window).resize(function(){
		fixLayout();
	});

	// Featured companies slider
	$('#featured-companies ul').bxSlider({prevText: '', nextText: ''});

	// twitter link target
	$('#twitter-feed ul li a').attr('target', '_blank');

	// Slider prev/next vertical align
	$('.bx-wrapper').each(function(){
		var arrows = $(this).find('.bx-prev, .bx-next');
		var top = ($(this).height()-arrows.height())/2;
		arrows.css('top', top+'px');
	});

	// Main menu animation
	moveImgToTarget($('#menu-main .current'), 0);

	$('#menu-main li a').hover(function(){
		moveImgToTarget($(this), 600);
	}, function(){
		moveImgToTarget($('#menu-main .current'), 400);
	});

	// Hover effect - logo change
	$('#logo').hover(
		function(){ // enter
			$('#logo-hover').stop().animate({left: '0'}, 300);
		}, function(){ // leave
			$('#logo-hover').stop().animate({left: '-250px'}, 300);
		}
	);

	// Hover effect - search box
	$('.search-box').hide(0);
	$('.search-link').mouseenter(function(){
		$(this).find('a').animate({right: '+=7px'},500);
		$(this).find('.search-box').animate({width:'show'},500, function(){
			$(this).find('input').focus();
		});
		$(this).unbind('mouseenter').find('a').click(function(e){
		  e.preventDefault();
      $('.search-box form').submit();
		});
	});

	// Front page paragraph width
	$('.frontpage .two-columns .item-text').each(function(){
		var strong = $(this).children('strong');
		strong.css('display', 'inline');
		$(this).children('p').width(strong.width()).css('margin-right', '0');
	});

	// Front page featured companies imgs
	var maxHeight = 0;
	$('.frontpage #featured-companies li').each(function(){
		if ($(this).height()>maxHeight) {maxHeight = $(this).height();}
	});
	$('.frontpage #featured-companies li').height(maxHeight);

	$('.frontpage #featured-companies li').each(function(){
		var diff = ($(this).height() - $(this).find('.company-logo').height())/2;
		$(this).find('.company-logo').css({marginTop:diff});
		diff = ($(this).height() - $(this).find('.company-text').height())/2;
		$(this).find('.company-text').css({marginTop:diff});
	});

	// Portfolio column + imgs
	var maxHeight = 0;
	$('.portfolio .four-columns li').each(function(){
		var diff = ($(this).find('.img').height() - $(this).find('.img img').height())/2;
		$(this).find('.img img').css({marginTop:diff});
		if ($(this).height()>maxHeight) {maxHeight = $(this).height();}
	});
	$('.portfolio .four-columns li').height(maxHeight+25);

	// Portfolio filter
	$('.portfolio #filter a').first().addClass('current');
	$('.portfolio #filter a').click(function(){
		var val = $(this).attr('href').substr(1);
		var items = $('.portfolio .four-columns li');
		var container = $('.portfolio .four-columns > .inner');
		var itemHeight = items.outerHeight(true);
		var targetHeight = itemHeight*items.length/4;
		if (container.data('current') !== val) {
			container.data('current', val);
			if (val == 'all') {
				container.animate({height:targetHeight}, 400, function(){
					items.fadeIn(400);
					container.css({height:'auto'});
				});
			} else {
				var hideItems = items.filter(function() {return $(this).data("category") !== val});
				var showItems = items.filter(function() {return $(this).data("category") == val});
				var targetHeight = itemHeight*Math.ceil(showItems.length/4);
				container.height(container.height());
				items.fadeOut(400);
				setTimeout(function(){
					container.animate({height:targetHeight}, 400, function(){
						showItems.fadeIn(400);
						container.css({height:'auto'});
					});
				}, 400);
			}
		}
		$('.portfolio #filter a').removeClass('current');
		$(this).addClass('current');
	});

	// Footer cols
	var maxHeight = 0;
	$('#footer-top .col').each(function(){
		if ($(this).height()>maxHeight) {maxHeight = $(this).height();}
	});
	$('#footer-top .col').height(maxHeight);

	// Menu img mover
	function moveImgToTarget(element, speed) {
		var number = ( $('#menu-main').position().left + element.position().left ) + element.outerWidth()/2 - $('.menu-active-img').width()/2;
		$('.menu-active-img').stop().animate({left: number+'px'}, speed);
	}

	// Layout fix function
	function fixLayout() {
		wWidth = $(window).width();
		if (wWidth < 1100) {
			$('.container-1100').width(1100);
			$('.container').not('.container-1100').each(function(){
				$(this).css({paddingRight: 1100-wWidth+'px', width: '100%'});
			});
		} else {
			$('.container').css({width: 'auto', paddingRight: '0'});
		}
		
		// var menuTwoTop = ($('#container-h1').height()-$('#menu2').height()+10)/2;
		// $('#menu2').css({left:$('#menu-main').position().left, top:menuTwoTop}).width($('#menu-main').width());
		$('#menu2').css({left:$('#menu-main').position().left}).width($('#menu-main').width());
		moveImgToTarget($('#menu-main .current'), 0);
	}

	// Contact form submit
	$('form .button-submit .button-25').click(function(){$(this).parents('form').submit(); return false;});

	// Contact form validation
	$('#contact-form input, #contact-form textarea').focus(function(){$(this).clearOnFocus();});
	$('#contact-form').submit(function(){
		var errorC = 0;
		var errorText = ''; var errorTextChanged = '';
		$(this).find('input, textarea').removeClass('error').each(function(){
			var val = $(this).val();
			if ($(this).is('[name="email"]') && isValidEmailAddress(val) == false) {
				errorC++;
				errorText += 'E-mail address not valid.<br />';
				$(this).addClass('error');
			} else {
				if (val == $(this).data('default') || $.trim(val) == '') {
					errorC++;
					if (errorTextChanged == 0) {
						errorTextChanged = 1;
						errorText += 'Please fill in higlighted columns properly.<br />';
					}
					$(this).addClass('error');
				}
			}
		});
		if (errorC > 0) {
			$('#error').html(errorText).slideDown();
			return false;
		} else {
			$('#error').slideUp().text('');
		}
	});

(function( $ ){

	$.fn.fadeOutNoSpace = function( speed, callbackFnk ) {
		var ourSpeed = 400;
		if ( speed ) {
			ourSpeed = speed;
		}
		this.each(function() {
			$(this).filter(':visible').css({position: 'absolute', top: $(this).position().top + 'px',	left: $(this).position().left + 'px', zIndex: '2'});
		});
		this.fadeOut(400, function(){
				$(this).css({position: 'relative', top: '0', left: '0', zIndex: '1'});

				if(typeof callbackFnk == 'function'){
					callbackFnk.call(this, data);
				}
		});

		return this;
	};
})( jQuery );

(function( $ ){
$.fn.clearOnFocus = function(){
		if ($(this).val() == $(this).data('default')) { $(this).val(''); }

		$(this).blur(function(){
			if ($.trim($(this).val()) == '') {$(this).val($(this).data('default'));}
		});
};
})( jQuery );

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

});

