if(typeof(EVERYZING) == 'undefined') { EVERYZING = {}; }


EVERYZING.topicLander_truncate_featured_media = function(maxLen, prefix, postfix, target){
    maxLen = parseInt(maxLen, 10);
    if (isNaN(maxLen)){
        maxLen = 160;
    }
    if (target == null || target.length == 0){
		target = ".ez-featuredStory .ez-highlight";
	}
    if (prefix == null){
        prefix = "&#8220;&#8230;";
    }
    if (postfix == null){
        postfix = "&#8230;&#8221;";
    }
    
    jQuery(target).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);
			var presliced = pre.length > maxLen/2;
            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 postsliced = text.length > i + i + targetPostLen;
			//console.log("presliced", presliced, "postsliced", postsliced, text.slice(0,20), text.slice(text.length - 20));
			
            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;
    });
};


EVERYZING.searchFilterSort = function(a, b){
	var intA = parseInt(a[0]);
	var intB = parseInt(b[0]);
	
	var val = intB - intA;
	return val==0? 0: val/Math.abs(val);
};

EVERYZING.combineFilterDupes = function(arr) {
	for (var i=0; i<arr.length; i+=1){
		for (var j=i+1; j<arr.length; j+=1){
			if ((arr[i][1]) == (arr[j][1])){
				var newCount = arr[i][0] + arr[j][0];
				arr.splice(j,1);
				arr.splice(i,1, [newCount, arr[i][1]]);
				return EVERYZING.combineFilterDupes(arr);
			}
		}
	}
    for (var i=0; i<arr.length; i+=1){
        if (arr[i][1] == "associated press" || arr[i][1] == "vonage"){
            arr.splice(i,1);
			continue;
        }
		arr[i][1] = (arr[i][1]).replace(/&amp;/g, "&");
        arr[i][1] = (arr[i][1]).replace(/^([a-z])/g, EVERYZING.capitalizeFilter);
		arr[i][1] = (arr[i][1]).replace(/(\s[a-z])/g, EVERYZING.capitalizeFilter);
        arr[i][1] = (arr[i][1]).replace(/o'reilly/ig, "O'Reilly");
        arr[i][1] = (arr[i][1]).replace(/mccain/ig, "McCain");
        arr[i][1] = (arr[i][1]).replace(/(\b)gm(\b)/ig, "$1GM$2");
        arr[i][1] = (arr[i][1]).replace(/imag/ig, "iMag");
        arr[i][1] = (arr[i][1]).replace(/fnc/ig, "FNC");
		arr[i][1] = (arr[i][1]).replace(/fns/ig, "FNS");
        arr[i][1] = (arr[i][1]).replace(/foxnews.com/ig, "FOXNews.com");
		arr[i][1] = (arr[i][1]).replace(/\bfox\b/ig, "FOX");
		arr[i][1] = (arr[i][1]).replace(/schering-plough/ig, "Schering-Plough");
		arr[i][1] = (arr[i][1]).replace(/s&p/ig, "S&P");
		arr[i][1] = (arr[i][1]).replace(/\bfbi\b/ig, "FBI");
		arr[i][1] = (arr[i][1]).replace(/\bcbs\b/ig, "CBS");
		arr[i][1] = (arr[i][1]).replace(/\bnasa\b/ig, "NASA");
		arr[i][1] = (arr[i][1]).replace(/\bpga\b/ig, "PGA");
		arr[i][1] = (arr[i][1]).replace(/\bgm\b/ig, "GM");
		arr[i][1] = (arr[i][1]).replace(/u\.s\./ig, "U.S.");
    }
	return arr;
};

EVERYZING.capitalizeFilter = function(str, strWord){
//    var ret = strWord.charAt(0).toUpperCase() + strWord.substring(1,strWord.length);
    var ret = strWord.toUpperCase();
    return ret;
};

EVERYZING.filterRender = function(arr, elem, url, min, max, param, currentParam){
	var html = "";
	for (var i=0; i<arr.length; i+=1){
		var name = arr[i][1];
		//var nameParam = encodeURIComponent(encodeURIComponent(name));
		var nameParam = encodeURIComponent(name);
		//var nameParam = name;
		var expanded = i>=min?" expanded":"";
		url = url.replace(/"/g, "&quot;");
		
		if (name == currentParam){
			html += '<a class="ez-searchFilter ez-searchFilterSelected' + expanded + '" href="' + url + '" title="Clear this filter">' + name + '</a>';
		} else {
			html += '<a class="ez-searchFilter' + expanded + '" href="' + url + param + nameParam + '">' + name + '</a>';
		}
		
		if ( (i + 1 == max) || (arr.length < max && i == arr.length -1) && i >= min){
            html += '<span class="ez-searchFilter more" onclick="jQuery(\'.expanded\', jQuery(this).parent()).css(\'display\', \'block\'); jQuery(this).hide();">See More <img src="' + EVERYZING.themeUrl + '/images/more_arrow.gif" /></span>';
            html += '<span class="ez-searchFilter expanded" onclick="jQuery(\'.expanded\', jQuery(this).parent()).css(\'display\', \'none\'); jQuery(\'.ez-searchFilter.more\', jQuery(this).parent()).show();">See Less <img src="' + EVERYZING.themeUrl + '/images/less_arrow.gif" /></span>';
			break;
		}
	}
	elem[0].innerHTML = html;
	jQuery('.expanded', elem.parent()).css('display', 'none');
};

EVERYZING.snippetVideoImgError = function(elem){
	jQuery(elem).parent().parent().hide();
};



// 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";
};

//blank out the searchbox value
EVERYZING.emptySearchBox = function() {
	var sObj = document.getElementById("ezsearch-string");
	if (typeof(sObj) != "undefined")
	   sObj.value = "";
};

EVERYZING.transcriptMod_openFullTranscript = function(){
    var pWidth = 520; // width of popup
    var pHeight = 520; // height of popup
    var leftPos = screen.width - pWidth - 100; // 100px from right of screen
    var topPos = (screen.height - pHeight)/2; // vertically centered
    
    EVERYZING.transcriptWindow = window.open(location.href.replace("/m/", "/transcript/"), "Transcript", "left=" + leftPos + ",top=" + topPos + ",width=" + pWidth + ",height=" + pHeight + ",resizable,scrollbars");
};

EVERYZING.closeTranscript = function() {
	if (EVERYZING.transcriptWindow) {
		EVERYZING.transcriptWindow.close();
	}
};

// 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) {
        var sub = document.getElementById("ez-search-form");
        sub.submit();	
		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;
        }
    });
};

