if(typeof(EVERYZING) == 'undefined') { EVERYZING = {}; }

// Collect full transcript text
var fullTranscriptText = "";

// actual clipping function
EVERYZING.clipText = function(obj,maxLen) {
	fullTranscriptText = obj.innerHTML;
	$ez(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 = $ez(target);    
    target.each(function(){
		EVERYZING.clipText(this,maxLen);
    });
};

// display or clip full transcript on media lander
EVERYZING.displayTranscript = function(tObj, tBtn, tClipLength) {
	tObj = document.getElementById(tObj);
	tBtn = document.getElementById(tBtn);
	if (tBtn.innerHTML == "+") { // expand
		tObj.innerHTML = fullTranscriptText;
		tObj.className = "ez-full";
		tBtn.innerHTML = "-";
	} else { // clip
		EVERYZING.clipText(tObj, tClipLength);
		tObj.className = "ez-clipped";
		tBtn.innerHTML = "+";
	}
}

//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 = $ez.trim(str.value);
	if (str.value.length > 0) {
		bool = true;
	}
	return bool;
}

// truncation
$ez.fn.truncateText = function(maxLen, postfix){
    $ez(this).each(function(){
		var target = $ez(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 = $ez.trim(content);
			
			content = content + postfix;
			
			this.innerHTML = content;
		}
	});
};

// populate the client's H1 tag
EVERYZING.populateH1 = function(text) {
	var pageTitle = document.getElementById("content").getElementsByTagName("h1");
	pageTitle[0].innerHTML = text;
}

EVERYZING.toggleHotTopics = function (tbtn, tobj) {
	tObj = document.getElementById(tobj);
	tBtn = document.getElementById(tbtn);
	if (tBtn.innerHTML == "+") { // expand
		tObj.className = "ez-featureBlock ez-hotTopicsList ez-full";
		tBtn.innerHTML = "-";
	} else { // clip
		tObj.className = "ez-featureBlock ez-hotTopicsList ez-clipped";
		tBtn.innerHTML = "+";
	}
}

EVERYZING.toggleSearchFilter = function (tbtn, tobj) {
	tObj = document.getElementById(tobj);
	tBtn = document.getElementById(tbtn);
	if (tBtn.innerHTML == "+") { // expand
		tObj.style.display = "block";
		tBtn.innerHTML = "-";
	} else { // clip
		tObj.style.display = "none";
		tBtn.innerHTML = "+";
	}
}

EVERYZING.MyToggleTranscript = function(tClipLength) {
    var tObjEl = document.getElementById('ez-fullTranscript');
    var tBtnEl = document.getElementById('ez-transcript-btn');
	
	if (typeof(EVERYZING.transcriptMod_ares_fullTranscriptText) == 'undefined' || EVERYZING.transcriptMod_ares_fullTranscriptText == '') {
		EVERYZING.transcriptMod_ares_fullTranscriptText = tObjEl.innerHTML;
		$ez(tObjEl).truncateText(tClipLength, '... "');
        tObjEl.className = "ez-clipped";
		tBtnEl.innerHTML = "more";
	}
    else if (tBtnEl.innerHTML == "more"){
        tObjEl.innerHTML = EVERYZING.transcriptMod_ares_fullTranscriptText;
        tObjEl.className = "ez-full";
        tBtnEl.innerHTML = "less";
    }else{
        EVERYZING.transcriptMod_ares_fullTranscriptText = tObjEl.innerHTML;
        $ez(tObjEl).truncateText(tClipLength, '... "');
        tObjEl.className = "ez-clipped";
        tBtnEl.innerHTML = "more";
    }
};

// these overwrite foundation functions to use local images for media thumbs
EVERYZING.snippetVideoImgError = function(source){
    source.src = EVERYZING.themeUrl + "/images/ez_video_icon.jpg";
    source.onerror = null;	
    return true;
}
EVERYZING.snippetAudioImgError = function(source){
    source.src = EVERYZING.themeUrl + "/images/ez_audio_icon.jpg";
    source.onerror = null;	
    return true;
}