var global_voted = false;

function getCheckedValue(obj){
	for(var i=0; i<obj.length; i++){
		if(obj[i].checked){
			return obj[i].value;
		}
	}
	return false;
}

function vote(isVote) {
	if (global_voted) return;

	var pollid = document.voteForm.pollid.value;
	
//	if (!global_isLogin && isVote) {
//		document.location.href = "http://www.daum.net/Mail-bin/login.html?url="+escape(document.location);
//		return;
//	}

	if (isVote) {
		eg = getCheckedValue(document.voteForm.eg);
		if (!eg) {
			alert3("답안을 선택 해주세요.");
			return;
		}
	} else {
		eg = 0;
//		document.getElementById("alertMsg").innerHTML = "";
		window.open('/mil_c21_/poll_result?pollid=' + pollid,'poll','toolbar=no,scrollbars=no,top=50,left=50,width=526,height=442');
		return;
	}
	
	ajaxAPI.callMethod("vote", {eg:eg,pollid:pollid}, "", "/mil_c21_/poll");
	
}

ajaxListener.vote_onLoad = function(success, responseText, responseXML) {
//alert(responseText);
	var result = ajaxAPI.getValue(responseXML, "result");
	if (result == "500 NOTLOGIN") {
//		alert3("로그인 하신 후 투표해주세요.");
		document.location.href = "http://www.daum.net/Mail-bin/login.html?url="+escape(document.location);
		// global_voted = false;
	} else if (result == "500 ALREADY VOTED") {
		alert3("이미 투표하셨습니다.");
	} else {
		var pollid = document.voteForm.pollid.value;
		window.open('/mil_c21_/poll_result?pollid=' + pollid,'poll','toolbar=no,scrollbars=no,top=50,left=50,width=526,height=442');
//		viewResult(responseXML);
	}
}

function alert3(msg) {
	alert(msg);
	/*
	document.getElementById("alertMsg").innerHTML = msg;
	var fade = new Fade('alertMsg', '#ffc', '#fff', 5, 100, 0);
	fade.init();
	*/
}

function viewResult(responseXML) {
//	document.getElementById("alertMsg").innerHTML = "";
	var d = document.getElementById("vote_result");
	var html = "";

	var total_cnt = ajaxAPI.getValue(responseXML, "totalcnt") * 1;
	var votes = responseXML.documentElement.getElementsByTagName('vote');	
	
	for (var t=0;t<votes.length;t++) {
		var vote_contents = votes[t].firstChild.nodeValue;
		var vote_rescnt = votes[t].getAttribute('rescnt')*1;
		var progress = parseInt(vote_rescnt / total_cnt * 100);

		html += "<div>" + vote_contents + "</div>";
		html += '<div>' + 
				'<table width="185" cellpadding="0" cellspacing="0" border="0">' +
				'<tr>' +
					'<td nowrap><img src="http://cafeimg.hanmail.net/top5/story/chal_g_01.gif" width="1" height="14" align="absmiddle"><br></td>' +
						'<td width="' + progress + '%" background="http://cafeimg.hanmail.net/top5/story/chal_g_02.gif"  align="right" style="padding-right:33px; padding-top:4px" valign="top"></td>' +
						'<td width="' + (100 - progress) + '%" background="http://cafeimg.hanmail.net/top5/story/chal_g_03.gif"></td>' +
					'<td nowrap><img src="http://cafeimg.hanmail.net/top5/story/chal_g_01.gif" width="1" height="14" align="absmiddle"><br></td>' +
				'</tr>' +
				'</table>' +
				'</div>';
		html += "<div>" + vote_rescnt + "명</div>";
	}
	
	html += "<br>";
	html += "<div>전체: " + total_cnt + "명</div>";
	
	d.innerHTML = html;
}

function Fade(id, startColour, endColour, count, speed, delay ) {

  /* Properties */

  this.id = id;                    /* id/object ref of element to fade */
  this.startColour = startColour;  /* initial colour (3 or 6 digit) hex */
  this.endColour = endColour;      /* final colour (3 or 6 digit) hex */
  this.count = count;              /* No of steps to take during fade */
  this.speed = speed;              /* Delay in ms between steps */
  this.delay = delay;              /* Initial delay before fade begins */

  if (typeof this.id == "string") {
    this.obj = document.getElementById(id);  
  } else {
    this.obj = this.id;
  }
  this.colour = new Array();
  this.steps = 0;

  /* Methods */

  this.init = init;
  this.fade = fade;
  this.parseColour = parseColour;

  function init() {

    first = this.parseColour(this.startColour, 'hex');
    last = this.parseColour(this.endColour, 'hex');

    this.colour = new Array();
    this.colour[this.count] = this.startColour;
    for (i=0; i<this.count; i++) {
      temp = "rgb(";
      temp += parseInt(first[0]+(last[0]-first[0])/this.count*i);
      temp += ",";
      temp += parseInt(first[1]+(last[1]-first[1])/this.count*i);
      temp += ",";
      temp += parseInt(first[2]+(last[2]-first[2])/this.count*i);
      temp += ")";
      this.colour[this.count-i] = temp;
    }
    this.colour[0] = this.endColour;

    var thisObj = this;
    setTimeout( function() { thisObj.fade() }, this.delay);
//	thisObj.fade();

  }

  function fade() {
      
    if (this.count >= 0) {

      this.obj.style.backgroundColor = this.colour[this.count--];

      var thisObj = this;
      setTimeout( function() { thisObj.fade() }, this.speed);
//      thisObj.fade();

    }
  }

  function parseColour(colour, t) {
    /* From: http://www.meyerweb.com/eric/tools/color-blend/ */
    var m = 1;
    col = colour.replace(/[\#rgb\(]*/,'');
    if (t == 'hex') {
      if (col.length == 3) {
        a = col.substr(0,1);
        b = col.substr(1,1);
        c = col.substr(2,1);
        col = a + a + b + b + c + c;
      }
      var num = new Array(col.substr(0,2),col.substr(2,2),col.substr(4,2));
      var base = 16;
    } else {
      var num = col.split(',');
      var base = 10;
    }
    if (t == 'rgbp') {m = 2.55}
    var ret = new Array(parseInt(num[0],base)*m,parseInt(num[1],base)*m,parseInt(num[2],base)*m);
    return(ret);
  }
}