// JavaScript Document

$(document).ready(function(){
	window.animating = {};
	window.animating.left = 0;
	
	var pathname = jQuery.url.segment(0);
	if (pathname == null || pathname == "index.php"){
		$('#nav_home').addClass('active');
	} else {
		$('#nav_'+pathname).addClass('active');
	}

	var subpathname = jQuery.url.segment(1);
	if (subpathname == null){
		$('nav.subnav ul li.index').addClass('active');
	} else {
		subpathname = str_replace('.php', '', subpathname);
		$('nav.subnav ul li.'+subpathname).addClass('active');
	}
	
	var leftsubpathname = jQuery.url.segment(2);
	
	var p = $('nav.subnav ul li.active a');
	var offset = p.offset();
	if (offset != null){
		var awidth = ( $('nav.subnav ul li.active a').width() /2)-14;
		$('#header_arrow').stop().animate(
			{left: offset.left + awidth}
		, 1000);
	}
	
	var p = $('footer section article.active');
	var offset = p.offset();
	if (offset != null){
		var awidth = $('footer section article.active .header').width();
		$('#footer_bar').stop().animate({
			left: offset.left,
			marginLeft: 0
		}, 1000, "easeOutExpo");
	}
	
	$('header nav#main ul li:not(.active) a').hover(
		function(){
			$(this).stop(true, true).animate({
				color: '#FFF'
			}, 100);
		}, 
		function(){
			$(this).animate({
				color: '#809BA6'
			}, 1000);
		}
	);
	
	$('header nav#main ul li#nav_home:not(.active) a').hover(
		function(){
			$('#nav_home_img_active').animate({
				opacity: 1
			}, 100);
		}, 
		function(){
			$('#nav_home_img_active').animate({
				opacity: 0
			}, 100);
		}
	);
	
	$('header #header_banner #subbanner').height( $('header #header_banner p').height() );

	$('nav.subnav ul li a').mouseenter(function(){
		var p = $(this);
		var offset = p.offset();
		var awidth = ( $(this).width() /2) - 14;
		$('#header_arrow').stop().animate(
			{left: offset.left + awidth}
		, "slow", "easeOutBack");
	});
	$('nav.subnav').mouseleave(function(){
		var p = $('nav.subnav ul li.active a');
		var offset = p.offset();
		var awidth = ( $('nav.subnav ul li.active a').width() /2) - 14;
		$('#header_arrow').stop().animate(
			{left: offset.left + awidth}
		, "slow", "easeOutBack");
	});
	
	
	$('nav.left_nav ul li:first-child').addClass("current");
	var q = $('nav.left_nav ul li:first-child');
	var qoffset = q.offset();
	if (qoffset != null){
		var qheight = ( $('nav.left_nav ul li:first-child').height() /2) -10;
		$('#left_arrow').stop().animate(
			{top: qoffset.top + qheight}
		, 0);
	}
	
	$('nav.left_nav ul li').mouseenter(function(){
		var q = $(this);
		var qoffset = q.offset();
		var qheight = ( $('nav.left_nav ul li:first-child').height() /2) - 10;
		$('#left_arrow').stop().animate(
			{top: qoffset.top + qheight}
		, "slow", "easeOutBack");
	});
	$('nav.left_nav ul').mouseleave(function(){
		var q = $('nav.left_nav ul li.current');
		var qoffset = q.offset();
		var qheight = ( $('nav.left_nav ul li:first-child').height() /2) - 10;
		$('#left_arrow').stop().animate(
			{top: qoffset.top + qheight}
		, "slow", "easeOutBack");
	});
	
	$('nav.left_nav ul li').click(function(){
		if (! $(this).hasClass('current') ){
			if (window.animating.left == 0 ) {
				window.animating.left = 1;
				var number = $(this).index();
				$('nav.left_nav ul li').removeClass('current');
				$(this).addClass('current');
				
				$('.content .text').stop().animate({
					marginLeft: 570,
					opacity: 0
				}, "slow", "easeInBack", function(){
					$('.content #text_'+number).stop().animate({
						opacity: 1,
						marginLeft: 0
					}, "slow", "easeOutBack", function(){
						window.animating.left = 0;
					});
				});
			}
		}
	});
	
	if (leftsubpathname != null){
		$('nav.left_nav ul li').removeClass('current');
		$("nav.left_nav ul li.go_"+leftsubpathname).addClass('current');

		var q = $('nav.left_nav ul li.current');
		var qoffset = q.offset();
		var qheight = ( $('nav.left_nav ul li:first-child').height() /2) - 10;
		$('#left_arrow').stop().animate(
			{top: qoffset.top + qheight}
		, 0);
		
		$('.content .text').stop().animate({
			marginLeft: 570,
			opacity: 0
		}, 0, function(){
			$('.content .go_'+leftsubpathname).stop().animate({
				opacity: 1,
				marginLeft: 0
			}, 0, function(){
				window.animating.left = 0;
			});
		});
	} else {
		if (subpathname != null){
			var number = 0;
			$('nav.left_nav ul li').removeClass('current');
			$(this).addClass('current');
			
			if($('nav.left_nav ul li:first-child').length != 0){
				var q = $('nav.left_nav ul li:first-child');
				var qoffset = q.offset();
				var qheight = ( $('nav.left_nav ul li:first-child').height() /2) - 10;
				$('#left_arrow').stop().animate(
					{top: qoffset.top + qheight}
				, 0);
				
				$('.content .text').stop().animate({
					marginLeft: 570,
					opacity: 0
				}, 0, function(){
					$('.content #text_'+number).stop().animate({
						opacity: 1,
						marginLeft: 0
					}, 0, function(){
						window.animating.left = 0;
					});
				});
			}
		}
	}
	
	$('.contentholder_badkamers .content .text .prev').click(function(){
		var margin = parseInt($(this).parent().find('ul').css("margin-left"));
		if (margin < -50){
			$(this).parent().find('ul').animate({
				"margin-left": "+=257"
			}, "slow");
		} else {
			$(this).parent().find('ul').animate({
				"margin-left": "10"
			}, "fast", function(){
				$(this).parent().find('ul').animate({
					"margin-left": "0"
				}, "fast", "easeOutElastic");
			});
		}
	});
	
	$('.contentholder_badkamers .content .text .next').click(function(){
		var aantal = $(this).parent().find('ul li').length -2;
		var margin = - $(this).parent().find('ul li').width() * aantal;
		var cur_margin = parseInt($(this).parent().find('ul').css("margin-left"));
		if (cur_margin > margin){
			$(this).parent().find('ul').animate({
				"margin-left": "-=257"
			}, "slow");
		} else {
			$(this).parent().find('ul').animate({
				"margin-left": cur_margin - 10
			}, "fast", function(){
				$(this).parent().find('ul').animate({
					"margin-left": cur_margin
				}, "fast", "easeOutElastic");
			});
		}
	});
	
	$('#tiletext p').animate({
			opacity: 0
		}, 0);
	$('ul.tiles li').mouseenter(function(){
		var tiletext = $(this).attr("title");
		$('#tiletext p').text(tiletext);
		$('#tiletext p').stop().animate({
			opacity: 1
		}, "fast");
	});
	$('ul.tiles li').mouseleave(function(){
		$('#tiletext p').stop().animate({
			opacity: 0
		}, "slow");
	});
	
	$('.contentholder_badkamers .content .text .up').click(function(){
		var margin = parseInt($(this).parent().find('ul.doubletiles, ul.tiles').css("margin-top"));
		if (margin < -50){
			$(this).parent().find('ul.doubletiles, ul.tiles').animate({
				"margin-top": "+=102"
			}, "slow");
		} else {
			$(this).parent().find('ul.doubletiles, ul.tiles').animate({
				"margin-top": "10"
			}, "fast", function(){
				$(this).parent().find('ul.doubletiles, ul.tiles').animate({
					"margin-top": "0"
				}, "fast", "easeOutElastic");
			});
		}
	});
	
	$('.contentholder_badkamers .content .text .down').click(function(){
		var aantal = Math.ceil($(this).parent().find('ul.doubletiles li, ul.tiles li').length /6)-0;
		var margin = - ($(this).parent().find('ul.doubletiles li, ul.tiles li').height() * aantal);
		var cur_margin = parseInt($(this).parent().find('ul.doubletiles, ul.tiles').css("margin-top"));
		if (cur_margin > margin){
			$(this).parent().find('ul.doubletiles, ul.tiles').animate({
				"margin-top": "-=102"
			}, "slow");
		} else {
			$(this).parent().find('ul.doubletiles, ul.tiles').animate({
				"margin-top": cur_margin - 10
			}, "fast", function(){
				$(this).parent().find('ul.doubletiles, ul.tiles').animate({
					"margin-top": cur_margin
				}, "fast", "easeOutElastic");
			});
		}
	});
	
	
	$('footer section article ul li a').hover(
		function(){
			$(this).stop(true, true).animate({
				color: '#044C65'
			}, 100);
		}, 
		function(){
			$(this).animate({
				color: '#C6C7C8'
			}, 700);
		}
	);
	
	$('footer').hover(
		function(){
			if ($('body').attr('id') != 'home'){
				$('footer').stop().animate({
					height: '169px'
				}, "slow", "easeOutCubic");
			}
		},
		function(){
			if ($('body').attr('id') != 'home'){
				setTimeout(function(){
					$('footer').animate({
						height: '32px'
					}, "slow", "easeOutCubic");
				}, 500);
			}
		}
	);
	

	/* Define the function that triggers to fade in the background */
	$("#home img#bg").load(function()
		{
			/* Fade in during 3 seconds */
			windowresize();
			$("#home img#bg").fadeTo(1000,1);

			/* Set the timeout to fade out*/
			setTimeout(function()
				{
					$("#home img#bg").fadeOut(1000, function(){

						/* Load the next image */
						setTimeout(LoadImages,100);
					});
				},5000);
 		});

	/* Start the slideshow one second after the page is ready */
	if ($('body').attr('id') == 'home'){
		setTimeout(LoadImages,1000);
	} else {
		$("img#bg").load(function(){
			setTimeout(function(){
				$("#background").css({
					"background-image" : "url:(" + $('img#bg').attr("src") + ")"
				});
				$("#background").css({
					"display":"block"
				});
				$("#background").fadeTo(3000,1);
			}, 1000);
		});
	}
	
	$(window).load(function() {
		windowresize();
	});

	$(window).resize(function(){
		windowresize();
	});

});

