var netm = netm == null ? {} : netm;
netm.Poll = function(container, options) {
	if (options == null || container == null) return;
	this.container = document.getElementById(container);
	if (this.container == null) return;
	
	if (typeof options == 'string') options = eval('(' + options + ')');
	
	this.id = options['id'];
	this.name = 'NetmPollWidget' + this.id;
	this.question = options['question'] || '';
	this.answers = options['answers'] || [];
	
	this.draw();
}
netm.Poll.prototype.setAnswers = function(options) {
	if (typeof options == 'string') options = eval('(' + options + ')');
	this.answers = options['answers'] || [];
}

netm.Poll.prototype.draw = function() {
	if (netm.Poll.getCookie(this.name) != null) {
		this.drawResults();
	} else {
		this.drawAnswers();
	}
}

netm.Poll.prototype.drawAnswers = function() {
	var text = 
	'<div class="poll">' + 
	'<span class="question">' + this.question + '</span>' +
	'<form name="' + this.name + '_form" id="' + this.name + '_form" method="post">' +
		'<table border="0" cellpadding="0" cellspacing="0" width="100%" class="answers">';
	
	var answers = this.answers;
	for(var i=0, len=answers.length; i<len; i++) {
		var answer = answers[i];
		var answerId = this.name + '_answer' + answer['id'];
		text += 
			'<tr>' +
				'<td valign="top">' +
					'<input type="radio" id="' + this.name + '_answer' + answerId + '" name="answer" value="' + answer['id'] + '" ' +
						'onclick="netm.Poll.changeAnswer(\'' + this.name + '_form\', \'' + answer['id'] + '\')" />' +
				'</td>' +
				'<td width="100%">' +
					'<label for="' + this.name + '_answer' + answerId + '">&nbsp;' + answer['content'] + '</label>' +
				'</td>' +
			'</tr>';
	}
	
	text += '<tr><td colspan="2">&nbsp;</td></tr>';
	text += '<tr><td colspan="2" align="center"><input type="submit" name="pollVoteBtn" value="' + netm.Poll.settings['vote'] + '" disabled /></td></tr>';
	text += '</table></form></div>';
	this.container.innerHTML = text;
	
	var me = this;
	document.forms[this.name + '_form'].onsubmit = function() {
		return me.vote();
	}
}

netm.Poll.prototype.drawResults = function() {
	var text = 
	'<div class="poll">' + 
	'<span class="question">' + this.question + '</span>' +
	'<table border="0" cellpadding="1" cellspacing="1" width="100%" class="results">';
	
	var answers = this.answers;
	for(var i=0, len=answers.length; i<len; i++) {
		var answer = answers[i];
		text += 
			'<tr>' +
				'<td valign="top" colspan="2">' + answer['content'] + '</td>' + 
			'</tr>' +
			'<tr>' +
				'<td valign="top">' +
					'<table cellpadding="0" cellspacing="0" border="0" width="100%">' +
						'<tr><td class="bar" style="background-color:#000000;height:20px" width="' + answer['percent'] + '%"></td><td>&nbsp;' + answer['percent'] + '%</td></tr>' + 
					'</table>' + 
				'</td>' +
			'</tr>';		
	}
	if (netm.Poll.settings['showAllResults']) {
		text += '</table><br/><div align="center"><a href="' + netm.Poll.settings['allResultsLink'] + '">' + netm.Poll.settings['allResultsText'] + '</a></div></div>';
	}
	this.container.innerHTML = text;
}

netm.Poll.prototype.vote = function() {
	netm.Poll.setCookie(this.name, true, '01/01/2100', netm.Poll.settings['domain']);
	var me = this;
	var answer = 0;
	var elems = document.forms[this.name + '_form'].elements;
	for(var i=0, len=elems.length; i<len; i++) {
		if (elems[i].type == 'radio' && elems[i].checked) {
			answer = elems[i].value;
			break;
		}
	}
	Ajax.request('polls_vote.action', {
		parameters: {'id': me.id, 'answer': answer },
		callback: function(response) {
			me.setAnswers(response.responseText);
			me.drawResults();
		}
	});
	return false;
}

//static
netm.Poll.settings = {
	domain: '/netm', 
	vote: 'Vote',
	showAllResult: false,
	allResultsLink: '#',
	allResultsText: 'Older Results'
}

netm.Poll.changeAnswer = function(formName, answerId) {
	document.forms[formName].elements['pollVoteBtn'].disabled = false;
}

netm.Poll.setCookie = function(name, value, expires, path, domain, secure) {
	var curCookie = name + '=' + escape(value) +
		((expires) ? '; expires=' + new Date(expires).toGMTString() : '') +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
  	document.cookie = curCookie;
}

netm.Poll.getCookie = function(name) {
  	var dc = document.cookie;
  	var prefix = name + '=';
  	var begin = dc.indexOf('; ' + prefix);
  	if (begin == -1) {
    	begin = dc.indexOf(prefix);
    	if (begin != 0) return null;
  	} else{
    	begin += 2;
  	}
  	var end = document.cookie.indexOf(';', begin);
  	if (end == -1) end = dc.length;
  	return unescape(dc.substring(begin + prefix.length, end));
}

netm.Poll.deleteCookie = function(name, path, domain) {
	if (netm.Poll.getCookie(name)) {
  		document.cookie = name + '=' +
    		((path) ? '; path=' + path : '') +
    		((domain) ? '; domain=' + domain : '') +
    		'; expires=Thu, 01-Jan-70 00:00:01 GMT';
  	}
}
