var cl_initPeliVote = function(rating_labels, current_vote) {
    
    var j_rating_label = $('#rating_label');
    var j_stars = $('.estrella');
    
    var show_stars = function(num_stars) {
        j_stars.each(function(i) {
            var status = ++i <= num_stars  ? 'full' : 'empty';
            $(this).attr('class', 'estrella estrella_'+status);
            j_rating_label.text(rating_labels[num_stars]);
        });
    }
    
    var show_message = function(label) {
        $('.mensaje_votar').children().css('display', 'none');
        $('#vote_msg_' + label).css('display', '');
    }
    
    var req = null;

    var vote = function(url, num_stars) {
        req = $.ajax({
            url: url,
            dataType: 'json',
            success: function(data) {
                if ('redirect' == data[0]) {
                    document.location = data[1];
                    return;
                }
                show_stars(num_stars);
                $('#user_rating').text(num_stars);
                show_message('voted');
            },
            error: function(req, textStatus, errorThrown) {
                show_message('ajaxerror');
            },
            complete: function() {
                window.setTimeout(function() { req = null; }, 500);
            }
        });
    }
    
    j_stars.click(function(e) {
        e.target.blur();
        if (req) { return false; }
        vote(e.target.href, this.id.substr(this.id.length-1)*1);
        return false;
    });
}

var cl_registerFormInit = function(url_validate, form) {
    
    // register validation hinting for form
    (function() {    
        var showError = function(field, error) {
            var p = $(field.parentNode);
            if (!p.children('span').html()) p.children('span').css('display', 'none');
            p.addClass('fila_form_error');
            p.children('span').html(error);
            p.children('span').fadeIn();
        }
        var hideError = function(field) {
            $(field.parentNode).children('span').html('').fadeOut();
            $(field.parentNode).removeClass('fila_form_error');
        }
        initAjaxFormValidator(form, url_validate, showError, hideError);
    })();
}

var cl_initEncuesta = function(form) {
    $(form).submit(function(e) {
        var option_id = $(form.option_id).filter(':checked').val();
        if (!option_id) {
            var err = $('.encuesta-error');
            err.fadeOut(function() { err.fadeIn(); });
            return false;
        }
        return true;
    });
}

cl_initPeliculaCommentForm = function(form) { /* nothing here for now */ }


// register a load event to make all offsite links / pdf links open in new windows
$(function() {
    var domain = document.domain.toLowerCase().replace('www.', '');
    $('a').each(function() {
        var href = this.href.toLowerCase();
        if ((0 == href.indexOf('http') && -1 == href.indexOf(domain) && -1 == href.indexOf('www.cinelatino.com')) ||
            (href.substr(-4) == '.pdf')) {
            this.target = '_blank';
        }
    });
});

