// NewsworthyAudioC2L.js source code
//©Copyright 2006 Newsworthy, Inc. All Rights Reserved

var x=new String(Math.random());
var m_oC2LWindow=null;    // Reference to the window's object
var m_iRefCount=0;        // Refernece counter (Guarantees that no infinte recursion occurs)
/*For Gr1 displaying page, set value to 585; else height=205*/
/*
//PostnewsweekTech
var m_sC2LWindowFeatures='location=0,menubar=0,status=0,width=365,height=600';

*/
//WashingtonTimes
var m_sC2LWindowFeatures='location=0,menubar=0,status=0,width=525,height=665';

/*
//COX
var m_sC2LWindowFeatures='location=0,menubar=0,status=0,width=358,height=650';
*/

var m_sC2LWindowName='_blank';
 



//All parameters except for UserName, Environment, and Debug are required
function OpenC2LWindow(PartnerKey,ArticleGUID,ArticleURL,PartnerSpecificParam,SiteKey,UserName,Environment,Debug)
{
    try
    {    
         

        var sHostName="";
//        switch(Environment)
//        {
//            case "QA":      //Quality Assurance
//                sHostName="http://QA.C2L.NewsworthyAudio.com/Partners/";
//                break;
//            
//            case "Test":    //Test Environment
//                sHostName="http://Test.C2L.NewsworthyAudio.com/Partners/";
//                break;            
//            case "Demo":    //Demo Environment
//                sHostName="http://Demo.C2L.NewsworthyAudio.com/Partners/";
//                break;
//            default:        //Default- Production
//                sHostName="http://C2L.NewsworthyAudio.com/Partners/";
//                break;
//        }
        //if (window.clientInformation.userAgent.indexOf( "MSIE " ) > 0)
    /*      var Ad0,Ad1,Ad3;
        
        var o=document.getElementById("ctl00_Content_ArticleURL");
        ArticleURL=o.options[o.selectedIndex].value;
         
        o=document.getElementById("ctl00_Content_SiteKey");
        SiteKey=o.options[o.selectedIndex].value;        
         
        o=document.getElementById("ctl00_Content_Ad0");
        if (o)
            Ad0=o.options[o.selectedIndex].value;   
        
        o=document.getElementById("ctl00_Content_Ad1");
        if (o)
            Ad1=o.options[o.selectedIndex].value;           
        
        o=document.getElementById("ctl00_Content_Ad3");
        if (o)
             Ad3=o.options[o.selectedIndex].value; 
             
      */ 
        PartnerSpecificParam="OAS_sitepage=" + document.getElementById("WTPageValue").value+ "";
        //Construct Url        
        
        var sUrl="";//sHostName + PartnerKey + "/" ;
        sUrl +="C2lPlayer.aspx?PartnerKey=" + PartnerKey;
        sUrl +="&SiteKey=" + SiteKey;
        sUrl +="&ArticleGUID=" + ArticleGUID;
        sUrl +="&ArticleURL=" + ArticleURL;
        sUrl +="&PartnerSpecificParam=" + PartnerSpecificParam;
        sUrl +="&UserName=" + typeof(UserName)=="string"?UserName:"";
       /*
//        if (typeof(Debug)=="string")
//        {
         sUrl +="&Debug=";
        sUrl +="&Ad0=http://" + location.hostname+ ":" + location.port+"/" + Ad0;
          sUrl +="&Ad1=http://" + location.hostname+ ":" + location.port+"/" + Ad1;          
          sUrl +="&Ad3=http://" + location.hostname+ ":" + location.port+"/" + Ad3;
          */
//        }
//      alert(sUrl);
//      return;
        
 //sUrl="C2lPlayer.aspx?PartnerKey=WashingtonTimes&SiteKey=WashingtonTimes&ArticleGUID=20070202-124744-1388R&ArticleURL=http://audio.washingtontimes.com/mp3/20070202-124744-1388R.mp3&PartnerSpecificParam=OAS_sitepage=www.washingtontimes.com/national";
 //sUrl="C2lPlayer.aspx?PartnerKey=WashingtonTimes&SiteKey=WashingtonTimes&ArticleGUID=20060912-100939-1986R&ArticleURL=http://audio.washingtontimes.com/mp3/20060912-100939-1986R.mp3&PartnerSpecificParam=OAS_sitepage=www.washingtontimes.com/audiotest";
   //  alert(sUrl);
        //If this window was launched before, kill it first
        if (m_oC2LWindow!=null)
        {
            if (!m_oC2LWindow.closed) 
                m_oC2LWindow.close();
            m_iRefCount=0;        // Reset counter
            m_oC2LWindow=null;
        }
 
        m_iRefCount++;    // increment counter
        m_oC2LWindow = window.open(sUrl, m_sC2LWindowName, m_sC2LWindowFeatures);
        m_oC2LWindow.focus();
    }
    catch(e)
    {
    
        //m_iRefCount<2 conditions makes sure that the recursive call occurs only once
        //if it was invoked once and the same exception is raised, the function will display 
        //an alert box with the error number and message.
        if ((e.number==-2147418094||e.number==-2147024891) && m_iRefCount<2)
        {
            /*
                Error number -2147418094 has the following massage associated with it...
                The callee (server [not server application]) is not available and disappeared; 
                all connections are invalid. The call did not execute.                
                ... which means that the client has closed the browser whose reference is stored in the 
                oC2LWindow object.
                
                Error number -2147024891 has this message
                Access is denied.
                                
                Set the oC2LWindow object to null and try again one more time.
             */
             oC2LWindow=null;
             OpenC2LWindow(PartnerKey,ArticleGUID,ArticleURL,PartnerSpecificParam,UserName,Environment,Debug);
        }
        else
            alert("Failed to execute OpenC2LWindow()\n\n"+ e.number+ "\n" + e.message);
    }
    
}
