$(function(){
    // Uniform formfields
    $('input, textarea, select, button').uniform();
    // End uniform formfields

    // Transform datepicker fields
    var dates =[[new Date(2011,10-1,22),new Date(2011,10-1,30),'']];
    var datepicker_options = {
        defaultDate: '22-10-2011',
        beforeShowDay: highlightDays,
        dateFormat: 'dd-mm-yy',
        dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za']
    };
    $('#datepicker').datepicker(datepicker_options);
    $('#datepickertill').datepicker(datepicker_options);
    function highlightDays(date) {
        for (var i = 0; i < dates.length; i++) {
            if (dates[i][0] <= date && dates[i][1] >= date) {
                return [true, 'ui-state-highlight', dates[i][3]];
            }
        }
        return [false, ''];
    }
    // End transform datepicker fields
    
    // Menu
    var openMenus = 0;
    $('div#overlay').hide().css({opacity: 0.5});
    $('div.menu ul li').has('div.megadropdown').hover(function() {
        if (!openMenus) {
            $('div#overlay').show().animate({'opacity':0.5}, {'duration':200, 'queue':false});
        }
        openMenus++;
        $(this).find('div.megadropdown').stop(true, true).slideDown(400, function() {
            $(this).show();
        });
    }, function(){
        $(this).find('div.megadropdown').stop(true, true).slideUp(400, function() {
            openMenus--;
            if (!openMenus) {
                $('div#overlay').animate({'opacity':0.01}, {'duration':200, 'queue':false, 'complete':function() {
                    if (!openMenus) {
                        $('div#overlay').hide();
                    }
                }});
            }
        });
    });
    // End menu

    // Form textfield reset
    $('input[type="text"]').focus(function() {
        $(this).parent().removeClass('text').addClass('textfocus');
    });
    $('input[type="text"]').blur(function() {
        $(this).parent().removeClass('textfocus').addClass('text');
    });
    // End form textfield reset
    
    // Quick search
    $('#qsearch').click(function() {
		$('#quicksearch').slideToggle();
		var txt = $('a.searchiconactive').is(':visible') ? 'Zoeken' : 'Sluiten';
		if (!($(this).find('a').hasClass('searchiconprogram'))) {
		    $(this).find('a.searchicon').toggleClass('searchiconactive');
            $(this).find('a.searchicon').text(txt);
		}
    });
    // End quick search
    
    // Footer
    var openitem = 0;
    $('.addthis').click(function() {
        openitem = $(this).find('a').attr('id');
        // Add ajax to add item to my ddw list and refresh inner html of MyDdwContent with returned html (with all content)
        $('#myddwfoot').find('#MyDdwContent').show('fast', function() {
            $(this).find('img').css('float:left');
            var tmp = openitem;
            setTimeout(function() {closeMyddwFoot(tmp)}, 1500);
        });
        function closeMyddwFoot(tmp) {
            if (tmp == openitem) {
                $('#MyDdwContent').hide('slow');
            }
        }
    });

    $('#toggleMyDdw, #MyDdwContent').hover(function() {
        openitem = 'mouseover'; // prevent myddwcontent from closing on mouseenter after using addthis
        $(this).parent().find('#MyDdwContent').stop('true','true').show('fast');
    }, function() {
        $(this).parent('#myddwfoot').mouseleave(function() {
            setTimeout(function() {
                $('#MyDdwContent').hide('fast');
            }, 100);
        });
    });    
    // End footer
    
    // Fullsearch
    $('form#fullsearch div.cattitle').click(function() {
        $(this)
            .toggleClass('cattitleclosed')
            .parent().find('div.catoptions').slideToggle()
        ; 
    });
    // End fullsearch
	
	// Programoverview
	$('.program_container').hide();
	$('.day_trigger').click(function(){
		if( $(this).next().is(':hidden') ) {
			// $('.day_trigger').removeClass('day_active').next().slideUp();
			$(this).toggleClass('day_active').next().slideDown();
		} else {
			$(this).removeClass('day_active').next().slideUp();
		}
	return false;
    // End programoverview
});

});
