var remail=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
var rempty=/.+/
var x="";

$(document).ready(function() {
	$('a.email1').each(function(){
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
		$(this).text(e);
	});
	if ($('.img-container').length){
		var first_img_path=$(".slider a:first").attr('href');
		$('.img-container').html('<img src="'+first_img_path+'"/>');
		$('.img-container').fadeIn(1000);
		$(".slider li:first").addClass("current");
	
		$(".slider a").live('click',function(){
			var img_path=$(this).attr('href');
			$(".slider li").removeClass("current");
			$(this).parent().addClass("current");
			$('.img-container').fadeOut(500, function() {
				$('.img-container').fadeIn(1400);
				$('.img-container').html('<img src="'+img_path+'"/>');
			});

			return false;
		});
	}
	$("a[rel=popup]").hover(
		function () {
			x=$(this).attr("title");
			$(this).attr("title","");
		}, 
		function () {
			$(this).attr("title",x);
		}	
	);
	$("a[rel=popup]").click(
		function () {
			if(x!="")
			$(this).attr("title",x);
		}	
	);

	$("a[rel=popup]").fancybox({
		'transitionIn'		: 'none',
		'padding'			: 3,
		'transitionOut'		: 'none',
		'titlePosition'		: 'over',
		'changeFade'		: 0,
		'cyclic'			: true,
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"><span id="lightbox_logo"></span><span id="nr">' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' &nbsp; </span>' + title : '') + '</span>';
		}
	});
	if($("a#example1").length){
		$("a#example1").fancybox({
			'titleShow'		: false
		});
	}
	if($("img.hascaption").length){
		$("img.hascaption").each(function() {
			$(this).wrap('<div class="figure"></div>')
			.after('<div class="caption">'+$(this).attr("title")+'</div>').removeAttr('title');
		});
	}
	if($(".figure").length){
		$(".figure").width($(this).find('img').width());
		$(".figure").mouseenter(function(){
			$(this).find('.caption').show();
		}).mouseleave(function(){
			$(this).find('.caption').hide();
		});
	}
	if($(".slider").length){
		$(".slider").jCarouselLite({
			btnNext:	".nextt",
			btnPrev:	".prev",
			visible:	6,
			auto: 0,
			beforeStart: function(a) {
			},
			afterEnd: function(a) {
			},
			easing:		"linear",
			circular:	true,
			scroll:		1,
			speed:		1000
		});
	}

	$('.error').hide();
	$('.text-input').css({backgroundColor:"#ebebeb"});

	$('.text-input').focus(function(){
		$(this).css({backgroundColor:"#eff4cb"});
	});
	$('.text-input').blur(function(){
		$(this).css({backgroundColor:"#ebebeb"});
	});

	$(".button").click(function() {

		$('.error').hide();
		var err=false;
		var comments = $("#comments").val();
		var email = $("#email").val();

		if (!rempty.test(comments)) {
			$("#comments_error").show();
			$("#comments").focus();
			err=true
		}
		if (!remail.test(email)) {
			$("#email_error").show();
			$("#email").focus();
			err=true
		}
		if (err) {
			return false;
		}

		var dataString = $("#contact_form form").serialize();
		$.ajax({
			type: "POST",
			url: "process.php",
			data: dataString,
			success: function() {
				$('#contact_form').html("<div id='message'></div>");
				$('#message').html("<h6>Comment Sent!</h6>")
				.append("<span>Your comment has been successfully submitted.</span>")
				.hide()
				.fadeIn(1500, function() {
					$('#message').append("<img id='checkmark' src='images/check.png' />");
				});
			}
		});
		return false;
	});
});
