
/* - ++resource++freitag.poll.javascripts/poll.js - */
// click outside plugin
(function(jQuery) {
   jQuery.fn.clickoutside = function(callback) {
      var outside = 1;
      self = jq(this);
      self.cb = callback;
      this.click(function() {
         outside = 0;
      });
      jq(document).click(function() {
         outside && self.cb();
         outside = 1;
      });
      return jq(this);
   }
})(jQuery);

function hide_active_elements() {
    active_elements = jq('div.book-info-active');
    active_elements.removeClass('book-info-active');
    active_elements.fadeOut('slow');
}

function unselect_elements() {
    active_elements = jq('div.book-selected');
    active_elements.removeClass('book-selected');
    active_elements.css('background-color', '#fff');
}

jq(document).ready(function() {
    // disable js functionality for msie
    if(jq.browser.msie) {
        return;
    }

    // hide and show
    jq('div#poll-starttext-js-not-enabled').hide('fast');
    jq('form#freitag-poll div.book-vote').hide('fast');
    jq('form#freitag-poll div.book-container').css('height', '300px');
    jq('form#freitag-poll div.book-title-no-js').hide('fast');
    jq('form#freitag-poll div.book-title-with-js').show('fast');
    jq('form#freitag-poll img.click-me-to-get-infos').show('fast');
    jq('form#freitag-poll div.vote-button-anker-link').hide('fast');
    jq('form#freitag-poll input#submit-poll').hide('fast');
    jq('div#poll-starttext-js-enabled').show('fast');
    jq('form#freitag-poll input#js-enabled').value = 1;

    images = jq('form#freitag-poll div.book-image img.click-me-to-get-infos');
    images.clickoutside(hide_active_elements);
    images.bind('click', function (event) {
        hide_active_elements(event);
        book_info_id = this.id.replace('book-image', 'book-info');
        book_info = jq('form#freitag-poll div#' + book_info_id);
        book_info.addClass('book-info-active');
        book_info.css('top', event.pageY - 140);
        book_info.css('left', event.pageX - 60);
        book_info.fadeIn('slow');
    });

    vote_buttons = jq('button.book-vote-button');
    vote_buttons.click(function(event) {
        event.preventDefault();
        unselect_elements();

        parts = this.id.split('+++++');
        category_id = parts[0];
        book_id = parts[1];
        radio_id = "radio-" + category_id + "-" + book_id;
        radio = jq("input#" + radio_id);
        radio.attr('checked', 'checked');

        // no selection - direct submit
        submit_button = jq("form#freitag-poll input#submit-poll");
        submit_button.click()
    });

});


