$(function() {
    $("a[rel^='prettyPhoto']").prettyPhoto({
        allowresize: false,
        callback: function(){
            window.location = window.location;
        }, 
    });

    $("#bied_button a").prettyPhoto({
        allowresize: false,
        /* Called when prettyPhoto is closed */
        callback: function(){
            window.location = window.location;
        }, 
    });

    $(".aanmelden-nieuwsbrief-button").click(function() {
        $("#aanmelden-nieuwsbrief-popup").slideToggle('slow')
    });
    
    $('.thumbnail img').mouseover(function() {
        var src = $(this).attr('src');
        src = src.replace('164x90', '268x327');
        $('.afbeelding img').attr('src', src);
    });
    
    // Printen
    $(".print").click(function() {
        print();
    });
    
    // Omzetten naar $('#comment-text, #keyword, #text').inputblur();
    $(".email").click(function() {
        if ($(this).val() === 'Vul hier een e-mailadres in ...') {
            $(this).val('');
        }
    });
    
    // Omzetten naar $('#comment-text, #keyword, #text').inputblur();
    $(".zoeken-input-text").click(function() {
        if ($(this).val() === 'Zoek op trefwoord ...') {
            $(this).val('');
        }
    });
    
    if ($(".fotoslider").length > 0) {
        var slidecurrent = 1;
        var countfotos = $(".fotoslider").attr("rel");
        $(".foto:first").removeClass('foto').addClass('fotoshow');
        $(".foto").removeClass("foto").addClass('fotohide');
        $(".fotohide").hide();
        
        
        if($(".back").click(function(){
            slidecurrent--;
             $(".fotoshow").removeClass("fotoshow").addClass('fotohide').prev().removeClass("fotohide").addClass('fotoshow');
             $(".fotohide").hide();
             $(".fotoshow").show();
            checkbuttons();
        }));
        if($(".next").click(function(){
            slidecurrent++;
            $(".fotoshow").removeClass("fotoshow").addClass('fotohide').next().removeClass("fotohide").addClass('fotoshow');
            $(".fotohide").hide();
            $(".fotoshow").show();
            checkbuttons();
        }));
        
        function checkbuttons(){
            if(slidecurrent == 1){
                $(".back").css("visibility","hidden");
            }
            else{
                $(".back").css("visibility","visible");
            }
            if(slidecurrent == countfotos){
                $(".next").css("visibility","hidden");
            }  
            else{
                $(".next").css("visibility","visible");
            }
        };
        checkbuttons();
    };
    
    $('.favorieten-actie').click(function() {
        var $this = $(this);
        var object = $this.attr('rel');

        $.get('/mijn-catalogus/favorieten/' + object, function(data) {
            $this.text(data);
        });
    });
    
    $('#j-veiling-container').click(function(event) {
        var $targ = $(event.target);

        if ($targ.is(".favorieten-actie")) {
            var objectID = parseInt($targ.attr('rel'));
            
            $.get('/mijn-catalogus/favorieten/' + objectID, function(data) {
                $targ.text(data);
            });
        }
    });
    
    $('.mijn-catalogus-favoriet-verwijderen').click(function() {
        var $this = $(this);
        var objectID = parseInt($this.attr('rel'));

        $.get('/mijn-catalogus/favorieten/' + objectID);

        $this.parent().parent().parent().parent().parent().parent().parent().remove();
    });
});

;jQuery.fn.inputblur = function(){
    this.each(function(){
        var startText;
        $(this).focus(function(){
            if (typeof(startText) == 'undefined'){
                startText = $(this).val();
            }
            if ($(this).val() == startText) {
                $(this).val('');
            }
        }).blur(function(){
            if ($(this).val() == '') {
                $(this).val(startText);
            }
        });
    });
};