/**
 * Indicates whether to submit poll vote or not.
 *
 * @var boolean
 */
var resultsOnly = true;
/**
 * Array of width of bars representing number of votes for each option.
 *
 * @var array
 */
var optionBars = new Array();
/**
 * The width of the graph.  Used to calculate percentage of width.
 *
 * @var int
 */
var graphWidth = 469;

/**
 * Animates the bars.
 */
function animateBars() {
  var bars = $$("div#graph div.bar");
  var barCount = 0;
  bars.each(function(element) {
    var bar = new Fx.Style(element, 'width', {
      duration: 1000,
      transition: Fx.Transitions.Sine.easeOut
    });
    bar.start(optionBars[barCount]);
    barCount++;
  });
}

/**
 * Opens the Poll modal box.
 */
function openPoll() {
  var vote;
  for (i = 0; i < document.pollForm.pollOption.length; i++) {
    if (document.pollForm.pollOption[i].checked) {
      vote = document.pollForm.pollOption[i].value;
    }
  }

  var url = "vote.php";
  var caption;
  var width = 500;
  var height = 300;

  if (resultsOnly) {
    url += "?action=results";
    caption = "Viewing results";
  } else if (vote > 0) {
    url += "?action=vote&option=" + vote;
    caption = "Vote submitted successfully!";
  } else {
    alert("Invalid option, please try again");
    return;
  }
  // IE likes caching things
  url += "&r=" + Math.floor(Math.random() * 100);

  openMoodalbox(url, caption, width, height);
}
function openTellFriend() {
  var url = "tell-friend.php";
  var caption = "Tell a friend submission";
  var width = 500;
  var height = 200;

  var theirName = $("theirName").value;
  var theirEmail = $("theirEmail").value;
  var yourName = $("yourName").value;
  var yourEmail = $("yourEmail").value;
  var message = $("message").value;
  var secret = $("secret").value;
  var sessionID = $("sessionID").value;

  url += "?tn=" + escape(theirName)
    + "&te=" + escape(theirEmail) 
    + "&yn=" + escape(yourName) 
    + "&ye=" + escape(yourEmail) 
    + "&m=" + escape(message)
    + "&S=" + escape(sessionID)
    + "&s=" + escape(secret);

  openMoodalbox(url, caption, width, height);
}
/**
 * Opens the MoodalBox.
 */
function openMoodalbox(url, caption, width, height) {
  try {
    MOOdalBox.options.evalScripts = true;
    MOOdalBox.open( // case matters
      url, // the link URL
      caption, // the caption (link's title) - can be blank
      "" + width + " " + height // width and height of the box - can be left blank
    );
  } catch (e) {
    alert(e);
  }
}

window.addEvent("domready", function() {
  try {
    var tellFriend = new Fx.Slide("tellFriendForm");
    tellFriend.hide();
    $("tellFriendLink").addEvent("click", function() {
      tellFriend.toggle();
    });
  } catch (e) {}

  try {
    var remoteForm = new Fx.Slide("remoteFormContent");
    remoteForm.hide();
    $("remoteFormLink").addEvent("click", function() {
      remoteForm.toggle();
    });
  } catch (e) {}

  try {
    var searchForm = new Fx.Slide("pageSearchForm");
    searchForm.hide();
    $("pageSearchLink").addEvent("click", function() {
      searchForm.toggle();
    });
  } catch (e) {}
});
