var loginshowed = false;

$(document).ready(function(){
    if (isshowloginpopup) {
        showloginpopup();
        $("#loginerror").show();
    }

    $("#q").keydown(function(e){
        if(e.keyCode==13){
            dosearch();
        }
    });

    animatedcollapse.addDiv('sitemap_links', 'fade=1,persist=1');
    animatedcollapse.init();

    setLinkIcon(".pdf", "link_pdf.png");
    setLinkIcon(".doc", "link_word.png");
    setLinkIcon(".docx", "link_word.png");
    setLinkIcon(".xls", "link_excel.png");
    setLinkIcon(".xlsx", "link_excel.png");
    setLinkIcon(".ppt", "link_powerpoint.png");
    setLinkIcon(".pptx", "link_powerpoint.png");
    setLinkIcon(".zip", "link_zip.png");

    $("a[href^='mailto:']").attr("style", "background:url('/content/images/link_mail.png') no-repeat left center;padding-left: 20px;line-height: 16px;");

    $("a[href^='http://']").attr("target", "_blank");
    $("a[href^='https://']").attr("target", "_blank");

    if ($.cookie && $.cookie("IsOlderThanLegalAge") != "true") {
        var url = "/Default/UpdateCart";
        $.getJSON(url, function(data) { });

        $("#welcome").show();
    }
    else {
        $("#welcome").remove();
    }

    $("input.btn_enter").click(function() {
        var checkbox = $("#older");
        if (checkbox[0].checked == true) {
            $.cookie("IsOlderThanLegalAge", "true", { expires: 1000 });
            $("#welcome").fadeOut("slow");
        }
        else {
            $("#errorMessage").show();
        }
    });

    var briefs = $(".box>h3+p");
    var links = $(".box>a.readmore");
    if (briefs.length == links.length) {
        for (var i = 0; i < briefs.length; i++) {
            $(briefs[i]).append($(links[i]));
        }
    }
});

function setLinkIcon(type, img) {
    $("a[href$='" + type + "']").attr("style", "background:url('/content/images/" + img + "') no-repeat left center;padding-left: 20px;line-height: 16px;");
}

function dosearch() {
    var q = $("#q");
    if (q && q.attr("value") != "") {
        window.open(searchpage + "?q=" + q.attr("value"), "_self");
        q.attr("value", "");
    }
}

function showloginpopup() {
    var p = $("#subcontent");
    if (p) {
        if (loginshowed) {
            p.hide();
            loginshowed = false;
        }
        else {
            p.show();
            loginshowed = true;
        }
    }
}

