jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){
	
	$('#slideshow').serialScroll({
		items:'li',
		prev:'#screen a.vorige',
		next:'#screen a.volgende',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:900,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: false //click on the images to scroll to them
	});
	
	menuFade();
	galleryFade();
	$('#slideshow a').lightBox();
	$('a.kaart').lightBox();
	toonDiensten();
});

function formcheck_contact()
{
	if (document.formname.ab.value=="")
	{
		alert("Gelieve je naam in te vullen.");
		document.formname.ab.focus();
		return false
	}
	if (document.formname.bem.value!="")
	{
		if (document.formname.bem.value.indexOf("@")==-1 || document.formname.bem.value.indexOf(".")==-1 || document.formname.bem.value.length<6) 
		{
			alert("Er zit een fout in je email adres.");
			document.formname.bem.focus();
			return false
		}
	}
	else
	{
		alert("Gelieve je email adres in te vullen.");
		document.formname.bem.focus();
		return false
	}
	if (document.formname.message.value=="")
	{
		alert("Gelieve een bericht of vraag in te vullen.");
		document.formname.message.focus();
		return false
	}
}

function menuFade() {
    $('#nav li').removeClass('hover');
    $('#nav li a').append('<span class="hover"></span>').each(function () {
        var $span = $('> span.hover', this).css('opacity', 0);
        $(this).hover(function () {
            $span.stop().fadeTo(300, 1);
        }, function () {
            $span.stop().fadeTo(700, 0);
        });
    });
}

function galleryFade() {
	$('#slideshow a img').css('opacity',0.8);
	$('#slideshow a img').each(function () {
		$(this).hover(function () {
			$(this).stop().fadeTo(300, 1);
		}, function () {
			$(this).stop().fadeTo(300, 0.8);
		});
	});
	
}

function getScrollTop(){
    if(typeof pageYOffset!= 'undefined'){
        //most browsers
        return pageYOffset;
    }
    else{
        var B= document.body; //IE 'quirks'
        var D= document.documentElement; //IE with doctype
        D= (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}

function overgordijnen(){
	var height = $('#container').height();
	$('div#overgordijnen-overlay').css('opacity',0);
	$('div#overgordijnen-overlay div.bg').css('opacity',0);
	$('div#overgordijnen-overlay div.bg div.content').css('opacity',0);
	$('div#overgordijnen-overlay').css('display','block');
	$('div#overgordijnen-overlay').css('height',height+'px');
	$('div#overgordijnen-overlay').css('z-index',100);
	
	$('div#overgordijnen-overlay').stop().fadeTo(300,1, function () {
		$('div#overgordijnen-overlay div.bg').stop().fadeTo(300,1,function () {
			$('div#overgordijnen-overlay div.bg div.content').stop().fadeTo(600,1);
		});
	});
}

function zonwering(){
	var height = $('#container').height();
	$('div#zonwering-overlay').css('opacity',0);
	$('div#zonwering-overlay div.bg').css('opacity',0);
	$('div#zonwering-overlay div.bg div.content').css('opacity',0);
	$('div#zonwering-overlay').css('display','block');
	$('div#zonwering-overlay').css('height',height+'px');
	$('div#zonwering-overlay').css('z-index',100);
	
	$('div#zonwering-overlay').stop().fadeTo(300,1, function () {
		$('div#zonwering-overlay div.bg').stop().fadeTo(300,1,function () {
			$('div#zonwering-overlay div.bg div.content').stop().fadeTo(600,1);
		});
	});
}

function gordijnrails(){
	var height = $('#container').height();
	$('div#gordijnrails-overlay').css('opacity',0);
	$('div#gordijnrails-overlay div.bg').css('opacity',0);
	$('div#gordijnrails-overlay div.bg div.content').css('opacity',0);
	$('div#gordijnrails-overlay').css('display','block');
	$('div#gordijnrails-overlay').css('height',height+'px');
	$('div#gordijnrails-overlay').css('z-index',100);
	
	$('div#gordijnrails-overlay').stop().fadeTo(300,1, function () {
		$('div#gordijnrails-overlay div.bg').stop().fadeTo(300,1,function () {
			$('div#gordijnrails-overlay div.bg div.content').stop().fadeTo(600,1);
		});
	});
}

function vouwgordijnen(){
	var height = $('#container').height();
	$('div#vouwgordijnen-overlay').css('opacity',0);
	$('div#vouwgordijnen-overlay div.bg').css('opacity',0);
	$('div#vouwgordijnen-overlay div.bg div.content').css('opacity',0);
	$('div#vouwgordijnen-overlay').css('display','block');
	$('div#vouwgordijnen-overlay').css('height',height+'px');
	$('div#vouwgordijnen-overlay').css('z-index',100);
	
	$('div#vouwgordijnen-overlay').stop().fadeTo(300,1, function () {
		$('div#vouwgordijnen-overlay div.bg').stop().fadeTo(300,1,function () {
			$('div#vouwgordijnen-overlay div.bg div.content').stop().fadeTo(600,1);
		});
	});
}

function toonDiensten() {
	
	$('a.overgordijnen').click(function () {
		if(getScrollTop()!=0){
			$.scrollTo( 0, 600, function (){
				overgordijnen();
			});
		} else {
			overgordijnen();
		}
	});
	
	$('a.zonwering').click(function () {
		if(getScrollTop()!=0){
			$.scrollTo( 0, 600, function (){
				zonwering();
			});
		} else {
			zonwering();
		}
	});
	
	$('a.gordijnrails').click(function () {
		if(getScrollTop()!=0){
			$.scrollTo( 0, 600, function (){
				gordijnrails();
			});
		} else {
			gordijnrails();
		}
	});
	
	$('a.vouwgordijnen').click(function () {
		if(getScrollTop()!=0){
			$.scrollTo( 0, 600, function (){
				vouwgordijnen();
			});
		} else {
			vouwgordijnen();
		}
	});
	
	$('a.sluiten').click(function () {
		$('div.overlay').fadeTo(300,0,function () {
			$('div.overlay').css('display','none');
			$('div.overlay').css('z-index',-1);
		});
	});
	$('div.overlay').click(function () {
		$('div.overlay').fadeTo(300,0,function () {
			$('div.overlay').css('display','none');
			$('div.overlay').css('z-index',-1);
		});
	});
}

var http_request = false;
function makeRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
		 if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
				//http_request.overrideMimeType('text/xml');
				http_request.overrideMimeType('text/html');
		 }
	} else if (window.ActiveXObject) { // IE
		 try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e) {
				try {
					 http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
		 }
	}
	if (!http_request) {
		 alert('Cannot create XMLHTTP instance');
		 return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}

