// slide show functions
function iniSlideshow(){
	var maxW=0;
	var maxH=0;
	$(this).each(function(){
		// get image dimensions
		var h=$(this).height();
		var w=$(this).width();
		
		// update maxW & H
		if(w>maxW)maxW=w;
		if(h>maxH)maxH=h;
		
		// get container dimensions and calculate margin with it
		var margin_w = parseInt(parseInt(($(this).parents(".slider").width()) - w)/2);
		var margin_h = parseInt(parseInt(($(this).parents(".slider").height()) - h)/2);

		// center image
		$(this).css('margin-top', margin_h+'px').css('margin-left', margin_w+'px').show();
		
		 // */
		//$(this).parents(".slider").css('height', '100%');
		
		// set slide ID
		//var slider = $(this).parents(".slider").attr("id");
		
		// set new title
		//$("#"+ slider +" div.imgtitle").html(this.alt);
		
		// set height on required divs.
		
		//if(h>$("#"+slider+" .orbit-wrapper").height()) $("#"+slider+" .orbit-wrapper").height(h).children(".pics").height(h);
	});
	
	// set img container width & height
	$("#"+ $(this).parents(".slider").attr("id") +" .pics").children('div').height(maxH).width(maxW);
}
function onAfter(){
	// store new values for the Zoom box
	$(this).parents(".slider").first()
		.data('current_title', $(this).find("img:first").attr('alt'))
		.data('current_img', $(this).find("img:first").attr('src'))
		.data('current_w', $(this).find("img:first").width())
		.data('current_h', $(this).find("img:first").height());
}

// center the body horizontally if possible.
function checkResize(){
	if($(window).height() > $("div#body").height()){
		$("div#body").css('margin-top', ($(window).height()-$("body").height())/2);
	}
}


/*/ promo slider function
function advance_slider(id){
	var max = $("div.promotext div.promo_container").data('max');
	var prev = id - 1;
	if( prev < 0) var prev = max;
	
	var next = id+1;
	if( id == max) var next = 0;
	
	// hide prev
	$("div.promotext div.promo_container").css('height', $("div.promotext div.promo_container").height()+'px');
	$("div.promotext div.promo_container div.slider_"+prev).slideUp(function(){
		$("div.promotext div.promo_container").animate({ height: $("div.promotext div.promo_container div.slider_"+id).height()+'px'});
		// show next
		$("div.promotext div.promo_container div.slider_"+id).slideDown();
	});
	
	// resume slider
	var slider_time = setTimeout(function(){ advance_slider(next) }, 5000);
} // */

