function initInputDefaultText(input, defaultText) {
	input.focus(function() {
		if($.trim($(this).attr('value')) == defaultText) {
			$(this).attr('value', '');
		}
	});

	input.blur(function() {
		if($.trim($(this).attr('value')).length == 0) {
			$(this).attr('value', defaultText);
		}
	});

	if($.trim(input.attr('value')).length == 0) {
		input.attr('value', defaultText);
	}

	input.parents('form').submit(function() {
		if(input.attr('value') == defaultText) {
			input.attr('value', '');
		}
	});
}
function initInputDefaultPassword(input, styleClass) {
	input.focus(function() {
		if($.trim($(this).attr('value')).length == 0) {
            $(this).removeClass(styleClass);
		}
	});

	input.blur(function() {
		if($.trim($(this).attr('value')).length == 0) {
            $(this).addClass(styleClass);

		}
	});

	if($.trim(input.attr('value')).length == 0) {
        $(this).addClass(styleClass);
	}
}

function initOpenLoginMenu(element, openElement) {
    element.mouseover(function() {
        openElement.toggle(true);
    })
    element.mouseleave(function() {
        openElement.toggle(false);
    })
}


$(function() {
	initInputDefaultText($('.header .search .query'), 'Vul zoekterm(en) in');
	initInputDefaultText($('.altijdInDeBuurt .postcode .query'), 'postcode');
	initInputDefaultText($('.wijkactie.postcode .query'), 'postcode');

    initInputDefaultText($('.xUserID'), 'Gebruikersnaam');
    initInputDefaultPassword($('.xPassword'), 'xPasswordBackground');

    initOpenLoginMenu($('.inlogbutton'), $('.loginform'));
    initOpenLoginMenu($('.loginform'), $('.loginform'));

	// Form buttons
	$('#experienceForm .button').click(function() {
		$('#experienceForm').submit();
		return false;
	});	
	$('#searchForm .button').click(function() {
		$('#searchForm').submit();
		return false;
	});	
	$('#vacancyForm .button').click(function() {
		$('#vacancyForm').submit();
		return false;
	});	
	
	// Make the complete product teaser clickable
	$('.product').click(function() {
		window.location.pathname = $('a.more', $(this)).attr('href');
	});



    var loadMailAFriendForm = function(event, ui) {
        $(".overlay-forward").load('/stuurpaginadoor/?paginaurl=' + encodeURI(window.location.href) + "&paginatitel=" + encodeURI(document.title));
    }

    var modal = $(".overlay-forward");
    modal.dialog({  modal:true,
                    width:478,
                    maxHeight: 400,
                    minHeight: 18,
                    autoOpen: false,
                    closeText: "sluiten",
                    close: loadMailAFriendForm});

    $('a.email').click(function(e) {
        bindStuurDoorFormToAjaxForm();
        modal.dialog("open");
        e.preventDefault();
    });

    loadMailAFriendForm();

    var actual = function(){
		var lis = $('.actual li');
		lis.click(function(){
			lis.each(function(){
				lis.removeClass('active');
				lis.find('ul').css('height', 0);
			})
			this.className += ' active';
			$(this).find('ul').css('height','auto');
			$('.actual .active').parent().parent().height(0);

			void($('.actual .active').parent().parent().css('height',$('.actual .active').parent().height() + $('.actual .active').find('ul').height()).height());
		}).find('a').not('li ul a').click(function(e){
			e.preventDefault();
		});
		void($('.actual .active').parent().parent().css('height',$('.actual .active').parent().height() + $('.actual .active').find('ul').height()));
	}();

});

function bindStuurDoorFormToAjaxForm() {
    $('#stuurdoorform').ajaxForm(function(responseText) {
        $(".overlay-forward").html(responseText);
        bindStuurDoorFormToAjaxForm();
    });
}
