﻿function InitFontSize() {
    if (readCookie("DLPOFontSize") != null) {
        if (document.getElementById("ContainerDiv") != null) {
                      
            var el = document.getElementById("ContainerDiv"); 
            
            for (i = 0; i < el.childNodes.length; i++) {
                var innerEl = el.childNodes[i];
                SetNodeSize(innerEl, 0, true);
            }
        }
    }
    
}

function ChangeFontSize(step) {
    if (document.getElementById("ContainerDiv") != null) {
        step = parseInt(step, 10);
        
        var el = document.getElementById("ContainerDiv"); 
        
        for (i = 0; i < el.childNodes.length; i++) {
            var innerEl = el.childNodes[i];
            SetNodeSize(innerEl, step, false);
        }
    }
}

function SetNodeSize(elem, step, isInit) {
    if (elem != null && elem.style != null && elem.tagName != "INPUT") {
        if (isInit) {
            var curFont = parseInt(elem.style.fontSize, 10);
            if (isNaN(curFont))
                var fsize = readCookie("DLPOFontSize");
                if (fsize != null) {
                    elem.style.fontSize = fsize + "px";
                }
            if (elem.hasChildNodes)
                SetNodeSize(elem.firstChild, step, isInit);
                
        } else {
            var curFont = parseInt(elem.style.fontSize, 10);
            if (isNaN(curFont)) {
                elem.style.fontSize = (11 + step) + 'px';
                createCookie("DLPOFontSize", 11 + step, 30);
            } else {
                if ((curFont + step) < 18 && (curFont + step) > 8) {
                    elem.style.fontSize = (curFont + step) + 'px';
                    createCookie("DLPOFontSize", curFont + step, 30);
                }
            }
           
            if (elem.hasChildNodes)
                SetNodeSize(elem.firstChild, step, isInit);
        }
        
    }
}



// Código em: http://www.quirksmode.org/js/cookies.html
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}



//código em http://labnol.blogspot.com/2006/01/add-to-favorites-ie-bookmark-firefox.html
function AddBookmark() 
{
    title = "Dicionário Priberam da Língua Portuguesa";
    url = "http://www.priberam.pt/dlpo/";

    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}

// código de http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	    do {
	        curleft += obj.offsetLeft;
	        curtop += obj.offsetTop;
	    } while (obj = obj.offsetParent);
	}
    return [curleft,curtop];
}

// Código de http://www.dynamic-tools.net/toolbox/copyToClipboard/
function copyToClipboard(s)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
    } 
    else 
    {
        var clipboarddiv = document.getElementById('divclipboardswf');
        if (clipboarddiv == null) {
            clipboarddiv = document.createElement('div');
            clipboarddiv.setAttribute("name", "divclipboardswf");
            clipboarddiv.setAttribute("id", "divclipboardswf");
            document.body.appendChild(clipboarddiv);
        }
        clipboarddiv.innerHTML = '<embed src="clipboard.swf" FlashVars="clipboard=' +
        encodeURIComponent(text) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    }
    //alert('A ligação foi copiada para o Clipboard');
    return false;  
}


function LinkThis(LinkURL) {
    var elemImg = document.getElementById("LinkThis");
    var elemDiv = document.getElementById("linkDiv");

    //window.alert(findPos(elemImg)[0] + ", " + findPos(elemImg)[1] + " - " + LinkURL);
    elemDiv.style.display = 'block';
    elemDiv.style.position = 'absolute';
    elemDiv.style.left = findPos(elemImg)[0] + "px";
    elemDiv.style.top = (findPos(elemImg)[1] + 20) + "px";

    copyToClipboard(window.location.href);
}
function CloseLinkThis() {
    var elemDiv = document.getElementById("linkDiv");
    elemDiv.style.display = 'none';
}


function MostraCitacao() {
    var elemImg = document.getElementById("CitacaoImg");
    var elemDiv = document.getElementById("citacaoDiv");

    elemDiv.style.display = 'block';
    elemDiv.style.position = 'absolute';
    elemDiv.style.left = (findPos(elemImg)[0]) + "px";
    elemDiv.style.top = (findPos(elemImg)[1] + 20) + "px";
}
function CloseCitacao() {
    var elemDiv = document.getElementById("citacaoDiv");
    elemDiv.style.display = 'none';
}



function SendPage(CompleteURL) {
    window.open(CompleteURL, "SENDPAGE", "");
}

function getSel() {
    var txt = '';
    if (window.getSelection) {
        txt = window.getSelection();
    }
    else if (document.getSelection) {
        txt = document.getSelection();
    }
    else if (document.selection) {
        rng = document.selection.createRange();
        txt = rng.text;

        rng.moveStart('word', -2);
        if (rng.text.indexOf("-") > 0)
            txt = rng.text;
        else {
            rng = document.selection.createRange();
            rng.moveEnd('word', 2);
            if (rng.text.indexOf("-") > 0)
                txt = rng.text;
        }
        
        // HUGOS 2008-04-10:: So faz o trim no IE. No Firefox nao esta a funcionar por causa do replace()
        txt = trim(txt);
    }
    else return;

    return txt;
}


function trim(str) {
    return str.replace(/^\s*|\s*$/g, "");
}


function SeleccionaEntrada(pal) {
    DefinePalavra(pal);
}

function DefinePalavra(pal) {
    if (pal == "")
        return;
    document.location.href = "default.aspx?pal=" + pal;
}
function encode_utf8(s) {
    return unescape(encodeURIComponent(s));
}


function showDiv(divID) {
    var elem = document.getElementById(divID);

    if (elem.style.display == "") {
        elem.style.display = "none";
    }
    else {
        elem.style.display = "";
    }
}

function autocompleter_formatitem(row, pos, num, s) {
    return '<div class="' + row[1] + '">' + row[0] + '</div>';
}

function create_autocomplete(id) {
    jQuery("#" + id).autocomplete("PalavraAutoComplete.aspx", { minChars: 1, selectFirst:false, autoFill: false, max: 15, scroll: false, onItemSelect: function() { Define(); } })
    .result(autocomplete_result).focus();
}

function autocomplete_result() {
    DefinePalavra(jQuery(this).val());
}