$(function() {

    $(".search-box .artist-a-z").append("<div class='sub-search-box clearfix' style='display:none;'></div>");

    $("#search > ul a.dd").click(function(e) {
        e.preventDefault();
        $("#search > ul li").removeClass("active");
        $(this).parent().addClass("active");
        var cls = $(this).attr("href").replace("/", ".").replace(".aspx", "");
        closeSearchBox(function() {
            $(".search-box > div").hide();
            $(".search-box " + cls).show();
            openSearchBox();
        });
    });

    $(".artist-a-z > ul").addClass("clearfix");
    $(".artist-a-z > ul > li > a").click(function(e) {
        e.preventDefault();
        $(".artist-a-z > ul > li > a").removeClass("active");
        $(this).addClass("active");
        var childList = $(this).parent().find("ul")[0];
        $(".sub-search-box").slideUp('fast', function() {
            if (childList != undefined) {
                $(".sub-search-box").empty().append($(childList).clone().addClass("clearfix")).slideDown('fast');
            }
        });


    });

    $("div[id*='ctrlCategorySummary'] p").each(function() {
        // Wrap contents												
        var html = $(this).html();
        var firstBrPos = html.toLowerCase().indexOf("<br>");
        if (firstBrPos >= 0 && firstBrPos != html.toLowerCase().lastIndexOf("<br>")) {
            $(this).html(html.substr(0, firstBrPos + 4) + "<span>" + html.substr(firstBrPos + 4) + "</span>");

            // Hide content
            $("span", this).hide();

            // Add links
            $("br:first", this).before(" <a href='javascript:void(0);' class='more'>More...</a>");
            $("span", this).append("<br /><a href='javascript:void(0);' class='close'>Close</a>");

            // Add link actions
            $("a.more", this).click(function() {
                $(this).hide();
                $(this).parent().find("span").slideDown();
            });

            $("span a.close", this).click(function() {
                $(this).parent().slideUp();
                $(this).parent().parent().find("a.more").show();
            });
        }
    });

});

$(window).load(
    function() {

        setTimeout(function() {

            $(".thumbnail.new, .grid_4.new").each(function() {
                var img = $(this).find("img");
                $(this).find("a").append("<img src='/images/new-small.gif' alt='' class='new-tab' />");
                var imgPos = $(img).position();
                var tab = $(this).find(".new-tab");
	       var left = (150 - $(img).width()) / 2;
	       var top = (150 - $(img).height()) / 2;
                $(tab).hide();
                $(tab).css({ position: "absolute", top: (top > 0 ? top - 2 : 0), left: left + ($(img).width() - 44) });
                $(tab).fadeIn();
            });

            $("#artwork-details .img.new").each(function() {
                var img = $(this).find("img");
                $(this).append("<img src='/images/new-big.gif' alt='' class='new-tab' />");
                var imgPos = $(img).position();
                var tab = $(this).find(".new-tab");
	       var left = (550 - $(img).width()) / 2;
	       var top = (550 - $(img).height()) / 2;
                $(tab).hide();
                $(tab).css({ position: "absolute", top: (top > 0 ? top - 2 : 0), left: left + ($(img).width() - 91) });
                $(tab).fadeIn();
            });

        }, 100);

    }
);

function openSearchBox(callback) {
    $("#search").animate({
        paddingBottom: "15px"
    }, 100);
    $(".search-box").slideDown('fast', callback);
}

function closeSearchBox(callback) {
    $("#search").animate({
        paddingBottom: "0px"
    }, 100);
    $(".search-box").slideUp('fast', callback);
}
