$(window).load(function(){
	/* Custom scrollbar */
	$("#scroll_content").jScrollPane();
	$("#scroll_content").bind('mousewheel');
});

var homeslider;
var aantal_slides_homeslider;
var huidige_slide_homeslider;

$(document).ready(function(){	

	//Tooltips
	$(".tipsy_tooltip").tipsy({ gravity:'w' });
	

	//Achtergrond instellingen
	backgroundResize();
	stelPaginaIn();
	$(window).resize(function(){ backgroundResize();stelPaginaIn(); });
	
		
	//Weergeven van de twitterfeed
	$("#home_twitter_feeds").tweet({
		username: "magnumopusnl",
		avatar_size: 0,
		count: 5,
		refresh_interval: 100,
		loading_text: "Tweets laden..."
	}); 
	
	
	if($("body").attr("class")=="site"){		
		/***************************************************
			DE JQUERY BIJ DE GEWONE PAGINA'S
		***************************************************/
		//Uitklappen van de pagina als je ergens binnenkomt
		$("#site_container_content_content").delay(200).slideDown(350, function(){
			$("#site_container").fadeIn(500); //Zichtbaar maken van de pagina als deze vanuit ander stramien komt
			stelPaginaIn();
			//Werking van de meestergalerij
			$("#meester_galerij").CloudCarousel({			
				xPos: 295,
				yPos: 50,
				minScale: 0.25,
				reflHeight: 100,
				xRadius: 300,
				yRadius: 40,
				bringToFront: true,
				buttonLeft: $("#galerij_nav_links"),
				buttonRight: $("#galerij_nav_rechts"),
				titleBox: $("#galerij_afbeelding_titel")
			});	
		});
		
		var slider=1;
		//Veranderen van de homesliderbalk
		$(".verander_home_slider").click(function() {
			$(".verander_home_slider").removeClass("aan");
			$(this).addClass("aan");
			verander_home_slider($(this).attr("rel"));
		});

		//Werking van de homeslider
		aantal_slides_homeslider = $("#site_container_content_slider_home_navigator div").length;
		huidige_slide_homeslider = 1;
		start_homeslider();
		$("#site_container_content_slider_home").mouseover(function() {
			stop_homeslider();
		}).mouseout(function(){
			start_homeslider();
		});
		
		//Werking van de headerfader (niet home)
		$('#site_container_content_slider').innerfade({
			speed: 1000,
			timeout: 6000,
			type: 'sequence',
			containerheight: '90px'
		});
		
		//Werking van de youtubeslider
		aantal_slides_youtubeslider = $("#youtubewidget .navigatie").attr("rel");
		huidige_slide_youtubeslider = 1;
		$("#youtubewidget_vorige_video").click(function(ev){
			ev.preventDefault();
			if(huidige_slide_youtubeslider==1){ huidige_slide_youtubeslider=aantal_slides_youtubeslider; } else { huidige_slide_youtubeslider--; }
			$("#youtubewidget .slider").animate({marginLeft:-(266*(huidige_slide_youtubeslider-1))+"px"},{queue:false,duration:600,easing:'easeOutExpo'});
		});
		$("#youtubewidget_volgende_video").click(function(ev){
			ev.preventDefault();
			if(huidige_slide_youtubeslider==aantal_slides_youtubeslider){ huidige_slide_youtubeslider=1; } else { huidige_slide_youtubeslider++; }
			$("#youtubewidget .slider").animate({marginLeft:-(266*(huidige_slide_youtubeslider-1))+"px"},{queue:false,duration:600,easing:'easeOutExpo'});
		});
		
		//Dichtklappen van de pagina als deze wordt verlaten
		$("a:not(.gebedsovergang)").click(function(ev){
			if($(this).attr("target")!="_blank"){
				ev.preventDefault();
				var link = $(this);
				$("#site_container_content_content").slideUp(350,function(){
					if(link.attr("href").indexOf("http://")!=-1){
						document.location = link.attr("href");
					} else {
						document.location = $("base").attr("href")+link.attr("href");
					}
				});
			}
		});	
		
		//Dichtklappen van de website en overgang naar gebedspagina
		$("a.gebedsovergang").click(function(ev){
			stelPaginaIn(); 
			if($(this).attr("target")!="_blank"){
				ev.preventDefault();
				var link = $(this);
				breedte_uiteindelijk=$(window).width()*5;
				hoogteverschil=Math.floor(((breedte_uiteindelijk-$(window).height())/2));
				offset=Math.floor(breedte_uiteindelijk/600)*65;
				$("#splash_afbeelding_container").css("display","block");
				$("#site_container").slideUp(350,function(){					
					$("#splash_afbeelding_container img").delay(200).queue(function(){
						$("#splash_afbeelding_container img").animate({width:breedte_uiteindelijk+"px"},{queue:false,duration:2700});
						$("#splash_afbeelding_container").animate({marginTop:"-"+(hoogteverschil+offset)+"px" },{queue:false, duration:2700});	
					});
					$("#splash_afbeelding_container").delay(1400).fadeOut(1500);			
					$("#bg-holder-gebed").delay(500).fadeIn(2500,function(){						
						if(link.attr("href").indexOf("http://")!=-1){
							document.location = link.attr("href");
						} else {
							document.location = $("base").attr("href")+link.attr("href");
						}
					});
				});
			}
		});	
		
		//Sliderwerking van fotoalbum
		var huidige_pagina=1;
		var aantal_paginas=$("#aantal_paginas_fotothumbslider").attr("value");
		if(aantal_paginas<2){ $("#fotothumbslider .navigatie_omlaag, #fotothumbslider .navigatie_omhoog").css("display","none"); }
		$("#fotothumbslider .navigatie_omhoog").live('click', function(ev){
			ev.preventDefault();
			if(huidige_pagina==1){ huidige_pagina=aantal_paginas; } else { huidige_pagina--; }
			fotoslider_bewegen_naar_pagina(huidige_pagina);
		});
		$("#fotothumbslider .navigatie_omlaag").live('click', function(ev){
			ev.preventDefault();
			if(huidige_pagina==aantal_paginas){ huidige_pagina=1; } else { huidige_pagina++; }
			fotoslider_bewegen_naar_pagina(huidige_pagina);
		});
		$("#fotoalbum_container #fotothumbslider .slider_container .slider img").live('click', function(ev){
			grote_fotoslider_bewegen_naar_pagina($(this).attr("rel"));
		});
	
		/***************************************************
		***************************************************/
	} else {
		/***************************************************
			DE JQUERY BIJ DE GEBEDSPAGINA
		***************************************************/
		//Initialiseren van de mp3 speler op de luister pagina
		$('.mp3_luistergebed').click(function(index){
			link_mp3=$(this);
			$("#scroll_container").fadeOut(500,function(){
				$("#mp3_speler").jPlayer("pause");
				$(".actie_zet_muziek_uit").css("display","none");
				$(".actie_zet_muziek_aan").css("display","inline");	
				$.ajax({
					type: "POST",
					url: "includes/ajax.muziekspeleraanpassen.php",
					data: "actie=uitzetten"
				});	
				$("#gebed_menu").fadeOut(500,function(){
					$("#mp3_speler_gebed_container").fadeIn(500);
					jwplayer("mp3_speler_gebed").setup({
						flashplayer:"/js/player.swf", 
						skin:'/js/solidgold.zip',
						file:"website_files/audio_"+link_mp3.attr("rel")+".mp3", 
						width:400,
						height:28,
						autostart:true,
						controlbar:"bottom"			
					}); 
				});
			});			
		});
		

		$('.reset_gebedspagina_stop_mp3').click(function(index){
			jwplayer("mp3_speler_gebed").stop(); 
			$("#mp3_speler_gebed_container").fadeOut(500,function(){
				$("#gebed_menu").fadeIn(500,function(){
					$("#scroll_container").fadeIn(500);					
				});
			});			
		});
			
		//Initieren van de muziekspeler (Ook kijken of er afgespeeld moet worden)
		$("#mp3_speler").jPlayer({
			ready: function (){
				if($("#mp3_speler").attr("rel")=="uit"){
					$(this).jPlayer("setMedia", {
						mp3: "../mp3/gebed_mom.mp3"
					});
					$(".actie_zet_muziek_uit").css("display","none");
				} else {
					$(this).jPlayer("setMedia", {
						mp3: "../mp3/gebed_mom.mp3"
					}).jPlayer("play");
					$(".actie_zet_muziek_aan").css("display","none");
				}
			},
			ended: function() { 
				$(this).jPlayer("play");
			}
		});
		
		$(".actie_zet_muziek_aan").click(function(){
			$("#mp3_speler").jPlayer("play");
			$(".actie_zet_muziek_aan").css("display","none");
			$(".actie_zet_muziek_uit").css("display","inline");
			//Ajax call naar php bestand dat de SESSION variabele gebedsmuziek op 'aan' zet
			$.ajax({
				type: "POST",
				url: "includes/ajax.muziekspeleraanpassen.php",
				data: "actie=aanzetten"
			});
		});
		
		$(".actie_zet_muziek_uit").click(function(){
			$("#mp3_speler").jPlayer("pause");
			$(".actie_zet_muziek_uit").css("display","none");
			$(".actie_zet_muziek_aan").css("display","inline");	
			//Ajax call naar php bestand dat de SESSION variabele gebedsmuziek op 'uit' zet	
			$.ajax({
				type: "POST",
				url: "includes/ajax.muziekspeleraanpassen.php",
				data: "actie=uitzetten"
			});			
		});
		
		//Wegklikken van de scroll
		$("#scroll_sluiten_knop").click(function(){
			$("#scroll_content_container").animate({width:'0px'}, 450, function(){
				$("#gebed_menu ul li").removeClass("active");
				$("#scroll_container").fadeOut(500);
			});
		});
		
		//Uitklappen van de pagina als je ergens binnenkomt
		$("#scroll_content_container").delay(200).animate({width:'630px'}, 350, function(){
			if($("#geen_scroll_tonen").attr("value")!="1"){
				$("#scroll_container").fadeIn(650, function(){
					$("#scroll_content").jScrollPane();
				}); //Zichtbaar maken van de pagina als deze vanuit ander stramien komt
			}
		});

		//Dichtklappen van de pagina als deze wordt verlaten
		$("a:not(.siteovergang)").click(function(ev){			
			if($(this).attr("target")!="_blank"){
				ev.preventDefault();
				var link = $(this);
				$("#scroll_content_container").animate({width:'0px'}, 250, function(){
					if(link.attr("href").indexOf("http://")!=-1){
						document.location = link.attr("href");
					} else {
						document.location = $("base").attr("href")+link.attr("href");
					}
				});
			}
		});	
		
		//Dichtklappen van de website en overgang naar gebedspagina
		$("a.siteovergang").click(function(ev){
			if($(this).attr("target")!="_blank"){
				ev.preventDefault();
				var link = $(this);
				$("#scroll_content_container").animate({width:'0px'}, 250, function(){
					$("#scroll_container").fadeOut(300);
					$("#bg-holder-gebed").fadeIn(600,function(){
						if(link.attr("href").indexOf("http://")!=-1){
							document.location = link.attr("href");
						} else {
							document.location = $("base").attr("href")+link.attr("href");
						}
					});
				});
			}
		});	
		

		//Openen en sluiten van een gebed op de gebeden pagina
		$(".gebedsregel_kop").click(function(){
			if($(".gebedsregel_inhoud[rel="+$(this).attr("rel")+"]").css("display") == "block"){
				$(".gebedsregel_inhoud[rel="+$(this).attr("rel")+"]").slideUp(500, function(){
					$("#scroll_content").jScrollPane();
				});
			} else {
				$(".gebedsregel_inhoud[rel="+$(this).attr("rel")+"]").slideDown(500, function(){
					$("#scroll_content").jScrollPane();
				});
			}
		});	
		/***************************************************
		***************************************************/
	}
});

