﻿PublicoScripts.Methods =
{
    Right: function (str, n) {
        if (n <= 0)
            return "";
        else if (n > String(str).length)
            return str;
        else {
            var iLen = String(str).length;
            return String(str).substring(iLen, iLen - n);
        }
    },
    insertAfter: function (referenceNode, newNode) {
        var myElement = document.createElement('div');
        myElement.innerHTML = newNode;
        try {
            referenceNode.parentNode.parentNode.insertBefore(myElement, referenceNode.parentNode.nextSibling);
        } catch (e) {
            //alert("1: " + e.message);
        }
    },
    setStars: function () {
        var imgElements = document.getElementsByTagName("img")
        for (iCount = 0; iCount < imgElements.length; iCount++) {
            if (imgElements[iCount].className != 'star') continue;

            imgElements[iCount].onmouseover = function () {
                this.style.cursor = 'pointer';
                var maxVal = parseInt(this.getAttribute("value"));
                var starElements = document.getElementsByName(this.name);
                for (iSum = 0; iSum < starElements.length; iSum++) {
                    if (parseInt(starElements[iSum].getAttribute("value")) <= maxVal) {
                        starElements[iSum].src = '/img/starOn.png';
                    }
                }
            }

            imgElements[iCount].onmouseout = function () {
                var starElements = document.getElementsByName(this.name);
                for (iSum = 0; iSum < starElements.length; iSum++) {
                    starElements[iSum].src = '/img/starOff.png';
                }
            }

            imgElements[iCount].onclick = function () {
                var starElements = document.getElementsByName(this.name);
                for (iSum = 0; iSum < starElements.length; iSum++) {
                    starElements[iSum].onmouseover = function () {
                        this.style.cursor = '';
                    };
                    starElements[iSum].onmouseout = null;
                    starElements[iSum].onclick = null;
                }
                PublicoScripts.service.setVote(this.getAttribute("item"), this.getAttribute("value"));
            }
        }
    },
    setMaxFields: function () {
        var tElements = document.getElementsByTagName("textarea");
        for (iCount = 0; iCount < tElements.length; iCount++) {
            if (tElements[iCount].className != 'full') continue;
            if (tElements[iCount].getAttribute("maxlength")) {
                tElements[iCount].onkeypress = function () {
                    var mLength = parseInt(this.getAttribute("maxlength"));
                    if (this.value.length > mLength) {
                        this.value = String(this.value).substring(0, mLength);
                        //return false;
                    }
                    $get("rText").innerHTML = "Restam " + (mLength - this.value.length) + " caracteres";
                }
            }
        }
    },
    checkValue: function (obj, value) {
        obj.style.backgroundColor = '#FFFFFF';
        if (obj.value == value)
            obj.value = "";
    },
    cancelReply: function () {
        $get("txtTitle").value = "O seu título";
        $get("txtTitle").disabled = false;
        PublicoScripts.service.commentRoot = "";
        $get("bntCancel").style.display = 'none';
        this.setAside($get("bntSend"), 0, 0);
    },
    setAside: function (obj, IEspace, NSspace) {
        if (document.all)
            obj.style.marginRight = IEspace + "px";
        else
            obj.style.marginRight = NSspace + "px";
    },
    isValidEmail: function (obj) {
        var rules = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (!rules.test(obj.value)) {
            obj.style.backgroundColor = '#D0AFAE';
            return false;
        }
        return true;
    },
    checkForInvalidValue: function (obj) {
        var myReg = /^\s+|\s+$/i;
        obj.value = obj.value.replace(myReg, "");
        if (obj.value == "") {
            obj.style.backgroundColor = '#D0AFAE';
            return false;
        }
        return true;
    },
    changeSize: function (sz) {
        var dElements = $get("text");
        var fSize = String(parseInt(dElements.style.fontSize)) == "NaN" ? 12 : parseInt(dElements.style.fontSize);
        dElements.style.fontSize = (fSize + sz) + "px";
    },
    replieToComment: function (title, id) {
        $get("txtTitle").value = "RE: " + title;
        $get("txtTitle").disabled = true;
        PublicoScripts.service.commentRoot = id;
        PublicoScripts.Methods.setAside($get("bntSend"), 65, 54);
        $get("bntCancel").style.display = 'inline';
        document.location = '#comente';
    },
    initiateEnvironment: function () {
        PublicoScripts.Methods.setStars();
        PublicoScripts.Methods.setMaxFields();
    },
    eventPush: function (obj, event, handler) {
        if (obj.addEventListener) {
            obj.addEventListener(event, handler, false);
        } else if (obj.attachEvent) {
            obj.attachEvent('on' + event, handler);
        }
    },
    setClosedLinks: function () {
        var closedElements = new Array("h1", "h2", "h3", "div");
        for (iCount = 0; iCount < closedElements.length; iCount++) {
            var mElements = document.getElementsByTagName(closedElements[iCount])
            for (iElement = 0; iElement < mElements.length; iElement++) {
                if (mElements[iElement].className != 'locked' && mElements[iElement].className != 'news locked') continue;
                mElements[iElement].onclick = function () {
                    $("#publico-login-popup-trigger").click();
                    //PublicoScripts.Popup.createHTMLContent('/pages/login.aspx', 570, 350);
                    return false;
                }
            }
        }
    },
    writeError: function (obj, text) {
        if ($get(obj)) {
            $get(obj).innerHTML = text;
            $get(obj).style.display = "block";
        }
    },
    doPageCount: function (section) {
        try {
            w_counter = new wreport_counter(WRP_SECTION, section, WRP_ID, WRP_ACC, WRP_CHANNEL);
            w_counter.add_content(section + " content");
            w_counter.count();
        } catch (e) { }
    },
    getPub: function (pos) {
        switch (pos) {
            case (1):
                try { Comunique.tag("PublicoJornal10x10");  } catch (e) { }
                break;
            default:
        }
    }
}

var fbBackup;

function PopNews(page, name, myWidth, myHeight, x, y) {
    popWind = window.open(page, name, "width=" + myWidth + ",height=" + myHeight + ",left=" + x + ",top=" + y + ",screenX=" + x + ",screenY=" + y + ",toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=no");
    if (popWind.opener == null) {
        popWind.opener = window;
    }
    else {
        popWind.focus();
    }
    window.opener = popWind;
}

function showLabels() {
    $("#subscriber-pane-holder").empty();
    $(".subscriber-login-email").inputLabel("Email");
    $(".subscriber-login-password").inputLabel("Palavra-chave");
}

function copyContent() {
    fbBackup = $("#subscriber-pane-holder").html();
}

function restoreContent() {
    $("#subscriber-pane-holder").html(fbBackup);
}
