/* Truncates the scenes around the keyword */
EVERYZING.truncate_scene = function(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(".ez-playerScenes .ez-scene .ez-snippetText").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;
    });
};


EVERYZING.relatedStoriesMod_trunc = function(){
    var RCB = jQuery("div.ez-relatedStoriesMod-block");
    jQuery("ul li", RCB).prepend("&#8226;&nbsp;");
};



// This is to override some defective menu behavior
jQuery(document).ready(function() {
	window.ShowLayer = function(el) {
	  var showEl = "gn" + el;
	  if (!DOM) return;
	  clearTimeout(timer);
	  HideAllLayers();
	  isOver = true;
	  var w_El = document.getElementById(showEl);
	  w_Anchor = "nav_" + el;
	  if (document.getElementById(w_Anchor)){
	    myObject = document.getElementById(w_Anchor);
	    //ddLeft = myObject.offsetLeft;
	    while (myObject.offsetParent) {
	      ddTop += myObject.offsetTop;
	      ddLeft += myObject.offsetLeft;
	      myObject = myObject.offsetParent;
	    }
	    ddTop += 21;
	    ddLeft -= 1;
	  }
	  if (w_El.className == "dd1") {var w_ElWidth = 130;}
	  else if (w_El.className == "dd2") {var w_ElWidth = 130;}
	  
	  if ((ddLeft + w_ElWidth) > document.body.clientWidth) {
	    ddLeft = document.body.clientWidth - w_ElWidth;
	  }
	  //console.log(ddLeft, document.body.clientWidth);
	  w_El.style.top = ddTop+"px";
	  w_El.style.left = ddLeft +"px";
	  ToggleSelect('hidden');
	  for (i = 0; i < tt.length; i++) {
	    w_nav = "nav_" + tt[i][1];
	    if (document.getElementById(w_nav)) {
	      w_nav = document.getElementById(w_nav);
	      if (w_nav.className == "navElOn") w_nav.className = "navElOnOver";
	    }
	  }
	  document.getElementById(w_Anchor).className = "navElOver";
	  w_El.style.visibility = "visible";
	  ddTop = 0; ddLeft = 0;
	}
});


jQuery(document).ready(function() {
	window.ShowChildLayer = function(childm, mItem) {
	  var childm = "gn" + childm;
	  clearTimeout(timer);
	  HideChildLayers();
	  var w_El = document.getElementById(childm);
	  if (document.getElementById(mItem)){
	    var myObject = document.getElementById(mItem);
	    while (myObject.offsetParent) {
	      ddTop = ddTop + myObject.offsetTop;
	      ddLeft = ddLeft + myObject.offsetLeft;
	      myObject = myObject.offsetParent;
	    }
	  }
	  if (w_El.className == "dd1") {var w_ElWidth = 130;}
	  else if (w_El.className == "dd2") {var w_ElWidth = 130;}
	  if ((ddLeft + (2*w_ElWidth)) > document.body.clientWidth) {
	    w_El.style.top = ddTop - 2+"px";
	    w_El.style.left = ddLeft - 127 +"px";
	  }
	  else {
	    w_El.style.top = ddTop - 2+"px";
	    w_El.style.left = ddLeft + 117+"px";
	  }
	
	  w_El.style.visibility = "visible";
	  ddTop = 0; ddLeft = 0;
	}
});


// THIS SHOUD NOT BE HERE, IT NEEDS TO MOVE TO A GERMANE LOCATION
jQuery(document).ready(function(){
	jQuery("body:not[#media-transcript]").children("div").css("width","980").css("margin","0 auto");
	jQuery(".ez-snippetMod-block").each(function(){
		jQuery(this).children(".ez-snippetMod-item:last").addClass('last-snippet'); 
	});
	
	if (jQuery('#snippetsTranscriptPane').jScrollPane) {
		jQuery('#snippetsTranscriptPane').jScrollPane({scrollbarOnLeft: false,showArrows: true,scrollbarWidth: 15,scrollbarMargin: 0});
	}
    
});
