var gallery = [];
var addPicture = function(url, description, active){
	gallery.push({
		url: url,
		description: description,
		active: active
	});
}
var isIE = '';

$(document).ready(function(){
	isIE = $.browser.msie;
//	initComingSoon();

	if($('a.tab').length > 0) {
		
		animateTimer(10000);
		setTimeout(function(){projectCarousel.intTimer = setTimeout('projectCarousel.getNextProject()', projectCarousel.TIMERINTERVAL)},3000)
		
		$("a.tab").click(function(){
			$('#timer span').stop(true).css({width: 0});
			projectCarousel.switchProject($(this), true);
			return false;
		});
	}

	if($('#inquiry').length > 0) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var name_error = email_error = false;
	
		$('#inquiry').submit(function() {
			if(document.getElementById('name').value == '') {
				name_error = true;
				document.getElementById('invalid').style.display = document.getElementById('invalid-name').style.display = 'block';
				document.getElementById('name-wrap').className = 'error';
				return false;
			} else {
				name_error = false;
			}
		
			if(reg.test(document.getElementById('email').value) == false) {
				email_error = true;
				document.getElementById('invalid').style.display = document.getElementById('invalid-email').style.display = 'block';
				document.getElementById('email-wrap').className = 'error';
				return false;
			} else {
				email_error = false;
			}
		
			return true;
		});
	
		$('#name').focus(function() {
			document.getElementById('invalid-name').style.display = 'none';
			document.getElementById('name-wrap').className = '';
			if(!email_error) {
				document.getElementById('invalid').style.display = 'none';
			}
		});
	
		$('#name').blur(function() {
			if(document.getElementById('name').value == '') {
				name_error = true;
				document.getElementById('invalid').style.display = document.getElementById('invalid-name').style.display = 'block';
				document.getElementById('name-wrap').className = 'error';
			} else {
				name_error = false;
			}
		});
	
		$('#email').focus(function() {
			document.getElementById('invalid-email').style.display = 'none';
			document.getElementById('email-wrap').className = '';
			if(!name_error) {
				document.getElementById('invalid').style.display = 'none';
			}
		});
	
		$('#email').blur(function() {
			if(reg.test(document.getElementById('email').value) == false) {
				email_error = true;
				document.getElementById('invalid').style.display = document.getElementById('invalid-email').style.display = 'block';
				document.getElementById('email-wrap').className = 'error';
			} else {
				email_error = false;
			}
		});
		
		
		$("#inquiry").submit(function(){
			var emailFieldValue = $('#email').get(0).value;
			var nameFieldValue = $('#name').get(0).value;
			var messageFieldValue = $('#message').get(0).value;
			$.ajax({
				type: "POST",
				url: "contactform.php",
				data: "email="+emailFieldValue+"&name="+nameFieldValue+"&message="+messageFieldValue,
				success: function(msg){
					if(msg == "Your message was sent"){
						$('p.success').show();
						$('p.hideafter').hide();
						$('#email').get(0).value = '';
						$('#name').get(0).value = '';
						$('#message').get(0).value = '';
					}else if(msg == "There was a problem."){
						alert("A problem has occured please try again!");
					}
				}
			});
			return false;
		});
		
	}

	if($('#gallery').length > 0) {
		$('#gallery_total').html(gallery.length);
		for(var i = 0; i < gallery.length; ++i) {
			$('ul.thumbnails').html($('ul.thumbnails').html() + '<li' + (i == 0 ? ' class="active"' : '') + '><img src="' + gallery[i].url + '" class="noscale" alt="" title="' + gallery[i].description + '" /></li>');
		}
	
		$('#main_image').hover(
			function() { $('.thumbnails-wrap').show(); },
			function() { $('.thumbnails-wrap').hide(); }
		);
	
		$('ul.thumbnails').galleria({
			insert    : '#main_image',
			onImage   : function(image, caption, thumb) {
			
				// fade in the image & caption
				image.css('display', 'none').fadeIn(1000);
				caption.hide(); // we dont need this crap here
			
				thumb.parent().parent().children().removeClass('selected');
				thumb.parent().addClass('selected');
			
				$('#gallery_caption').html(caption.html());
			
				for(var i = 0; i < gallery.length; ++i) {
					if(gallery[i].description == caption.html()) {
						$('#gallery_current').html((i + 1).toString());
						break;
					}
				}
			},
		
			onThumb : function(thumb) {
			
				// fetch the thumbnail container
				/*var _li = thumb.parents('li');
			
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
				// fade in the thumbnail when finnished loading
				thumb.css({display: 'none',opacity: _fadeTo}).fadeIn(1500);
			
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast', 0.3); } // don't fade out if the parent is active
				)*/
			}
		});
	
		//$('.thumbnails-wrap').hide();
	}
});

