/** 
 *  BBCode Sonda (c)Agora S.A.
 **/
var  inf ='Ładowanie sondażu ...';
var  pVotes = null;
var  pTitle = null;
var  idKey = null;

/* get Sonda */
function openBBCPoll(title, votes){

	idKey = getThreadParam();
	var forumId = getForumParam();

	pVotes = votes;
	pTitle = title;

	if( idKey == null ) idKey = forumId;
	
	if( idKey == null ) return null;

	innerTxt(inf);

	var flaga = isVisited(idKey);

	if (flaga){
		AjaxForum.getPoll(idKey,forumId, showVotes);
	}else{
		AjaxForum.getPoll(idKey,forumId, showForm);
	}
}


function isVisited(tId){

 var cookie_name="BBCSONDA";
 try{

	if( tId == null ) return false;

	var postsCookie = readCookie(cookie_name);

	var tIdhex = d2h(tId);

	if( postsCookie != null ){
		var ttab = postsCookie.split(' ');
		for(i=0;i<ttab.length;i++){
		    if (ttab[i] == tIdhex )
			return true;
		}
	    }
 }catch(e){}
 return false;
}

function pushSondaCookie(tId){

	if (tId==null) return null;

	var max_cookie_length=300;
	var cookie_name="BBCSONDA";
	try{
	var postsCookie=readCookie(cookie_name);
	if( tId == null ) return null;
	var tIdhex = d2h(tId);
	var newTab = new Array();
	newTab[0] = tIdhex;

	if( postsCookie != null ){
		var ttab = postsCookie.split(' ');
		var j=1;
		for(i=0;i<ttab.length;i++){
		 if( ttab[i] != null && ttab[i] != tIdhex && ttab[i] != 0){
				
			newTab[j++]=ttab[i];
		}
	    }
	}

	var newCookie = "";
	for(i=0;i<newTab.length;i++){
		if( newCookie.length + (" "+newTab[i]).length > max_cookie_length )
			break;
		if( i>0 )
			newCookie += " ";
		newCookie += newTab[i];
	}

	if( newCookie != postsCookie ){
	  	createCookie(cookie_name,newCookie,30,0,'');
	 }
	}catch(e){
	}

	return null;
}


function innerTxt(txt){
var ele= null;
    try{
	ele = document.getElementById("bbcPoll");
	if (ele != null){
		ele.innerHTML = txt;
	}
	}
	catch (e){
	    alert(e);
	}
}

function isSelected() {

 var radio = document.getElementsByName('bbcSVote');

 for (var i = 0; i < radio.length; i++) {
	if (radio[i].checked) {
	  return radio[i].value;
	}
 }
 alert('Wybierz jedna  z opcji..');
 return -1;
}


function addVote(){

 var nrVote = isSelected();
 var countVotes = 0;
 var forumId = getForumParam();

 if (nrVote<0)
	return false;

 pushSondaCookie(idKey);
 AjaxForum.addPollVote(idKey,forumId, nrVote, showVotes);

}


function showForm(sonda){

 var votes = pVotes;
try{

 if( sonda.expired ) {
	showVotes(sonda);
	return;
 }

 var txt = '<div class="bbcPollVote"><h2>'+pTitle+'</h2>\n<fieldset class="bbcpBox"><ul>\n';

 for (var i = 0; i < votes.length; i++) {
   txt += '<li><input type="radio" value="'+i+'" name="bbcSVote" id="bbcPollQuestion'+i+'" /><label for="bbcPollQuestion'+i+'">'+votes[i]+'</label></li>\n';
 }

 txt += '</ul>\n<p class="submit"><a class="bbcPollSubmit" href="javascript:addVote();">Zagłosuj</a></p></fielset>\n</div>';

 innerTxt(txt);
}catch(e){
		alert("error:"+e);
}
 return;
}


function showVotes(sonda){
 var txt = "";
 var sum = 0;
 var ele = 0;
 var sr  = 0;
 var count=0;
 var zero = false;


try{
 
 var result = sonda.votes;

 txt = '<div class="bbcPollResult"><h2>'+pTitle+'</h2><div class="bbcpBox"><ul>\n';

 if (result != null){
   result = result.split(" ");
   for (var j=0; j< result.length; j++){
	ele = parseInt(result[j]);
	if (!isNaN(ele))
		sum += ele;
   }
 }else{
   sum = 0;
 }

 for (var i=0; i < pVotes.length; i++)
 {
	zero = false;
	if (sum>0 && result != null && result[i] != null && !isNaN(result[i])) {
		count = result[i];
		if( count == 0 )
			zero = true;
		sr = Math.round( ((count/sum)*100)) ;
		if( sr == 0 && ! zero)
			sr = 1;
	}else{ 
		count=0;
		sr = 0;
		zero = true;
	}

	txt += '<li><div class="c0">'+pVotes[i]+'</div>\n'+
                '<div class="c1">'+
                ' <div class="bar0"><div class="bar" style="width:'+sr+'%;">'+(count==0 ? '' : '<span class="cnt">'+count+'</span>' )+'</div></div>'+
                ' <span>'+sr+'%</span>\n'+
                '</div>\n'+
                '</li>\n';
  }


 txt += '</ul>\n<p class="sum">Głosów: <span>'+sum+'</span>'+(sonda.expired ? ' (Głosowanie zakończone.)':'')+'</p></div></div>';
}catch(e){
	alert("error:"+e);
}
 innerTxt(txt);
}


