/* opt/settings - tablica asocjacyjna, opcje wyświetlania

 hideMainTitle - ustaw na true, aby ukryć tytuł głosowania
 hideMainCount - ustaw na true, aby ukryć pole z sumą wszystkich głosów
 hideMainGenerationDate - ustaw na true, aby ukryć
 hideCandidateNumber - ustaw na true, aby ukryć nawias, z numerem kandadata
 hidePercent - ustaw na true, aby ukryć procent głosów oddanych na kandydata
 hideCount - ustaw na true, aby ukryć liczbę głosów oddanych na kandydata
 hideSmsNumber - ustaw na true, aby ukryć numer telefonu, pod króry wysyłamy SMS
 hideSmsContent - ustaw na true, aby ukryć treść SMSa do wysłania
 hideBar - ustaw na true, aby ukryć pasek obrazujący oddaną liczbę głosów
 pollBarImgSrc - ścieżka do obrazka z paskiem głosowania, tło pod paskiem zdefiowane jest w pliku CSS
*/
$.fn.smsPoll = function(url, opt) {
    var el=$(this);
    el.pollPreloader(true);

    $.getJSON(url+"&callback=?",function(data){
        if (data != null) {
            var settings = {
                type: "horizontal",
                hideMainTitle: false,
                hideMainCount: false,
                hideMainGenerationDate: false,
                hideCandidateNumber: false,
                hidePercent: false,
                hideCount: false,
                hideSmsNumber: false,
                hideSmsContent: false,
                hideBar: false,
                pollBarImgSrc: "/images/poll_bar.jpg"
            };
 
            // dodaj do tablicy "settings" opcje domyśle, jeśli ich nie zdefiniowano
            jQuery.extend(settings, opt);

            var poll="";

            if (!settings.hideMainTitle) {
                poll += "<h3>" + data.name + "</h3>";
            }

            if (data.options != null) {
                poll += "<ul>";
                if (settings.type == "vertical") {
                    poll += "<li id=\"caption\">Nr kandydata</li>";
                }
                $.each(data.options, function(i,option) {
                    // nazwy tych pól są różne dla panoramix i mobiltek
                    var optionText = "";
                    if (url.indexOf("mobiltek") > 0) {
                        optionText = option.number;
                    } else { // panoramix i inne zawierają pola <text>
                        optionText = option.text;
                    }

                    if (settings.type == "horizontal") {
                        poll += "<li>";
                        poll += "<p>";
                        poll += "<strong>" + option.name + "</strong>";
                        if (!settings.hideCandidateNumber) {
                            poll += " (nr " + optionText + ")";
                        }
                        poll += "</p>";

                        if (!settings.hideBar) {
                            if (option.width > 0) {
                            poll += "<img src=\"" + settings.pollBarImgSrc + "\" width=\"" + option.width + "\" alt=\"sonda\" /><br />"; // height obazka zaszyte w stylach
                            }
                        }
                        poll += "<span>";
                        if (!settings.hidePercent) {
                            poll += "<strong>" + option.percent + "%" + "</strong>, ";
                        }
                        if (!settings.hideCount) {
                            poll += option.votes + " głos";
                            // dostaw końcówkę, aby utworzyć "głos", "głosy" lub "głosów"
                            poll += getPolishNumeralEnding(parseInt(option.votes));
                            poll += ", "; 
                        }
                        poll += "<span>";
                        if (!settings.hideSmsNumber || !settings.hideSmsContent) {
                            poll += "SMS" ;
                            if (!settings.hideSmsNumber) {
                                poll += " pod nr <strong>" + data.premiumNumber + "</strong>";
                            }
                            if (!settings.hideSmsContent) {
                                poll += " o treści <strong>" + data.prefix + '.' + optionText + "</strong>";
                            }
                        }
                        poll += "</span></span>";
                        poll += "</li>";
                    }
                    else if (settings.type == "vertical") {
                        poll += "<li id=\"item" + i + "\" style=\"height: " + option.width + "px\">";
                        poll += "<span style=\"height: " + option.width + "px\">" + option.percent + "%</span>";
                        poll += "<p>" + option.text + "</p>";
                        poll += "<div>" + option.name + "<br/>" + option.votes + " głosów</div>";
                        poll += "</li>";
                    }
                });
                poll += "</ul>";
            }

            if (!settings.hideMainCount || !settings.hideMainGenerationDate) {
                poll += "<p>";
                if (!settings.hideMainGenerationDate) {
                    if (data.generationDate != null) {
                        poll += "Czas wygenerowania danych: " + data.generationDate + "<br />";
                    }
                }
                if (!settings.hideMainCount) {
                    if (data.votes != null) {
                        poll += "<span>Liczba wszystkich głosów: " + data.votes + "</span>";
                    }
                }
                poll += "</p>";
            }
            poll = utf2iso(poll);
            el.append(poll);
        };
        el.pollPreloader(false);
    });
};

// dostaw końcówkę, aby utworzyć odmianę słowa, np.: "głos", "głosy" lub "głosów"
function getPolishNumeralEnding(numeral)
{
    var m = numeral % 10;
    var mm = numeral % 100;
    if (numeral == 1) { 
        // 1 głos
        return "";
    }
    else if (mm >= 11 && mm <= 19) {
        // ...naście głosów
        return "ów";
    } else {
       // reszta regularnie
        if (m >= 2 && m <= 4) {
            return "y";
        } else {
            return "ów";
        }
    }
};

$.fn.pollPreloader = function(show) {
    if (show == true) {
        $container = $(this);
        $container.append("<div id=\"preloader\"></div>");
        $preloader = $(this).find('#preloader');
        // dodaj preloader
        var preloaderImageSize = 32;
        var w = parseInt($container.css("width"));
        var x = w/2 - preloaderImageSize/2;
        var y = w/2 - preloaderImageSize/2;
        $preloader.css('float', 'left');
        $preloader.css('width', w+'px');
        $preloader.css('margin', '0');
        $preloader.css('padding', '10px 0');
        $preloader.css('text-align', 'center');
        $preloader.html("<img src=\"/images/preloader.gif\" width=\""+preloaderImageSize+"\" height=\""+preloaderImageSize+"\" alt=\"loading\" />");
    } else {
        // usuń preloader
        $(this).find('#preloader').remove();
    }
};
