var poll_skin = "default";
var poll_seqid;
function submitPoll() {
	var checkValue = 0;
	for (var i = 0; i < document.ajax_poll_frm.ajax_radio_poll.length; i ++) {
		if (document.ajax_poll_frm.ajax_radio_poll[i].checked) {
			checkValue = document.ajax_poll_frm.ajax_radio_poll[i].value;
			break;
		}
	}

	ajaxHttpRequest("http://" + getServiceDomain("event.ijji.com") + "/poll.nhn?seqid=" + poll_seqid + "&answer=" + checkValue, "drawPoll");
}

function drawPoll(json) {
	if (json.showInput) {
		drawInputPoll(json);
	} else {
		drawResultPoll(json);
	}
}

function getPoll(seqid, skin) {
	poll_skin = skin;
	poll_seqid = seqid;
	ajaxHttpRequest("http://" + getServiceDomain("event.ijji.com") + "/poll.nhn?seqid=" + poll_seqid, "drawPoll");
}

function draw(view) {
	document.getElementById("ajax_poll").innerHTML = view;
}

function drawResultPoll(json) {
	var pollResultView = "";
	pollResultView +='<table width="146" border="0" cellspacing="0" cellpadding="0">';
	pollResultView +='  <tr>';
	pollResultView +='    <td><img src="http://images.ijjimax.com/v2/poll/poll_topimg_' + poll_skin + '.gif" width="146" height="21"></td>';
	pollResultView +='  </tr>';
	pollResultView +='  <tr>';
	pollResultView +='    <td align="center" background="http://images.ijjimax.com/v2/poll/poll_bgimg_' + poll_skin + '.gif">';
	pollResultView +='	  <table width="134" border="0" cellspacing="0" cellpadding="0">';
	pollResultView +='        <tr>';
	pollResultView +='          <td style="padding:4 0; font-family:verdana, tahoma, sans-serif; font-size:9px; color:#646464;">' + json.poll.question + '</td>';
	pollResultView +='        </tr>';
	pollResultView +='        <tr>';
	pollResultView +='          <td height="1" background="http://images.ijjimax.com/v2/poll/poll_dot_line.gif"></td>';
	pollResultView +='        </tr>';
	pollResultView +='        <tr>';
	pollResultView +='          <td style="padding:3 0">';
	pollResultView +='		   ';
	pollResultView +='		   <table width="100%" border="0" cellspacing="0" cellpadding="0"  >';

	for (var i = 0; i < json.poll.answers.length; i++) {
		pollResultView +='			  <tr> ';
		pollResultView +='                <td style="font-family:verdana, tahoma, sans-serif; font-size:9px; color:#646464;">' + json.poll.answers[i] + ' <strong>' + json.poll.rates[i] + '%</strong></td>';
		pollResultView +='              </tr>';
		pollResultView +='			  <tr> ';
		pollResultView +='                <td style="padding:2 0">';
		pollResultView +='				  <table width="' + json.poll.rates[i] + '%" border="0" cellspacing="0" cellpadding="0">';
		pollResultView +='                    <tr>';
		pollResultView +='                      <td height="3" bgcolor="#888888"></td>';
		pollResultView +='                    </tr>';
		pollResultView +='                  </table>';
		pollResultView +='				 </td>';
		pollResultView +='           </tr>';
	}

	pollResultView +='            </table>';
	pollResultView +='			';
	pollResultView +='		</td>';
	pollResultView +='        </tr>';
	pollResultView +='      </table>';
	pollResultView +='  </td>';
	pollResultView +='  </tr>';
	pollResultView +='  <tr>';
	pollResultView +='    <td height="1" background="http://images.ijjimax.com/v2/poll/poll_topimg_' + poll_skin + '.gif"></td>';
	pollResultView +='  </tr>';
	pollResultView +='</table>';

	draw(pollResultView);
}

function drawInputPoll(json) {
	var pollInputView = "";
	pollInputView +='<form name="ajax_poll_frm">';
	pollInputView +='<table width="146" border="0" cellspacing="0" cellpadding="0">';
	pollInputView +='  <tr>';
	pollInputView +='    <td><img src="http://images.ijjimax.com/v2/poll/poll_topimg_' + poll_skin + '.gif" width="146" height="21"></td>';
	pollInputView +='  </tr>';
	pollInputView +='  <tr>';
	pollInputView +='    <td align="center" background="http://images.ijjimax.com/v2/poll/poll_bgimg_' + poll_skin + '.gif">';
	pollInputView +='	  <table width="134" border="0" cellspacing="0" cellpadding="0">';
	pollInputView +='        <tr>';
	pollInputView +='          <td style="padding:4 0; font-family:verdana, tahoma, sans-serif; font-size:9px; color:#646464;">' + json.poll.question + '</td>';
	pollInputView +='        </tr>';
	pollInputView +='        <tr>';
	pollInputView +='          <td height="1" background="http://images.ijjimax.com/v2/poll/poll_dot_line.gif"></td>';
	pollInputView +='        </tr>';
	pollInputView +='        <tr>';
	pollInputView +='          <td style="padding:3 0">';
	pollInputView +='		  ';
	pollInputView +='		  <table width="100%" border="0" cellspacing="0" cellpadding="0" >';

	for (var i = 0; i < json.poll.answers.length; i++) {
		pollInputView +='              <tr>';
		pollInputView +='                <td width="20"><input type="radio" name="ajax_radio_poll" value="' + i + '"></td>';
		pollInputView +='                <td style="font-family:verdana, tahoma, sans-serif; font-size:9px; color:#646464;"><label for="ajax_radio_poll' + i + '" > ' + json.poll.answers[i] + '</label></td>';
		pollInputView +='              </tr>';
	}
	pollInputView +='         </table>';
	pollInputView +='		';
	pollInputView +='		</td>';
	pollInputView +='        </tr>';
	pollInputView +='        <tr>';
	pollInputView +='          <td height="1" background="http://images.ijjimax.com/v2/poll/poll_dot_line.gif"></td>';
	pollInputView +='        </tr>';
	pollInputView +='        <tr>';
	pollInputView +='          <td height="30" align="center"><a href="javascript:void(submitPoll());"><img src="http://images.ijjimax.com/v2/poll/poll_bt_ok.gif" width="36" height="18" border="0"></a></td>';
	pollInputView +='        </tr>';
	pollInputView +='      </table>';
	pollInputView +='  </td>';
	pollInputView +='  </tr>';
	pollInputView +='  <tr>';
	pollInputView +='    <td height="1" background="http://images.ijjimax.com/v2/poll/poll_topimg_' + poll_skin + '.gif"></td>';
	pollInputView +='  </tr>';
	pollInputView +='</table>';
	pollInputView +='</form>';

	draw(pollInputView);
}