(function ($) {


    //extend jquery with the plugin
    $.fn.extend({
        jPoll: function (config) {

            //use defaults or properties supplied by user
            config = $.extend({}, $.jPoll.defaults, config);

            //init widget
            if (config.pollImage != "" && config.pollImagePos == "top") {
                $("<img src='" + config.pollImage + "' class='poll-main-image'>").appendTo($(this));
            }
            $("<h2>").text(config.pollHeading).appendTo($(this));

            if (config.pollImage != "" && config.pollImagePos == "middle") {
                $("<img src='" + config.pollImage + "' class='poll-main-image'>").appendTo($(this));
            }

            $("<form>").attr({
                id: "pollForm",
                action: config.ajaxOpts.url
            }).appendTo($(this));
            for (var x = 0; x < config.groupIDs.length; x++) {
                $("<div>").addClass(config.rowClass).appendTo($(this).find("form"));
                $("<input type='radio' name='" + config.groupName + "' id='" + config.groupIDs[x] + "'>").addClass("choice").appendTo($(this).find("form").children(":last")).click(function () {
                    ($(".error").length != 0) ? $(".error").slideUp("slow") : null;
                });
                $("<label>").text(config.groupIDs[x]).attr("for", config.groupIDs[x]).appendTo($(this).find("form").children(":last"));
                if (config.showImagesOnOption) {
                    $("<img>").attr("src", config.images[x]).appendTo($(this).find("form").children(":last"));
                }
            }

            if (config.pollImage != "" && config.pollImagePos == "bottom") {
                $("<img src='" + config.pollImage + "' class='poll-main-image' >").appendTo($(this));
            }

            $("<div>").attr("id", "buttonRow").addClass(config.rowClass).appendTo($(this).find("form"));
            $("<button type='submit'>").text("Vote!").appendTo("#buttonRow").click(function (e) {
                e.preventDefault();

                //record which radio was selected
                var selected;
                $(".choice").each(function () {
                    ($(this).attr("checked") == true) ? selected = $(this).attr("id") : null;
                });

                //print message if no radio selected and errors enabled
                if (config.errors == true) {
                    (selected == null && $(".error").length == 0) ? $("<p>").addClass("error").text("Please make a selection!").css({ display: "none" }).insertAfter("#pollForm").slideDown("slow") : null;
                }

                //add additional request options
                var addOpts = {
                    type: "get",
                    data: "&choice=" + selected + "&v=" + new Date().getTime(),
                    dataType: "json",
                    success: function (result) {
                        SaveVoteInCookie(result.CookieName);

                        $("#pollContainer").html(result.Results);

                    }
                };
                //merge ajaxOpts widget properties and additional options objects
                ajaxOpts = $.extend({}, addOpts, config.ajaxOpts);

                //make request if radio selected
                return (selected == null) ? false : $.ajax(ajaxOpts);
            });

            //return the jquery object for chaining
            return this;
        }
    });
})(jQuery);

function SaveVoteInCookie(cookieName) {
    if (document.cookie) {
        index = document.cookie.indexOf(cookieName);
    } else {
        index = -1;
    }

    if (index == -1) {
        document.cookie = cookieName + "=1; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
    } else {
        countbegin = (document.cookie.indexOf("=", index) + 1);
        countend = document.cookie.indexOf(";", index);
        if (countend == -1) {
            countend = document.cookie.length;
        }
        count = eval(document.cookie.substring(countbegin, countend)) + 1;
        document.cookie = cookieName + "=" + count + "; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
    }
}

