// ** initiate cufon

$(document).ready(function() {
	Cufon.now(); // voor IE om de delay te voorkomen
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('#header ul li a', {hover: true});
	Cufon.replace('ul#steps li a', {hover: true});
	Cufon.replace('ul#stepsHome', {hover: true});
	Cufon.replace('.popup h3', {hover: true});
	Cufon.replace('.buttons a.level1 p');
	Cufon.replace('.buttons a.level2 p');
	Cufon.replace('.buttons a.level3 p');
	
	$(".gotostep1").click(function(){	
			
		$("#stap1.stepContainer").show();	
		$("#stap2.stepContainer").hide();
		$("#stap3.stepContainer").hide();

		$(".gotostep2 a").removeClass('current');
		$(".gotostep3 a").removeClass('current');		
		$(".gotostep1 a").addClass('current');	
		
		Cufon.replace('ul#steps li a', {hover: true});
		
	});

	$(".gotostep2").click(function(){
		$("#stap1.stepContainer").hide();
		$("#stap2.stepContainer").show();	
		$("#stap3.stepContainer").hide();
		
		$(".gotostep1 a").removeClass('current');
		$(".gotostep3 a").removeClass('current');	
		$(".gotostep2 a").addClass('current');
		
		Cufon.replace('ul#steps li a', {hover: true});	
	});

	$(".gotostep3").click(function(){
		
		$("#stap1 input").each( function (index) {
			var themaid = $(this).attr("id");
			
	   		if( $(this).is(':checked') ) {
	   			$("#stap3 #thema-"+ themaid).show();
			   	$("#stap3 #titel-"+ themaid).show();
	   		} else {
	   			$("#stap3 #thema-"+ themaid).hide();
	   			$("#stap3 #titel-"+ themaid).hide();
	   		}
		});
		
		$("#stap2 input").each(function() {

			var partijid = $(this).attr("id");
			
	   		if( $(this).is(':checked') ) {
	   			$("#stap3 #partij-"+ partijid).show(); 
	   			$("#stap3 #programContainer ul."+ partijid).show();
	   			$("p."+ partijid).show();
	   		} else {
	   			$("#stap3 #partij-"+ partijid).hide();
	   			$("#stap3 #programContainer ul."+ partijid).hide();
	   			$("p."+ partijid).hide();
	   		}
	  	});
		
		$("#stap1.stepContainer").hide();
		$("#stap2.stepContainer").hide();	
		$("#stap3.stepContainer").show();

		$(".gotostep1 a").removeClass('current');	
		$(".gotostep2 a").removeClass('current');
		$(".gotostep3 a").addClass('current');
				
		Cufon.replace('ul#steps li a', {hover: true});
		
/*		// init carousel
		$("#partijSlider").jcarousel({ 
			scroll: 1,
			animation: "slow",
			itemLoadCallback: {onAfterAnimation: onSlideCarousel}
		});
*/
		PartijSlider.init();
				
		// init accordion
		$("#programContainer").accordion({
				autoHeight: false
		});		
		
	});
	
	$("#stap3 #programContainer ul").each(function() {
		$(this).hide();	
	});

	$("#button_slider_next").click(function(e) { 
		PartijSlider.slideNext();
		e.stopPropagation();
	});
	
	$("#button_slider_prev").click(function(e) { 
		PartijSlider.slidePrevious();
		e.stopPropagation();
	});
	
	// ** initiate jquery scripts 
	
	// Fancybox

	//init fancybox
	$("#colofon").fancybox({
		'titlePosition'		: 'outside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding'			: 30
	});
	
	//init fancybox
	$("#uitleg").fancybox({
		'titlePosition'		: 'outside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding'			: 30
	});
	
	// Carousel & Accordion 

	// bij reladen van de pagina checken welke checkboxen al aan staan
	$(".buttons input:checkbox:checked").each(function(){
		$(this).parent().addClass('checked');
	});
		
	// check en uncheck themas
	$(".buttons input:checkbox").change(function() {
   		if( $(this).is(':checked') ) {
   			$(this).parent().addClass('checked');
   		} else {
   			$(this).parent().removeClass('checked');
   		}
  	});

	// laat thema's in stap 3 al of niet zien aan de hand van selectie in stap 1
	$("#stap3 .programOverflowContainer>div, #stap3 h3").hide();
	$("#stap3 #partijSlider>li").hide();
	
	/*$("#stap1 input:checkbox").change(function() {

		var themaid = $(this).attr("id");
		
   		if( $(this).is(':checked') ) {
   			$("#stap3 #thema-"+ themaid).show();
		   	$("#stap3 #titel-"+ themaid).show();
   		} else {
   			$("#stap3 #thema-"+ themaid).hide();
   			$("#stap3 #titel-"+ themaid).hide();
   		}
  	});
  	
  	// laat partijen in stap 3 al of niet zien aan de hand van selectie in stap 2
	$("#stap2 input:checkbox").change(function() {

		var partijid = $(this).attr("id");
		
   		if( $(this).is(':checked') ) {
   			$("#stap3 #partij-"+ partijid).show(); 
   			$("#stap3 #programContainer ul."+ partijid).show();
   			$("p."+ partijid).show();
   		} else {
   			$("#stap3 #partij-"+ partijid).hide();
   			$("#stap3 #programContainer ul."+ partijid).hide();
   			$("p."+ partijid).hide();
   		}
  	});*/
  
});