/***************************************************
	FUNCTIES EN SOW!
***************************************************/

//Bewegen van de pfotoslider
function fotoslider_bewegen_naar_pagina(pagina){
	$("#fotothumbslider .slider_container .slider").animate({ marginTop: -(pagina-1)*375 },{queue:false, duration:400});
}
function grote_fotoslider_bewegen_naar_pagina(pagina){
	$("#fotoviewer #fotoslider_container #fotoslider").animate({ marginLeft: -(pagina-1)*505 },{queue:false, duration:400});
}

function start_homeslider(){
	homeslider = setInterval(function(){
		if(huidige_slide_homeslider==aantal_slides_homeslider){ huidige_slide_homeslider=1; } else { huidige_slide_homeslider++; }
		$(".verander_home_slider").removeClass("aan");
		$(".verander_home_slider[rel="+huidige_slide_homeslider+"]").addClass("aan");
		verander_home_slider(huidige_slide_homeslider);		
	},6000);
}

function stop_homeslider(){
	window.clearInterval(homeslider);
}


function verander_home_slider(ga_naar_slider){ 
	$("#site_container_content_slider_home_afbeeldingslider").animate({ left: -(910*(ga_naar_slider-1)) },{queue:false, duration:600, easing: 'easeOutExpo'});
	$("#site_container_content_slider_home_tekstslider").animate({ top: -(135*(ga_naar_slider-1)) },{queue:false, duration:600, easing: 'easeOutExpo'});
	slider=ga_naar_slider;
}

