if(typeof(EVERYZING) == 'undefined') { EVERYZING = {}; }

// Collect full transcript text
var fullTranscriptText = "";

// actual clipping function
EVERYZING.clipText = function(obj,maxLen) {
	fullTranscriptText = obj.innerHTML;
	jQuery(obj).truncate(maxLen, {
        chars: / /,
        leave: false,
        trail: [true, "...\"", ""]
    });
}

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");
}

//copy text to clipboard
EVERYZING.copyToClipboard = function(inElement, path) {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="' + path + 'clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
	inElement.className = "ez-copied";
}

// 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.innerHTML = "show less";
	} else { // clip
		EVERYZING.clipText(tObj, tClipLength);
		tObj.className = "ez-clipped";
		tBtn.innerHTML = "show more";
	}
}

//blank out the searchbox value
EVERYZING.emptySearchBox = function() {
	var sObj = document.getElementById("ezsearch-string")
	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;
};


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;
		}
	});
};

