$(document).ready(function(){
	if ($("form.validate").length > 0) startValidation();
	
	if ($("#rightbar").height() > $("#leftbar").height()) {
		$("#leftbar").css("height", $("#rightbar").height() + "px");
	}

	//$('#slideshow ul').jcarousel({scroll: 1, auto: 1,wrap: 'last'});	
	
	// $('#slideshow ul').jcarousel({
		// scroll: 1, 
		// auto: 1,
		// wrap: 'circular',
		// itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
	    // itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
	// });
	
	$('#verstuur').mouseover(function(){$(this).attr("src", "assets/images/verstuur_hover.jpg")});
	$('#verstuur').mouseout(function(){$(this).attr("src", "assets/images/verstuur.jpg")});
	
	if ($('#fixheight').height() < 195) $('#fixheight').css("height", "195px");
	
	$("#callmeback").click(function(){
		if ($("body #fadeback").length < 1) {
			$("body").prepend('<div id="fadeback"></div>');
			$("body #fadeback").css({
				zIndex : "10",
				backgroundColor : "black",
				opacity: "0.5",
				display: "none",
				position: "absolute",
				top: 0,
				left: 0,
				height: $(document).height() + "px",
				width: $(document).width() + "px"
			});
			$("body").prepend('<div id="contactcontainer"></div>');
			$("body #contactcontainer").css({
				zIndex : "100",
				display: "none",
				color: "white",
				fontSize: "10pt",
				fontFamily: 'Trebuchet MS',
				padding: "10px",
				position: "absolute",
				top: (parseInt($(window).height()/2) - parseInt(233/2)) + "px",
				left: (parseInt($(document).width()/2) - parseInt(350/2)) + "px",
				height: "233px",
				width: "350px",
				textAlign: "left"
			});		

			
			$.ajax({
				type: "GET",
				data: "sample=123",
				url: "call-me-back-form.php",
				success: function(msg) {
					$("body #contactcontainer").append(msg);
					$("body #contactcontainer form").submit(formsubmit);
					$("body #contactcontainer form #submitbtn").mouseover(function(){$(this).attr("src", "assets/images/verstuur_hover.jpg")});
					$("body #contactcontainer form #submitbtn").mouseout(function(){$(this).attr("src", "assets/images/verstuur.jpg")});
				}
			});							
			
			$("body #fadeback").click(hideBackFade);
		}
		
		$("body #fadeback").fadeIn("fast", function(){
			$("body #contactcontainer").fadeIn("fast");
		});	
		
		return false;
	});
});

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

function hideBackFade() {
	$("body #contactcontainer").fadeOut("fast", function(){
		$("body #fadeback").fadeOut("fast");
	})		
}

function startValidation() {
	$("form.validate").each(
		function (i, val) {			
			$(this).find(":text").each(
				 function () {
					if ($(this).val() != "") $(this).attr("oldval", $(this).val());
					
					$(this).blur(function() {
						if ($(this).hasClass("required")) {
							if ($(this).hasClass("valid")) $(this).removeClass("valid");
							if ($(this).hasClass("invalid")) $(this).removeClass("invalid");
							
							if ($(this).val() == "") { 
								$(this).addClass("invalid");
							} else if ($(this).hasClass("emailadd")) {
								var tEmail = new REgExp("\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$");
								if (!tEmail.test($(this).text)) $(this).addClass("invalid");
							} else {
								$(this).addClass("valid");
							}
						}
						
						if ($(this).attr("oldval") != "") {
							if ($(this).val() == "") {
								$(this).val($(this).attr("oldval"));
							}
						}
					});
					
					$(this).focus(function() {
						if ($(this).val() == $(this).attr("oldval")) {
							$(this).val("");
						}
					});					
				}
			);			
	
			$(this).submit(formsubmit);		
		}
	);
}

function formsubmit() { 
	if ($(this).hasClass("callback")) {
		if (validateChildren(this)) {
			frmurl = $(this).attr("action");
			frmdata = $(this).serialize();
			myform = $(this);
			$.ajax({
				type: "POST",
				url: frmurl,
				data: frmdata,
				success: function(msg) {
					if (msg == "success") {
						myform.fadeOut("normal", function(){myform.parent().find("#sentmessage").html("Thank you!<br/>Message Sent! <a href=\"#\">Send again</a>").fadeIn();});
					} else {
						myform.fadeOut("normal", function(){
							myform.parent().find("#sentmessage").html("Error sending! <a href=\"#\">Try again</a>.").fadeIn();
							myform.parent().find("#sentmessage a").click(function(){
								myform.parent().find("#sentmessage").fadeOut("normal", function(){myform.fadeIn("normal");});
								return false;
							});
						});									
					}
				}
			});
		} 
		return false;
	} else {				
		return validateChildren(this);
	}
}

function validateChildren(myForm) {
	if ($(myForm).find(".required").length > 0) {
		if ($(myForm).find(".valid").length == $(myForm).find(".required").length) {
			return true;
		} else {
			$(myForm).find(".required").each(
				function() {
					if ($(this).val() == "") $(this).addClass("invalid");
				}
			);
			return false;
		}
	} else {
		retval = true;
		$(myForm).find("input[type='text']").each(function(){
			if ($(this).val() == $(this).attr("oldval")) retval = false;			
		});		
		return retval;
	}
}


function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<a href="'+item.href+'"><img src="' + item.url + '" alt="' + item.title + '" /><a>';
};

/* --------------------------------------------------------------------------- */
