var child_window;
var isChild = false;
var forbiddenChannelFlag = false; // flag to disable the parent's reload when channel is forbidden

//var externalVideoReplaceImage_es = "/videos/img/replaceExternalVideo_es.png";
//var externalVideoReplaceImage_gl = "/videos/img/replaceExternalVideo_gl.png";
//var minExternalVideoReplaceImageWidth = 408;
//var marginExternalVideoReplaceImage = 10;
 
var external_opener_functions = {

// open url in new popup (or focus if popup exists)
openExternalVideo: function(url, width, height, replacingText){
     
   if (external_opener_functions.hasChild()){
     child_window.focus();
   } else {
    
     // Remove upper bar
     $('upper_stream_video_bar').innerHTML = '<!-- -->';          
      
     // Replace video by message 
     var replacePlayer = external_opener_functions.createReplacementElement(width,height,replacingText);     
     Element.replace('player',replacePlayer);
    
     // Open child window
     child_window = window.open (url, "child_", "height="+(height)+",width="+width+",resizable=no,status=no,toolbar=no,location=no");
   }
         
},

// reload externalUrl in child popup or url in same window as window has child or not
openConditionalExternalVideo: function(url,externalUrl, width, height){   
  
  if (external_opener_functions.hasChild()) {
     
     child_window.external_opener_functions.reloadWithAjax(externalUrl); 
     
    if (isIE() && !isIE6()) {
      width=width+11;
      height=height+79; //38
    } else if (isIE6()){
      width=width+10;
      height=height+36;
    } else {
      width=width;
      height=height+56;
    }
     
     child_window.resizeTo(width,height);
     child_window.focus();
     
     return false;
  } else {
  
     window.location=url;
     return false;
  }
   
},

// reload 'channel_specific_content' using Ajax (except <redirect> case)
reloadWithAjax: function(url) {

    new Ajax.Request(url + external_opener_functions.joiner(url) + 'ajax=1', {
      method: 'get',
      onSuccess: function(transport) {

        var notice = $('channel_specific_content');        

        // Not allowed channel: <redirect>url/notAllowed</redirect>
        if ((transport.responseText==null) || 
             (transport.responseText!=null && transport.responseText.include('<redirect>'))) {
             
             // prevent opener reload
             forbiddenChannelFlag = true;
             
             // remove <redirect> and reload parent with its redirect url 
             var regexp = /\x3c[^\x3e]+\x3e/g;                          
             opener.location = transport.responseText.replace(regexp, '');
                                     
             window.close();              
        } else {        
          notice.update(transport.responseText);
        }
      }
    });

},

// Add unload event that reload parent
initializeExternalStream: function(){ 
    
  Event.observe(window, 'unload', function(event) {
      
    // test if parent exists
    if ((!forbiddenChannelFlag) && (opener != null) && (opener != undefined) && ((opener.location != null) && (opener.location != undefined)) ) {    
         
      try {
        // goes to old opener url's channel            
        opener.location.reload();
      } catch(err) {
        //opener was refreshed because invalid credentials (and unload event didn't stop)
      }
    }
    
  });
  
},

// true if window has child
hasChild : function() {
    
    try {
    return !((child_window == undefined || child_window == null) || 
            (child_window != null && child_window != undefined && (child_window.document == null || child_window.document == undefined ))) ;
    } catch (err) {
      return false;
    }       
      
},

// return '&' as url has '?' or '?' if it hasn't  
joiner : function(url){
  
  return ((url == null) || (url.indexOf('?') == -1)) ? '?' : '&';
  
},

// Add unload event to window to close child
// TODO: not used, remove it when this won't be used 
initializeClosing : function(){
  
  Event.observe(window, 'unload', function(event) {      
    
    if (external_opener_functions.hasChild()) {
    
      try {
    
        Event.stopObserving(child_window.document, 'unload');
      
        child_window.close();
    
      } catch (err) {
        // child_window's reference lost
      }
       
      child_window = null;
    }
  });
},

// Creates an image instead of video's stream
// and changes size of $("upper_stream_video_bar") if it's too short
createReplacementElement : function(width,height,replacingText) {
      
     var offPlayer = new Element('div',{ 'id':'player' });
     offPlayer.style.height=height+"px";
     offPlayer.style.width= width + "px";
     offPlayer.style.backgroundColor='black';
     offPlayer.style.textAlign='center';   

     offPlayer.innerHTML = '<!-- -->';//<!-- -->&nbsp;';
       
     offPlayer = external_opener_functions.windowPText(replacingText,offPlayer);
     
     //var imgStyle = "position: absolute; margin-left: -204px; top: 50%;";
     //var imgPlayer = new Element('img', {'id':'externalTextVideo','src': externalVideoReplaceImage, 'align' : 'center'});   
     //offPlayer.appendChild(imgPlayer);
     
     return offPlayer;    
},

// Append a styled p-text to offPlayer and returns offPlayer
windowPText: function(text, offPlayer) {
      
   var text1 = new Element('table',{ 'class' : 'replace_streaming_text_table' });   
   try {
     text1.innerHTML = "<thead></thead><tbody><tr><td><p class='replace_streaming_text'>"+text+"</p></td></tr></tbody>";
   } catch(err) {
     
     // IE6 doesn't accept innerHTML in table element's type
     newRow = text1.insertRow(-1);     
     newCell = newRow.insertCell(0);
     newCell.innerHTML="<p class='replace_streaming_text'>"+text+"</p>";
 
   }
   offPlayer.appendChild(text1);
   
   return offPlayer;
},

// Append a h2-R-text to offPlayer and returns offPlayer 
windowText: function(text, offPlayer) {
   
   var style = "font-size: 20px; height: 20x; color:#111111";
      
   var text1 = new Element('div',{ });
   text1.innerHTML = "<h2 style='"+style+"'>"+text+"</h2>";
   header_images.do_replacement_with_length(text1.childElements()[0], -1);
   
   offPlayer.appendChild(text1);
   
   return offPlayer; 
}

};

