$(function() {
    initIcnsOver();
    initExternalLinks();
    putH2s();

    $(".aNew:last", $(".newsList")).css("border", "0");
    $(".newsList .aNew").bind("click", function() {
        document.location.href = $("a:first", this).attr("href");
    });

    $("#calMonth").bind("change", function() {
        $("#calForm").submit();
    });

    if (typeof menuLink != "undefined") {
        var ondiv = $("#pageMenu a[href=" + menuLink + "]").parent("div");
        if (!ondiv.hasClass("noOn"))
            ondiv.attr("className", "btnRed");

    }

    document.title += $("h1").text();

    $("a.icnOver").click(function() {
        _gaq.push(['_trackEvent', 'icns_show', this.href]);
        return true;
    });

    $(window).load(initTrackExternal);

});
function initTrackExternal() {
    $('a[href]').each(function() {
        var href = $(this).attr('href');

        if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
            $(this).click(function() {
                var extLink = href.replace(/^https?\:\/\//i, '');
                _gaq.push(['_trackEvent', 'external_click', extLink]);
                window.setTimeout(function(){
                    document.location.href = href;
                },100)
                return false;
            });
        }
    });
}

function initIcnsOver() {
    $("#icns li a").bind("mouseover", function() {
        var s = $("img", this).attr("src");
        if (!s.indexOf(".over.png") >= 0)
            $("img", this).attr("src", s.replace(".png", ".over.png"));
    });
    $("#icns li a").bind("mouseout", function() {
        var s = $("img", this).attr("src");
        if (s.indexOf(".over.png") >= 0)
            $("img", this).attr("src", s.replace(".over.png", ".png"));
    });
    $("#icns li a").bind("click", function(e) {
        var dvid = $(this).attr("href");

        _gaq.push(['_trackEvent', 'icns_click', dvid]);

        $(dvid).css("left", $(this).offset().left - 2);
        $(dvid).css("top", $(this).offset().top + $(this).height() - 5);


        if (window.onThumb)
        {
            window.onThumb.hide();
            if (window.onThumb.attr("id") == $(dvid).attr("id"))
            {
                window.onThumb = null;
                $(document.body).unbind("click", thumbOutClick);
                return false;
            }
        }


        $(dvid).show();
        window.onThumb = $(dvid);

        $(document.body).bind("click", thumbOutClick);

        return false;
    });
}

function thumbOutClick(e) {
    if (e.pageX >= window.onThumb.offset().left &&
        e.pageX <= window.onThumb.offset().left + window.onThumb.width() &&
        e.pageY >= window.onThumb.offset().top &&
        e.pageY <= window.onThumb.offset().top + window.onThumb.height())
        return true;
    window.onThumb.hide();
    window.onThumb = null;
    $(document.body).unbind("click", thumbOutClick);
    return false;
}

function initExternalLinks() {

    var excepts = new Array("novasoportunidades.gov.pt", "testemunhos.novasoportunidades.gov.pt", "candidaturas.gepe.min-edu.pt", "212.55.143.11", "sigo.gepe.min-edu.pt");

    var as = document.getElementsByTagName("A");
    for (var i = 0; i < as.length; i++) {
        var anA = as[i];


        if ((anA.href.indexOf("http://") == 0 || anA.href.indexOf("https://") == 0) &&
            anA.href.indexOf(document.location.hostname) < 0
                ) {

            var isException = false;

            for (var j = 0; j < excepts.length; j++)
                if (anA.href.indexOf(excepts[j]) > 0)
                    isException = true;


            //redo the target
            if (!anA.onclick && !isException)
            {
                //anA.onclick = externalClick;
                anA.target = "_top";

                anA.oldRef = anA.href;
                anA.href = "/outerFrame.jsp?link=" + escape(anA.oldRef);
            }

            //add the image
            if (anA.getElementsByTagName("IMG").length == 0 && anA.childNodes.length <= 1)
            {
                var ok = false;
                var el = anA;
                while (!ok && el.parentNode && el.parentNode != el)
                {
                    if (el.id /*&& (el.id == "pageBody")*/)
                        ok = true;
                    el = el.parentNode;
                }
                if (ok)
                    anA.innerHTML = anA.innerHTML + " <img src='/img/externo.png'/>";
            }
        }
    }
}

function putH2s() {

    var h2s = $(".aNew h2");

    var count = 0;

    if ($(h2s).length > 1) {

        $(h2s).each(function() {
            var a = document.createElement("a");

            var idLnk = "a" + count;

            $(a).attr("id", idLnk);
            $(this).wrap(a);

            var bks = $("#bookmarks");

            var divLnk = document.createElement("div");
            var lnk = document.createElement("a");
            $(lnk).attr("href", "#" + idLnk);
            $(lnk).html($(this).html());
            $(divLnk).html(lnk);
            $(divLnk).css("paddingBottom", "10px");

            $(divLnk).addClass("bookmarkEntry");
            $(bks).append(divLnk);

            count++;
        });
    } else {
        $("#bookmarks").remove();
    }
}
;

function externalClick() {
    var hitExt = escape(this.href);
    var u = "/hit?TYPE=EXT&SRC_ID=" + (parseInt(calcMD5(hitExt), 16) % 1000) + "&TXT=" + hitExt;
    new Image().src = u;
    return true;
} 