/*
 * Initializes the baldr snippets
 */
EVERYZING.snippetImageMod_initBaldr = function(){
    
    /* Switch the currently displayed snippet highlight */
    var switchTextHighlights = function(highlight){
        var marker = $ez(highlight);
        
        var highlightText = $ez(".ez-box-content div[@ts='"+marker.attr("ts")+"']", marker.parent().parent());
        if (highlightText.length == 0) return; // snippet bubble not included
        
        marker.unbind("mouseover");
        marker.siblings('.active-highlight').bind("mouseover", function(){switchTextHighlights(this);});
        
        marker.addClass('active-highlight');
        marker.siblings('.active-highlight').removeClass('active-highlight');
        
        highlightText.show();
        highlightText.siblings().hide();
        highlightText.bind("click", function(){
              location.href = marker.attr('href');
          });
    };
    
    /* Initialize the markers */
    $ez(".ez-snippetImageMod-item .ez-timeline").each(function(){
        var currentTimeline = $ez(this);
        var timeStamps = $ez('.ez-timeline-marker', currentTimeline);
        
        
        timeStamps.each(function(){
            var stamp = $ez(this);
        
            stamp.bind("mouseover", function(){
                switchTextHighlights(this);
            });
        });
        
        switchTextHighlights(timeStamps.slice(0,1));
    });
};


/* 
 * Truncates the snippets around the keyword and optionally adds prefix/postfix
 * There should only be bold tags (keywords) inside the ez-highlight elements
 */
EVERYZING.snippetImageMod_baldr_truncate = function(maxLen, prefix, postfix){
    maxLen = parseInt(maxLen, 10);
    if (isNaN(maxLen)){
        maxLen = 160;
    }
    
    if (prefix == null || prefix.length == 0){
        prefix = "&#8220;&#8230;";
    }
    if (postfix == null || postfix.length == 0){
        postfix = "&#8230;&#8221;";
    }
    
    $ez(".ez-snippetImageMod-item .ez-highlight").each(function(){
        var content = this.innerHTML;
        content = content.replace(/<B/g, '<b');
        content = content.replace(/<\/B>/g, '</b>');
        
        var text = $ez(this).text();
        
        if (text.length > maxLen) {
            var i = content.indexOf('<b');
            var j = content.indexOf('</b>');
            
            var pre = content.slice(0, i);
            pre = pre.slice(-1 * maxLen / 2);
            pre = pre.replace(/^[a-z0-9.]* /i, '');
            
            var post = content.slice(i, content.length);
            
            var targetPostLen = maxLen - pre.length;
            var targetPostText = text.slice(i, targetPostLen + i);
            targetPostText = targetPostText.replace(/&amp;/g, '&');
            
            var elemLen = 0;
            var buf = post;
            buf = buf.replace(/&amp;/g, '&');
            
            while (buf.indexOf(targetPostText) == -1) {
                var i = buf.indexOf('<');
                var j = buf.indexOf('>');
                elemLen += j - i + 1;
                
                buf = buf.slice(0, i) + buf.slice(j + 1, buf.length);
            }
            
            post = post.slice(0, targetPostLen + elemLen);
            post = post.replace(/[a-z0-9<>]+$/i, '');
            
            var newContent = pre + post;
            newContent = $ez.trim(newContent);
            
            this.innerHTML = newContent;
        }
        
        this.innerHTML = prefix + this.innerHTML + postfix;
    });
};