/**
 * New phone switching code
 *
 * Replace phone numbers easily on a website.
 *
 * How to use this script:
 * - Copy this script (phone.js) to the root directory of the site
 * - Add the following 2 lines to the footer of the site, right before </body>
 * <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 * <script type="text/javascript" src="phone.js"></script>
 * - Edit the bottom of this file with the proper numbers, images, referrers, etc.
 *
 * Future changes
 * - Support for multiple numbers (ex. Call (800) 123-1234 or 504-555-5555)

 * @author Luke Ledet <lledet@searchinfluence.com>
 * @created 2010-06-09
 *
 * Changelog
 * - 2010-06-15: Changed the _getReferalValue() to use a regular expression
 *               instead of nested loops and conditionals.
 * - 2010-06-09: Created
 */
var PhoneSwitcher = {
 number: null,
 image_container: '',
 image_selector: '',
 images_dir: '',
 phone_list: [],

 set: function() {
   for(var i in this.phone_list) {
     if (this._matchSource(this.phone_list[i]) && this._matchKeywords(this.phone_list[i])) {
       this._change(this.phone_list[i]);
     }
   }
 },

 add: function(phone) {
   this.phone_list.push(phone);
 },

 _matchKeywords: function(phone) {
   if (!phone.keywords)
     return true;

   var regex = new RegExp(phone.keywords, 'gi');
   return window.location.href.match(regex);
 },

 _matchSource: function(phone) {
   if (!phone.source)
     return true;

   return phone.source == this._getReferalValue();
 },

 _getReferalValue: function() {
   //?utm_source=GooglePPCutm_medium=Banner&utm_campaign=Ad1Image1
   if (matched = window.location.href.match(/utm_source=(.*?)&/))
     return matched[1];

   if (matched = document.cookie.match(/utmcsr=(.*?)\|/))
     return matched[1];

   return 'self';
 },

 _change: function(phone) {
   // Use this instead of replace(//) because that way doesn't support variables in the regex
   var regex = new RegExp(this.number, 'gi');

   // Replace the number in the body
   jQuery('body').html(jQuery('body').html().replace(regex, phone.number));

   if (phone.image) {
     // Change the background image of an element
     if (this.image_container)
       jQuery(this.image_container).css('background-image', 'url(' + this.images_dir + phone.image + ')');

     // Replace the image
     if (this.image_selector)
       jQuery(this.image_selector).attr('src', this.images_dir + phone.image);
   }

   console.log('Switched ' + this.number + ' with ' + phone.number);
 }
};

// Legacy support
function setPhone() {
 PhoneSwitcher.set();
}

/**
* Configuration
*
* image_container: a CSS selector of the element with a background image of the phone number (ex: #header strong.phone)
* image_selector: a CSS selector of the image to be replaced (ex: #phone_image) usually used in place of image_container, not together
* number: the current number that will be replaced (ex: 504-208-3900)
*/
PhoneSwitcher.image_selector = '#phone_number';
PhoneSwitcher.images_dir = '/';
PhoneSwitcher.number = '631-473-7070';

/**
* Adding numbers to switch
*
* Keys:
* number: required, replace with this number
* image: optional, replace with this image based on PhoneSwitcher.image_container or PhoneSwitcher.image_selector
* source: optional, the referrer (ex: Facebook, GooglePPC, YahooPPC) If this is not set, the number will be switched regardless of the source
* keywords: optional, keywords that must be in the URL
*/
PhoneSwitcher.add({number: '631-791-9875', image: 'number-facebook.jpg', source: 'facebook'});
PhoneSwitcher.add({number: '631-791-9591', image: 'number-GooglePPC.jpg', source: 'googleppc'});
PhoneSwitcher.add({number: '631-791-9877', image: 'number-Yahoo.jpg', source: 'yahoo'});

PhoneSwitcher.set();

