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

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

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

   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]);
     }
   }

	this.called = true;
 },

 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.toLowerCase() == this._getReferalValue().toLowerCase();
 },

 _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));
   jQuery('body *').replaceText(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);
   }

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

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

function phone() {
 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 = '\\(480\\) 945-3300';
PhoneSwitcher.image_selector = '.phone img';
PhoneSwitcher.images_dir = '/i/';

/**
* 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: '(602) 904-6723', source: 'google', image: 'phone_602-904-6723.jpg'});

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

