$(document).ready(function(){

    // clear all input and textares on load
    $('#search input').val('');
    $('textarea').val('');

    // focus first text element in #content
    $('.registration #content :input[type=text]:first').focus();


    // search clear
    $('#search input').focus(function(){ $(this).css({'background-image':'none'}); });
    $('#search input').blur(function(){
        if($('#search input').val() == '' ){
            $(this).css({'background-image':'url("/media/images/elements/search-txt.png")'});
        }
    });


    // grid
    $('.grid-item .detail').hide();
    $('.grid-item').hover(function(){
        $(this).find('.detail').slideDown('fast');
    },
    function(){
        $(this).find('.detail').slideUp('fast');
    }
    );


    // modify piece widget
    $('.modify-icon').hover(
    function(){
        $(this).find('span').show();
    },
    function(){
        $(this).find('span').hide();
    }
    );


    // comments
    $('.leave-comment textarea').focus(function(){
        $(this).css({'background-image':'none'});
        $(this).parents().find('#id_submit').show();
    });
    $('.leave-comment textarea').blur(function(){
        if($(this).val() == '' ){
            $(this).css({'background-image':'url("/media/images/elements/leavecomment-txt.png")'});
            $(this).parents().find('#id_submit').hide();
        }
    });


    // rating
    $('.rating li a').click(function() {
        $(this).parent().parent().parent().find('div span').css({"background": "#E21F26"});

        // handle rating
        var _this = this;
        // for grid
        $(this).parents('.grid-item').append('<div class="loading_indicator"></div>');
        // for detail page
        $(this).parents('.rating-box').append('<div class="loading_indicator"></div>');

        $.getJSON($(this).attr('href'), function(data) {
            if (data.not_authenticated) {
                document.location = data.location;
            } else {
                if ($('#piece_score').length) {
                    // for detail page
                    $('#rating_percentage').animate({'width': 320 * (data.rating / 5)});
                    $('#piece_total_votes').text(data.votes);
                    $('#piece_score').text(data.score.toFixed(2));
                    $('#user_piece_score').text('Your rating: ' + data.rating);
                } else {
                    // for grid
                    $(_this).parents('div:first').find('div span').animate({'width': 62 * (data.rating / 5)});
                }
                // ga vote tracking
                if (!artocrats.user_is_staff) {
                    try {
                    pageTracker._trackEvent("Pieces", "Vote");
                    } catch(e) {
                    }
                    // , data.id, data.rating);
                }
            }
            $(_this).parents('.grid-item').find('.loading_indicator').remove();
            $(_this).parents('.rating-box').find('.loading_indicator').remove();
        });

        return false;

    });


    // feedback
    $('#feedback').focus(function(){
        if ($(this).attr('focused') == undefined) {
            $(this).val('');
            $(this).css({'color': '#333'});
            $(this).attr('focused', 1);
        }
        $(this).parent().parent().parent().animate({
            height: "150px"
        }, 200 );
        $(this).animate({
            height: "100px"
        }, 200 );
        $('#feedbackform button').slideDown("fast");
    });

    $('#feedbackform').ajaxForm({beforeSubmit: function() {
        return $('#feedback').val() != '' && $('#feedback').attr('focused') != undefined;
    },
    success: function() {
        $('#feedback').val('');
        $('#feedbackform p').remove();
        $('#feedback').parent().parent().find('button').after("<p>Thanks!</p>");
        return false;
    }});


    // piece upload form hiding
    $('#form-box .submit-form').hide();
    $('#form-box .submit-form:first').show();
    $('#add-another').click(function(){
        $('.submit-form:hidden:first').show()
        if($('.submit-form:hidden').length == 0){
            $(this).hide();
        }
        return false;
    });


});

