﻿PublicoScripts.service = function() {
    var objOfReplie = "";
    var commentPageNumber = 1;
    var commentOrder = 0;
    var topSelected = "lidas";

    return {
        commentRoot: "",
        getReplies: function(id, thread, obj, th) {
            obj.onclick = null;
            objOfReplie = obj.parentNode.parentNode;
            PublicoWScript.Jornal.GetReplies(id, th, onSuccessReplies, onError);
            PublicoScripts.Methods.doPageCount("RESPOSTAS");
        },
        setVote: function(item, value) {
            PublicoWScript.Jornal.addVote(item, value);
            if (!isNaN(item)) {
                this.setCounter(articleID, 'JORNAL', 'VOTADO', cadernoName, articleTitle);
                PublicoScripts.Methods.doPageCount("VOTOS");
            }
        },
        sendComment: function() {
            var isOk2Go = true;
            var title = $get("txtTitle");
            var text = $get("txtText");
            var name = $get("txtName");
            var email = $get("txtEmail");
            var location = $get("txtLocation");

            PublicoScripts.Methods.checkValue(title, title.getAttribute("defaultValue"));
            PublicoScripts.Methods.checkValue(text, text.getAttribute("defaultValue"));
            PublicoScripts.Methods.checkValue(name, name.getAttribute("defaultValue"));
            PublicoScripts.Methods.checkValue(email, email.getAttribute("defaultValue"));
            PublicoScripts.Methods.checkValue(location, location.getAttribute("defaultValue"));

            if (!PublicoScripts.Methods.checkForInvalidValue(title)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(text)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(name)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(email)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(location)) isOk2Go = false;
            if (!PublicoScripts.Methods.isValidEmail(email)) isOk2Go = false;

            if (isOk2Go) {
                $get("bntSend").value = "Aguarde...";
                PublicoWScript.Jornal.sendComment($get("idNot").value,
                                $get("txtTitle").value,
                                $get("txtName").value,
                                $get("txtText").value,
                                $get("txtLocation").value,
                                $get("txtEmail").value,
                                $get("chkAnonimo").checked,
                                this.commentRoot, onSuccessComentSent, onError);
                PublicoScripts.Methods.doPageCount("COMENTARIO");
            }
        },
        getPage: function(num) {
            var id = $get("idNot");
            commentPageNumber += num;
            getComments(id.value, commentPageNumber, commentOrder);
        },
        getComments: function(id, p, o) {
            PublicoWScript.Jornal.GetComments(id, p, o, onSuccessComents, onError);
            PublicoScripts.Methods.doPageCount("COMENTARIOS");
        },
        getTopComment: function(o) {
            var id = $get("idNot");
            commentOrder = o;
            commentPageNumber = 1;
            PublicoScripts.service.getComments(id.value, 1, o);
            PublicoScripts.Methods.doPageCount("TOP_COMENTARIO");
        },
        setCounter: function(item, origin, type, category, title) {
            PublicoWScript.Jornal.setCounter(item, origin, type, category, title, document.location.href, null, onError);
        },
        getTop: function(tipo, obj) {
            $get(topSelected).className = "";
            obj.className = "on";
            topSelected = obj.id;
            $get("topLinks").innerHTML = "";
            PublicoWScript.Jornal.getTop(tipo, onSuccessTop, onError);
            PublicoScripts.Methods.doPageCount("TOPS");
        },
        doUserLogin: function() {
            var user = $get("txtUserName");
            var pass = $get("txtPassword");
            var writeCookie = $get("keepLogin").checked;
            var isOk2Go = true;

            if (!PublicoScripts.Methods.checkForInvalidValue(user)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(pass)) isOk2Go = false;

            if (isOk2Go) {
                try {
                    PublicoWScript.Jornal.doLogin(user.value, pass.value, writeCookie, onSuccessLogin, onErrorLogin);
                    PublicoScripts.Methods.doPageCount("LOGIN");
                } catch (e) {
                    document.forms[0].submit();
                }
            }
        },
        doUserLoginPoped: function () {
            var user = $get("subscriber-login-email");
            var pass = $get("subscriber-login-password");
            var writeCookie = $get("subscriber-login-remember").checked;
            var isOk2Go = true;

            if (!PublicoScripts.Methods.checkForInvalidValue(user)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(pass)) isOk2Go = false;

            if (isOk2Go) {
                try {
                    PublicoWScript.Jornal.doLogin(user.value, pass.value, writeCookie, onSuccessLoginPoped, onErrorLoginPoped);
                    PublicoScripts.Methods.doPageCount("LOGIN");
                } catch (e) {
                    document.forms[0].submit();
                }
            }
        },
        sendToMail: function(opt) {
            var isOk2Go = true;
            var nameFrom = $get("txtNomeFrom");
            var emailFrom = $get("txtEmailFrom");
            var comment = $get("txtTextFrom");
            if ($get("txtNomeTo")) {
                var nameTo = $get("txtNomeTo");
                var emailTo = $get("txtEmailTo");
            }

            PublicoScripts.Methods.checkValue(nameFrom, nameFrom.getAttribute("defaultValue"));
            PublicoScripts.Methods.checkValue(emailFrom, emailFrom.getAttribute("defaultValue"));
            if (nameTo) {
                PublicoScripts.Methods.checkValue(nameTo, nameTo.getAttribute("defaultValue"));
                PublicoScripts.Methods.checkValue(emailTo, emailTo.getAttribute("defaultValue"));
            }

            if (!PublicoScripts.Methods.checkForInvalidValue(nameFrom)) isOk2Go = false;
            if (!PublicoScripts.Methods.checkForInvalidValue(emailFrom)) isOk2Go = false;
            if (!PublicoScripts.Methods.isValidEmail(emailFrom)) isOk2Go = false;
            if (nameTo) {
                if (!PublicoScripts.Methods.checkForInvalidValue(nameTo)) isOk2Go = false;
                if (!PublicoScripts.Methods.checkForInvalidValue(emailTo)) isOk2Go = false;
                if (!PublicoScripts.Methods.isValidEmail(emailTo)) isOk2Go = false;
            }

            if (isOk2Go) {
                $get("btnSendMail").value = "Aguarde...";
                $get("btnSendMail").disabled = true;
                var link = "";
                try {
                    link = $get("linkName").value;
                } catch (e) { link = ""; }
                if (nameTo)
                    PublicoWScript.Jornal.sendToMail(opt, nameFrom.value, emailFrom.value, nameTo.value, emailTo.value, comment.value, link, onSuccessSend, onError)
                else
                    PublicoWScript.Jornal.sendToMail(opt, nameFrom.value, emailFrom.value, "", "", comment.value, link, onSuccessSend, onError)
                PublicoScripts.Methods.doPageCount("MAIL");
            }
        },
        getStats: function(nid) {
            if ($get("statsBox").innerHTML == "") {
                if (!isNaN(nid)) {
                    PublicoScripts.Methods.doPageCount("ESTATISTICAS");
                    PublicoWScript.Jornal.getNewsStats(nid, onSuccessStats, onError);
                }
            }
        },
        logOff: function() {
            document.cookie = "myPublico=expires=Fri, 01 Jan 1900 02:47:11 UTC;";
            PublicoWScript.Jornal.logOut(null, onError);
            document.location = document.location.href;
        }
    }

    function onSuccessReplies(result) {
        if (result != "") {
            PublicoScripts.Methods.insertAfter(objOfReplie, result);
            PublicoScripts.Methods.setStars();
        }
    }

    function onSuccessComentSent(result) {
        if (result) {
            $get("bntSend").value = "Enviado";
            $get("bntSend").disabled = true;
            if (parseInt($get("idNot").value) > 0)
                PublicoScripts.service.setCounter($get("idNot").value, 'JORNAL', 'COMENTADO', $get("ctl00_ctl00_ContentPlaceHolder1_NewsDetail_Caderno").innerHTML, $get("ctl00_ctl00_ContentPlaceHolder1_NewsDetail_newsTitle").innerHTML);
        } else {
            $get("errMsg").innerHTML = "Erro ao enviar comentário!";
        }
    }

    function onSuccessComents(results) {
        if (results) {
            try { $get("lnkPrev").style.display = results.hasPrevious == true ? 'inline' : 'none'; } catch (e) { }
            try { $get("lnkNext").style.display = results.hasMore == true ? 'inline' : 'none'; } catch (e) { }
            $get("Comments").innerHTML = results.html;
            PublicoScripts.Methods.setStars();
        }
    }

    function onSuccessTop(results) {
        if (results) {
            $get("topLinks").innerHTML = results;
        }
    }
    function onError(err) {
        //alert(err.get_message());
    }
    function onSuccessLogin(results) {
        if (results == "")
            document.location.href = document.location.href;
        else {
            PublicoScripts.Methods.writeError("errMsgLogin", results);
        }
    }
    function onSuccessLoginPoped(results) {
        if (results == "")
            document.location.href = document.location.href;
        else {
            PublicoScripts.Methods.writeError("errMsgLoginPoped", results);
        }
    }
    
    function onErrorLogin(err) {
        PublicoScripts.Methods.writeError("errMsgLoginPoped", err.get_message());
    }

    function onErrorLoginPoped(err) {
        PublicoScripts.Methods.writeError("errMsgLogin", err.get_message());
    }

    function onSuccessSend(results) {
        if (results) {
            try { PublicoScripts.service.setCounter(articleID, 'JORNAL', 'ENVIADO', cadernoName, articleTitle); } catch (e) { }
            PublicoScripts.Popup.closePopup();
            return false;
        } else {
            alert("Não foi possível enviar o email.\nPor favor tente de novo!");
            $get("btnSendMail").value = "Enviar";
            $get("btnSendMail").disabled = false;
        }
    }
    function onSuccessStats(results) {
        if (results) {
            $get("statsBox").innerHTML = "<b>" + results.nReader + " pessoas</b> leram este texto<br /><b>" + results.nPrint + " pessoas</b> imprimiram este texto<br /><b>" + results.nSend + " pessoas</b> enviaram este texto a um amigo<br /><b>" + results.nComment + " pessoas</b> comentaram este texto";
        } else {
            $get("statsBox").innerHTML = "<b>0 pessoas</b> leram este texto<br /><b>0 pessoas</b> imprimiram este texto<br /><b>0 pessoas</b> enviaram este texto a um amigo<br /><b>0 pessoas</b> comentaram este texto";
        }
    }
} ();
