/**
 * 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: '',
  phone_list: [],
  image_list: [],
  called: false,
  debug: false,

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

    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');
    return window.location.href.match(regex);
  },

  _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 = document.cookie.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);
  }
};

/**
 * Configuration
 *
 * images_dir: the directory holding the images
 */
PhoneSwitcher.images_dir = '/wp-content/themes/deroberts2/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: '951.506.1040', to: '951.813.2209', source: 'Googleppc'});
PhoneSwitcher.addNumber({from: '(951) 506-1040', to: '(951) 813-2209', 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 : selector take css - ids w/#; select classes, etc - tag.div tag;
 * type: optional, background or src (default is background)
 * keywords: optional, keywords that must be in the URL
 */
PhoneSwitcher.addImage({src: 'call-us-at-315-779-4139.gif', source: 'Googleppc', selector: '.phone'});
PhoneSwitcher.addImage({src: 'call-Facebook.jpg', source: 'facebook', selector: '#top-margin-phone-number'});

jQuery(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);


