$(function(){

    /*
		Navigation
	*/
	$('nav > ul > li > a.dropdown').mouseenter(function(){
       
	   // reset others
	   $('.nav-sub').not($('ul#nav-top > li > .nav-sub')).stop(false, true).hide();
	   
        var button = $(this);
		var header = $('body > header');
        var submenu = $(this).next('div.nav-sub');

	    submenu.css({
	        position: 'absolute',
	        top: $(this).offset().top  + $(this).outerHeight() + 'px', 
	       	left: ($(this).offset().left - 8) + 'px',
	       	width: ($(this).outerWidth() + 16)+ 'px',
	    	zIndex: 1000
	    });
	    
	    submenu.find('ul, li').css({
	       	width: ($(this).outerWidth() + 16)+ 'px'
	    });
	    submenu.find('span').css({
	       	width: $(this).outerWidth()+ 'px'
	    });
	    
	    submenu.find('li a').css({
	       	width: ($(this).outerWidth() - 16)+ 'px'
	    });
	    submenu.find('li.last a').css({
	       	width: ($(this).outerWidth() - 21)+ 'px'
	    });
	    	    	    
		submenu.show();
		button.addClass('hover');
		header.addClass('dropdown');
        submenu.mouseleave(function(ev){
			if (!($(ev.relatedTarget).is(button) || $(ev.relatedTarget).parents().is(button))){
        		 $(submenu).hide();
        		 button.removeClass('hover');
				 header.removeClass('dropdown');
			}
        });
        button.mouseleave(function(ev){
			if (!($(ev.relatedTarget).is(submenu) || $(ev.relatedTarget).parents().is(submenu))){
         		$(submenu).hide();
         		button.removeClass('hover');
				header.removeClass('dropdown');
			}
        });
    });

    /*
		Login
	*/
    var loginButton = $('a#login');
    if (loginButton.size() > 0) {
    	
	    var loginSubmenu = loginButton.next('div.nav-sub');
	    loginSubmenu.css({
	        position: 'absolute',
	        top: loginButton.offset().top  + loginButton.outerHeight() + 'px', 
	       	left: (loginButton.offset().left + (loginButton.outerWidth()/2) - (loginSubmenu.outerWidth()/2)) + 'px',
	    	zIndex: 1000
	    });
	    
	    loginButton.click(function(){
	    	
			loginSubmenu.show();
			loginButton.addClass('hover');
			
	        return false;
	    });
	    
	    $('html').click(function(){
	    	loginSubmenu.hide();
     		loginButton.removeClass('hover');
	    });
		
	    loginSubmenu.click(function(ev){
			ev.stopPropagation();
		});
	}
	
	$("#search_term").click(function(){
		$(this).animate({
			width:150
		});
	});
	$("#search_term").focusout(function(){
		$(this).animate({
			width:80
		});
	});
	
	// Banner
	
	$('#banner-organisatie a').mouseenter(function(){
		var id = $(this).parent('li').attr('id') + '-div';
		$('.banner-middle:visible').hide();
		//$('#banner-middle-main').fadeTo(200, 0.5);
		$('#' + id).fadeIn(200);
		$('#banner-organisatie a').removeClass('active');
		$(this).addClass('active');
		$(this).parent('li').css({opacity:1.0});
		$('#banner-organisatie a').not(this).parent('li').css({opacity:0.5});
		Cufon.refresh();
	});
	$('#banner-organisatie a').click(function(){
		return false;
	});
	$('#banner .container').mouseleave(function(ev){
			$('.banner-middle:visible').not('#banner-middle-main').fadeOut(400);
			$('#banner-middle-main').fadeIn(400);
			$('#banner-organisatie a').parent('li').css({opacity:1.0});
			$('#banner-organisatie a').removeClass('active');
			Cufon.refresh();
	});
	
	
	// Side
	$('#content-side h3.toggle:not(.expanded)').next('ul').hide();
	$('#content-side h3.toggle').bind('click', function() {
		$(this).toggleClass('expanded').next('ul').slideToggle();
		$(this).parents('.side-block').find('h3.toggle').not($(this)).removeClass('expanded');
		$(this).parents('.side-block').find('ul:visible').not($(this).next('ul')).slideUp();	
	}); 	

	// question-list
	$('.question-list h3.toggle:not(.expanded)').next('div.question-list-content').hide();
	$('.question-list h3.toggle').bind('click', function() {
		if ($(this).hasClass('expanded')) {
			$(this).next('div.question-list-content').slideUp( function(){
				$(this).prev('h3.toggle').removeClass('expanded')
			});		
		} else {
			$(this).addClass('expanded')
			$(this).next('div.question-list-content').slideDown();
		}
		
		$('.question-list h3.toggle').not($(this)).removeClass('expanded');
		$('.question-list div.question-list-content:visible').not($(this).next('div.question-list-content')).slideUp();
	}); 

	// FAQ-list
	$('.faq-list h3.toggle:not(.expanded)').next('div.faq-list-content').hide();
	$('.faq-list h3.toggle').bind('click', function() {
		if ($(this).hasClass('expanded')) {
			$(this).next('div.faq-list-content').slideUp( function(){
				$(this).prev('h3.toggle').removeClass('expanded')
			});		
		} else {
			$(this).addClass('expanded')
			$(this).next('div.faq-list-content').slideDown();
		}
		
		$('.faq-list h3.toggle').not($(this)).removeClass('expanded');
		$('.faq-list div.faq-list-content:visible').not($(this).next('div.faq-list-content')).slideUp();
	}); 
	
	//colorbox
	$("a[href$='.jpg']").fancybox({
		'titlePosition' : 'over',
		'overlayColor' : '#170a2a',
		'overlayOpacity' : 0.5,
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
		return '<span id="fancybox-title-over">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	}); 

	// ie6 ->> nog specifieker maken voor de elementen
	$('body').supersleight();
	
	//© in <sup>-tags plaatsen
	$('body').superscript("©");
	
	// Cufon
	$('input.clearDefault').clearDefault();
	Cufon.replace('h1.slogan', {
		fontFamily: 'InterFace Light'
	});
	Cufon.replace('#content header h1', {
		fontFamily: 'InterFace',
		textShadow: '#000 1px 1px'
	});
	Cufon.replace('#banner ul#banner-organisatie a', {
		fontFamily: 'InterFace',
		textShadow: '#000 1px 1px',
		hover: true
	});
	Cufon.replace('#banner h1', {
		fontFamily: 'InterFace',
		textShadow: '#000 1px 1px',
		hover: true
	});
	Cufon.replace('header p', {
		fontFamily: 'InterFace Light'
	});
	Cufon.replace('blockquote p.quote', {
		fontFamily: 'InterFace Light'
	});
	Cufon.replace('article blockquote', {
		fontFamily: 'InterFace Light'
	});
});
(function($){
    $.fn.extend({
        clearDefault: function(){
            return this.each(function(){
                var default_value = $(this).val();
                $(this).focus(function(){
                    if ($(this).val() == default_value) 
                        $(this).val("");
                });
                $(this).blur(function(){
                    if ($(this).val() == "") 
                        $(this).val(default_value);
                });
            });
        }
    }); 
})(jQuery);
