// JavaScript Document
var selected = null;

function select_val(id){
	selected = id;
}

// ajax request to select a poll
function display_poll(div_id){
	poll_answ = selected;
	$.ajax({
		type:"POST",
		url:'/poll/get/',
		dataType: 'json',
		data:{'poll_answ':poll_answ},
		error:function(resp){},
		success:function(resp){
			if(resp.status == 0) {
				display_poll_questions(resp.poll_quest,resp.poll, div_id);
				return true;
			} else if(resp.status == 1) {
				display_poll_results(resp.poll_quest,resp.poll, div_id);
				return true;
			} else {
				$('div#'+div_id).html();
				return false;
			}
		}
	});
}

//ajax request to select a poll with the given domain
function display_poll_for_domain(div_id, domain){
	poll_answ = selected;
	$.ajax({
		type:"POST",
		url:'/poll/get/',
		dataType: 'json',
		data:{'poll_answ':poll_answ, 'domain':domain},
		error:function(resp){},
		success:function(resp){
			if(resp.status == 0) {
				display_poll_questions(resp.poll_quest,resp.poll, div_id);
				return true;
			} else if(resp.status == 1) {
				display_poll_results(resp.poll_quest,resp.poll, div_id);
				return true;
			} else {
				$('div#'+div_id).html();
				return false;
			}
		}
	});
}

// display poll with ossible answers
function display_poll_questions(poll_quest, poll, div_id){
	// set up question
	html = '<form action="#" class="question"><span class="question_title" id="poll_'+(poll['id']).toString()+'">'+poll['quest']+'</span><br/><ul class="questions">';
	// set up answers
	for (i=0;i<poll_quest.length;i++){
		q = poll_quest[i];
		html = html + 
			'<li><input type="radio" style="margin:-4px 5px 0 0" name="question" onclick="select_val(' 
			+ q['id'] + ');"/>' + q['answer'] + '</li>';
	}
	html = html + '</ul><span onclick="display_poll(\'' + div_id + '\');"><img src="http://static.etvnet.com/images/btn_poll_submit.jpg" style="cursor: pointer; margin-left: 90px; margin-top:4px;"/></span></form>';
	if (poll['link'] != null){
		html = html + '<a href="' + poll['link'] + '" class="poll_link">' + poll['link_name'] + '</a>';
	}
	$('div#'+div_id).html(html);
}

// display poll results
function display_poll_results(poll_quest, poll, div_id){
	total = 0;
	// set up question
	html = '<div class="answer"><span class="question_title" id="poll_'+(poll['id']).toString()+'">'+poll['quest']+'</span><br/><ul class="answers">';
	// find max
	for (i=0;i<poll_quest.length;i++){
		q = poll_quest[i];
		html = html + 
			'<li>' + '<span class="answer_text">' + q['answer'] + '</span>' + ' ' + '<div id="progressbar' + i.toString() 
			+ '" class="progress_div"></div><span class="percent_text" id="prog_text' + i.toString() 
			+ '"></span>' + '</li>';
		total = total + q['total']
	}
	html=html+'</ul>';
	if (poll['link'] != null){
		html = html + '<a href="' + poll['link'] + '">' + poll['link_name'] + '</a>';
	}
	$('div#'+div_id).html(html);
	// set up results
	for (i=0;i<poll_quest.length;i++){
		q = poll_quest[i];
		$('div#progressbar'+i.toString()).html('<span class="progressbar" style="width:' + q['total']/total*100 + '%;"></style>');
		$('#prog_text'+i.toString()).html(parseInt((q['total']/total)*100)+'%');
		html=html+'</div>';
	}
}

