var fast_rand = {
  good_numbers: [],
  votes:        [],
  max_votes:    3,
  alerting:     false,
  
  Vote: function(voteFor, client) {
    var y = 0; 
    if (document.documentElement && document.documentElement.scrollTop) { y = document.documentElement.scrollTop; }
    else if (document.body && document.body.scrollTop) { y = document.body.scrollTop; }

    // Make sure that voteFor is a number and is a valid number.
    if (ljg.isUndef(voteFor, "number")) { return; }
    var good_number = false;
    for (var i = 0; i < this.good_numbers.length; i++) { if (this.good_numbers[i] == voteFor) { good_number = true; break; }}
    if (!good_number) { return; }
    
    // Check to see if there's already an alert in place.
    if (this.alerting) {
      this.Close_Alert();
    }

    // Determine if this item is currently in the votes.
    for (var i = 0; i < this.votes.length; i++) {
      if (this.votes[i] == voteFor) { 
        // Already in votes, so remove it and make sure that the radio button is not checked.
        this.votes.splice(i,1);
        ljg.get('rec_op_' + voteFor).checked = false;
        return;
      }
    }

    // This vote has not been selected; check to see if there have already been max votes.
    if (this.max_votes <= this.votes.length) { 
      // Inform the user that they can only vote three times
      this.Do_Alert("Attention", "You may only cast your vote for  " + this.max_votes + " recommendations.", client.x - 25, client.y + y - 35);
      ljg.get('rec_op_' + voteFor).checked = false;
      return;
    }

    // Add this vote to the array.
    this.votes.push(voteFor);
    ljg.get('rec_op_' + voteFor).checked = true;
  },
  
  Do_Alert: function(alertTitle, alertText, x, y) {
    if (this.alerting) { }
    this.alerting = true;
    this.msgbox = ljg.make("DIV", { css: "alert_window" });
    this.msgpad = ljg.make("DIV", { css: "alert_window_pad" });
    this.msgcloser = ljg.make("DIV");
    this.msgcloser.innerHTML = "<div style='background-color: #8895AB; height: 22px; overflow: hidden; color: #fff;'><div style='float: left; padding-left: 10px; font-weight: bold;'>" + alertTitle + 
                               "</div><div style='float: right; width: 18px; margin: 0px; font-weight: bold; font-size: 10px;'><div style='padding: 2px;'><div style='border: 1px solid #fff; padding: 2px; line-height: 10px; cursor: pointer;' title='Close' onClick=\"fast_rand.Close_Alert();\" onMouseOver=\"this.style.border='1px solid #ddf'; this.style.color='#ddf';\" onMouseOut=\"this.style.border='1px solid #fff'; this.style.color='#fff';\">X</div></div></div><div style='clear: both;'></div></div>";
    this.msgbox.appendChild(this.msgcloser);
    this.msgbox.appendChild(this.msgpad);
    this.msgpad.innerHTML = alertText;
    document.body.insertBefore(this.msgbox, document.body.firstChild);
    this.msgbox.style.top  = y + "px";
    this.msgbox.style.left = x + "px";
    this.msgy = window.scrollY;
  },
  
  Close_Alert: function() {
    document.body.removeChild(this.msgbox);
    this.msgbox   = null;
    this.alerting = false;
  },
  
  Init: function() {
    for (var i = 0; i < this.good_numbers.length; i++) {
      var btn = ljg.get('rec_op_' + this.good_numbers[i]);
      if (btn.checked) { this.Vote(this.good_numbers[i], { x: 100, y: 100 }); }
    }
  },

  Cast_Votes: function(override) {
    if (this.alerting) { this.Close_Alert(); }

    // Calculate alert / form placement.
    var y = 0; var w = 0; var h = 0; var x = 0;
    if (document.documentElement && document.documentElement.scrollTop) { y = document.documentElement.scrollTop; w = document.documentElement.clientWidth; h = document.documentElement.clientHeight; }
    else if (document.body && document.body.scrollTop) { y = document.body.scrollTop; w = document.body.clientWidth; h = document.body.clientHeight; }

    x = Math.floor(w/2) - 150;
    y = Math.floor(h/2) - 100 + y;

    override = ljg.fix(override, false);

    // Let the user know that they can submit more votes (or need to select at least one.)
    if (this.votes.length == 0 || (!override && this.votes.length < this.max_votes)) {
      var recText    = "recommendation" + ((this.max_votes - this.votes.length > 1) ? "s" : "");
      var choiceText = "choice" + ((this.max_votes - this.votes.length > 1) ? "" : "s");
      var voteText   = "Vote" + ((this.max_votes - this.votes.length > 1) ? "" : "s");
      var selectText = (this.votes.length == 0) 
                       ? "You must select at least one recommendation to vote for."
                       : "You may vote for " + (this.max_votes - this.votes.length) + " more " + recText + ". Would you like to continue voting or submit your current " + choiceText + "?" +
                         "<div style='padding: 10px 0px;'>" +
                         "<input type='button' value='Continue Voting' onClick=\"fast_rand.Close_Alert();\"/>&nbsp;&nbsp;&nbsp;" + 
                         "<input type='button' value='Submit " + voteText + "' onClick=\"fast_rand.Cast_Votes(true);\" /></div>"
      this.Do_Alert("Attention", selectText, x, y);
      return;
    }

    // Construct the form row data structure. Makes the HTML compilation cleaner.
    var voteFields = [];
    for (var i = 0; i < this.votes.length; i++) {
      voteFields.push("<input type='hidden' name='rr_vote_" + i + "' value='" + this.votes[i] + "' />" + ljg.get('rec_title_' + this.votes[i]).innerHTML);
    }
    voteFields = "<table cellspacing='0' cellpadding='0' border='0'><tr><th>-</th><td>" + voteFields.join('</td></tr><tr><th>-</th><td>') + "</td></tr></table>";

    var formRowStruct = [{ label: "First Name", field: "<input class='txt' type='text' name='rr_firstname' />" },
                         { label: "Last Name",  field: "<input class='txt' type='text' name='rr_lastname' />"  },
                         { label: "Email",      field: "<input class='txt' type='text' name='rr_email' />"     },
                         { label: "Votes",      field: voteFields                                  }];

    var formRows = [];
    for (var i = 0; i < formRowStruct.length; i++) {
      formRows.push("<th><div style='width: 65px;'>" + formRowStruct[i].label + "</div></th><td>" + formRowStruct[i].field + "</td>");
    }
 
    // Create form for final submission.
    var formHtml = "<form action='/goals/randrecs' method='POST'>" +
                   "<div style='text-align: left; padding-bottom: 10px;'>Please enter your contact information. This information is required to ensure poll accuracy and will not be used for marketing purposes.</div>" + 
                   "<div style='padding-left: 10px;'><table cellspacing='0' cellpadding='0' border='0'><tr>" + formRows.join('</tr><tr>') + "</tr></table></div>" +
                   "</form>";

    this.Do_Alert("Cast Your Votes", formHtml, x, y);
  },
  
  Status_Report: function(id) {
    
  }
};

