// Define a generic log() function for Fireburg's console.log()
if( (typeof console != "undefined") && (typeof console.log != "undefined") ) {
    var log = console.log;
} else {
    // No Firebug available, define an anonymous function
    var log = function(){};
}

$(document).ready(function() {
    $(".jsonly").show();
    $(".jshide").hide();

    $(".jstoggle").click(function() {
        var id = $(this).attr('toggle');
        $("#" + id).slideDown();
        $(this).parent().fadeOut();
    });

    $("#thumbs a").hover(
        function() {
            $(this).find(".meta").show();
        },
        function() {
            $(this).find(".meta").hide();
        }
    );

    $("#thumbnav a").click(function() {
        if($(this).hasClass('current')) {
            return false;
        }

        var el = $(this).attr('href');
        el = el.slice(1);

        $("#thumbnav a").removeClass('current');

        $("#thumbs > div").hide();
        $("#" + el).show();

        $(this).addClass('current');

        return false;

    });

    $('a[rel="scrollpage"]').click(function() {
        var el = $(this).attr('href');
        el = el.slice(1);
        var pos = $("a[name='" + el + "']").offset();
        $("html").animate({
            scrollTop: pos.top + 'px'
        }, { duration: 300 });
        return false;
    });

    $("#author").click(function() {
        $("#commentform_extra").slideDown();
    });

    // When clicking on the input field, delete that value
    $(".search .input").click(function() {
        if($(this).val() == "Doorzoek de site...") {
            $(this).attr('value', '');
        } else {
            return true;
        }
    });

    $(".rate").click(function() {
        try {
            if ($(this).hasClass('rated')) return false;

            // Hide the other button temporarily
            if ($(this).attr('id') == 'btnRatePositive') {
                $("#btnRateNegative").hide();
            } else {
                $("#btnRatePositive").hide();
            }

            $(this).css('font-size', '3.6em');
            $(this).text('EEN MOMENT AUB');

            // Do an AJAX call
            $.getJSON(
                HAY_THEME_URL + '/vote.php',
                { "postid" : HAY_POST_ID, "rating" : $(this).attr('data-rating')},

                function(data) {
                    // write to the buttons
                    if(data.error) {
                        // error!
                        if (data.error == "IP_HAS_VOTED") {
                            alert('U heeft eerder al gestemd op deze bijdrage!');
                        } else {
                            alert('Er is een fout opgetreden bij het stemmen. Probeer het later nog eens');
                        }
                        return false;
                    }


                    var html;
                    html  = '<p class="col g6">' + data.positive +' mensen vonden dit mooi</p>';
                    html += '<p class="col g6">' + data.negative +' mensen vonden dit mooi</p>';
                    $("#rating").html(html);
                }
            );
            return false;
        } catch(e) {
            return false;
        }
    });
});