// $Id$
/**
 * Wright IMC Primary Javascript File
 * written for Wright IMC by Anova Solutions (www.anovasolutions.com)
 * Copyright 2008 Wright IMC
 * ALL CODE CONTAINED HEREIN IS THE PROPERTY OF WRIGHT IMC WITH THE EXCEPTION
 * OF THE JQUERY JAVASCRIPT LIBRARY
 *
 * This code is dependent upon the jQuery Javascript library, which can be 
 * downloaded at http://www.jquery.com
 */
 
/** CONFIGURATION **/

/**
 * the name of the cookie that will be set when a visitor comes in via PPC or organic
 */
var sCookieName = "wimc-trk"; // whatever you want to name the cookie

/**
 * the number of days to have the cookie stick around
 */
var nDaysToLive = 3; // set to zero if you want the cookie to terminate when the browser is closed

/**
 * the DOM Element Id of the <img> tag that contains the image that will be swapped
 */
var sSwapId		= "wimc-header-img";   // DOM id of the image to swap


/**
 * the site's root URL...there *must* be a closing slash, eg 'http://www.google.com/'
 */
var sUrl = "http://www.hhrlaw.com/";

/**
 * the paths to the images that will be presented, dependent up on the referring source
 */
var sGImg	= sUrl +"images/phone-800-448-4164.jpg"; // image to swap for a Google refer
var sYImg	= sUrl +"images/phone-800-448-4164.jpg"; // image to swap for a Yahoo refer
var sMImg	= sUrl +"images/phone-800-448-4164.jpg"; // image to swap for a Microsoft refer

/**
 * the domain(s) for which the cookie will apply...this will probably need to be 
 * like ".mydomain.com" so that it will apply across all subdomains (the period at the start is key)
 */
var sDomain = ".hhrlaw.com";

/*********************************************
 ** CORE: Don't edit anything south of here **
 *********************************************/
$(document).ready(function(){

	var sRfr = determineReferer();
	
	if(sRfr != null)
	{
		switch(sRfr)
		{
			case "G":
				$("#" + sSwapId).attr("src",sGImg)
				break;
			case "Y":
				$("#" + sSwapId).attr("src",sYImg)
				break;
			case "M":
				$("#" + sSwapId).attr("src",sMImg)
				break;
			case "g":
				$("#" + sSwapId).attr("src",sGImg)
				break;
			case "y":
				$("#" + sSwapId).attr("src",sYImg)
				break;
			case "m":
				$("#" + sSwapId).attr("src",sMImg)
				break;
		}
		
		$.cookie(sCookieName, sRfr, { path: '/', expires: nDaysToLive, domain: sDomain });
	}
});

function determineReferer()
{
	var sRfr = null;
	
	// 1) look for a cookie
	if($.cookie(sCookieName) != null)
	{
		sRfr = $.cookie(sCookieName);
	}
	// 2) look for something from the querystring
	else if($.query.get('ppc').length > 0)
	{
		sRfr = $.query.get('ppc');
	}
	// 3) look at the referring url
	else
	{
		sTmp = extractReferrerFromUrl();
		if(sTmp)
		{
			sRfr = sTmp;
		}
	}
	
	return sRfr;
}

function extractReferrerFromUrl()
{
	var sArg = document.referrer;
	
	var oRe = new RegExp("^(http:\/\/[a-zA-Z0-9]+\.)(google|msn|yahoo)\.(com)");
	var rgMatches = oRe.exec(sArg);
	
	var sTmp = null
	if(rgMatches != null)
	{
		switch(rgMatches[2])
		{
			case 'google':
				sTmp = 'G';
				break;
			case 'yahoo':
				sTmp = 'Y';
				break;
			case 'msn':
				sTmp = 'M';
				break;
		}
	}
	
	return sTmp;
}