/**
 * 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.
 *
 * @author Luke Ledet <lledet@searchinfluence.com>
 * @created 2010-06-09
 */
var PhoneSwitcher = {
  images_dir: '',
  referral_data: '',
  phone_list: [],
  image_list: [],
  called: false,
  debug: false,

  set: function() {
    if(this.called) return;

    // If our cookie doesn't exist, get data from Google's cookie and set our
    // cookie.
    if (document.cookie.indexOf('si_ref_data') == -1) {
      if(this.debug)
        console.log('si cookie not found');

      this.referral_data = this._get_cookie('__utmz');

      this._set_cookie('si_ref_data', this.referral_data);
    } else {
      if(this.debug)
        console.log('si cookie found');

      this.referral_data = this._get_cookie('si_ref_data');
    }

    if(this.debug)
      console.log('Source: ' + this._getReferalValue());

    for(var i=0; i<this.phone_list.length; i++)
      if (this._matchSource(this.phone_list[i]) && this._matchKeywords(this.phone_list[i]))
        this._change_number(this.phone_list[i]);

    for(var i=0; i<this.image_list.length; i++)
      if (this._matchSource(this.image_list[i]) && this._matchKeywords(this.image_list[i]))
        this._change_image(this.image_list[i]);

    this.called = true;
  },

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

  addImage: function(image) {
    this.image_list.push(image);
  },

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

    var regex = new RegExp(phone.keywords, 'gi');

    if (matched = window.location.href.match(/utm_keyword=(.*?)(&|$)/)) {
      return matched[1].match(regex);
    }

    if (matched = this.referral_data.match(/utmctr=(.*?)(;|\||$)/)) {
      return matched[1].match(regex);
    }

    return false;
  },

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

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

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

    if (matched = this.referral_data.match(/utmcsr=(.*?)(;|\||$)/))
      return matched[1];

    return 'self';
  },

  _change_number: function(phone) {
    // Use this instead of replace(//) because that way doesn't support variables in the regex
    var regex = new RegExp(phone.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'gi');

    // Replace the number in the body
    jQuery('body *').replaceText(regex, phone.to);

    if (this.debug)
      console.log('Switched ' + phone.from + ' with ' + phone.to);
  },

  _change_image: function(image) {
    if(image.type == 'src')
      jQuery(image.selector).attr('src', this.images_dir + image.src);
    else
      jQuery(image.selector).css('background-image', 'url(' + this.images_dir + image.src + ')');

    if (this.debug)
      console.log('Switched ' + image.selector + ' with ' + image.src);
  },

  /* from http://www.elated.com/articles/javascript-and-cookies/ */
  _get_cookie: function (cookie_name) {
    var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results)
      return (unescape (results[2]));
    else
      return null;
  },

  _set_cookie: function (cookie_name, value) {
    // Expires in two years
    expire_date = new Date();
    expire_date.setDate(expire_date.getDate() + 730);

    document.cookie = cookie_name + "=" + value + ";expires=" + expire_date;
  }
};

/**
 * Configuration
 *
 * images_dir: the directory holding the images
 */
//PhoneSwitcher.images_dir = '/wp-content/themes/dr_martin_wp/images/';

/**
 * Adding numbers to switch
 *
 * Keys:
 * from: required, replace this number
 * to: required, replace with this number
 * 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.addNumber({from: '1.866.654.2226', to: '1.866.535.2839', source: 'GooglePPC1'});
PhoneSwitcher.addNumber({from: '1.866.654.2226', to: '1.866.377.0865', source: 'GooglePPC2'});
PhoneSwitcher.addNumber({from: '1.866.654.2226', to: '1.866.660.8840', source: 'GooglePPC3'});
PhoneSwitcher.addNumber({from: '1.866.654.2226', to: '1.866.615.3634', source: 'GooglePPC4'});
PhoneSwitcher.addNumber({from: '1.866.654.2226', to: '1.866.646.9625', source: 'GooglePPC5'});
PhoneSwitcher.addNumber({from: '1.866.654.2226', to: '1.866.620.4145', source: 'googlePPC'}); 
**/

/**
 * Adding images to switch
 *
 * Keys:
 * src: required, name of the new image
 * source: optional, the referrer (ex: Facebook, GooglePPC, YahooPPC) If this is not set, the image will be switched regardless of the source
 * selector: jQuery selector of the container of the image
 * type: optional, background or src (default is background)
 * keywords: optional, keywords that must be in the URL
 */
//PhoneSwitcher.addImage({src: 'call-GooglePPC.jpg', source: 'GooglePPC', selector: '#top-margin-phone-number'});
//PhoneSwitcher.addImage({src: 'call-Facebook.jpg', source: 'Facebook', selector: '#top-margin-phone-number'});

/*jQuery(function() {
  PhoneSwitcher.set();
});*/
jQuery(document).ready(function() {
  PhoneSwitcher.set();
});

/*
 * jQuery replaceText - v1.1 - 11/21/2009
 * http://benalman.com/projects/jquery-replacetext-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){$.fn.replaceText=function(b,a,c){return this.each(function(){var f=this.firstChild,g,e,d=[];if(f){do{if(f.nodeType===3){g=f.nodeValue;e=g.replace(b,a);if(e!==g){if(!c&&/</.test(e)){$(f).before(e);d.push(f)}else{f.nodeValue=e}}}}while(f=f.nextSibling)}d.length&&$(d).remove()})}})(jQuery);


