$.fn.poll = function(options) {

	var defaults = {
	    id:0,
		chm: 'N*p0*,000000,0,-1,11',
		divId: 'votingmodule'
	};
	
	var opts = $.extend(defaults, options);

	$(document).ready(function(){
		$("#"+defaults.divId).html("<img src='/images/loader.gif' class='loader'/>");
		// loading the voting module...
		$.ajax({
			url: "/ajax/votingmodule.php",
			data: "pollId="+defaults.id,
			success: function(msg){
				$("#"+defaults.divId).html(msg);
				initVoting();
				initBars();
			}
		});
	});

	function initBars(){
		var titles;
		var votes;
		var titles;

		var test = new Array(10, 2);

		//var api = new jGCharts.Api(); 

		$.each($("#"+defaults.divId).find(".barchart"), function(index, value){
		barchart = $(this);
		var c = $(value).find(".p_answersCount").val();
		titles = $(value).find(".p_answersTitles").val();//.split("|");
		votesstr = $(value).find(".p_answersResults").val();//.split(",");
		votesstr2 = $(value).find(".p_answersResults").val().split(",");
		totalVotes = $(value).find(".p_answersTotal").val();

		strtitle = "";
		count = 0;
		for(var i=0;i<votesstr2.length;i++){
			strtitle += "t"+votesstr2[i]+",000000,0,"+(i)+",13,1|";
		}
		strtitle = strtitle.substring(0, (strtitle.length - 1));

		back = "1";
		for(var y=1;y<c;y++){
			back+=",1";
		}

defaults.chs = "250x"+((c * 31)+30);


	jQuery('<img>').attr('src','http://chart.apis.google.com/chart?chs='+defaults.chs+'&chds=0,1&cht=bhs&chd=t:'+votesstr+'|'+back+'&chco=fda73a,dcdcdc&chbh=17,15&chm='+defaults.chm+'&chxl=0:|'+titles+'&chxt=y').appendTo($(barchart));

//&chtt='+totalVotes+' votes submitted
//jQuery('<img>').attr('src','http://chart.apis.google.com/chart?chs='+defaults.chs+'&cht=bhs&&chxl=0:|'+titles+'&chxt=y,x&chco=fda73a,dcdcdc&chd=t:'+votesstr+'|8,2&chm='+strtitle+"&chxr=0,0,50").appendTo($(barchart));

//chs='+defaults.chs+'&cht=bhs&chco=fda73a,dcdcdc&chd=t:'+votesstr+'|8,2&chm=ttest,000000,0,1,11|ttest2,000000,0,1,11'
//chs=300x150&cht=bhs&chco=3D7930,A2C180&chd=t:40,60|60,40&chm=tApril+mobile+hits,000000,0,0,13|tMay+mobile+hits,000000,0,1,13,1

			/*jQuery('<img>').attr('src', api.make({
				data: votes,
				type : 'bhs',
				size : '250x100',
	fillarea: 'N*p0*,000000,0,-1,11',
				axis_labels : titles,
				})).appendTo($(barchart));*/
		});
	}

	function initVoting(){
		$(".votebtn").click(function(){
		var pollId = $(this).parent().find(".vpollId").val();
		var qs = $(this).parent().find(".pollqs").val();
		var questionIds = qs.split(',');
	
		postdata = "qIds="+qs;

		for ( var i in questionIds )
		{
			if(questionIds[i]!=""){
				var radios = $("input[name='q"+questionIds[i]+"']");
				var val;
				if(radios.length>0){
					$.each(radios, function(index, value) { 
						if($(value).attr("checked")){
							val = $(value).val();
						}
					});

					postdata+="&q_"+questionIds[i]+"="+val;
				}else{
					// its a drop down list
					val = $("select[name='q"+questionIds[i]+"'] option:selected").val();
					postdata+="&q_"+questionIds[i]+"="+val;
				}
			}
		}

		$("#"+defaults.divId).html("<img src='/images/loader.gif' class='loader'/>");
			// add the votes...
			$.ajax({
				url: "/ajax/submitVote.php",
				data: postdata,
				method: "post",
				success: function(msg){
					// update the cookie...
					var cookie = readCookie("iiti_answered");
					if(cookie!=null){
						createCookie("iiti_answered", cookie+","+qs+","+pollId, 100);
					}else{
						createCookie("iiti_answered", qs, 100);
					}
					// show the bars
					$.ajax({
						url: "/ajax/votingmodule.php",
						data: "pollId="+defaults.id,
						success: function(msg){
							$("#"+defaults.divId).css("display", "none").html(msg).fadeIn(1000);
							initVoting();
							initBars();
						}
					});
				}
			})

		});
	}


	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
}