function alertContents() {
	if (http_request.readyState == 4) {
		 if (http_request.status == 200) {
				//alert(http_request.responseText);
				result = http_request.responseText;
				document.getElementById('output').innerHTML = result;
				if(result=="Uw bericht werd verstuurd en we zullen dit zo vlug mogelijk beantwoorden."){
					$('input#naam').val("");
					$('input#e-mail').val("");
					$('input#tel').val("");
					$('textarea#bericht').val("");
				}
				showResult();      
		 } else {
				document.getElementById('output').innerHTML = "Er is een fout opgetreden. Probeer later opnieuw aub."; 
				showResult(); 
		 }
	}
}

function showResult(){
		if(getScrollTop()!=0){
				$.scrollTo( 0, 600, function (){
				showResult2();
			});
		} else {
			showResult2();
		}
}

function showResult2(){
	var height = $('#container').height();
	$('div.overlay-not').css('opacity',0);
	$('div.overlay-not div.bg').css('opacity',0);
	$('div.overlay-not div.bg div.content').css('opacity',0);
	$('div.overlay-not').css('display','block');
	$('div.overlay-not').css('height',height+'px');
	$('div.overlay-not').css('z-index',100);
	
	$('div.overlay-not').stop().fadeTo(300,1, function () {
		$('div.overlay-not div.bg').stop().fadeTo(300,1,function () {
			$('div.overlay-not div.bg div.content').stop().fadeTo(600,1);
		});
	});
	
	$('a.sluiten-not').click(function () {
		$('div.overlay-not').fadeTo(300,0,function () {
			$('div.overlay-not').css('display','none');
			$('div.overlay-not').css('z-index',-1);
		});
	});
	$('div.overlay-not').click(function () {
		$('div.overlay-not').fadeTo(300,0,function () {
			$('div.overlay-not').css('display','none');
			$('div.overlay-not').css('z-index',-1);
		});
	});
}

function get(obj) {
	
	var naam = escape($('input#naam').val());
	var mail = escape($('input#e-mail').val());
	var tel = escape($('input#tel').val());
	var bericht = escape($('textarea#bericht').val());
	
	var getstr = "?naam="+naam+"&mail="+mail+"&tel="+tel+"&bericht="+bericht;
	
	makeRequest('get.php', getstr);
}
