Contests = {};
/*(function() {
  var undefined, select_popup, curent_select_id, send_link, cancel_link, navigator;

  var bind_select = function(contest_id) 
  {
    select_popup.win.find('a#-gjs-link').unbind('click').bind('click', function() {
      var li = jQuery(this).parent('li');
      var span = jQuery(this).find('span').eq(0);
      var id = li.attr('id');
      if(!id)
        return;
      if(id == curent_select_id)
      {
        curent_select_id = undefined;
        span.removeClass('border');
        send_link.hide();
      }
      else
      {
        curent_select_id = id;
        select_popup.win.find('.border').removeClass('border');
        span.addClass('border');
        send_link.show();
      }
    });
  };

  var bind_send = function(contest_id, responce)
  {
    send_link = select_popup.win.find('#-gjs-send').eq(0);
    cancel_link = select_popup.win.find('#-gjs-cancel').eq(0);
    curent_select_id = undefined;
    
    delete navigator;
    navigator = new navigationSlider(select_popup.win.find('#-gjs-slider'), 691, 'member_contest/ajax_select_slider/' + contest_id, true);
    navigator.forward_link.bind('click', function() {navigator.right(responce.last_id, function() {bind_select();});});
    navigator.rearward_link.bind('click', function() {navigator.left(0);});

    send_link.hide();
        
    send_link.bind('click', function() {
      if(!curent_select_id) 
        return;
      jQuery.ajax(GameVars.ajax({
        async: true, 
        url: 'member_contest/ajax_send_participant',
        data: {id: contest_id, participant_id: curent_select_id},
        success: function(data)
        {
          if(data.errors)
            return GameModalWindow.message(data.errors);
          select_popup.close();

          var w = GameModalWindow.simpleWin();
          w.buildOverlay(); w.buildWindow(); w.win.attr('id', 'photo_sent'); w.addCloseButon(); w.show();

          if(data.view)
            jQuery('#-gjs-current-my-participant-' + contest_id).show().html(data.view);
          jQuery('#-gjs-click-participate-' + contest_id).hide();
        }
      }));
    });

    cancel_link.bind('click', function() {
      select_popup.close();   
    });
  }

  Contests.clickParticipate = function(contest_id) {
    if(!select_popup || select_popup.is_removed)
    {
      select_popup = GameModalWindow.simpleWin();
      select_popup.buildOverlay();
      select_popup.buildWindow();
    };
    select_popup.hide();
    jQuery.ajax(GameVars.ajax({
      async: true, 
      url: 'member_contest/ajax_select_participant',
      data: {id: contest_id},
      success: function(data)
      {
        if(data.errors) 
          return GameModalWindow.message(data.errors);
        select_popup.win.html(data.view);
        bind_select(contest_id);
        bind_send(contest_id, data);
        select_popup.show();
      }
    }))
  };
})();       */
(function() {

  var send_callback = function(contest_id, select_id, select_form) {
    jQuery.ajax(GameVars.ajax({
      async: true, 
      url: 'member_contest/ajax_send_participant',
      data: {id: contest_id, participant_id: select_id},
      success: function(data)
      {
        if(data.errors)
          return GameModalWindow.message(data.errors);
        select_form.select_popup.close();
        delete select_form;

        var w = GameModalWindow.simpleWin();
        w.buildOverlay(); w.buildWindow(); w.win.attr('id', 'photo_sent'); w.addCloseButon(); w.show();

        if(data.view)
          jQuery('#-gjs-current-my-participant-' + contest_id).show().html(data.view);
        jQuery('#-gjs-click-participate-' + contest_id).hide();
      }
    }));
  };

  Contests.clickParticipate = function(contest_id) 
  {
    jQuery.ajax(GameVars.ajax({
      async: true, 
      url: 'member_contest/ajax_select_participant',
      data: {id: contest_id},
      success: function(data)
      {
        if(data.errors) 
          return GameModalWindow.message(data.errors);
        var select_popup = new Photo.selectForm({
          html: data.view,
          navigator_url: 'member_contest/ajax_select_slider/' + contest_id,
          last_id: data.last_id,
          send_callback: function(id, select_popup) {
            send_callback(contest_id, id, select_popup);
          }
        });
      }
    }))
  };
})();

Contests.clickCancelParticipate = function(id, participant_id) 
{
  GameModalWindow.confirmCallback('Снять работу с конкурса?', function() {
    jQuery.ajax(GameVars.ajax({
      async: true, 
      url: 'member_contest/ajax_cancel_participate',
      data: {id: id, participant_id: participant_id},
      success: function(data)
      {
        if(data.errors)
          return GameModalWindow.message(data.errors);
        jQuery('#-gjs-current-my-participant-' + id).hide();
        jQuery('#-gjs-click-participate-' + id).show();
      }
    }));  
  });
};

(function() {
  var link_vote, link_cancel_vote;
  
  var find_links = function(vote)
  {
    link_vote = link_vote || jQuery('#-gjs-vote-contest').eq(0);
    link_cancel_vote = link_cancel_vote || jQuery('#-gjs-cancel-vote-contest').eq(0);
    if(vote) {
      link_vote.hide(); 
      link_cancel_vote.show();
    } else {
      link_vote.show(); 
      link_cancel_vote.hide(); 
    }
  }

  Contests.vote = function(preffix, id) {
    base_estimate(preffix + '_contest_opinion', id, 1, function(message) {find_links(1);}, true);  
  };
  
  Contests.cancelVote = function(contest_id, estimated_id) {
    jQuery.ajax(GameVars.ajax({
      async: true, 
      url: 'member_contest/ajax_cancel_vote',
      data: {contest_id: contest_id, estimated_id: estimated_id},
      success: function(data)
      {
        if(data.errors)
        {
          GameModalWindow.message(data.errors); 
          return;
        }
        find_links(0);
      }
    }));
  };
})();

(function() {
  var sliders = {};
  Contests.getSlider = function(contest_id, el, url) 
  {
    if(!sliders[contest_id])
      sliders[contest_id] = new navigationSlider(el, 70, url + contest_id, false, true);
    return sliders[contest_id];
  };
})();
