/* ============================================ 

 Author:   Lee W Butler, May 24 20010, LeeWButler(AT)gmail(DOT)com
 
 About:    These functions allow a flag value to be appended to a URL. The values presence triggers an alternative 
           image to be shown. A cookie is also set to persist the flag so that the alt-image can be shown for a 
           longer period of time, and across page views and subdomains.
           
           There are three images in play. 1) A Placeholder, 2. The Default, 3. The Alt. The alt image gets swapped 
           in if the flag is present - the default image if the flag is not. To avoid the swaps blink-effect, use 
           a transparent placeholder image. For an even smoother effect, make your default & alt images animated 
           gifs that fade into view.
         
=============================================== */



/* CONFIG
----------------------------------------- */

var url_flag_to_trigger_alt_img   = "gclid=";     // Will show the alt image, if this value is found in your url. 
var url_flag_to_trigger_dflt_img  = "phone=reset";   // For dev purposes. Appended to a URL to stop the display of the alt image.
var days_to_display_alt_img       = 4;     // How long the alt image should display.
var alt_img_src                   = "http://www.wetplanetwhitewater.com/resources/images/touts/phonePPC.gif";     // Alt image file.
var dflt_img_src                  = "http://www.wetplanetwhitewater.com/resources/images/touts/phoneOrganic.gif";    // Default image file.
var img_id                        = "XcallTodayPhone";     // The html ID of the image you want to swap.
var domain                        = "www.wetplanetwhitewater.com";     // To be effective across subdomains, leave the subdomain blank. (No "www", etc.)



/* LIBRARY
----------------------------------------- */

function getCookieVal (cookiename){
  // Returns the cookie value or false
  var val = document.cookie.match ( '(^|;) ?' + cookiename + '=([^;]*)(;|$)' ); 
  return  val ? unescape(val[2]) : false;
}

function isValOnUrl(val){  
  url = window.location.href;  
  return (url.indexOf(val) != -1) ? true : false; 
}

function isAltImgCookieSet(){   
  return (getCookieVal("showAltImg") == "yes") ? true : false;
}

function setAltImgCookie(){ 
  var cdate = new Date();   // current date & time
  cdate.setTime(cdate.getTime() + (1000 * 60 * 60 * 24 * days_to_display_alt_img));
  document.cookie = "showAltImg=yes; domain=" + domain + "; expires=" + cdate.toGMTString();  
}

function deleteAltImgCookie(){   
  var cdate = new Date();   // current date & time
  cdate.setTime(cdate.getTime() - 1); // set to the past
  document.cookie = "showAltImg=; domain=" + domain + "; expires=" + cdate.toGMTString();  
}

function changeImg(newsrc){ 
  var img = document.getElementById(img_id); 
  if(typeof img != "undefined"){  
    img.src = newsrc; 
  }  
}


/* MAIN UTILITY
----------------------------------------- */
function doImgSwap(){ 

  // Is there a URL flag?
  if(isValOnUrl(url_flag_to_trigger_alt_img)){
    setAltImgCookie();
  }else if(isValOnUrl(url_flag_to_trigger_dflt_img)){
    deleteAltImgCookie();
  } 
  
  // Or is there a cookie flag?
  if(isAltImgCookieSet()){
    changeImg(alt_img_src);
  }else{
    changeImg(dflt_img_src);
  }
}


/* DO IT
----------------------------------------- */
// Fire on every page load...

// www.WetPlanetWhiteWater.com uses motools.js
// so we can use its domeready event
window.addEvent('domready',doImgSwap); 

// mytrip.WetPlanetWhiteWater.com uses prototype.js 
// so we can use its event observe.
// Event.observe(window, 'load', function() { doImgSwap.init() }); 