// ** Focus input field

addLoadEvent(focusField);

function focusField(){
	$('input[type="text"]').focus(function() { $(this).removeClass("idleField").addClass("focusField") });  
	$('input[type="text"]').blur(function() { $(this).removeClass("focusField").addClass("idleField") }); 
	$('textarea').focus(function() { $(this).removeClass("idleField").addClass("focusField") });  
	$('textarea').blur(function() { $(this).removeClass("focusField").addClass("idleField") }); 
}

// ** Voer class 'current' toe aan active item active item 

addLoadEvent(highlightPage);

function highlightPage(){
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById) return false;
	if(!document.getElementById("nav")) return false;
	var nav = document.getElementById("nav");
	var links = nav.getElementsByTagName("a");
	for (var i=0; i<links.length; i++){
		var linkurl = links[i].getAttribute("href");
		var currenturl = window.location.href;
		// met indexOf kan je dingen aan elkaar vergelijken, nu willen we vergelijken of de urls hetzelfde zijn, als dat niet het geval is krijg je -1 terug. 
		if (currenturl.indexOf(linkurl) != -1){
			var currentClass = links[i].className;
			links[i].className = currentClass+"current";
			// haal de link tekst op en maak die tekst lowercase
			// var linktext = links[i].lastChild.nodeValue.toLowerCase();
			// document.body.setAttribute("id", linktext);
		}
	}
}

// ** Voeg class 'hover' toe aan element voor ie6

// voorbeeld: 
// addLoadEvent(function(){ addHoverClass('navigation','li'); });

function addHoverClass(element, child_node_name) {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	var root = document.getElementById(element);
	var children = root.getElementsByTagName(child_node_name);	
	for (var i = 0; i < children.length; i++){
		var child = children[i];
		child.onmouseover = function(){
			this.className += " hover"; }
		child.onmouseout = function(){
			this.className = this.className.replace(" hover", ""); }
	}
}

// Functie om een functie aan te roepen
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

var PartijSlider = {
		
	offset : 0,
	list_item_width : 246,
	max_offset : 0,
	children_count : 0, 
	
	init : function () {
		//var offset = $("#partijSlider").css("left");
		var offset = 0;
				
		var visible_children = $("#partijSlider li:visible");
		PartijSlider.children_count = visible_children.length;
		
		$("#button_slider_prev").hide();
		if (PartijSlider.children_count < 4) {
			PartijSlider.max_offset = 0;
			$("#button_slider_next").hide();
		} else {
			PartijSlider.max_offset = (PartijSlider.children_count*PartijSlider.list_item_width) - (3*PartijSlider.list_item_width);
		}
		
		// If the slider position is in a previous state, resit it to '0'
		PartijSlider.slide(offset);
	},
	
	slide : function (new_offset) {
		
		new_offset = parseInt(new_offset);
				
		// alert("new_offset: "+new_offset+" max_offset: "+(-PartijSlider.max_offset));
				
		if ((new_offset > 0) || (new_offset < -PartijSlider.max_offset)) {
			return false;
		}

		if (PartijSlider.children_count > 3) {
			if (new_offset == 0) {
				$("#button_slider_prev").hide('fast');
				$("#button_slider_next").show('fast');
			} else {
				$("#button_slider_prev").show('fast');
				
				if (new_offset == -PartijSlider.max_offset) {
					$("#button_slider_next").hide('fast');
				} else {
					$("#button_slider_next").show('fast');
				}
			}
		}
		
		PartijSlider.offset = new_offset;
		
		$("#partijSlider").animate({
			left : PartijSlider.getOffset()
		});
		
		$(".programOverflowContainer>div").each(function(){
			$(this).animate({
				opacity: '0',
				color: 'white'
			}, {
				duration: 350, 
				complete: function() {
					$(this).css("left", PartijSlider.getOffset()).animate({
						opacity: '1',
						color: 'black'
					});
				}
			});
		});
		
	},
	
	getOffset : function () {
		return PartijSlider.offset;
	},
	
	slidePrevious : function() {
		var offset = PartijSlider.getOffset();
		var new_offset = offset+PartijSlider.list_item_width;
		//$("#partijSlider").css("left", PartijSlider.setOffset(new_offset));
		PartijSlider.slide(new_offset);
	},
	
	slideNext : function() {
		var offset = PartijSlider.getOffset();
		var new_offset = offset-PartijSlider.list_item_width;
		//$("#partijSlider").css("left", PartijSlider.setOffset(new_offset));
		PartijSlider.slide(new_offset);
	}
}

/*
// Callback function van de carousel, die de accordion content mee laat sliden.
function onSlideCarousel(){

	// get the offset width from the jcarousel
	var offset = $("ul#partijSlider").css("left");
	
	if ( offset == 0 ) { return false; }
	
	$(".programOverflowContainer>div").each(function(){
		$(this).animate({
			opacity: '0',
			color: 'white'
		}, {
			duration: 350, 
			complete: function() {
				$(this).css("left",offset).animate({
					opacity: '1',
					color: 'black'
				});
			}
		});
	});
}*/