function stelPaginaIn(){ 
	$links = $("#pagina_kolom_links").outerHeight();
	$rechts = $("#pagina_kolom_rechts").outerHeight();
	//alert("Links = " + $links + " | Rechts = " + $rechts);
	if($links<$rechts){ $("#pagina_kolom_links").css("padding-bottom",($rechts-$links)+"px"); } else { $("#pagina_kolom_rechts").css("padding-bottom",($links-$rechts)+"px"); } 
	
	//Instellen van de gebeds optiebalk
	/*hoogte_scherm = $(window).height();
	hoogte_gebeds_container = $("#gebedspagina_container").outerHeight();
	hoogte_gebedoptiebalk = $("#gebed_optiebalk").outerHeight();
	if(hoogte_scherm>(hoogte_gebeds_container+hoogte_gebedoptiebalk)){
		$("#gebed_optiebalk").css("margin-top",(hoogte_scherm-(hoogte_gebeds_container+hoogte_gebedoptiebalk))+"px");
	}*/
	
	//Centreren van de splashafbeelding
	breedte_scherm = $(window).width();
	hoogte_scherm = $(window).height();
	breedte_splashvenster = $("#splash_afbeelding_container").outerWidth();
	hoogte_splashvenster = $("#splash_afbeelding_container").height();
	$("#splash_afbeelding_container").css("left","-"+(Math.round((breedte_splashvenster-breedte_scherm)/2))+"px");
	$("#splash_afbeelding_container").css("top",(Math.round((hoogte_scherm-hoogte_splashvenster)/2))+"px");
}

function backgroundResize() { 
	var orgW = 1470;var orgH = 935;
	var h = $(window).height();var w = $(window).width();
	var ratioW = w / orgW;var ratioH = h / orgH;
	if(ratioW > ratioH){ $('.bg-img').css({'height':(w * orgH / orgW), 'width':w}); } else { $('.bg-img').css({'height':h, 'width':(h * orgW / orgH)});	}
	$('#bg-holder').css({'display':'block'});
}
