// JavaScript Document
var dwProgressBar = new Class({
	Implements: [Options],
	options: {
		container: $$('body')[0],
		boxID:'',
		percentageID:'',
		displayID:'',
		startPercentage: 0,
		displayText: false,
		speed:10
	},
	initialize: function(options) {
		this.setOptions(options);
		this.createElements();
	},
	createElements: function() {
		var box = new Element('div', { id:this.options.boxID });
		var perc = new Element('div', { id:this.options.percentageID, 'style':'width:0px;' });
		perc.inject(box);
		box.inject(this.options.container);
		if(this.options.displayText) { 
			var text = new Element('div', { id:this.options.displayID });
			text.inject(this.options.container);
		}
		this.set(this.options.startPercentage);
	},
	calculate: function(percentage) {
		return ($(this.options.boxID).getStyle('width').replace('px','') * (percentage / 100)).toInt();
	},
	animate: function(to) {
		$(this.options.percentageID).set('morph', { duration: this.options.speed, link:'cancel' }).morph({width:this.calculate(to.toInt())});
		if(this.options.displayText) { 
			$(this.options.displayID).set('text', to.toInt() + '%'); 
		}
	},
	set: function(to) {
		this.animate(to);
	}
});

function load_md(id,link)
{
	var params = {"voteServerId":"1","voteId":id};
	var phprpc = new PHPRPC_Client('http://vote.xinmin.cn/index.php?app=common&controller=Vote.Service&action=phprpc', ['getVote']);
	phprpc['getVote']
	(
		params,
		function(vote)
		{
			var html= '<div class="title"><a href="'+link+'" target="_blank">'+vote.question+'</a></div>';
			if(vote.mode == "0")
			{
				//单选
				html += '<form id="vote_1_'+id+'" method="post" onSubmit="return check('+id+',\''+link+'\');">';
			}else{
				//多选
				html += '<form id="vote_1_'+id+'" method="post" onSubmit="return check2('+id+',\''+link+'\');">';
			}
			
			for (var i=0; i < vote.options.length; i++)
			{
				html += '<div id=put-bar-here'+i+'></div>';
			}
			html += '<div class="buttom">&nbsp;&nbsp;';
			if(vote.isVote){
				//是否已经投票
			}
			html += '<input type="submit" class="xmsub" value="">';
			html += '<input type="button" class="xmmore" value="" onClick="window.open(\'http://news.xinmin.cn/xmmd/\')"><span class="xmtotal">已有'+vote.total+'网友投票</span></div>';
			html += '</form>';
			$('xmmd').innerHTML = html;
			for (var i=0; i < vote.options.length; i++)
			{
				pb = new dwProgressBar({
					container: $('put-bar-here'+i),
					startPercentage: vote.options[i].percent,
					speed:1000,
					boxID: 'box',
					percentageID: 'perc'+i,
					displayID: 'text'+i,
					displayText: true
				});
				if(vote.mode == "0")
				{
					$('text'+i).innerHTML = '<div class="qusl"><input id="options_1_'+id+'" type=radio name=options value='+i+'>'+vote.options[i].content+'</div><div class="qusr">'+vote.options[i].count+'</div>';
				}else{
					$('text'+i).innerHTML = '<div class="qusl"><input id="options_1_'+id+'" type="checkbox" name="options[]" value='+i+' />'+vote.options[i].content+'</div><div class="qusr">'+vote.options[i].count+'</div>';
				}
			}
			return false;
		}
	)
}
//检查radio
function check(id,link)
{
	var qus = document.getElementsByName('options');
	for (var i=0; i<qus.length; i++)
	if(qus[i].checked) return insert(id,$G('options_1_'+id, 'vote_1_'+id),link);
	alert("对不起, 您一个都没有选");
	return false;
}
//检查options
function check2(id,link)
{
	var obj = document.getElementsByName('options[]');
 	var len = obj.length;
	var value = 0;
	for(var i=0;i<len;i++){
		if(obj[i].checked==true){
			value = 1;
		}
	}	
	if(value != 0)
	{
		return insert(id,$G('options_1_'+id, 'vote_1_'+id),link);
	}else{
		alert("对不起, 您一个都没有选");
		return false;
	}
}
function insert(id,postValue,link)
{
	var params2 = {"voteServerId":"1","voteId":id,"options":postValue};
	var phprpc = new PHPRPC_Client('http://vote.xinmin.cn/index.php?app=common&controller=Vote.Service&action=phprpc', ['vote']);
	phprpc['vote']
	(
		params2,
		function(vote)
		{
			alert(vote.message);
			load_md(id,link);
		}
	)
	return false;
}
/* 没用 获取ip */
function GetLocalIPAddress()
{
	var obj = null;
	var rslt = "";
	try
	{
		obj = new ActiveXObject("rcbdyctl.Setting");
		rslt = obj.GetIPAddress;
		obj = null;
	}
	catch(e)
	{}
	return rslt;
}