function animateTimer(timeInterval){
	var MAXWIDTH = $('#timer').width() + 'px';
	$('#timer span').animate({width: MAXWIDTH}, (timeInterval-1000)).animate({width: 0}, 1000);
}
	
var projectCarousel = {
	TIMERINTERVAL: 7000,
	RESTARTINTERVAL: 10000,
	nextProject: $(".active", ".tabs"),
	intTimer: '',
	switchProject: function(srcObj, clearTimer){
		if(!srcObj) srcObj = this.nextProject;
		$(".active").removeClass("active");
		$(srcObj).addClass("active");
		$(".mid-box").hide();
		$(".left-box").hide();
		var content_show = $(srcObj).attr("rel");
		if(!isIE){
			$("#"+content_show).fadeIn();
		}else{
			$("#"+content_show).show();
		}		
		var content_showb = $(srcObj).attr("rel");
		if(!isIE){
			$("#"+content_showb+"b").fadeIn();
		}else{
			$("#"+content_showb+"b").show();
		}		
		if(clearTimer){
			clearTimeout(this.intTimer);
			//this.intTimer = setTimeout('projectCarousel.getNextProject()', projectCarousel.RESTARTINTERVAL)
			//animateTimer(projectCarousel.RESTARTINTERVAL);
		}else{
			this.intTimer = setTimeout('projectCarousel.getNextProject()', projectCarousel.TIMERINTERVAL)
			animateTimer(projectCarousel.TIMERINTERVAL);
		}
	}, 
	getNextProject: function(){
		if($(".active", ".tabs").next().is('a')){
			this.nextProject = $(".active", ".tabs").next().get(0);
		}else{
			this.nextProject = $("a", ".tabs").get(0);
		}
		this.switchProject();
	}
}

jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
	this.each(function() {
		$(this).css({'position':'relative'});
		for (var x=1; x<=intShakes; x++) {
			$(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
			.animate({left:intDistance}, ((intDuration/intShakes)/2))
			.animate({left:0}, (((intDuration/intShakes)/4)));
		}
	});
	return this;
};

function showOverlay(overlayCover, lightboxCont){
	var docHeight = $(document).height();
	overlayCover.height(docHeight).fadeTo("slow", 0.81);
	lightboxCont.fadeTo("slow", 1)
}

function initComingSoon(){
	var overlayCover = $('#overlay-cover');
	var lightboxCont = $('#lightbox-container');
	$('#email-field').get(0).value = '';
	overlayCover.hide();
	lightboxCont.hide();
	overlayCover.fadeTo(0, 0, function(){
		overlayCover.css('display', 'block');
	});
	lightboxCont.fadeTo(0, 0, function(){
		lightboxCont.css('display', 'block');
	});
	setTimeout(function(){showOverlay(overlayCover, lightboxCont)},1000)

	$("#coming-soon-form").submit(function(){
		var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		var emailFieldValue = $('#email-field').get(0).value;
		if(pattern.test(emailFieldValue)){
			$.ajax({
				type: "POST",
				url: "coming-soon-form.php",
				data: "email="+emailFieldValue,
				success: function(msg){
					if(msg == "Your message was sent"){
						$("#coming-soon-form").hide();
						$('#success-msg').addClass('active');
						$('#success-msg').text('Thanks for submitting your email, we\'ll let you know as soon as we launch!')
					}else if(msg == "There was a problem."){
						alert("A problem has occured please try again!");
					}
				}
			});
		}else{
			$('#coming-soon-form').shake(2, 10, 400);
			$('#email-field').get(0).value = '';
		}
		return false;
	});
}