// this function prepares the ajax calls to fetch the preview text for 
// all middle events (teaser text)
function prepareTeasers() {
    $('#body_home #events .event').each(function(i) {
        // set up z-index for noncompliant browsers...you know who you are...
        $(this).css('z-index', 1000 - i);
        // if this event is a middle event skip
        if($(this).is('.small')) return false;
        var div_name = 'info_div';
        if ($(this).is('.big')) {
            div_name += '_big';
        }
        var href = $('a.title',this).attr('href');
        var parts = Array();
        parts = href.split('/');
        var event_id = parts[(parts.length-2)];
        
        var str = document.location.toString();
        var pos = str.indexOf("/");
        pos = str.indexOf("/", pos + 1);
        pos = str.indexOf("/", pos + 1);
        pos = str.indexOf("/", pos + 1);
        pos = str.indexOf("/", pos + 1);
        var url = str.substring(0,pos+1);
        
        // if it isn't numeric skip this link -> it is a search string
        if (!is_numeric(event_id)) return;
        
        $(this).hover(
        function() {
            // over
            if ( $('.'+div_name, $(this)).length == 0 )
            {
                // create info_div
                $('a.buy', $(this)).after('<div class="'+div_name+'"></div>');
                $('.'+div_name, $(this)).loading().load(url+event_id+'/teaser.html', function() {
                    // check if the ajax return is empty
                    if ($('p.no_info', $(this)).length == 1)
                    {
                        // make sure it remains hidden
                        $(this).addClass('hidden');
                        $(this).hide();
                    }
                    else
                    {
                        $(this).show();
                    }
                });
            }
            else
            {
                // do not show empty divs
                if (!$('.'+div_name, $(this)).is('.hidden')) {
                    // show the div
                    $('.'+div_name, $(this)).show();
                }
            }
        },
        function() {
            // out
            $('.'+div_name, $(this)).hide();
        });
    });
}

// used to cut too long breadcrumbs
function cut_breadrcrumbs() {
    var max = 60;   // breadcrumb max length
    var len = 0;    // cumulative length
    $('#breadcrumbs li a').each(function () {
        len += $(this).text().length;
    });
    if (len > max)
    {
        // cut the last li element by the margin
        $('#breadcrumbs li:last a').each(function () {
            var text = $(this).attr('innerHTML');
            $(this).attr('innerHTML', text.substring(0, text.length - (len - max))+"...");
        });
    }
}

$(document).ready(function(){
    prepareTeasers();
    cut_breadrcrumbs();
});