/**
 * Funktionen für Standard-Voting-Templates
 */
function voting( vid, qid, vstate, lpath, tpath, resmod, restxt, resdsplmode, blockip, blockhours, dunkel, hell,prefix,suffix,misc_ivw_pixel,title,misc_runs_in) {
	this.votingId		= vid;				//ID des Votings
	this.questionId		= qid;				//ID der Frage
	this.votingState	= vstate;			//status des votings
	this.livePath		= lpath;			//Pfad zum Voting
	this.templatePath	= tpath;			//pfad zum Voting
	this.resultMode		= resmod;			//modus für die ergebnisdarstellung (popup oder AJAX)
	this.resultText		= restxt;			//Danketext
	this.resultsActive	= resdsplmode;		//wie sollen die Ergebniswerte angezeit werden
	this.blockIP		= blockip;			//ipblock modus an?
	this.blockHours		= blockhours;		//Anzahl der Stunden, die das Cookie gültig ist
	this.farbeDunkel	= dunkel;			//dunkle farbe (zb bestes ergebnis)
	this.farbeHell		= hell;				//helle farbe (zb schlechtes Ergebnis)
	this.misc_ivw_pixel	= misc_ivw_pixel;				//ivw Pixel String
	this.title		= title;				//voting title für den Besucherpixel
	this.misc_runs_in	= misc_runs_in;				//Anzeigemode
	
	
	this.prefix		= prefix;			//Anfangssatz
	this.suffix		= suffix;			//Endsatz
		    	
	this.tempData		= null;				//zwischenspeicher für votings bei zwischenergebnissen
	this.jsonData		= null;


	this.resultValue	= null;
	this.radioBtn		= '0';
	this.FSPHP_temp		= '';
	
	

	this.submitVote = function(answerId) {
		//ein cookie speichern mit der gewählten Antwort
		//TODO vielleicht antwortIDs im cookie ablegen
		if (this.blockIP) T$.cookie('vt_question_'+ this.questionId, 1, this.blockHours);
		
		
		var answerstrings = Array();//todo nur votingchilds suchen document.getelementbyid votingidkey
			answers = document.getElementsByName('answers[]_'+this.votingId);
			for (var i=0; i<answers.length; i++) {
				var currentAnswer = answers[i];
				if (currentAnswer.checked) answerstrings.push( 'answers[]='+ currentAnswer.value );
			}
			
		var url = this.livePath +'index.php?act=vote&id='+ this.votingId +'&qid='+ this.questionId +'&seed='+ (Math.floor ( Math.random ( ) * 9999 + 1 ))+'&'+answerstrings.join('&');
	
		//this.resultValue = radioBtn;
		
		this.mGetJsonp(url,0);
		
	}
		
	this.mGetJsonp = function (url,voted) {
		votingObj = this;
		T$.ajax({
		type: "GET",
		dataType: 'jsonp',
		jsonp: 'jsoncallback',
		url: url,
		success: function (data) {
			//Die serverseitige funktion vote() liefert entweder ein jsonp oder ein weiterleitungs-URL			
			if(data['weiterleiten_url'] && voted != 1)
				{
					votingObj.showResults(data['weiterleiten_url'],0);
				}
				else{
					 votingObj.jsonData = data;
					 votingObj.drawResult(true);
				}
			}
		});
	}
	
	
	/**
	*Ergebnisse als lightbox zeigen
	*/
	this.showResults = function(sUrl,back) {
		if(misc_runs_in 	== 	1 	)
		{
			window.open(this.livePath+sUrl,'votingwindow','width=845,height=700,resize=no,scrollbars=no,menubar=no,directories=no');
		}else{
		if(this.misc_runs_in==1 && back==1)
		{
			FSPHP_ebene_nr	=	1;
			FSPHP_ToLb(this.livePath+sUrl,'Fotoshow_'+this.votingId,null,null,null,null,1,1,0,1,this.votingId);
		}
		else
			FSPHP_ToLb(this.livePath+sUrl,'Fotoshow_'+this.votingId	,null,null,null,null,1,1,0,0,this.votingId);
		}
	}
	/**
	 * zeichnet das Ergebnis
	 */
	 this.drawResult = function(noVote) {
	 	
	 	 var resultstring	=	'';
	 	data = this.jsonData; 
	 	this.FSPHP_temp	=	 T$( "#FSPHP_votingSolution_"+this.votingId ).html();	
	 	var voteText = "";
		var str= "";
		var img="";
		var votes="";
		if ( this.resultsActive > 0)
		{
		data = this.jsonData;
		
		if (data.voting.results_show_uniquevotes == '1') 
		{
		if(data.uniqueVotes == 1){	voteText='Es hat 1 Besucher abgestimmt';}	
		else { voteText = 'Es haben '+data.uniqueVotes+' Besucher abgestimmt.';}
		}
		
		if(this.prefix){	prefixText='<div style="clear:both;width:209px;overflow:hidden;padding-bottom:20px;line-height:16px;">'+this.prefix+'</div>';}	
		else{ prefixText= '';}
		if(this.suffix){	suffixText='<div style="clear:both;width:209px;overflow:hidden;padding-bottom:2px;line-height:16px;">'+this.suffix+'</div>';}	
		else{ suffixText= '';}                          
	
		for (i in data.answers ){

			if(parseInt(data.answers[i]["votes"]) == 1){votes ="Stimme";}else votes ="Stimmen";
			
						
			if(data.answers[i]["bestAnswer"] ==1 /*&& data.voting["results_highlight_best"]*/){
				img='<img class="FSPHP_solution_balken" style="width:'+parseInt(data.answers[i]["percent"])+'%" src="http://af.fs-t-online.de/voting/templates/all.standard2010/BalkenDunkel_middle.png"/>';
			}
			else{
				img ='<img class="FSPHP_solution_balken" style="width:'+parseInt(data.answers[i]["percent"])+'%" src="http://af.fs-t-online.de/voting/templates/all.standard2010/BalkenHell_middle.png"/>';
			}
			
				if (this.resultsActive == 2) resultstring = '<div class="FSPHP_solution_procent">'+	data.answers[i]["percent"]+'%</div >';
				else if (this.resultsActive == 3) resultstring = '<div class="FSPHP_solution_votes" >'+data.answers[i]["votes"]+' ' +votes+'</div>';
				else if (this.resultsActive == 4) resultstring ='<div class="FSPHP_solution_procent" >'+	data.answers[i]["percent"]+'%</div >'+'<div class="FSPHP_solution_votes" nowrap="nowrap">'+data.answers[i]["votes"]+' ' +votes+'</div>';
				else resultstring = '';
				
				str += '<div class="FSPHP_solution_row" style="height:50px;"  id="'+i+' ">'+
				'<div class="FSPHP_solution_row_voting" style="width:auto;height:auto;top:0px;"> <h4>'+data.answers[i]["answer"] +' </h4></div>'+
			'<div class="FSPHP_solution_balkenrahmen" style="top:0px;line-height:0px;float:none;clear:both;width:200px;" >'+img+'</div><div style="float:none;clear:both;"></div>'+resultstring+'</div><div style="clear:both;float:none;height:10px"></div>';			

		}
		T$('#FSPHP_votingSolution_'+this.votingId).html('<h2>'+data.question.question+'</h2><div  id="FSPHP_Stimmen" class="Ttst" style="overflow:hidden;width:209px;margin-top:8px; margin-bottom:8px;">'+prefixText+'<div style="clear:both;height:auto;width:200px;">'+voteText +'</div><div class="FSPHP_solution_tabelle" style="width:209px;margin-bottom:0px;">'+ 
			str +'</div>'+suffixText+
			'<div class="FSPHP_solution_dank_unten" style="margin-top:2px;">'+this.resultText+'<div style="margin-bottom:2px"></div></div>');
		}else
		T$('#FSPHP_votingSolution_'+this.votingId).html('<div>Die Anzeige von Ergebnissen ist deaktiviert.</div>');
		//ivw Pixel
		var FSPHP_portal	= this.misc_ivw_pixel.split(";");
		var title=this.title;
		T$('#FSPHP_votingSolution_'+this.votingId).append('<div style="display:none">');
		  try
		   {
			 webt.site=FSPHP_portal[0];webt.pi('CP/'+FSPHP_portal[1]+';'+FSPHP_portal[2]+'/'+title.replace(/ /g,'')+'.html');
		   }catch(e){}
		   T$('#FSPHP_votingSolution_'+this.votingId).append('<img src="http://toi.ivwbox.de/cgi-bin/ivw/CP/'+FSPHP_portal[1]+';'+FSPHP_portal[2]+'/'+title.replace(/ /g,'') +'.html?r='+data.FSPHP_referer+'&d='+Date.parse(new Date())+'" width="1" height="1" alt="" />');
		   T$('#FSPHP_votingSolution_'+this.votingId).append('<img src="http://toi.passul.t-online.de/cgi-bin/CP/'+FSPHP_portal[1]+';'+FSPHP_portal[2]+'/'+title.replace(/ /g,'') +'.html?r='+data.FSPHP_referer+'&d='+Date.parse(new Date())+'" width="1" height="1" alt="" />');
		   T$('#FSPHP_votingSolution_'+this.votingId).append('</div>');
	

	 }
	 

	this.drawVotedMessage = function() {
		out = Array();	
	 	out[out.length] = '<div id="info" style="height:24px; padding:5px;">Ihr Stimme wurde gewertet.</div>';
	 	out[out.length] = '<div><p class="tsml">&nbsp;</p></div>';
	 	
	 	T$( "#FSPHP_votingSolution_"+this.votingId ).html( out.join('') );
	}
	
	/**
	 * stellt das voting wieder her beim Klick auf den zurueck zum Voting button
	 */
	this.restoreVoting = function() {
		//console.log( this.tempData ); //schreibt den Text in die Console des Firebug-Plugins
		T$( "#FSPHP_votingSolution_"+this.votingId ).html( this.FSPHP_temp );
	}

	/**
	 * prüft, ob das cookie gesetzt ist, dass der Benutzer für dieses Voting bereits abgestimmt hat
	 * bei AJAX Votings lädt er die Ergebnisse, ansonsten wird nur eine Meldung ausgegeben, dass das
	 * Ergebnis nicht geladen werden konnte
	 */
	this.checkVotingBlock = function() {
		if ((this.blockIP == '1') && T$.cookie('vt_question_'+this.questionId)) {
	
				var url = this.livePath +'index.php?act=renderResult&id='+ this.votingId +'&qid='+ this.questionId +'&seed='+ (Math.floor ( Math.random ( ) * 9999 + 1 ))+"&ajax=1";
				this.mGetJsonp(url,0);
		}
		if ((this.votingState == '30') && (this.resultsActive > 0))
			{
				var url = this.livePath +'index.php?act=renderResult&id='+ this.votingId +'&qid='+ this.questionId +'&seed='+ (Math.floor ( Math.random ( ) * 9999 + 1 ))+"&ajax=1";
				this.mGetJsonp(url,0);
			}
		
		return false;
	}
	
	//radio buttons in T$
	this.rbChange =	function (idSpan, idRadio)
	{
	if(T$('#'+idSpan).hasClass("radioOFF"))
	{
	T$("span").removeClass("radioON");
	T$('#'+idSpan).addClass("radioON");

	T$('#'+idRadio).attr('checked', true);
	}else{
	T$('#'+idSpan).removeClass("radioON");
	T$('#'+idSpan).addClass("radioOFF");

	T$('#'+idRadio).attr('checked', false);
	}
	this.radioBtn = idRadio;
	}
	
	//Checkbox in T$
	this.cbChange = function (idSpan, idCheckbox)
	{
	if(T$('#'+idSpan).hasClass("checkboxOFF"))
	{
	T$('#'+idSpan).removeClass("checkboxOFF");
	T$('#'+idSpan).addClass("checkboxON");

	T$('#'+idCheckbox).attr('checked', true);
	}else{
	T$('#'+idSpan).removeClass("checkboxON");
	T$('#'+idSpan).addClass("checkboxOFF");

	T$('#'+idCheckbox).attr('checked', false);
	}
}
}