if(typeof(EVERYZING) == 'undefined') { EVERYZING = {}; }

EVERYZING.truncate_to = function(target, maxLen){
    maxLen = parseInt(maxLen);
    if (isNaN(maxLen) || maxLen <= 0){ return; }    
    var target = jQuery(target);    
    target.each(function(){
         jQuery(this).truncate(maxLen, {
            chars: / /,
            leave: false,
            trail: [true, "...", ""]
        });
    });
};

// 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, "...\"", ""]
    });
}



// 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");
}

/*Function for ListenLive Link on header*/    	
var playerWin = null;
var newPopupWindow;
function popupLinkOption(url,width,height)
{
	newPopupWindow=window.open(url,'name','height='+height+',width='+width+',left=100,top=100,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no,location=no');
}




//copy text to clipboard
EVERYZING.copyToClipboard = function(inElement, path) {
	if( window.clipboardData && clipboardData.setData )
	{
		var text = inElement.value;
		inElement.focus();
		inElement.select();
		clipboardData.setData("Text", text);
	}
	else
	{
	    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.innerHTML == "+") { // expand
		tObj.innerHTML = fullTranscriptText;
		tObj.className = "ez-full";
		tBtn.innerHTML = "-";
	} else { // clip
		EVERYZING.clipText(tObj, tClipLength);
		tObj.className = "ez-clipped";
		tBtn.innerHTML = "+";
	}
}

	
var iChars = '!@#$%^&*()+=-[]\\\;./{}|\:<>?';
	function checkspecialchar()
	{
   		for (var splchar = 0; splchar < document.getElementById('ezsearch-string').value.length; splchar++) 
   		{
     		if (iChars.indexOf(document.getElementById('ezsearch-string').value.charAt(splchar)) != -1) 
     		{
    			alert ('Entered String has special characters. \nThese are not allowed.\n Please remove them and try again.');
       			document.getElementById('ezsearch-string').value="";
			}
		}
	}
	function checkspecialchar2()
	{
   		for (var splchar = 0; splchar < document.getElementById('ezsearch-string2').value.length; splchar++) 
   		{
     		if (iChars.indexOf(document.getElementById('ezsearch-string2').value.charAt(splchar)) != -1) 
     		{
    			alert ('Entered String has special characters. \nThese are not allowed.\n Please remove them and try again.');
       			document.getElementById('ezsearch-string2').value="";
			}
		}
	}

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, '&');
            
            while (buf.indexOf(text) == -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;
        }
    });
};

