/*
 * Initializes the baldr snippets
 */
EVERYZING.snippetMod_initBaldr = function(){
	
    /* Switch the currently displayed snippet highlight */
	var switchTextHighlights = function(highlight){
	    var marker = jQuery(highlight);
		
	    var highlightText = jQuery('.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(){
            if (location.href.indexOf('fedsearch') == -1){
				location.href = marker.attr('href');
			}else{
				parent.location.href = marker.attr('href');
			}
		      
		  });
	};
	
	/* Initialize the markers */
	jQuery(".ez-snippetMod-item .ez-timeline").each(function(){
		var currentTimeline = jQuery(this);
		var timeStamps = jQuery('.ez-timeline-marker', currentTimeline);
		
		
		timeStamps.each(function(){
			var stamp = jQuery(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.snippetMod_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;";
    }
	
	jQuery(".ez-snippetMod-item .ez-highlight").each(function(){
		var content = this.innerHTML;
		content = content.replace(/<B/g, '<b');
		content = content.replace(/<\/B>/g, '</b>');
		
		var text = jQuery(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 = jQuery.trim(newContent);
			
			this.innerHTML = newContent;
		}
		
		this.innerHTML = prefix + this.innerHTML + postfix;
	});
};