$(function() {

    handleHomeCxClick();

    handleCxImg();

    handleCxImgEN();

    handleCxImgES();

    handleTopImg();

    var tit = $("h1").text();
    if(tit && tit.length){
        document.title += " / "+tit;
    }
});

function handleCxImg() {
    var pgCl = $("#page").attr("className");
    if (pgCl) {
        var cx = $("." + pgCl + ".cxHead");

        cx.after($('<img class="cxThumb" src="/img/pt.icn/' + pgCl + '.png" />'));
        cx.css("borderBottom", "0")
        cx.next("img").css("borderBottom", "1px solid white")
    }
}

function handleCxImgEN() {
    var pgCl = $("#page").attr("className");
    if (pgCl) {
        var cx = $("." + pgCl + ".cxHeadEN");

        cx.after($('<img class="cxThumb" src="/img/en.icn/' + pgCl + '.png" />'));
        cx.css("borderBottom", "0")
        cx.next("img").css("borderBottom", "1px solid white")
    }
}

function handleCxImgES() {
    var pgCl = $("#page").attr("className");
    if (pgCl) {
        var cx = $("." + pgCl + ".cxHeadES");

        cx.after($('<img class="cxThumb" src="/img/es.icn/' + pgCl + '.png" />'));
        cx.css("borderBottom", "0")
        cx.next("img").css("borderBottom", "1px solid white")
    }
}


function handleHomeCxClick() {
    $("img.cxThumb").css("cursor", "pointer").click(function() {
        var a = $("a", $(this).prev(".cxHead"));
        if (a && a.attr("href")) {
            document.location.href = a.attr("href");
        }
    });

}

function handleTopImg() {
    var topI = $("#pageMain .txt > img");
    if(!topI.length)
        topI = $("#pageMain .txt > p img");
    if(!topI.length)
        topI = $("#pageMain .txt > p iframe");

    if(topI){
        $("#pageMain h1").after(topI);
    }
}