var sliderMinHeight;
var sliderMaxHeight;

$(document).ready(function() {
    artists_navigation_tooltip();
    artists_navigation_height();
    spalten_height_anpassen();
});

function artists_navigation_tooltip() {
        //Combine the passed in options with the default settings
        settings = jQuery.extend({
            popupId: "thumbPopup",
            popupCSS: {'border': '0', 'background': '#FFFFFF'},
            imgSmallFlag: "_t",
            imgLargeFlag: "_l",
            cursorTopOffset: 10,
            cursorLeftOffset: -190
        });
        
        //Create our popup element
        popup =
        $("<div />")
        .css(settings.popupCSS)
        .attr("id", settings.popupId)
        .css("position", "absolute")
        .appendTo("body").hide();
        
        //Attach hover events that manage the popup
        $("#artist_navigation li").hover(setPopup);
}

function artists_navigation_height(){
    
    $("#artist_navigation").append('<li style="clear:left;float:none;"></li>');
    $("#artist_navigation").parent().append('<div id="show_all">alle anzeigen</div>');
    
    //Höhe setzen
    sliderMaxHeight = $("#artist_navigation").outerHeight(true);
    show_rows = 3;
    row_height = $("#artist_navigation li:first-child").outerHeight(true);
    sliderMinHeight = show_rows*row_height+"px";
    slide_artists_up(0);
}

function setPopup(event)
{   
    this_obj = event.currentTarget;
    
    $(this_obj).data("hovered", true);

    $(this_obj).bind("load", {thumbImage: this}, function(event)
    {
        //Only display the larger image if the thumbnail is still being hovered
        if ($(event.data.thumbImage).data("hovered") == true) {
            $(popup).empty().append(this);
            updatePopupPosition(event);
            $(popup).show();
        }
    })
    
    title_txt       = $(this_obj).find('span.title').text();
    subtitle_txt    = $(this_obj).find('span.subtitle').text();
    $(popup).append($("<div class='tooltip'><span class='subtitle'>"+subtitle_txt+"</span><span class='title'>"+title_txt+"</span></div>"));
    $(popup).show();
    
    updatePopupPosition(event);
    $(this_obj).mousemove(updatePopupPosition);
    $(this_obj).mouseout(hidePopup);
}

function updatePopupPosition(event)
{   
    this_obj = event.currentTarget;
    
    var windowSize = getWindowSize();
    var popupSize = getPopupSize();
    if (windowSize.width + windowSize.scrollLeft < event.pageX + popupSize.width + settings.cursorLeftOffset){
        $(popup).css("left", event.pageX - popupSize.width - settings.cursorLeftOffset);
    } else {
        $(popup).css("left", event.pageX + settings.cursorLeftOffset);
    }
    if (windowSize.height + windowSize.scrollTop < event.pageY + popupSize.height + settings.cursorTopOffset){
        $(popup).css("top", event.pageY - popupSize.height - settings.cursorTopOffset);
    } else {
        $(popup).css("top", event.pageY + settings.cursorTopOffset);
    }
}

function hidePopup(event)
{
    this_obj = event.currentTarget;
    $(this_obj).data("hovered", false);
    $(popup).empty().hide();
}

function getWindowSize() {
    return {
        scrollLeft: $(window).scrollLeft(),
        scrollTop: $(window).scrollTop(),
        width: $(window).width(),
        height: $(window).height()
    };
}

function getPopupSize() {
    return {
        width: $(popup).width(),
        height: $(popup).height()
    };
}

function slide_artists_up(duration){
    $("#artist_navigation").stop();
    $("#artist_navigation").animate({ "height": sliderMinHeight }, duration);
    $("#show_all").html('alle anzeigen');
    $("#show_all").addClass("down");
    $("#show_all").removeClass("up");
    $("#show_all").click(function() { slide_artists_down(500); })
}

function slide_artists_down(duration){
    $("#artist_navigation").stop();
    $("#artist_navigation").animate({ "height": sliderMaxHeight }, duration);
    $("#show_all").html('schließen');
    $("#show_all").addClass("up");
    $("#show_all").removeClass("down");
    $("#show_all").click(function() { slide_artists_up(500); })
}
function spalten_height_anpassen(){
    
    $("#spalte_rechts div div.box:last").css('margin-bottom',0)
    
    haupt_height = $("#spalte_haupt").outerHeight(true);
    rechts_height = $("#spalte_rechts").outerHeight(true);
    if(haupt_height<rechts_height){
        margin = parseInt($("#spalte_haupt").css('margin-top'))+parseInt($("#spalte_haupt").css('margin-bottom'));
        padding = parseInt($("#spalte_haupt").css('padding-top'))+parseInt($("#spalte_haupt").css('padding-bottom'));
        $("#spalte_haupt").css('height',(rechts_height-(margin+padding)-1)+'px');
    }
}