function windowresize() {

	var windowheight = $(window).height();
	var windowwidth = $(window).width();
	
	$("#background").css({
		"height": (windowheight)
	});
	
	var backgroundheight = $("#background").height();
	var backgroundwidth = $("#background").width();
	
	var imageheight = $("#background img").height();
	var imagewidth = $("#background img").width();
	
	var aspectratioimg = imagewidth/imageheight;
	var aspectratiobackground = backgroundwidth/backgroundheight;
	
	if (aspectratiobackground > aspectratioimg) { //widescreen
		$("#background img").width(backgroundwidth);
		$("#background img").height(backgroundwidth / aspectratioimg);
		$("#background img").css({
			"position": "absolute",
			"left": "50%",
			"top": "50%",
			"margin-left": -($("#background img").width() / 2),
			"margin-top":  -($("#background img").height() / 2)
		})
	} else { // standard
		$("#background img").height(backgroundheight);
		$("#background img").width(backgroundheight * aspectratioimg);
		$("#background img").css({
			"position": "absolute",
			"left": "50%",
			"top": "50%",
			"margin-left": - ($("#background img").width() / 2),
			"margin-top": -($("#background img").height() / 2)
		})
	}
};

var iPrev = -1;
var iRnd = -1;

function LoadImage(iNr)
{
	/* Assign the new image to the background */
	$("img#bg").attr("src", aImages[(iNr+1)]);

};

function LoadImages()
{

	/* Select a new random image number */
	while(iPrev == iRnd)
	{
		iRnd = Math.floor(Math.random()*(aImages.length-1));
	}

	/* Show the selected image */
	LoadImage(iRnd);

 	iPrev = iRnd;

};

function str_replace (search, replace, subject, count) {
 
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
 
    s = [].concat(s);
 
    if (count) {
 
        this.window[count] = 0;
 
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
 
        if (s[i] === '') {
 
            continue;
 
        }
 
        for (j=0, fl=f.length; j < fl; j++) {
 
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
 
            if (count && s[i] !== temp) {
 
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
 
        }
 
    }
 
    return sa ? s : s[0];
 
}
