$(document).ready(function(){

	/* Transform the <ul> in #news-agenda to tabs, using infoTabs */
	$('div#news-agenda').infoTabs({cancelLinkAction:1});

	/* Turning the whole .call-to-action-box into an anchor, to whatever the anchor in .call-to-action/#case links to */
    $(".teaser").click(function(){
    // search for onclick attribute
       if ($(this).find("a").attr('onclick')) {
        // open popup or new window?
            var clickfunction = $(this).find("a").attr('onclick').toString();
            if(clickfunction.indexOf('_blank')== -1){
                // popup, trigger inside anchor click, and stoppropagation there
                   $(this).find("a").trigger("click");return false;
            }else{
                // new window, beacause of target _blank in click event
                window.open($(this).find("a").attr("href"));return false;
            }
        } else {
        // regular in-site link, just replace location of window
            window.location=$(this).find("a").attr("href");return false;
        }
	});
    
    $(".teaser").find("a").click(function(e) {
    // triggered from above, so stop propagation here!
        e.stopPropagation();
    })

	$(".teaser.hover").click(function(){
    // search for onclick attribute
       if ($(this).find("a").attr('onclick')) {
        // open popup or new window?
            var clickfunction = $(this).find("a").attr('onclick').toString();
            if(clickfunction.indexOf('_blank')== -1){
                // popup, trigger inside anchor click, and stoppropagation there
                   $(this).find("a").trigger("click");return false;
            }else{
                // new window, beacause of target _blank in click event
                window.open($(this).find("a").attr("href"));return false;
            }
        } else {
        // regular in-site link, just replace location of window
            window.location=$(this).find("a").attr("href");return false;
        }
	});
    
	/* Adding a class to the .teaser-box and replacing the image inside with another image at :hover */
	$('.teaser').hover(function() {
		$(this).addClass('hover');
        if ($('.teaser.hover .read_more img').length) {// implies *not* zero 
            var src = $('.teaser.hover .read_more img').attr("src");
            var newsrc = src.substring(0, src.lastIndexOf('/')) + "/arrow-h.gif";
    		$('.teaser.hover .read_more img').attr("src", newsrc); 
        }
	}, function() {
		$(this).removeClass('hover');
            if ($('.teaser .read_more img').length) {// implies *not* zero 
            var src = $('.teaser .read_more img').attr("src");
            var newsrc = src.substring(0, src.lastIndexOf('/')) + "/arrow.png";
    		$('.teaser .read_more img').attr("src", newsrc); 
        }
	});
	
	/* Adding a class to the main_nav-items at :hover, in case there's a dropdown menu that needs to be displayed.  */
	$('#main_nav li').hover(function() {
	$(this).addClass('hover');
	}, function() {
	$(this).removeClass('hover');
	});	
	
	/* Adding a class to hovered submit buttons */
	$('.submit').hover(function() {
	$(this).addClass('hover');
	}, function() {
	$(this).removeClass('hover');
	});

	/* Supersleighting PNG's for IE6, using the ss plugin for jQuery */
    var thisScript = document.getElementById('functions');// the id in page for this file
    var thisScriptSrc = thisScript.getAttribute('src');
    var xSrc = thisScriptSrc.substring(0, thisScriptSrc.lastIndexOf('/')) + "/x.gif";
	$('#case').supersleight({shim: (xSrc)});
	$('.box').supersleight({shim: (xSrc)});
	
	/* Emptying contents of #direct_naar inputs, and not emptying when text is entered by user */
	$('#nav #direct_naar input').click(
		function() {
			if (this.value == this.defaultValue) {
			this.value = '';
			}
		}
	);
	/* After losing focus, displaying the default input value */
	$('#nav #direct_naar input').blur(
		function() {
			if (this.value == '') {
			this.value = this.defaultValue;
			}
		}
	);

	/* Adding text enlarger to #service_nav (because without js it doesn't work, so better not to show it either) */
	$('#service_nav').prepend('<li>Tekstgrootte <a href="#" id="decrease-textsize"><abbr title="verkleinen">&mdash;</abbr></a> <a href="#" id="increase-textsize"><abbr title="vergroten">+</abbr></a></li>');

	/* Adding 'view more' link to aside_lists with 'long' class and hiding the 'more' part 
    var openmoreSrc = thisScriptSrc.substring(0, thisScriptSrc.lastIndexOf('/')) + "/open-more.gif";
	$(".aside_list.long ul:first").append('<img class="open-more" src="' + openmoreSrc + '" title="Toon meer"  alt="Pijltje naar beneden" />');
    
	$(".aside_list.long ul:last").hide();
	/* Using the 'view more' link to display the 'more' part and add a 'close' link 
	$(".aside_list.long .open-more").click(
		function () {
			$(".aside_list.long ul:last").slideDown('slow');
			$(".aside_list.long img.open-more").hide();
			$(".aside_list.long .close-more").click(
				function () {
				 	$(".aside_list.long ul:last").slideUp();
					$(".aside_list.long .open-more").slideDown(600);
					return false
				}
			);
			return false
		}
	);
    var closemoreSrc = thisScriptSrc.substring(0, thisScriptSrc.lastIndexOf('/')) + "/close-more.gif";
	$(".aside_list.long ul:last").append('<img class="close-more" src="' + closemoreSrc + '" title="Toon minder"  alt="Pijltje naar boven" />');
	*/
    /* Fold second try */
    var openmoreSrc = thisScriptSrc.substring(0, thisScriptSrc.lastIndexOf('/')) + "/open-more.gif";
    var closemoreSrc = thisScriptSrc.substring(0, thisScriptSrc.lastIndexOf('/')) + "/close-more.gif";
    $(".aside_list ul.more").each(function() {
        $("li:gt(5)", this).hide(); /* :gt() is zero-indexed */
    });
    $("ul.more").after("<a href='javascript:void(0);' class='expand'><img src='"+ openmoreSrc +"' class='open-more' title='Toon meer' alt='Pijltje naar beneden' /></a>");
    $("a.expand").live("click", function() {
        var li = $(this).parent().children("ul.more").find("li");
        li.slideDown(600);
        $(this).removeClass('expand').addClass('decrease').html("<img src='" + closemoreSrc +"' class='close-more' title='Toon minder' alt='Pijltje omhoog' />");
        return false;
    });
 
    $("a.decrease").live("click", function() {
        $(this).parent().find("li:gt(5)").slideUp();
        $(this).removeClass('decrease').addClass('expand').html("<img src='" + openmoreSrc + "' class='open-more' title='Toon meer' alt='Pijltje naar beneden' />");
    });
    
	/* Autocomplete */
	//$('#aandoening').hintbox({ url: '/aandoeningen.json',matchHint: true,json: true});
	//$('#specialisme').hintbox({ url: '/specialismen.json',matchHint: true,json: true});
	//$('#arts').hintbox({ url: '/artsen.json',matchHint: true,json: true});

    $('input#aandoening').jsonSuggest(alysisAutosuggestData.aandoeningen, {onSelect:callback},alysisAutosuggestData.aandoeningenOverviewUrl);
    $('input#specialisme').jsonSuggest(alysisAutosuggestData.specialismen, {onSelect:callback}, alysisAutosuggestData.specialismenOverviewUrl);
    $('input#arts').jsonSuggest(alysisAutosuggestData.artsen, {onSelect:callback},alysisAutosuggestData.artsenOverviewUrl);
    
	/* A-Z overzicht  */
	
	/* A-Z overzicht: when user selects any letter, hide all and remove columns */
	$(".alphabet a").click(
		function () {
			$(".alphabetical > li").hide();
			//$(".alphabetical").css('float','none'); 
			$(".alphabetical").siblings("ol").removeClass('showColumn').addClass('hideColumn'); 
			return false
		}
	);	
	/* A-Z overzicht: view one letter only and add class 'selected' to that letter */
	$(".alphabet a").click(
		function () {
			var letter = $(this).text();
			$('.alphabetical li h3').filter(function (index) { return $(this).text() == letter; }).parent().show(); // check if there's a <li> that contains a <h3> with the same content as the clicked <a>, and if so, show it
			$('.alphabetical li h3').filter(function (index) { return $(this).text() == letter; }).parent().parent().removeClass('hideColumn').addClass('showColumn'); 
			$(".alphabet a").removeClass('selected'); // make sure previously selected items don't have 'selected' class
			$(this).addClass('selected'); // add 'selected' class to selected item
		}
	);
}); 

function callback(item) {
    top.location.href = item.link;
}