(function($){
    $.fn.scLightPollBuilder=function(options) {
    
        var defaults = {
			adtext:null,
			adlink:null,
			scriptUrl:"http://site.myoutwardhound.com/poll/"
		}
		
		var settings = $.extend({},defaults,options); 
		
		if (settings.scriptUrl==null)
		    return null;

        function scLightPollBuilderClass() {
		
		    this.question=null;
		    this.answers=null;
		    this.submitOnce=false;
    		
		    this.createScriptTag=function(url) {
               var old=$("#scPollScriptWorker");
               if (old.length>0) {
                 $(old).remove();
               }
               var script = document.createElement('script');
               script.id = 'scPollScriptWorker';
               script.type = 'text/javascript';
               script.src = url;
               var heads=document.getElementsByTagName("head");
               if (heads && heads[0]) heads[0].appendChild(script);
		    }
    		
            this.initialize=function() {
                $("#scPollmain").css("display","block");
                this.getPoll();
            }
        
            this.setPollData=function(question, ans) {
                this.question=question;
                this.answers=new Array();
                for (var i=0;i<ans.length;i++) {
                    this.answers[i]=new Array();
                    this.answers[i][0]=ans[i];
                    this.answers[i][1]=0;
                }
            }
            
            this.setPollResultData=function (results) {
                for (var i=0;i<results.length;i++) {
                    this.answers[i][1]=results[i];
                }
            }
            
            this.getPoll=function () {
                //if (!this.inProcess) this.createScriptTag(settings.scriptUrl + "getPoll.php");
				if (!this.inProcess) this.createScriptTag(settings.scriptUrl + "sc-pollMain.php?pollcall=getpoll");
            }
            
            this.getPollResults=function () {
               // if (!this.inProcess) this.createScriptTag(settings.scriptUrl + "getPollResults.php");
			   if (!this.inProcess) this.createScriptTag(settings.scriptUrl + "sc-pollMain.php?pollcall=getresults");
			   
            }
            
            this.submitPollForm=function(index) {
            	//if (!this.inProcess) this.createScriptTag(settings.scriptUrl + "processPoll.php?pollcall=getprocess&pollanswer=" + index);
				if (!this.inProcess) this.createScriptTag(settings.scriptUrl + "sc-pollMain.php?pollcall=getprocess&pollanswer=" + index);
            }
            
            this.renderPoll=function () {
                var question = this.question;
			    // show question
			    $("<h3 />").html(question).appendTo("#scPoll");
			    // create answer holder
			    $("<ul />").appendTo("#scPoll");
			    // create answers and radios
			    for (var i=0;i<this.answers.length;i++) {
				    var useval = this.answers[i][0];
				    $("<li />").append($("<input type='radio' />").attr({"value": useval,"name":"pollanswer"})).append(" "+useval).appendTo("#scPoll ul");
			    };
			    $("<div id='scPollSubmit' />").append(
				    $("<input type='submit' />").attr({"value":"Submit"}).click(function(e){
					    if (scLightPollBuilder && !scLightPollBuilder.submitOnce) {
					        var objIndex = $('#scPoll ul li input:radio:checked').parent().index();
					        scLightPollBuilder.submitOnce=true;
				            scLightPollBuilder.submitPollForm(objIndex);
					    }
				    })
			    ).appendTo("#scPoll");
            }
            
            this.renderPollResult=function() {
                var resTotal = 0;
			    var finaltext=null;
			    $("<div />").attr("id","results").appendTo("#scPollbody");
    			
			    for (var i=0;i<this.answers.length;i++) {
			        resTotal += this.answers[i][1];
			    }
    			
			    $.each($("#scPoll ul li"),function(i){
                    if (scLightPollBuilder) {
                        $(this).addClass("result");
				        $(this).find("input:radio").remove();
				        var singRes = Math.round(scLightPollBuilder.answers[i][1]/resTotal*100);
				        $(this).append($("<span />").text(" ("+singRes+"%) ")).append($("<div />").animate({"width":singRes*1.5+"px"},400));
			        }
			    });
    			

			    if (settings.adlink) {
			        finaltext = $("<a class='ad' />").attr("href",settings.adlink).text(settings.adtext);
			    } else {
			        if (settings.adtext) {
			            finaltext = $("<span class='ad' />").text(settings.adtext);
			        }
			    }
    			
			    if(finaltext) {
				    $("div#scPollSubmit").append(finaltext);
			    }
    			
			    $("div#scPollSubmit").text(resTotal+" total votes").find("input").fadeOut(300);
            }    
		
		};
		
		return new scLightPollBuilderClass();
		
    }
    
}
)(jQuery);

var scLightPollBuilder=jQuery("#scPollmain").scLightPollBuilder(window.scPollBuilderOptions);

jQuery(function() {
    if (scLightPollBuilder) 
        scLightPollBuilder.initialize();
    }
);
