// Scripts used by the "Featured Partners" divs

function displayPicture(szDivID){
   var szOutput = "";
   var szFeaturedString = "";
   var nArrayLength = 0;   
   // Get the featured string based on the selected div ID
   // The arrays and their counters are declared globally in default-test.asp
   if (szDivID.indexOf("featlib")!=-1){
        if (aFeaturedLib.length > 0)        
            szFeaturedString = aFeaturedLib[nLibDisplayCount].toString();
        else {
            // if it's an empty array, set up a spacer record
            aFeaturedLib[aFeaturedLib.length] = "../spacer.gif|http://www.overdrive.com|60|Coming Soon";
            szFeaturedString = aFeaturedLib[0].toString();
        }
        nArrayLength = aFeaturedLib.length;
        nLibDisplayCount = controlCounter(nLibDisplayCount,nArrayLength);
   } else if(szDivID.indexOf("featpub")!=-1){
        if (aFeaturedPub.length > 0)
            szFeaturedString = aFeaturedPub[nPubDisplayCount].toString();
        else {
            // if it's an empty array, set up a spacer record
            aFeaturedPub[aFeaturedPub.length] = "../spacer.gif|http://www.overdrive.com|60|Coming Soon";
            szFeaturedString = aFeaturedPub[0].toString();
        }
        nArrayLength = aFeaturedPub.length;
        nPubDisplayCount = controlCounter(nPubDisplayCount,nArrayLength);
   } else {
        if (aFeaturedRet.length > 0)
            szFeaturedString = aFeaturedRet[nRetDisplayCount].toString();
         else {
            // if it's an empty array, set up a spacer record
            aFeaturedRet[aFeaturedRet.length] = "../spacer.gif|http://www.overdrive.com|60|Coming Soon";
            szFeaturedString = aFeaturedRet[0].toString();
        }
        nArrayLength = aFeaturedRet.length;
        nRetDisplayCount = controlCounter(nRetDisplayCount,nArrayLength);
   }
   
    // The szFeaturedString is expected in the following format:
    // File Name|Redirect URL|Timeout|Alt Text   
    var szFileName = szFeaturedString.split("|")[0].toString();  
    var szRedirectURL = szFeaturedString.split("|")[1].toString();      
    var szTimeout = szFeaturedString.split("|")[2].toString();  
    var szAltText = szFeaturedString.split("|")[3].toString();  

    // href and img variables
    var szFeatHref = document.getElementById(szDivID + "href");
    var szFeatImg = document.getElementById(szDivID + "img");
    // Set up the link but don't link to publishers
    if (szFeatHref != null && szDivID.indexOf("pub")==-1){
        szFeatHref.href = szRedirectURL;
        szFeatHref.target = "_blank";
    }
    // Show the AltText as Link Text for mobile users
    if (szFeatHref != null && szFeatImg == null){
        szFeatHref.innerHTML = szAltText;
    }
    
    // Hide the mobile "noscript" divs
    // <noscript> does not work well with Pocket PC 2003 or Smartphone 2003
    var mobilelibnoscript = document.getElementById("mobilelibnoscript");
    if (mobilelibnoscript != null){
        mobilelibnoscript.innerHTML = "";
    }
    var mobilepubnoscript = document.getElementById("mobilepubnoscript");
    if (mobilepubnoscript != null){
        mobilepubnoscript.innerHTML = "";
    }
    var mobileretnoscript = document.getElementById("mobileretnoscript");
    if (mobileretnoscript != null){
        mobileretnoscript.innerHTML = "";
    }
    
    // Set up the image
    if (szFeatImg != null){        
            szFeatImg.alt = szAltText;
            szFeatImg.title = szAltText; // For the people who want "the hover text" to show up while hovering over an image
            szFeatImg.src = "Images/partners/" + szFileName;                
    }
    setTimeout("displayPicture(\"" + szDivID + "\")", szTimeout*1000);
}

function controlCounter(nControlCounter, nArrayLength){
    if (nControlCounter == null)
        nControlCounter = 0;
    else if ((nControlCounter + 1) < nArrayLength)
        nControlCounter++;
    else 
        nControlCounter = 0;
    return nControlCounter;
}

function preLoadImages(aImagesToPreload){
    for (var nPreloadCounter=0; nPreloadCounter < aImagesToPreload.length; nPreloadCounter++){
      var szFileName = aImagesToPreload[nPreloadCounter].split("|")[0].toString();    
      var imgtest = new Image();
      imgtest.src = "Images/partners/" + szFileName;
    }
}