if(typeof(EVERYZING) == 'undefined') { EVERYZING = {}; }

// Collect full transcript text
var fullTranscriptText = "";

// actual clipping function
EVERYZING.clipText = function(obj,maxLen) {
	fullTranscriptText = obj.innerHTML;
	jQuery(obj).truncateText(maxLen, '&#8230;&#8221;');
}

EVERYZING.truncate_to = function(target, maxLen){
    maxLen = parseInt(maxLen);
    if (isNaN(maxLen) || maxLen <= 0){ return; }    
    var target = jQuery(target);    
    target.each(function(){
		EVERYZING.clipText(this,maxLen);
    });
};

// opens faq pop
EVERYZING.openFaq = function(url){
	var pWidth = 520; // width of popup
	var pHeight = 520; // width of popup
	var leftPos = screen.width - pWidth - 100; // 100px from right of screen
	var topPos = (screen.height - pHeight)/2; // vertically centered
	var win = window.open(url, "Transcript", "left=" + leftPos + ",top=" + topPos + ",width=" + pWidth + ",height=" + pHeight + ",resizable,scrollbars");
}

// display or clip full transcript on media lander
EVERYZING.displayTranscript = function(tObj, tBtn, tClipLength) {
	tObj = document.getElementById(tObj);
	tBtn = document.getElementById(tBtn);
	if (tObj.className == "ez-clipped") { // expand
		//tObj.innerHTML = fullTranscriptText;
		tObj.className = "ez-full";
		tBtn.className = tBtn.className + " ez-transcript-btn-minus";
	} else { // clip
		//EVERYZING.clipText(tObj, tClipLength);
		tObj.className = "ez-clipped";
		tBtn.className = "ez-transcript-btn";
	}
}

//blank out the searchbox value
EVERYZING.emptySearchBox = function() {
	var sObj = document.getElementById("ezsearch-string");
	if (sObj != null) {
		sObj.value = "";	
	}	
}

// validate search
EVERYZING.validateSearch = function() {
	var bool = false;
	var str = document.getElementById("ezsearch-string");
	str.value = jQuery.trim(str.value);
	if (str.value.length > 0) {
		bool = true;
	}
	return bool;
}

// populate the client's H1 tag
EVERYZING.populateH1 = function(text) {
	var pageTitle = document.getElementById("content").getElementsByTagName("h1");
	pageTitle[0].innerHTML = text;
}

jQuery.fn.truncateText = function(maxLen, postfix){
    jQuery(this).each(function(){
		var target = jQuery(this);
		var content = this.innerHTML;
		var text = target.text();
		
		if (text.length > maxLen){
			text = text.slice(0,maxLen);
			text = text.replace(/&amp;/g, '&');
			
			var elemLen = 0;
			var buf = content;
			buf = buf.replace(/&amp;/g, '&');
			buf = buf.replace(/>\r\n</g, '');
			
			while (buf.indexOf(text) == -1 && i != -1 && j != -1) {
				var i = buf.indexOf('<');
				var j = buf.indexOf('>');
				elemLen += j - i + 1;
				
				buf = buf.slice(0,i) + buf.slice(j+1,buf.length);
			}
            
			content = content.slice(0, maxLen + elemLen);
			
			// Remove trailing word fragment
			content = content.replace(/[a-z0-9]+$/i, '');
			content = jQuery.trim(content);
			
			content = content + postfix;
			
			this.innerHTML = content;
		}
	});
};

/* 
 * Truncates the text object around the keyword and optionally adds prefix/postfix
 * There should only be bold tags (keywords) inside the text elements
 */
EVERYZING.target_kw_truncate = function(textObj, 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(textObj).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;
    });
};