	// Run when document is loaded
	$(function(){
		// Zinnen
		foldFourLinesBig();
		
		foldFourLinesSmall();
	
	
		$('.moreButton').bind('click',function(){
			openFourLines();
		});
		
		// Slideshow
		slideshow();
		
		// Horizontal Slider
		horizontalSlider();
		
		// Plus Knop
		findLimits();
		$('.plus-button').bind('click',function(){
			plusMultiple($(this));
		});
		
		// Delete Knop
		var cururl = location.href;
		$('.cms-delete-button').bind('click',function(){
			var nexturl = location.protocol + '//' + location.hostname + $(this).attr("rel");
			confirmDelete(cururl,nexturl);
		});
		
		// Help Icoon Hover
		$('.help-icon').hover(
			function(){
				$(this).find('.help-text').css("display","block");
			},
			function(){
				$(this).find('.help-text').css("display","none");
			}
		);
		

		
	});

	// Horizontal Slider
	function horizontalSlider(){
		// Settings
		var wait 				= 7; 	// seconds
		var teller				= 0;	// Startpunt
		var firstrun			= true	// Firstrun
		
		// Declare
		var width = $('#horizontal-slide').width();
		var numberOfItems = $('#horizontal-slide-inner').find('.horizontal-slide-inner-item').size();
		var totalwidth = width * numberOfItems;
		$('#horizontal-slide-inner').css('width',totalwidth);
		
		// run horizontalSlider
		var cycleTimeout = setTimeout(function(){ runHorizontalSlider(wait,width,totalwidth,numberOfItems,teller,firstrun) }, wait*1000);
	}
	
	function runHorizontalSlider(wait,width,totalwidth,numberOfItems,teller,firstrun){
		teller++;
		var newLeft = teller * width;
		firstrun = false;
			if(teller==numberOfItems){
				newLeft = 0;
				teller = 0;
				$('#horizontal-slide-inner').animate({"left": "0px"},'slow',"swing");
			}else{
				$('#horizontal-slide-inner').animate({"left": "-=940px"},'slow',"swing");
			}
		
//		$('#horizontal-slide-inner').css('left','-100px');
		var cycleTimeout = setTimeout(function(){ runHorizontalSlider(wait,width,totalwidth,numberOfItems,teller,firstrun) }, wait*1000);
	}

	// Slideshowscript v.2.0
	function slideshow(){
		$.getJSON("/media/ajaxslideshow/slideshow", function(json){
			// Settings
			var wait 				= 7; 	// seconds
			var transitionLength 	= 2;	// seconds
			var randomstart			= true;	// boolean
	
			// Declare
			var pictures 			= new Array();
			if(randomstart==true){
				var teller = Math.floor(Math.random()*json.length);
			}else{
				var teller				= 0;
			}
			var firstrun			= true;
	
			for (var i=0; i < json.length; i++) {
				pictures[i] = '/App/Resources/media/images/originals/' + json[i]['filename'];
			};
		
			$('#slideshow').html("<div></div>");
			runSlideshow(wait,transitionLength,pictures,teller,firstrun);
		 });
	}

	function runSlideshow(wait,transitionLength,pictures,teller,firstrun){
		picLength = pictures.length;
		$('#slideshow div').css("background-image","url("+pictures[teller]+")");	// Stel de achtergrond van het voorgrondplaatje in die we straks willen weg faden
		$('#slideshow div').css("display","block");		// Maak zichtbaar om straks weg te kunnen faden.

		// Zoek het nummer van het nieuwe plaatje
		if(!firstrun){
			teller++;
			if(teller > (picLength-1)){
				teller=0;
			}
		 }else{
		 	firstrun = false; // Na deze ronde is dit niet meer de eerste ronde	
		}
	
		// Preload
		imageObj = new Image();	
		imageObj.src = pictures[teller];
		imageObj.onload = function(){
		// // 	// Stel de achtergrond van het achterste plaatje in die na de fade zichtbaar wordt.
			$('#slideshow').css("background-image","url("+pictures[teller]+")");
			$('#slideshow div').fadeOut(transitionLength*1000);
			var cycleTimeout = setTimeout(function(){ runSlideshow(wait,transitionLength,pictures,teller,firstrun) }, wait*1000);
		}
	}
	// End of Slideshowscript

	var limit = new Array();
	
	function findLimits(){
		// Zoek de verschillende plusjes op en bepaal het limiet
		$('.plus-button').each(function(index){
			var temparray = $(this).attr("rel").split('.');
			limit[temparray[0]] = temparray[1];
		});	
	}

	function plusMultiple(object){
		var myhtml = object.parent().parent().find('.repeatable')[0].innerHTML;
		myhtml = '<fieldset class="list-item">' + myhtml + '</fieldset>';
		object.parent().parent().find('.plus').before(myhtml);
	}

	function confirmDelete(cururl,nexturl){
		if(confirm('Weet je zeker dat je dit item wilt verwijderen?')){
			location.href = nexturl;
		}else{
			location.href = cururl;
		}
	}
	
	var fourLines = new Array();
	
	function foldFourLinesBig(){
		if ( typeof($('#four-lines-big'))[0] !== "undefined" ) {
			fourLines[0] =  $('#four-lines-big')[0].innerHTML;		
			var splitparagraaf 	= '<p>' + $('#four-lines-big p:first')[0].innerHTML + '</p>';
			var tmp = $('#four-lines-big')[0].innerHTML.split(splitparagraaf);
			if(typeof(tmp[0])!=="undefined"){
				var moreTxt = '<p><a class="moreButton" href="#">Meer...</a></p>';
			}else{
				var moreTxt = '';
			}
			// console.log(tmp[0]);
			$('#four-lines-big')[0].innerHTML = tmp[0] + splitparagraaf  + moreTxt;
		}
	}
	
	function openFourLines(){
//		console.log("bla");
		$('#four-lines-big')[0].innerHTML = fourLines[0];
		$('#four-lines-small')[0].innerHTML = fourLines[1];
	}
		
	function foldFourLinesSmall(){
		if ( typeof($('#four-lines-small'))[0] !== "undefined" ) {
			fourLines[1] =  $('#four-lines-small')[0].innerHTML;
			if(fourLines[1]!==""){
				var splitparagraaf 	= '<p>' + $('#four-lines-small p:first')[0].innerHTML + '</p>';
				var tmp = $('#four-lines-small')[0].innerHTML.split(splitparagraaf);
				var moreTxt = '<p><a class="moreButton" href="#">Meer...</a></p>';
				$('#four-lines-small')[0].innerHTML = tmp[0] + splitparagraaf + moreTxt;
			}
		}
	}
