$(document).ready(function() {
// sfhover
	$('.nav li').hover(
		function(){
			$(this).addClass('sfhover');
		},
		function(){
			$(this).removeClass('sfhover');
		}
	);
// external links
$('a[rel="external"]').attr('target', '_blank');
// capabilities gallery
	lgMax = $('.list-gallery li').length;
	$('.list-gallery li').click(function(){
		$('.list-gallery li').removeClass('active');
		$(this).addClass('active');
		ind = $('.list-gallery li').index($(this));
		imgIndex = ind<10 ? '0'+ind : ind;
		$('.img-desc').text($(this).text());
		$('.list-gallery img').attr('src', 'images/capabilities/'+imgIndex+'.jpg')
	})
	$('.list-gallery .lg-in-forward').click(function(){
		ind = $('.list-gallery li').index($('.list-gallery li.active'))+1;
		ind %= lgMax;
		$('.list-gallery li:eq('+ind+')').click();
	})
	$('.list-gallery .lg-in-back').click(function(){
		ind = $('.list-gallery li').index($('.list-gallery li.active'))-1;
		if (ind<0) {
			ind= lgMax-1;
		}
		$('.list-gallery li:eq('+ind+')').click();
	})

// image rotation
	if($('.image-area').length > 0) {
		var replaceInterval = 2000;
		var offset = 20;
		var imgsTotal = $('.image-area img').length;
		var double = imgsTotal == 3 ? true : false;
		var current = [0, 1, 2, 3];
		var oldImgIndex = 0;
		var imgBase = 'images/image-area/';
		var imgExt = '.jpg';

		$.preload($.merge( $.merge([],imgSingle), imgDouble), {
		    base: imgBase,
		    ext: imgExt,
			enforceCache: true,
			onFinish: function() {
				$('.image-area img').each(function(){
					$(this).css({
						left: $(this).position().left,
						top: 0,
						zIndex: 3
					})
				})
				$('.image-area img').each(function(){
					$(this).css({
						position: 'absolute'
					})
				})
				setTimeout(function(){replaceImg(current, double, oldImgIndex)}, replaceInterval);
			}
		});


		function replaceImg(current, double, old) {
			var oldImgIndex;
			do {
				oldImgIndex = Math.floor(Math.random()*imgsTotal)
			} while (oldImgIndex == old);
			var replaceDouble = double && oldImgIndex == 0;
			images = replaceDouble ? imgDouble : imgSingle;
			var newImgIndex;
			do {
				newImgIndex = Math.floor(Math.random()*images.length);
			} while ($.inArray(newImgIndex, current) > -1);

			newUrl = images[newImgIndex];
			oldImg = $('.image-area img:eq('+oldImgIndex+')')
			newImg = '<img alt="" class="newImg" src="'+imgBase+images[newImgIndex]+imgExt+'" />'
			newLeft = oldImg.position().left;

			oldImg
				.after(newImg)
				.css({zIndex: 1})
				.animate({
					left: newLeft-offset,
					opacity: 0
				},function(){$(this).remove()})
			$('.image-area img.newImg')
				.removeClass('newImg')
				.css({
					position: 'absolute',
					left: newLeft+offset,
					top: 0,
					opacity: 0,
					zIndex: 3
				})
				.animate({
					left: newLeft,
					opacity: 1
				})

			current[oldImgIndex] = newImgIndex;
			replaceInterval = Math.floor(Math.random()*1000)+2000
			setTimeout(function(){replaceImg(current, double, oldImgIndex)}, replaceInterval)
		}
	}
	
// ajax form
	if($('body.contact').length>0){
		$('#contact-form').ajaxForm({
			target: '#contact-form-container',
			beforeSubmit: validation
		});
		$('<div id="busy"><div class="busy-inner"></div></div>')
			.ajaxStart(function() {$(this).show();})
			.ajaxStop(function() {$(this).hide();})
			.appendTo('#contact-form-container');
	}
});

imgDouble = [
	"double01",
	"double02",
	"double03",
	"double04",
	"double05",
	"double06",
	"double07",
	"double08",
	"double09"
]
imgSingle = [
	"single01",
	"single02",
	"single03",
	"single04",
	"single05",
	"single06",
	"single07",
	"single08",
	"single09",
	"single10",
	"single11",
	"single12",
	"single13",
	"single14",
	"single15",
	"single16",
	"single17"
]

function validation() {

	var errors = 0;
	$("input.required").each(function(){
		if ($(this).val() == "") {
			$(this).parent('div').children("label").addClass("error");
			errors++;
		} else {
			$(this).parent('div').children("label").removeClass("error");
		}
	});	
	$("input#email").each(function(){
		var email_value = $(this).val();
		if (email_value.indexOf("@") < 0 || email_value.indexOf(".") < 0) {
			$(this).parent('div').children("label").addClass("error");
			errors++;
		}else {
			$(this).parent('div').children("label").removeClass("error");
		}
	});
	$("textarea.required").each(function(){
		if ($(this).val() == "") {
			$(this).parent('div').children("label").addClass("error");
			errors++;
		} else {
			$(this).parent('div').children("label").removeClass("error");
		}
	});
	
	//return errors;
	if (errors > 0) {
		return false;
	} else {
		return true;
	}
}

