/* --{ F2F } -- */
function F2F(){};
F2F.from = function(){ return new F2F(); }
F2F.is_ok = function(o){ return o!=null && typeof o!="undefined"}; 
F2F.prototype.setHeader = function(h){this.$header = h;}
F2F.prototype.setId = function(id){this.$id = id;}
F2F.prototype.getId = function(){return this.$id;}
F2F.prototype.setRespuestas = function(r){this.$resps = r;}
F2F.prototype.getRespuestas = function(){return this.$resps;}
// --{ add } --
// avoid set/get function handling $data to improve performance
F2F.prototype.add = function( o, n )
{
	if ( !n ) n = 1;
	if ( !F2F.is_ok(this.$data) ) this.$data = {};
	for(var f in o)
	{
		if ( !F2F.is_ok(this.$data[f]) ) this.$data[f] = {};
		if ( !F2F.is_ok(this.$data[f][o[f]]) ) this.$data[f][o[f]] = 0;
		this.$data[f][o[f]] += n;
	}
}
F2F.prototype.getDataForField = function(p) 
{
	var o = {};
	if ( F2F.is_ok(this.$data) && F2F.is_ok(this.$data[p]) ) 
	{
		return this.$data[p];
	}
}
F2F.prototype.porcentajes = function( co ) 
{
	 var sum = 0;
	 var pco = {};
	 for( var c in co ) {
		  sum += co[c];
	 }
	 var last, acum, tp;
	 acum=0;
	 for( var c in co ) {
		  tp = Math.round(co[c]*100/sum);
		  pco[c] = tp;
		  last = c;
		  acum += tp;
	 }
	 pco[last] += 100 - acum;
	 return pco;
}
F2F.prototype.show = function( tr )
{
	var html = '';
	var res,pcs;
	var id = this.getId();
	var resps = this.getRespuestas();
	if ( enc )
	{
		res = enc.getDataForField( "voto" );
		pcs = enc.porcentajes( res );
	}
	var opt;
	var tmp;
	
	var cnt=0;
	var r;
	for(var i=0; i<resps.length; i++)
	{
		r = resps[i];
		if ( !F2F.is_ok(res) )
		{
			res = {}
		}
		if ( !F2F.is_ok(res[r]) )
		{
			res[r] = pcs[r] = 0;
		}
		tmp = tr;
		tmp = tmp.replace( /__val__/g,  r );
		tmp = tmp.replace( /__id__/g,   id );
		tmp = tmp.replace( /__ind__/g,  cnt );
		tmp = tmp.replace( /__porc__/g, pcs[r] );
		tmp = tmp.replace( /__vot__/g,  res[r] );
		html += tmp;
		cnt++;
	}
	return html;
}
// F2FPoll
function F2FPoll(){};
F2FPoll.$data = {};
F2FPoll.is_ok = function(o){ return o!=null && typeof o!="undefined"};
// set
F2FPoll.set = function( key, value )
{
	F2FPoll.$data[key] = value;	
}
// get
F2FPoll.get = function( key )
{
	if ( this.is_ok(F2FPoll.$data[key]) )
	{
		return F2FPoll.$data[key];
	}
	return null;
}
// add
F2FPoll.add = function( id, val )
{
var f = document.forms["F2F1stF"];
if ( this.is_ok(f) )
{
	var c = F2FPoll.get( "channel" );
	var goUrl = F2FPoll.get( "nextPage" );
	if ( this.is_ok(goUrl) )
	{
		f.ok.value = goUrl.toString().replace( /__id__/g, id );
	}
	else
	{
		f.ok.value = "";
	}
	f.id.value   = (c?c:'') + id;
	f.voto.value = val;
	f.submit();
}
}
