String.prototype.trim = function() {
    var a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

var QuickPoll = function(urlArg, pollarg, containerIdArg,
                         submitBtnSrcArg, resultsBtnSrcArg,
                         pollFullRateBarSrcArg, pollEmptyRateBarSrcArg) {

    this.url = urlArg;
    this.poll = pollarg;
    this.containerId = containerIdArg;
    this.pollFullRateBarSrc = pollFullRateBarSrcArg;
    this.pollEmptyRateBarSrc = pollEmptyRateBarSrcArg;
    this.mainDiv = null;
    this.mainPollContainer = document.createElement("div");
    this.mainPollContainer.className = "quick_poll";
    this.mainPollContainer.id = "poll_" + this.poll.id;
    this.choicesDivs = [];
    this.answersDiv = [];

    this.captionContainer = document.createElement("div");
    this.captionContainer.className = "caption scheme_1";

    this.contentContainer = document.createElement("div");
    this.contentContainer.className = "border";

    this.questionContainer = document.createElement("div");
    this.questionContainer.className = "title";
    this.questionContainer.innerHTML = this.poll.description;

    this.answersContainer = document.createElement("div");
    this.answersContainer.className = "answerContainer";

    this.linetop = document.createElement("div");
    this.linebottom = document.createElement("div");
    this.linetop.className = "line";
    this.linebottom.className = "line";

    this.submitBtnSrc = submitBtnSrcArg;
    this.resultsBtnSrc = resultsBtnSrcArg;

    this.divButtons = document.createElement("div");
    this.divButtons.className = "poll_buttons";
    this.radios = [];
    var selfLink = this;
    this.submitButton = document.createElement("input");
    this.submitButton.type = "image";
    this.submitButton.src = this.submitBtnSrc;
    this.submitButton.id = "submitbtn";
    this.submitButton.className = "submitButton";

    this.submitButton.onclick = function() {
        var choiceId = 0;
        for(var index in selfLink.radios) {
           if(selfLink.radios[index].checked) {
               choiceId = selfLink.radios[index].value;
           }
        }

        if(choiceId != 0) {
           $("#" + selfLink.mainPollContainer.id).fadeTo("fast", 0.33);
           var callback = function(poll, textStatus) {
               selfLink.poll = poll;
               selfLink.outPutResults();
               $("#" + selfLink.mainPollContainer.id).fadeTo("fast", 1);
           }

           jQuery.post(selfLink.url, {"method" : "vote", "choiceId" : choiceId}, callback, "json");
        } else {
            
        }
    }

    this.resultsButton = document.createElement("input");
    this.resultsButton.type = "image";
    this.resultsButton.src = this.resultsBtnSrc;
    this.resultsButton.className = "resultButton";
    this.resultsButton.id = "resultbtn";

    this.resultsButton.onclick = function(event) {
        $("#" + selfLink.mainPollContainer.id).fadeTo("fast", 0.33);
        selfLink.outPutResults();
        $("#" + selfLink.mainPollContainer.id).fadeTo("fast", 1);
        /*jQuery.post(selfLink.url,
                {"method" : "getPoll", "pollId" : selfLink.poll.id},
                function(poll, textStatus) {
                    selfLink.poll = poll;
                    $("#" + selfLink.mainPollContainer.id).fadeTo("fast", 1);
                }, "json");*/
    }

    this.divButtons.appendChild(this.submitButton);
    this.divButtons.appendChild(this.resultsButton);
                    
    this.contentContainer.appendChild(this.questionContainer);
    this.contentContainer.appendChild(this.answersContainer);
    this.contentContainer.appendChild(this.divButtons);
    this.init();
};

QuickPoll.prototype = {

    init : function() {
        this.mainDiv = document.getElementById(this.containerId);
        this.mainPollContainer.appendChild(this.captionContainer);
        this.mainPollContainer.appendChild(this.contentContainer);

        var cookiestring = window.document.cookie.split(";");
        var cookieValue;

        for(var cookieIndex in cookiestring) {
            var tmp = cookiestring[cookieIndex].split("=");
            var key = tmp[0].trim();
            if (key == "polls") {
                cookieValue = tmp[1].trim();
                break;
            }
        }

        var contains = false;

        if(cookieValue) {
            cookieValue = cookieValue.replace(/^\"/, '');
            cookieValue = cookieValue.replace(/\"$/, '');
            
            var pollsIds = cookieValue.split(/,/);
            for(var pollInd in pollsIds) {
                var t = pollsIds[pollInd].trim()
                var idNum = parseInt(t);
                if(idNum == this.poll.id) {
                    contains = true;
                    break;
                }
            }
        }

        this.outputFrameTable();
        if(contains) {
            this.outPutResults();
        } else {
            this.outPutForm();
        }

        this.mainDiv.appendChild(this.mainPollContainer);
    },

    getTotalVoices : function() {
        var total = 0;
        for (var index in this.poll.choices) {            
            var choiceLoc = this.poll.choices[index];
            total += choiceLoc.voices;
        }
        return total;
    },

    outPutForm : function() {
        for (var index in this.poll.choices) {
            var choice = this.poll.choices[index];
            if(navigator.userAgent.indexOf("MSIE") != -1) {
                try{
                    this.radios[index] = document.createElement("<input type=\"radio\" value=\"" + choice.id + "\" name = \"choice" + this.poll.id + "\"/>");                    
                } catch(e) {
                    alert(e.message);
                }
            } else {
                this.radios[index] = document.createElement("input");
                this.radios[index].type = "radio";
                this.radios[index].value = choice.id;
                this.radios[index].name = "choice" + this.poll.id;
            }

            this.choicesDivs[choice.id].appendChild(this.radios[index]);
        }
    },

    outputFrameTable : function() {
        this.captionContainer.innerHTML = "Quick Poll";
        this.answersContainer.appendChild(this.linetop);
        for (var index in this.poll.choices) {
            var choice = this.poll.choices[index];

            this.answersDiv[choice.id] = document.createElement("div");
            this.answersDiv[choice.id].innerHTML = choice.name;
            this.answersDiv[choice.id].className = "answer";

            this.choicesDivs[choice.id] = document.createElement("div");
            this.choicesDivs[choice.id].className = "choice";

            this.answersContainer.appendChild(this.answersDiv[choice.id]);
            var clearingDiv = document.createElement("div");
            clearingDiv.className = "clearing";
            this.answersContainer.appendChild(this.choicesDivs[choice.id]);
            this.answersContainer.appendChild(clearingDiv);
        }
        
        this.answersContainer.appendChild(this.linebottom);
        this.divButtons.appendChild(this.submitButton);
        this.divButtons.appendChild(this.resultsButton);
    },

    outPutResults : function() {
        var totalVoices = this.getTotalVoices();
        this.captionContainer.innerHTML = "Poll Results";

        for (var index in this.poll.choices) {
            var choice = this.poll.choices[index];
            this.choicesDivs[choice.id].innerHTML = "";
            this.answersDiv[choice.id].className = "answerResult";
            var mainAnswerDiv = document.createElement("div");
            mainAnswerDiv.className = "globalAnswer";

            var innerDiv = document.createElement("div");
            innerDiv.className = "innerDiv";
            var bars = Math.ceil(choice.voices * 15 / totalVoices);

            for (var i = 1; i < 16; i++) {
                var itemDiv = document.createElement("div");
                var bar = document.createElement("img");
                if (i <= bars) {
                    bar.src = this.pollFullRateBarSrc;
                    itemDiv.className="darkDiv";
                } else {
                    bar.src = this.pollEmptyRateBarSrc;
                    itemDiv.className="lightDiv";
                }

                bar.width = 6;
                bar.height = 10;
                itemDiv.appendChild(bar);
                innerDiv.appendChild(itemDiv);
            }
            mainAnswerDiv.appendChild(innerDiv);
            var divEmpty = document.createElement("div");
            divEmpty.className = "emptyDiv";
            mainAnswerDiv.appendChild(divEmpty);
            var divTotal = document.createElement("div");
            divTotal.className = "totalDiv";
            divTotal.innerHTML = choice.voices;
            mainAnswerDiv.appendChild(divTotal);
            this.divButtons.innerHTML = "Total votes: " + totalVoices;
            this.choicesDivs[choice.id].appendChild(mainAnswerDiv);
        }
    }
}

var PollBlock = function(urlArg, quickpollContainer,  submitBtnSrcArg, resultsBtnSrcArg,
                         pollFullRateBarSrcArg, pollEmptyRateBarSrcArg) {
    this.quickpollContainerId = quickpollContainer;
    var selfLink = this;
    var callback = function(polls, textStatus) {
        for(var index in polls) {
            new QuickPoll(urlArg, polls[index], selfLink.quickpollContainerId,
                    submitBtnSrcArg, resultsBtnSrcArg,
                    pollFullRateBarSrcArg, pollEmptyRateBarSrcArg);
        }
    };

    jQuery.post(urlArg, {"method" : "getAllPolls"}, callback, "json");    
}