
function attach_string_to_url(url, string) {
    var operator = '?';
    if (url.search('\\?') > -1) operator = '&';
    if (string) {
        url = url + operator + string;
    }
    return url;
}

function load_tabbed_widget(widget, url) {
    var get_data = window.location.toString().split('?')[1];

    if (get_data) attach_string_to_url(url, get_data);

    var options = {
        'ajax': 1
    };

    $(widget).bind('reviews_loaded', function() {
        widget.find('.sorting_anchors a').each(function() {
            $(this).bind('click', function () {
                var href = $(this).attr('href').replace('?', '');
                var tmp_url = attach_string_to_url(url, href);

                widget.load(tmp_url, options, load_callback);
                return false;
            });
        });
    });

    var load_callback = function(response, status, xhr) {
        var widget = $(this);
        if (widget.find('.reviews_stats_box').length > 0 || widget.find('form').length > 0) {
            var rating = widget.find('.reviews_stats_box').clone();
            var hint = 'Write a review, speak your mind.';
            widget.find('.reviews_stats_box').remove();
            $('#' + widget.attr('id') + '_tab .label').find('.reviews_stats_box').remove();
            $('#' + widget.attr('id') + '_tab .label').append(rating);
            widget.find('form[name=add_review_entry_form]').bind('submit', submit_review_entry);
            widget.find('div[id^=add_review_entry_buttonbox_]').hide();
            widget.find('div.questionnaire_box').hide();
            widget.find('textarea[name=body]').inactivedefault(hint);
            widget.find('textarea[name=body]').attr('style', 'height: 20px');
            widget.find('textarea[name=body]').focus(function () {
                $(this).removeAttr('style');
                widget.find('div[id^=add_review_entry_buttonbox_]').show();
                widget.find('div.questionnaire_box').show();
            });
            $(widget).find('.your_opinion .review_box div').click(function () {
                var form = $(this).parents('form');
                submit_anonymous_rating(
                    form,
                    function(){
                        preselect_top_rating($('form#'+form.attr('id').replace('add_review_entry_form', 'add_simple_review_entry_form')));
                    }
                );
            });
            $('#' + widget.attr('id') + '_tab').show();
            widget.show();
            $('#' + widget.attr('id') + '_tab').parent().find('li:visible a').eq(0).click();
            preselect_top_rating($('form#'+widget.find('form').attr('id').replace('add_review_entry_form', 'add_simple_review_entry_form')));
            $(this).trigger('reviews_loaded');
        }
    };

    widget.load(url, options, load_callback);
};

function top_rating_clicked(e) {
    var form = $(this).parents('form');
    var value = $(form).find('.your_opinion .review_box input:checked').val();
    var simple_form_id = $(form).attr('id').replace('add_review_entry_', 'add_simple_review_entry_');
    $('form#'+simple_form_id).find('.your_opinion .review_box input[value='+value+']').attr('checked', 'checked');
    $('form#'+simple_form_id).find('.your_opinion .review_box input[value='+value+']').change();
    $('form#'+simple_form_id).find('span.star-rating-control div.half-star').removeClass('half-star');
    var full_form_id = $(form).attr('id').replace('add_simple_review_entry_', 'add_review_entry_');
    $('form#'+full_form_id).find('.your_opinion .review_box input[value='+value+']').attr('checked', 'checked');
    $('form#'+full_form_id).find('.your_opinion .review_box input[value='+value+']').change();
};

function preselect_top_rating(form) {
    // display top rating stars according to average value
    var f_value = parseFloat(form.parent().find('span.ratings_value').html());
    var value = Math.round(f_value);
    var input = form.find('.your_opinion .review_box input[value='+value+']');
    input.attr('checked', 'checked');
    input.change();
    input.parent().find('span.star-rating-control div').removeClass('half-star');
    if (f_value < value) {
        input.parent().find('span.star-rating-control div#'+input.attr('id')).addClass('half-star');
    }
};

