jQuery(function() {

	initBgGallery();

	initSlideBlock();

	

	// SLIDE DE SWITCHER IN

	if("#switcher"){

		jQuery("#switcher").css("marginTop","77px").delay(1000).animate({marginTop:"0px"}, 1000, "easeInOutExpo");

	}

	if($(".fancybox").length>0){
		$(".fancybox").fancybox({

			'padding'			: 0,

			'margin'			: 0,

			'scrolling'			: 'no',

			'titleShow'			: false,

			'autoScale'			: false,

			'autoDimensions'	: false,

			'width'				: 490,

			'height'			: 540,

			'transitionIn'		: 'none',

			'transitionOut'		: 'none',

			'type'				: 'iframe'

		});	
	}	
	
	

	// TOOLTIPS OP DE KALENDER VAN DE CONTACTPAGINA

	if($(".tooltip").length>0){

		var href = "";

		$(".tooltip").tipTip({maxWidth: "auto", edgeOffset: 0, defaultPosition: "top"});//, keepAlive: true

		$(".tooltip").fancybox({

			'padding'			: 0,

			'margin'			: 0,

			'scrolling'			: 'no',

			'titleShow'			: false,

			'autoScale'			: false,

			'autoDimensions'	: false,

			'width'				: 490,

			'height'			: 540,

			'transitionIn'		: 'none',

			'transitionOut'		: 'none',

			'type'				: 'iframe'

		});	

		$(".tooltip").mouseleave(function(){

			$("#tiptip_holder").hide();

		});		

		/*$(".tooltip").hover(function(){

			href = $(this).attr('href');

		});

		$("#tiptip_holder").click(function(){

		    $("#tiptip_holder").hide();

			$.fancybox({

				'padding'			: 0,

				'margin'			: 0,

				'scrolling'			: 'no',

				'titleShow'			: false,

				'autoScale'			: false,

				'autoDimensions'	: false,

				'width'				: 490,

				'height'			: 540,

				'transitionIn'		: 'none',

				'transitionOut'		: 'none',

				'type'				: 'iframe',

				'href'				: href

			});	    

		    return false;			

		});*/	

	}

	

	// GALLERIJ EN FILM PAGINA

	if($(".gallery a[rel^='prettyPhoto']").length>0){

		$(".gallery a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'normal',theme:'light_square',slideshow:6000, autoplay_slideshow: false});

	}

	if($(".movies a[rel^='prettyPhoto']").length>0){

		$(".movies a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'normal',theme:'light_square', slideshow:false});

	}

	

	// CONTACTFORMULIER

	if($('form[name=reservatieformulier]').length>0){

		$('form[name=reservatieformulier]').submit(function(){

			if($(this).data('formstatus') !== 'submitting'){

				var form = $(this),

				formData = form.serialize(),

				formUrl = form.attr('action')+"&js=test",

				formMethod = form.attr('method'), 

				responseMsg = $('#boodschap');

				form.data('formstatus','submitting');

				responseMsg.hide().addClass('response-waiting').html('Please wait').fadeIn(1000);

				$.ajax({

					url: formUrl,

					type: formMethod,

					data: formData,

					success:function(data){

						var responseData = jQuery.parseJSON(data);

						responseMsg.fadeOut(500,function(){

							$(this).removeClass('response-waiting');

							if(responseData.status=="success"){

								$('input[name=naam]').val('');

								$('input[name=tel]').val('');

								$('input[name=email]').val('');

								$('input[name=bericht]').val('');

								$('.reservatieformulier').fadeOut(500,function(){

									$(this).html(responseData.message).fadeIn(1000);

									timer = setInterval(function(){

										clearInterval(timer);

										parent.$.fancybox.close();

									}, 3000);

								});

							}else{

								$(this).addClass(responseData.klass).html(responseData.message).fadeIn(1000,function(){

									setTimeout(function(){

										responseMsg.fadeOut(500,function(){

											$(this).removeClass(responseData.klass);

											form.data('formstatus','idle');

										});

									},3000)

								});	

							}

						});

					}

				});

			}

			return false;

		});

	}	

});



// initBgGallery

function initBgGallery() {

	var holder = document.getElementById('bg');

	if(holder) {

		jQuery('#bg img').each(function() {

			backgroundStretcher.stretchImage(this);

		});

		backgroundStretcher.setBgHolder(holder);

	}

	

	var set = jQuery('#bg img'),

		switcher = jQuery('#switcher > li'),

		activeClass = 'active',

		animSpeed = 900,

		delay = 6000,

		curInd = 0,

		prevInd = 0,

		timer,

		autorotate = true;

	

	set.hide().eq(curInd).show();

	

	function nextSlide() {

		prevInd = curInd;

		if(curInd < set.length-1) curInd++; else curInd = 0;

		switchSlide();

	}

	function autoSlide() {

		if(timer) clearTimeout(timer);

		if(autorotate) timer = setTimeout(nextSlide,delay);

	}

	autoSlide();

	

	function switchSlide() {

		set.eq(prevInd).fadeOut(animSpeed);

		set.eq(curInd).fadeIn(animSpeed);

		switcher.removeClass(activeClass).eq(curInd).addClass(activeClass);

		autoSlide();

	}

	

	switcher.each(function(i) {

		var btn = jQuery(this);

		

		btn.click(function() {

			prevInd = curInd;

			curInd = i;

			switchSlide();

			return false;

		})

	});

	

	jQuery(window).focus(function() {

		autorotate = true;

		autoSlide();

	}).blur(function() {

		if(timer) clearTimeout(timer);

		autorotate = false;

	})

}



// initSlideBlock

function initSlideBlock() {	

	jQuery('.slide-block').each(function() {

		var slideBox = jQuery('> .block', this),

			opener = jQuery('.open-close', this),

			close = jQuery('.close', this),

			animate = false;

		

		slideBox.css({

			display: 'none'

		})

		

		opener.click(function() {

			if(!animate) {

				animate = true;

				if(slideBox.is(':hidden')) {

					slideBox.addClass('open-state');

					slideBox.slideDown(function() {

						animate = false;

					})

				}

				else {

					slideBox.slideUp(function() {

						animate = false;

						slideBox.removeClass('open-state');

					})

				}

			}

			return false;

		});

		

		close.click(function() {

			if(!animate) {

				animate = true;

				slideBox.slideUp(function() {

					animate = false;

					slideBox.removeClass('open-state');

				})

			}

			return false;

		})

	});

	jQuery('.slide-content').each(function() {

		var holder = jQuery(this),

			slideBox = jQuery('> .content-holder', this),

			opener = jQuery('.open-close', this),

			close = jQuery('.close', this),

			animate = false,

			activeClass = 'active';

		

		opener.click(function() {

			if(!animate) {

				animate = true;

				if(slideBox.is(':hidden')) {

					holder.addClass(activeClass);

					slideBox.slideDown(function() {

						animate = false;

					})

				}

				else {

					slideBox.slideUp(function() {

						animate = false;

						holder.removeClass(activeClass);

					})

				}

			}

			return false;

		});

		

		close.click(function() {

			if(!animate) {

				animate = true;

				slideBox.slideUp(function() {

					animate = false;

					holder.removeClass(activeClass);

				})

			}

			return false;

		})

	})	

}



// image stretch module

backgroundStretcher = {

	images: [],

	holders: [],

	viewWidth: 0,

	viewHeight: 0,

	stretchByWindow: false, // or entire page

	init: function(){

		this.addHandlers();

		this.resizeAll();

		return this;

	},

	stretchImage: function(obj) {

		var img = new Image();

		img.onload = this.bind(function(){

			obj.iRatio = img.width / img.height;

			obj.iWidth = img.width;

			obj.iHeight = img.height;

			obj.style.msInterpolationMode = 'bicubic'; // IE7 fix

			this.resizeImage(obj);

		});

		img.src = obj.src;

		this.images.push(obj);

	},

	setBgHolder: function(obj) {

		this.holders.push(obj);

		this.resizeAll();

	},

	resizeImage: function(obj) {

		if(obj.iRatio) {

			var slideWidth = this.viewWidth;

			var slideHeight = slideWidth / obj.iRatio;

			if(slideHeight < this.viewHeight) {

				slideHeight = this.viewHeight;

				slideWidth = slideHeight * obj.iRatio;

			}

			obj.style.width = slideWidth+'px';

			obj.style.height = slideHeight+'px';

			obj.style.top = (this.viewHeight - slideHeight)/2+'px';

			obj.style.left = (this.viewWidth - slideWidth)/2+'px';

		}

	},

	resizeHolder: function(obj) {

		obj.style.width = this.viewWidth+'px';

		obj.style.height = this.viewHeight+'px';

	},

	getWindowWidth: function() {

		return typeof window.innerWidth === 'number' ? window.innerWidth : document.documentElement.clientWidth;

	},

	getWindowHeight: function() {

		return typeof window.innerHeight === 'number' ? window.innerHeight : document.documentElement.clientHeight;

	},

	getPageWidth: function() {

		if(!document.body) return 0;

		return Math.max(

			Math.max(document.body.clientWidth, document.documentElement.clientWidth),

			Math.max(document.body.offsetWidth, document.body.scrollWidth)

		);

	},

	getPageHeight: function() {

		if(!document.body) return 0;

		return Math.max(

			Math.max(document.body.clientHeight, document.documentElement.clientHeight),

			Math.max(document.body.offsetHeight, document.body.scrollHeight)

		);

	},

	resizeAll: function() {

		// crop holder width by window size

		for(var i = 0; i < this.holders.length; i++) {

			this.holders[i].style.width = '100%'; 

		}

		

		// delay required for IE to handle resize

		clearTimeout(this.resizeTimer);

		this.resizeTimer = setTimeout(this.bind(function(){

			// hide background holders

			for(var i = 0; i < this.holders.length; i++) {

				this.holders[i].style.display = 'none';

			}

			

			// calculate real page dimensions with hidden background blocks

			this.viewWidth = this[this.stretchByWindow ? 'getWindowWidth' : 'getPageWidth']();

			this.viewHeight = this[this.stretchByWindow ? 'getWindowHeight' : 'getPageHeight']();

			

			// show and resize all background holders

			for(i = 0; i < this.holders.length; i++) {

				this.holders[i].style.display = 'block';

				this.resizeHolder(this.holders[i]);

			}

			for(i = 0; i < this.images.length; i++) {

				this.resizeImage(this.images[i]);

			}

		}),10);

	},

	addHandlers: function() {

		if (window.addEventListener) window.addEventListener('resize', this.bind(this.resizeAll), false);

		else if (window.attachEvent) window.attachEvent('onresize', this.bind(this.resizeAll));

	},

	bind: function(fn, scope, args) {

		var newScope = scope || this;

		return function() {

			return fn.apply(newScope, args || arguments);

		}

	} 

}.init();
