$(document).ready(function() {
    if (0 < $('#toolkitHerstelstrategieen').length) {
     
        // //  Onload Toolkit Herstelstrategieën page:
        $('.radiolist').show();
        $('.reports').show();
        $('.intfield').hide();
        
		// Open first field
		//$('.intfield:first').show();
		//$(".intbar:first > a[class!='fbox']").removeClass();
		//$(".intbar:first > a[class!='fbox']").addClass("opened");
        
		$("input[id$='0']").attr("checked", true); // set all first radiobuttons 'checked' 
        $("p[class^='report']").hide(); // hide all reports
        $("p[id$='o0']").show(); // show all first reports
		
        		
		// Show/Hide fields onclick on the bar
        $(".intbar > a[class!='fbox']").click(function() {
            if (this.className == 'opened' || this.className == 'closed') {
                $('#field' + this.parentNode.id.substr(3) + '').slideToggle('fast');
                if (this.className == 'closed') {
                    this.className = 'opened';
                } else {
                    this.className = 'closed';
                }
            }
        });

        // Add title to 'fbox' classed hrefs in left column; get title from begrippenlijst list in right column
        var cur, fmatch;
        var cas = $("div.colLeft a.fbox");
        for (var i = 0; i < cas.length; i++) {
            try {
                cur = cas[i].href.substr(cas[i].href.indexOf("/items/"));
                fmatch = $("#ulBegrippenLijst a.fbox[href='" + cur + "']");
                if (0 < fmatch.length && fmatch[0].getAttribute('title')) {
                    if (cas[i].innerHTML != "Uitleg") {
                        cas[i].setAttribute("title", "" + fmatch[0].getAttribute('title') + "");
                    }
                }
            } catch (err) {
                // do nothing
            }
        }

        
		// //  ~~~~~~~~~ Bar 1 [Natura 2000-gebied] ~~~~~~~~~ // //	

        // Click Open Session Link
        $('#intStartOpenLink').click(function() {
            $('#field1').slideUp('fast', function() {
				$('#intStartNew').hide();
				$('#intStartOpen').show();
				$('#field1').slideDown('fast');
			});
        });
		// Click Cancel-Open Button
        $('#intStartOpenCancel').click(function(e) {
            e.preventDefault();
			$('#field1').slideUp('fast', function() {
				$('#intStartNew').show();
				$('#intStartOpen').hide();
				$('#field1').slideDown('fast');
			});
        });
		// Click Open Button
        $('#intStartOpenSubmit').click(function(e) {
            e.preventDefault();
			alert('Deze functie is nog niet actief.');
        });
		// Click New Button
        $('#intStartNewSubmit').click(function(e) {
            e.preventDefault();
			if ($('#intStartNewInput').val() != "") {
				$('#bar1 > a:first').removeClass();
				$('#bar1 > a:first').addClass('closed');
				$('#bar2 > a:first').removeClass();
				$('#bar2 > a:first').addClass('opened');
				$('#field1').slideUp('fast', function() {
					$('#intStartNew').hide();
					$('#intStartReady').show();
					$('#field2').slideDown('fast');
				});
			} else {
				alert('Kies een Natura 2000-gebied om te starten. Of  open een eerder opgeslagen sessie.');	
			}
        });
		
		// //  ~~~~~~~~~ Bar 2 [Habitattypen] ~~~~~~~~~ // //	

        // Click 'Deze habitattypen meenemen' Button
        $('#intHabitattypenCheckboxesSubmit').click(function() {
            $('#bar2 > a:first').removeClass();
			$('#bar2 > a:first').addClass('closed');
			$('#bar3 > a:first').removeClass();
			$('#bar3 > a:first').addClass('opened');
			$('#field1').slideUp('fast');
			$('#field2').slideUp('fast', function() {
				$('#intHabitattypenCheckboxes').hide();
				$('#intHabitattypenReady').show();
				$('#field3').slideDown('fast');
			});
        });
		
		// //  ~~~~~~~~~ Bar 3 [Gebiedsanalyse] ~~~~~~~~~ // //	
		
		// Button "Voldoende Informatie" Gebiedsanalyse (collapsible area 1) for the three habitattypes
		$('#intGebiedsanalyseSubmit1').click(function(){
			if( $('#intGebiedsanalyseInfoInput1:checked').val() == 'on' )	{
				$('#intGebiedsanalyseInfoCheckbox1').hide();
				$('#intGebiedsanalyseInfoReady1').show();
				$('#intGebiedsanalyse1 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intGebiedsanalyse1 div.collapsibleArea-deactivated').removeClass().addClass('collapsibleArea-closed')",1000);
			} else {
				alert('Zorg dat u over voldoende informatie beschikt over het habitattype voordat u overgaat tot gebiedsanalyse.');	
			}
		})
		$('#intGebiedsanalyseSubmit2').click(function(){
			if( $('#intGebiedsanalyseInfoInput2:checked').val() == 'on' )	{
				$('#intGebiedsanalyseInfoCheckbox2').hide();
				$('#intGebiedsanalyseInfoReady2').show();
				$('#intGebiedsanalyse2 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intGebiedsanalyse2 div.collapsibleArea-deactivated').removeClass().addClass('collapsibleArea-closed')",1000);
			} else {
				alert('Zorg dat u over voldoende informatie beschikt over het habitattype voordat u overgaat tot gebiedsanalyse.');	
			}
		})
		$('#intGebiedsanalyseSubmit3').click(function(){
			if( $('#intGebiedsanalyseInfoInput3:checked').val() == 'on' )	{
				$('#intGebiedsanalyseInfoCheckbox3').hide();
				$('#intGebiedsanalyseInfoReady3').show();
				$('#intGebiedsanalyse3 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intGebiedsanalyse3 div.collapsibleArea-deactivated').removeClass().addClass('collapsibleArea-closed')",1000);
			} else {
				alert('Zorg dat u over voldoende informatie beschikt over het habitattype voordat u overgaat tot gebiedsanalyse.');	
			}
		})
		
		// Previous + Next buttons
		$('#intGebiedsanalyseNext1').click(function() { 
			$('#intGebiedsanalyse1 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intGebiedsanalyse1').slideUp('fast', function() { $('#intGebiedsanalyse2').slideDown('fast') })",250);
		}) 
		$('#intGebiedsanalyseNext2').click(function() { 
			$('#intGebiedsanalyse2 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intGebiedsanalyse2').slideUp('fast', function() { $('#intGebiedsanalyse3').slideDown('fast') })",250);
		}) 
		$('#intGebiedsanalysePrevious2').click(function() { 
			$('#intGebiedsanalyse2 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intGebiedsanalyse2').slideUp('fast', function() { $('#intGebiedsanalyse1').slideDown('fast') })",250);
		}) 
		$('#intGebiedsanalysePrevious3').click(function() { 
			$('#intGebiedsanalyse3 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intGebiedsanalyse3').slideUp('fast', function() { $('#intGebiedsanalyse2').slideDown('fast') })",250);
		}) 
		
		// Checkboxes voldoende ingevuld
		$('#field3 input:checkbox').click(function(){ checkCheckboxesGebiedsanalyse($(this)) })
		function checkCheckboxesGebiedsanalyse(clickedCheckbox) {
			if($('#field3 input:checkbox:checked').length == 12) {
				alert('U heeft de gebiedsanalyse voltooid.\nU kunt verder naar de oplossingsrichting per habitattype.')
				$('#field3 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intGebiedsAnalyseSubmit').removeAttr('disabled').attr('value', 'Gebiedsanalyse voltooid, ga verder');",1000);
			} else {
				$('#intGebiedsAnalyseSubmit').removeAttr('disabled').attr('disabled', 'disabled').attr('value', 'Gebiedsanalyse nog niet voltooid');
				$('#bar4 > a:first').addClass('deactivated');
				$('#field4').slideUp('fast');
				var checkBoxId = clickedCheckbox.attr('id');
				if (clickedCheckbox.is(':checked')) {
					if (checkBoxId.indexOf('intGebiedsanalyseInfoInput') < 0) {
						clickedCheckbox.closest('.collapsibleArea-opened').find('span').eq(0).trigger('click');
					}
				}
			}
		}
		
		// Button voldoende Gebiedsanalyse ingevuld
		$('#intGebiedsAnalyseSubmit').click(function() {
			$('#bar3 > a:first').removeClass().addClass('closed');
			$('#bar4 > a:first').removeClass().addClass('opened');
			$('#field1').slideUp('fast');
			$('#field2').slideUp('fast');
			$('#field3').slideUp('fast', function() {
				$('#field4').slideDown('fast');
			});
		});
		
		
		// //  ~~~~~~~~~ Bar 4 [Oplossingsrichting en maatregelen] ~~~~~~~~~ // //	
		
		// Previous + Next buttons
		$('#intOplossingsrichtingNext1').click(function() { 
			$('#intOplossingsrichting1 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intOplossingsrichting1').slideUp('fast', function() { $('#intOplossingsrichting2').slideDown('fast') })",250);
		}) 
		$('#intOplossingsrichtingNext2').click(function() { 
			$('#intOplossingsrichting2 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intOplossingsrichting2').slideUp('fast', function() { $('#intOplossingsrichting3').slideDown('fast') })",250);
		}) 
		$('#intOplossingsrichtingPrevious2').click(function() { 
			$('#intOplossingsrichting2 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intOplossingsrichting2').slideUp('fast', function() { $('#intOplossingsrichting1').slideDown('fast') })",250);
		}) 
		$('#intOplossingsrichtingPrevious3').click(function() { 
			$('#intOplossingsrichting3 div.collapsibleArea-opened > span').trigger('click');
			setTimeout("$('#intOplossingsrichting3').slideUp('fast', function() { $('#intOplossingsrichting2').slideDown('fast') })",250);
		}) 
		
		// Checkboxes voldoende ingevuld
		$('#field4 input:checkbox').click(function(){ checkCheckboxesOplossingsrichting($(this)) })
		function checkCheckboxesOplossingsrichting(clickedCheckbox) {
			if($('#field4 input:checkbox:checked').length == 3) {
				alert('U heeft de oplossingsrichting en maatregelen voltooid.\nU kunt verder naar synthese maatregelenpakket.')
				$('#field4 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intOplossingsrichtingSubmit').removeAttr('disabled').attr('value', 'Oplossingsrichting en maatregelen voltooid, ga verder');",1000);
			} else {
				$('#intOplossingsrichtingSubmit').removeAttr('disabled').attr('disabled', 'disabled').attr('value', 'Oplossingsrichting en maatregelen nog niet voltooid');
				$('#bar5 > a:first').addClass('deactivated');
				$('#field5').slideUp('fast');
				var checkBoxId = clickedCheckbox.attr('id');
				if (clickedCheckbox.is(':checked')) {
					clickedCheckbox.closest('.collapsibleArea-opened').find('span').eq(0).trigger('click');
				}
			}
		}
		
		// Button voldoende Oplossingsrichting en maatregelen ingevuld
		$('#intOplossingsrichtingSubmit').click(function() {
			$('#bar4 > a:first').removeClass().addClass('closed');
			$('#bar5 > a:first').removeClass().addClass('opened');
			$('#field1').slideUp('fast');
			$('#field2').slideUp('fast');
			$('#field3').slideUp('fast');
			$('#field4').slideUp('fast', function() {
				$('#field5').slideDown('fast');
			});
		});
		
		
		// //  ~~~~~~~~~ Bar 5 [Synthese] ~~~~~~~~~ // //	
		
		// Checkbox voldoende ingevuld
		$('#field5 input:checkbox').click(function(){ checkCheckboxSynthese($(this)) })
		function checkCheckboxSynthese(clickedCheckbox) {
			if($('#field5 input:checkbox:checked').length == 1) {
				alert('U heeft de sythese voltooid.\nU kunt verder naar de beoordeling.')
				$('#field5 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intSyntheseSubmit').removeAttr('disabled').attr('value', 'Synthese voltooid, ga verder');",1000);
			} else {
				$('#intSyntheseSubmit').removeAttr('disabled').attr('disabled', 'disabled').attr('value', 'Synthese nog niet voltooid');
				$('#bar6 > a:first').addClass('deactivated');
				$('#field6').slideUp('fast');
				var checkBoxId = clickedCheckbox.attr('id');
				if (clickedCheckbox.is(':checked')) {
					clickedCheckbox.closest('.collapsibleArea-opened').find('span').eq(0).trigger('click');
				}
			}
		}
		
		// Button voldoende Synthese ingevuld
		$('#intSyntheseSubmit').click(function() {
			$('#bar5 > a:first').removeClass().addClass('closed');
			$('#bar6 > a:first').removeClass().addClass('opened');
			$('#field1').slideUp('fast');
			$('#field2').slideUp('fast');
			$('#field3').slideUp('fast');
			$('#field4').slideUp('fast');
			$('#field5').slideUp('fast', function() {
				$('#field6').slideDown('fast');
			});
		});
		
		
		// //  ~~~~~~~~~ Bar 6 [Beoordeling] ~~~~~~~~~ // //	
		
		// Checkbox voldoende ingevuld
		$('#field6 input:checkbox').click(function(){ checkCheckboxBeoordeling($(this)) })
		function checkCheckboxBeoordeling(clickedCheckbox) {
			if($('#field6 input:checkbox:checked').length == 1) {
				alert('U heeft de beoordeling voltooid.\nSla uw sessie op en download het Word document.')
				$('#field6 div.collapsibleArea-opened > span').trigger('click');
				setTimeout("$('#intBeoordelingSubmit').removeAttr('disabled').attr('value', 'Beoordeling voltooid!');",1000);
			} else {
				$('#intBeoordelingSubmit').removeAttr('disabled').attr('disabled', 'disabled').attr('value', 'Beoordeling nog niet voltooid');
				var checkBoxId = clickedCheckbox.attr('id');
				if (clickedCheckbox.is(':checked')) {
					clickedCheckbox.closest('.collapsibleArea-opened').find('span').eq(0).trigger('click');
				}
			}
		}
		
		// Button voldoende Synthese ingevuld
		$('#intBeoordelingSubmit').click(function() {
			$('#bar6 > a:first').removeClass().addClass('closed');
			$('#field1').slideUp('fast');
			$('#field2').slideUp('fast');
			$('#field3').slideUp('fast');
			$('#field4').slideUp('fast');
			$('#field5').slideUp('fast');
			$('#field6').slideUp('fast');
		});
		
		

        // overflow:hidden; should solve IE8 bug (disappearing margins i.c.w. jquery slidetoggle)
        $('#toolkitHerstelstrategieen > *').css("overflow", "hidden");
    }
});