// wait for DOM to be ready
$(function(){
	checkResize();
	
	// and bind the same action on window resize
	$(window).resize(function(){checkResize();});
	
	// print email address if needed (decompose it to prevent spamm bot from grabbing it
	var data='fo@';
	data = 'in'+data+'<span>nothingrg';
	data = data.replace('nothing', 'domainedubou')+'.c</span>'+'om';
	data='<a href="mailto:'+data.replace('</span>', '').replace('<span>', '')+'">'+data+'</a>';
	$(".put_email_here").html(data);
	
	// basic email decryption for other emails
	$(".email").each(function(){
		var content = $(this).html();
		$(this).html(content.replace('[at]','@').replace('[dot]','.').replace('_REMOVE_ME_',''));
	});
	
	/*/ start promo slide
	$("div.promotext div.promo_container").children("div").each(function(i){
		if(i != 0) $(this).hide(); // hide all but the 1st one
		else{
			$("div.promotext div.promo_container").data('current', 0); // set current slide
			// set next slide
	   		var slider_time = setTimeout(function(){ advance_slider($("div.promotext div.promo_container").data('current')+1) }, 5000);
		}
		
		// number each item
		$(this).addClass('slider_'+i);
		
		// store max slides
		$("div.promotext div.promo_container").data('max', i);
	}); // */
	
	// make all slides start
	$('.slider').each(function(){
		var slider = $(this).attr("id");
		/*$("#"+slider+" .pics").cycle({
			prev: "#" + slider + " .controller .prev",
			next: "#" + slider + " .controller .next",
			pauseOnPagerHover: true,
			before: onBefore,
			after: onAfter,
			speed: 500, // transition time in miliseconds)
			timeout: 3000, // time between pictures (in miliseconds)
			fit: 0
			// width: 250
			// height: $(this).width(),
		});*/
		
		// position ini slide show
		iniSlideshow.call($(this).find('.pics').children("div").children("img"));
		
		// slideshowify
		$("#"+slider+" .pics").orbit({
			animation: 'fade',					// fade, horizontal-slide, vertical-slide, horizontal-push
			animationSpeed: 1000,				// how fast animtions are
			timer: true, 						// true or false to have the timer
			advanceSpeed: 3000, 				// if timer is enabled, time between transitions 
			pauseOnHover: true, 				// if you hover pauses the slider
			startClockOnMouseOut: true, 		// if clock should start on MouseOut
			startClockOnMouseOutAfter: 800, 	// how long after MouseOut should the timer start again
			directionalNav: true, 				// manual advancing directional navs
			captions: true, 					// do you want captions?
			captionAnimation: 'fade', 			// fade, slideOpen, none
			captionAnimationSpeed: 500, 		// if so how quickly should they animate in
			bullets: false,						// true or false to activate the bullet navigation
			bulletThumbs: false,				// thumbnails for the bullets
			bulletThumbLocation: '',			// location from this file where thumbs will be
			afterSlideChange: onAfter	 		// empty function 
		});
		
		// load required data
		onAfter.call($("#"+slider+" .pics").children("div:eq(0)"));
		
		// hide timer
		$("#"+slider+" .timer").hide();
		
		// hide/show controller
		$("#"+slider).hover(function(){
			$(this).children(".controller").fadeIn();
			//$("#"+slider+" .pics").cyclePause=1;
		},
		function(){
			$(this).children(".controller").fadeOut();
			//$("#"+slider+" .pics").cycle('resume');
		});
		
		// zoom function
		$(this).find('.pics:first').click(function(){
			// save this much used var
			//var diapo=$(this);
//alert(diapo.cyclePause);
			// pause slide show
			//$("body").data('paused', 1);			
			//$(diapo).cycle('pause');

			// initialize data
			var height=$(this).parents('.slider:first').data('current_h');
			var width=$(this).parents('.slider:first').data('current_w');
			var title=$(this).parents('.slider:first').data('current_title');
			var img=$(this).parents('.slider:first').data('current_img');
			console.log($(this).parents(".slider:first"));
			
			// apply a site mask
			$("<div id=\"mask\"></div>").appendTo("body").fadeTo(400, .5);
			
			// create zoombox (hidden)
			$('<div id="zoombox">'+title+'<br></div>').appendTo("body").append('<img src="'+img.replace('/thumbs/', '/large/')+'" alt="'+title+'" />');
			// put image in it
			$("#zoombox img").css({width:(width*2)+'px', height: (height*2)+'px'});
			// recenter zoombox and show it (32 = 15*2 padding)
			$("#zoombox").css({'margin-top': '-'+(($("#zoombox").height()+30)/2)+'px', 'margin-left':'-'+(width)+'px'}).fadeTo(400, 1);
			
			// pause the slide show (the wrong way, but at least it works)
			
			//$("#mask, #zoombox, #zoombox img").hover(function(){diapo.cyclePause=1;}, function(){diapo.cyclePause=0;});
			
			// bind unmask
			$("#mask, #close-mask, #zoombox img, #zoombox").click(function(){				
				// function to hide the zoombox
				$("#zoombox").fadeOut(function(){
					$(this).remove();
				});
				$("#mask").fadeOut(function(){
					$(this).remove();
				});
			});
		});
		
		/*/ also pause then mous on controller
		$("#"+slider+" .controller div").hover(function(){
			
		}, function(){
			
		});*/
		
		
		// slide promos
		
		// hide all and init data
		$("div.promo").fadeTo(1,0).children("div").each(function(){
			$(this).css('padding-top', '6px');
			// get height
			if($(this).height()>$("div.promo").data('maxHeight')) $("div.promo").data('maxHeight')=$(this).height();
			//console.log($("div.promo").data('current'));
			// first loop ?
			if($("div.promo").data('current')==null){
				// init data
				$("div.promo").data('current', 0);
				
				// get class
				if($(this).hasClass('newstext')) var classs='newstext';
				else var classs='promotext';
				
				// store class name
				$("div.promo").children("div").eq(0).find("img.promo_img:first").data('container_class', classs);
			}
			
			// place content in one element (to get better animations)
			else {
				// get class
				if($(this).hasClass('newstext')) var classs='newstext';
				else var classs='promotext';
				
				// get and place IMG and store class in it
				$("div.promo").children("div").eq(0).find("img.promo_img:last").after($(this).find("img.promo_img").hide().data('container_class', classs));
				
				// get and place promo_description
				$("div.promo").children("div").eq(0).find("div.promo_description:last").after($(this).find("div.promo_description").hide());
				
				// hide element
				$(this).remove();
				
				// show first if this was the last one
				if($("div.promo").children("div").length===1) $("div.promo").fadeTo(1000,1).children("div:first").show();
			}
		});
		
		// create function
		function slide(){
			// get current and next
			var current = $("div.promo").data('current');
			var next=current+1;
			if($("div.promo").find("div.promo_description").eq(next).length ==0) var next=0;
			
			// hide container and set fixed height
			$("div.promo").height($(this).data('maxHeight'));
			
			// hide current
			$("div.promo div.promo_description").eq(current).fadeTo(500, 0, function(){$(this).hide();});
			$("div.promo img.promo_img").eq(current).fadeTo(500, 0, function(){
				$(this).hide();
				// update container class
				$("div.promo").children("div:first").removeClass("newstext promotext").addClass($("div.promo img.promo_img").eq(next).data('container_class'));
				
				// show next
				$("div.promo div.promo_description").eq(next).show().fadeTo(900, 1);
				$("div.promo img.promo_img").eq(next).show().fadeTo(900, 1, function(){
					// store data
					$("div.promo").data('current', next);
					// call next slider function
					$(this).fadeTo(5000, 1, function(){ slide(); });
				});
			});
		}
		// start slider
		slide();
	});
	
	// show HOME hint
	$("#logo").hover(function(){
		$("#logo-sub").fadeIn("fast");
	},function(){
		$("#logo-sub").hide();
	});
	
	// make current submenu visible
	$("#menu li.current ul").show();
	
	// and so we also make it hide with JS
	$("#menu li:not(.current):not(#menu li li), a#logo").hover(function(){
		$("#menu li.current ul").hide();
	}, function(){
		$("#menu li.current ul").show();
	});
	
	// preload hover image
	$("<img>").attr({"src":'/images/header_logo_hover.png','class':'removeme'}).css('display', 'none').appendTo('body');
	//setTimeout('$(".removeme").remove();', 200); --> will delete cache :'(
	
	/*/ calender hover effect
	$("#td.date_has_event").hover(function(){
		
	}, function(){
		
	}); // */
	
	// empty focus fields
	$(".placeholder").focus(function(){
		if($(this).val() == $(this).data("placeholder") || $(this).data("placeholder")==null){
			$(this).data("placeholder", $(this).val());
			$(this).val("");
		}
	});
	$(".placeholder").blur(function(){
		if($(this).val()==''){
			$(this).val($(this).data("placeholder"));
		}
	});
	
	// captcha reloading:
	$("input[name='captcha']").hover(function(){
		//make reload button appear
		
		// change CSS
		//$("input[name='captcha']").css('background-image', 'url(/captcha-'+(Math.round(new Date().getTime() / 1000))+'.jpg)');
	}, function(){
		// make it disapear
	});
	
	// calender quick access menu
	$("#calender_jumper").show().change(function(){
		window.location.href=($("#calender_jumper_format").val().replace('MM',$(this).val()));
	});
	
	// taal veranderen
	if($("#show_lang_change").length==1){
		$('<div></div>').css({'backgroundColor':'white','width':'300px','padding':'20px 30px'}).addClass('roundbox').html($("#show_lang_change").val()).prependTo('#body').wrap($('<div></div>').css({'paddingRight':'120px','paddingTop':'110px', 'opacity':0,'top':0,'position':'absolute', 'right':0,'z-index':1005, 'marginRight':'40px','background-image':'url(/images/lang_switch_arrow.png)', 'background-repeat':'no-repeat', 'background-position':'top right'})).parent()
		// wait ±1sec
		.fadeTo(2500, 0, function(){
			// show message
			$(this).animate({'opacity':1,'top':'40px'}, 400, function(){
				// wait ±2 sec
				$(this).fadeTo(4000,1, function(){
					// hide
					$(this).animate({'opacity':0,'top':'300px'}, 400, function(){
						// remove element and hidden
						$(this).remove();
						$("#show_lang_change").remove();
					});
				});
			});
		});
	}
});
