jQuery.preloadImages = function(){
    for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
    }
};

function GetArtist(id) {
    $.ajax({
        type: "GET",
        url: "/artists/artist_detail/",
        data: ({ id: id }),
        success: function(html){
            $('.artists-toggler a').removeClass('active');
            $('.artists-toggler a[rel|=hide]').addClass('active');
            $('#artists').slideUp();
            $(".artist-detail").html(html);
            $('.vertical-scroll,').jScrollPane({ 'showArrows' : true, 'scrollbarWidth' : 21, 'scrollbarMargin': 2 });
            $(".tabs").tabs();
        }
    });
}

$(document).ready(function(){
	$('#artists .scroll').jScrollHorizontalPane({ 'showArrows' : true, 'scrollbarHeight' : 23, 'scrollbarMargin': 2, 'wheelSpeed': 100 });
    
    $("#settings_loading").bind("ajaxStart", function(){
        $(this).show();
    }).bind("ajaxStop", function() {
        $(this).hide();
    });

    $("div.scrollable").scrollable({
        size: 3,
        loop: true
    });

    // preload the modal window images - some browsers won't show them otherwise
    $.preloadImages("/static/images/fancy_closebox.png");

    // open links in new window
    $('#bio a, .artist-link, .news a, #extras a').click(function(){
        window.open(this.href);
        return false;
    });

    // Ajax for filters
    $('.filtering ul li a').click(function(){  
        var filter = ($(this).text());
        var sort = ($('.sorting .jqTransformSelectWrapper span').text());
        $.ajax({
            type: "POST",
            url: "/artists/filter_sort/",
            data: ({ filter: filter, sort: sort }),
            success: function(html){
                $("#artists").html(html);
                $('#artists .scroll').jScrollHorizontalPane({ 'showArrows' : true, 'scrollbarHeight' : 23, 'scrollbarMargin': 2 });
            }
        });
    });
    
    // Ajax for sorts
    $('.sorting ul li a').click(function(){  
        var sort = ($(this).text());
        var filter = ($('.filtering .jqTransformSelectWrapper span').text());
        $.ajax({
            type: "POST",
            url: "/artists/filter_sort/",
            data: ({ filter: filter, sort: sort }),
            success: function(html){
                //$("#artists").html(html);
                //$('#artists .scroll').jScrollHorizontalPane({ 'showArrows' : true, 'scrollbarHeight' : 23, 'scrollbarMargin': 2 });
                $(".artist-data").quicksand( $(html).find('a'), {
                    attribute: 'id',
                    easing: 'easeInOutQuad' },
                    function() {
                    $('#artists .scroll').jScrollHorizontalPane({ 'showArrows' : true, 'scrollbarHeight' : 23, 'scrollbarMargin': 2 });
                });
            }
        });
    });
    
    // Fetch artist details for selected artist
    $('a.artist').live("click", function() {
        var id = $(this).attr("id");
        GetArtist(id);
        return false;
    });
});