// NewsworthyAudioC2L.js source code
//©Copyright 2006, 2007 Newsworthy, Inc. All Rights Reserved

var m_oC2LWindow=null;    // Reference to the window's object
var m_iRefCount=0;        // Reference counter (Guarantees that no infinite recursion occurs)

/*For Gr1 displaying page, change values to 525 and 750*/
var m_sC2LWindowFeatures='location=0,menubar=0,status=0,width=525,height=750';
/*var m_sC2LWindowFeatures='location=0,menubar=0,status=0,width=664,height=530'*/
var m_sC2LWindowName='_blank';
var m_nwamp3_file='';
var m_nwauri='http://c2l.media.newsworthyaudio.com/Partners/';

//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 (PartnerKey == "AP" || PartnerKey == "WashingtonTimes") m_sC2LWindowFeatures='location=0,menubar=0,status=0,width=664,height=530';
        
	    if (ArticleURL == "") ArticleURL = m_nwamp3_file;    

        //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:"";
        sUrl +=typeof(Debug)=="string"?"&Debug=":"";
              

        //If this window was launched before, kill it first
        if (m_oC2LWindow!=null)
        {
            m_iRefCount=0;        // Reset counter
            m_oC2LWindow=null;
        }
        
        //var s = PartnerKey + '-' + ArticleGUID + '-' + ArticleURL + '-' + PartnerSpecificParam + '-' + SiteKey + '-' + UserName + '-' + Environment + '-' + Debug;
        //alert('In C2lWindow ' + s);     
        //alert('In C2lWindow ' + sUrl);

        m_iRefCount++;    // increment counter
        m_oC2LWindow = window.open(sUrl, m_sC2LWindowName, m_sC2LWindowFeatures);
        
        if (m_oC2LWindow == null)
        {
             alert("Failed to launch Click-2-Listen Window.\nCheck to see if Pop-ups are disabled and try again.\n");
        }
    }
    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.
             */
        //var s = PartnerKey + '-' + ArticleGUID + '-' + ArticleURL + '-' + PartnerSpecificParam + '-' + SiteKey + '-' + UserName + '-' + Environment + '-' + Debug;
        //alert(e);
        //alert(s);
        //alert(sUrl);
        
             oC2LWindow=null;
             OpenC2LWindow(PartnerKey,ArticleGUID,ArticleURL,PartnerSpecificParam,UserName,Environment,Debug);
        }
        else
            alert("Failed to execute OpenC2LWindow()\n\n"+ e.number+ "\n" + e.message);
    }
    
}


/*
    
    Suggestion: Call method in Page Load event
    Description: Executes inline script to check the availability of the current page’s audio article from Newsworthy’s server. 
            If the nwa_mp3_exists is true, then show; otherwise hide.
    Parameters:
    partnerKey		Current partners key 
    siteKey		Current site key
    articleGUID		Current article key
    linkID		C2L hyperlink or icon or div container id;
    linkID2             SUpport for a second C2L hyperlink on the same page for the same article;

*/
function SetC2LVisibility(partnerKey,siteKey,articleGUID,linkID,linkID2) 
{

    try 
    {
        
        var obj=document.getElementById(linkID); // get the link/icon container object

        if (typeof(nwamp3) == "undefined") return;

        if (nwamp3)                     // Does the mp3 file exist?
        {
            
            if (partnerKey == "WashingtonTimes" || partnerKey == "washingtontimes" || partnerKey == "PostNewsweekTech" || partnerKey == "postnewsweektech" || partnerKey == "AP" || partnerKey == "ap")
            {
                 m_nwamp3_file = m_nwauri + partnerKey + "/Audio/" + articleGUID + ".mp3";
            }
            else
            {
                 m_nwamp3_file = m_nwauri + partnerKey + "/Audio/" + siteKey + "/" + articleGUID + ".mp3";
            }
                    
          
            
            /**CONFIGURABLE:
            If visiblity is controlled with visibility style; make sure the class (e.g. c2l-link) has visibility:hidden (preferred)
            or use display:none. Depending on that setting, make the necessary adjustment in the lines below. **/
            
                //If linkID container exists, make it visible  
                if (obj!=null)
                    obj.style.visibility='visible';   // Make visible
                //  obj.style.display='none';     // Turn off the display  
                //  obj.style.display='inline';   // Set display on
 
               
                if (typeof(linkID2) != "undefined") 
                {
                        obj=document.getElementById(linkID2); 
                        if (obj==null) return;  
                        obj.style.visibility='visible'; // Make visible

                        //  obj.style.display='none';     // Turn off the display  
                        //  obj.style.display='inline';     // Set to display
		        }  
        }

    }
    catch(e)
    {
        //alert("SetC2LVisibility error\n" + e.message);  // Swallow all errors   
    }

}

