
function __getTagReferer() 
{
    var keywords = '';
    var ref = document.referrer;
    if(ref && ref != '') {
        var regSearchEng = new RegExp("\.google|\.sapo|\.yahoo", "i");
        if(ref.match(regSearchEng)) {
            var regSearch = new RegExp("(&|\\?)(q|p)=(.*?)&?", "i");
            if(ref.match(regSearch)) {
                var regKeywords = new RegExp("(.*)(&|\\?)(q|p)=([^&]+)?((&)?(.*)?)", "ig");
                keywords = ref.replace(regKeywords, "$4");
                keywords = unescape(keywords);
                keywords = keywords.replace(/\+/g, ' ');
                keywords = keywords.replace(/"/g, ' ');
                keywords = keywords.replace(/(^\s+|\s+$|\n)/g, '');
                keywords = keywords.replace(/\s\s/g, ' ');
            }
        }
    }
    return keywords;
}

function __setTagVars() 
{
    var myvars = {
        url: location.href,
        title: document.title,
        tags: __getTagReferer()
    }
    return myvars;
}

function __postTags(defaultTags)
{
    var obj = __setTagVars();
    var urlTags = location.href;
    if(defaultTags == '') {
        urlTags = 'http://tags.sapo.pt/post?v=3&url='+encodeURIComponent(obj['url'])+'&title='+encodeURIComponent(obj['title'])+'&referer='+encodeURIComponent(obj['tags']);
    } else {
        urlTags = 'http://tags.sapo.pt/post?v=3&url='+encodeURIComponent(obj['url'])+'&title='+encodeURIComponent(obj['title'])+'&referer='+encodeURIComponent(obj['tags'])+'&tags='+encodeURIComponent(defaultTags);
    }
    location.href = urlTags;
}

function __setButton() 
{
    var aScripts = document.getElementsByTagName('SCRIPT');
    var re = new RegExp("\/tagthis.js", "i");
    var __b = false;
    var __tags = false;
    var __buttonName = false;
    var __code = false;
    for(var i=0; i < aScripts.length; i++) {
        if(aScripts[i].src.match(re)) {
            if(typeof(aScripts[i].code) == 'undefined') {
                __b = __getButtonCode(aScripts[i].src);
                __tags = __getDefaultTags(aScripts[i].src).replace(/(\'|\")/g, '\\$1');
                switch(__b) {
                    case 1:
                        __buttonName = '<a href="#" id="_tagthis_id_'+__b+'_" onclick="__postTags(\''+__tags+'\'); return false;"><img border="0" src="http://tags.sapo.pt/images/v2/pipiButtonGuardarPagina.png" alt="Guardar esta P&aacute;gina" title="Guardar esta P&aacute;gina" /></a>';
                        __code = i;
                        break;
                    
                    case 2:
                        __buttonName = '<a href="#" id="_tagthis_id_'+__b+'_" onclick="__postTags(\''+__tags+'\'); return false;"><img border="0" src="http://tags.sapo.pt/images/v2/pixelButtonGuardarPagina.gif" alt="Guardar esta P&aacute;gina" title="Guardar esta P&aacute;gina" /></a>';
                        __code = i;
                        break;

                    case 3:
                        __buttonName = '<a href="#" id="_tagthis_id_'+__b+'_" onclick="__postTags(\''+__tags+'\'); return false;"><img border="0" src="http://tags.sapo.pt/images/v2/80x15ButtonSAPOTags.gif" alt="Guardar esta P&aacute;gina" title="Guardar esta P&aacute;gina" /></a>';
                        __code = i;
                        break;

                    default:
                        __buttonName = '<a href="#" id="_tagthis_id_0_" onclick="__postTags(\''+__tags+'\'); return false;">'+__getTextCode(aScripts[i].src)+'</a>';
                        __code = i;
                        break;
                    
                }
            }
        }
    }
    aScripts[__code].code=true;
    return __buttonName;
}

function __getTextCode(jslocation)
{
    var t = false;
    if(jslocation) {
        if(typeof(__text) == 'undefined' || !__text) {
            var mText = new RegExp("(&|\\?)t=([_0-9a-z-])", "i");
            if(jslocation.match(mText)) {
                var reText = new RegExp("(.*)(&|\\?)t=([^&]*)&?(.*)", "gm");
                t = jslocation.replace(reText, "$3");
            } else {
                t = 'Guardar esta P&aacute;gina';
            }
        } else {
            t = __text;
            __text=false;
        }
        return decodeURIComponent(t).replace(/^\s+|\s+$|\n/g, '').replace(/(<[^>]>|\+)/g, ' ');
    }
}

function __getDefaultTags(jslocation)
{
    var tags = '';
    if(jslocation) {
        var mTags = new RegExp("(&|\\?)tags=([^&]*)($|&)", "i");
        if(jslocation.match(mTags)) {
            var reTags = new RegExp("(.*)(&|\\?)tags=([^&]*)&?(.*)", "gm");
            tags = jslocation.replace(reTags, "$3");
        }
    }
    return decodeURIComponent(tags).replace(/^\s+|\s+$|\n/g, '').replace(/(<[^>]>|\+)/g, ' ');
}

function __getButtonCode(jslocation)
{
    if(jslocation) {
        var reGet = new RegExp("(.*)\\?b=([0-9])(.*)", "gm");
        var b = parseInt(jslocation.replace(reGet, "$2"));
        return b;
    }
}

var __tag_button__ = __setButton();

document.write(__tag_button__);


