function switchToTab(tabName)
{
  $(".Chroma2 .TabTitle").removeClass('active');
  $(".Chroma2 .TabTitle.t_"+tabName).addClass('active');
  
  $(".Chroma2 .TabContainer").removeClass('active');
  $(".Chroma2 .TabContainer.t_"+tabName).addClass('active');
}

function nodePoll_submitVote(nodeId, urlBase, returnPath)
{
  var choiceId = $("input[name='choice']:checked").val();
  if (choiceId == null) {
    alert('Выберите вариант ответа');
    return;
  }

  $("#button_vote").attr('disabled', 'disabled');
  $("#span_votingInProgress").css('display', '');

  var voteParameters = '&nodeid=' + nodeId + '&choice=' + choiceId
                       + '&returnPath=' + encodeURIComponent(returnPath);

  var sendVoteFrame = document.createElement('iframe');
  sendVoteFrame.id = 'iframe_voting';
  sendVoteFrame.name = 'iframe_voting';
  $(sendVoteFrame).css('position', 'absolute').css('top', '-1000px').css('height', '0');
  document.body.appendChild(sendVoteFrame);

  $(sendVoteFrame).load(function() {
    var iframeDocument;
    try {
      iframeDocument = sendVoteFrame.contentWindow.document;
      iframeDocument.documentElement; // This ensures access denied exception in Opera.
    } catch(ex) {
      iframeDocument = null;
    }
    if (!iframeDocument
        || !/nodePollSendFrame(?:)Signature/.test(iframeDocument.documentElement.innerHTML))
    {
      $("#span_votingInProgress").css('display', 'none');
      $("#span_votingError").css('display', '');
    }
  });
  sendVoteFrame.src = urlBase + 'node?action=sendPollVoteFrame' + voteParameters;
}

var contentRating_ratingStateMap = {};

function contentRating_applyControlsVisibility(contentType, contentId)
{
  var fullContentId = 't' + contentType + '_i' + contentId;
  var ratingState = contentRating_ratingStateMap[fullContentId];

  var showSendControls = (ratingState == 'enabled');
  var showSendInProgress = (ratingState == 'sending' || ratingState == 'sending2');
  var showSendCompleted = (ratingState == 'sent');

  $(".ContentRatingContainer .sendControl."+fullContentId).css('display', showSendControls ? '' : 'none');
  $(".ContentRatingContainer .sendControlInactive."+fullContentId).css('display', !showSendControls ? '' : 'none');
  $(".ContentRatingContainer .sendInProgressElement."+fullContentId).css('display', showSendInProgress ? '' : 'none');
  $(".ContentRatingContainer .ratingElement."+fullContentId).css('display', !showSendInProgress ? '' : 'none');
}

function contentRating_sendScore(contentType, contentId, score, urlBase)
{
  var fullContentId = 't' + contentType + '_i' + contentId;
  if (contentRating_ratingStateMap[fullContentId] != 'enabled') {
    alert('Посылка оценки невозможна');
    return;
  }

  $("#iframe_contentRating_"+fullContentId).remove();

  var sendScoreParameters = '&contentType=' + contentType + '&contentId=' + contentId
                            + '&score=' + score + '&skin=chroma2' + '&cookie=' + (new Date().getTime());

  var sendScoreFrame = document.createElement('iframe');
  sendScoreFrame.id = 'iframe_contentRating_' + fullContentId;
  sendScoreFrame.name = 'iframe_contentRating_' + fullContentId;
  $(sendScoreFrame).css('position', 'absolute').css('top', '-1000px').css('height', '0');

  document.body.appendChild(sendScoreFrame);
  // In IE6, it doesn't reliably work unless we set 'src' after adding
  // the element to the DOM.

  contentRating_ratingStateMap[fullContentId] = 'sending';
  contentRating_applyControlsVisibility(contentType, contentId);

  $(sendScoreFrame).load(function() {
    if (contentRating_ratingStateMap[fullContentId] != 'sending') return;
    
    var responseContent;
    try {
      var iframeDocument = sendScoreFrame.contentWindow.document;
      responseContent = iframeDocument.documentElement.textContent
                        || iframeDocument.documentElement.innerText;
    } catch(ex) {
      responseContent = '';
    }

    if (!/contentRatingSendFrame(?:)Signature/.test(responseContent)) {
      alert('Не удалось отправить оценку');
      contentRating_ratingStateMap[fullContentId] = 'error';
      contentRating_applyControlsVisibility(contentType, contentId);
      return;
    }

    // Wait for "phase two" initiation from the frame code.
    // It can happen even *before* this handler is called, though.
  });
  sendScoreFrame.src = urlBase + 'contentRating?action=sendScoreFrame' + sendScoreParameters;
}

function contentRating_notifyPhaseTwo(contentType, contentId)
{
  var fullContentId = 't' + contentType + '_i' + contentId;
  var sendScoreFrame = document.getElementById('iframe_contentRating_' + fullContentId);

  contentRating_ratingStateMap[fullContentId] = 'sending2';

  $(sendScoreFrame).load(function() {
    var responseContent;
    try {
      var iframeDocument = sendScoreFrame.contentWindow.document;
      responseContent = iframeDocument.documentElement.textContent
                        || iframeDocument.documentElement.innerText;
    } catch(ex) {
      responseContent = '';
    }

    if (/^contentRatingSend(?:)Signature:error:(.+)$/.test(responseContent)) {
      var error = RegExp.$1;
      alert('Ошибка отправки оценки: ' + error);
      contentRating_ratingStateMap[fullContentId] = 'error';
      contentRating_applyControlsVisibility(contentType, contentId);
      return;
    }

    if (/^contentRatingSend(?:)Signature:success:(.+)$/.test(responseContent)) {
      var newRatingHtml = RegExp.$1;
      $(".ContentRatingContainer .ratingElement."+fullContentId).html(newRatingHtml);

      contentRating_ratingStateMap[fullContentId] = 'sent';
      contentRating_applyControlsVisibility(contentType, contentId);
    } else {
      alert('Не удалось отправить оценку');
      contentRating_ratingStateMap[fullContentId] = 'error';
      contentRating_applyControlsVisibility(contentType, contentId);
    }

    // We are done with it.
  });
}

