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, '...&#34;');
}

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 (tBtn.className == "ez-transcript-btn ez-collapsed") { // expand
		tObj.innerHTML = fullTranscriptText;
		tObj.className = "ez-full";
		tBtn.className = "ez-transcript-btn ez-expanded";
	} else { // clip
		EVERYZING.clipText(tObj, tClipLength);
		tObj.className = "ez-clipped";
		tBtn.className = "ez-transcript-btn ez-collapsed"
	}
}

//blank out the searchbox value
EVERYZING.emptySearchBox = function() {
	var sObj = document.getElementById("ezsearch-string")
	sObj.value = "";
	sObj.className = "ezsearch-string ezsearch-string-active";
}

// validate search
EVERYZING.validateSearch = function(defaultText) {
	var bool = false;
	var str = document.getElementById("ezsearch-string");
	str.value = jQuery.trim(str.value);
	if (str.value.length > 0 && str.value != defaultText) {
		bool = true;
	}
	return bool;
}

// 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;
}

// resize logo images in snippets to maintain proper dimensions
EVERYZING.resizeLogos = function() {
	var arrImg = document.getElementsByTagName("img");
	for (var i = 0; i < arrImg.length; i++) {
		if (arrImg[i].id.indexOf("thumb_") != -1 &&	
		arrImg[i].src.indexOf("tbologo80x60.jpg") != -1) {
			arrImg[i].className = "ez-logoThumb";
		}
	}
}

// write out today's date
EVERYZING.todaysDate = function() {
	var d = new Date;
	var arrMo = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	document.write(arrMo[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear());
}

// Truncate function
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;
    });
};

// Browser Detection
EVERYZING.mobileApps = new Array("iphone", "android", "blackberry");
EVERYZING.userAgent = navigator.userAgent.toLowerCase();
EVERYZING.isMobile = function() {
    for (i = 0; i < EVERYZING.mobileApps.length; i++) {
        if (EVERYZING.userAgent.indexOf(EVERYZING.mobileApps[i]) > -1) {
            return true;
        }
    }
    return false;
}

// Flash Player Version Detection - Rev 1.5
// Detect Client Browser type
// Copyright 2006 Adobe Systems, Inc. All rights reserved.
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
    // NS/Opera version >= 3 check for Flash plugin in plugin array
    var flashVer = -1;
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            var descArray = flashDescription.split(" ");
            var tempArrayMajor = descArray[2].split(".");
            var versionMajor = tempArrayMajor[0];
            var versionMinor = tempArrayMajor[1];
            if ( descArray[3] != "" ) {
              tempArrayMinor = descArray[3].split("r");
            } else {
              tempArrayMinor = descArray[4].split("r");
            }
              var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
              var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
            }
        }
        // MSN/WebTV 2.6 supports Flash 4
        else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
        // WebTV 2.5 supports Flash 3
        else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
        // older WebTV supports Flash 2
        else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
        else if ( isIE && isWin && !isOpera ) {
        flashVer = ControlVersion();
    }
    return flashVer;
}

EVERYZING.hasFlash = function() {
    var versionStr = GetSwfVer(); 
    if (versionStr == -1) {
        return false;
    }
    return true;
}
