$(function() {
    
    var detect = {
        /**
         * Inspects the referrer page for google/yahoo search terms.
         * @return {string}
         */
        searchTerm: function() {
        
            // Grab the referring page
            var ref = document.referrer;
        
            // We're interested in the params only
            if (ref.indexOf('?') === -1) return;
        
            // Grab just the params
            var qs = ref.substr(ref.indexOf('?') + 1);
        
            // Split the params into an array
            var qsa = qs.split('&');
        
            for (var i = 0, len = qsa.length; i < len; i++) {
            
                // Split into key => value
                var qsip = qsa[i].split('=');
            
                // Need both key & value
                if (qsip.length === 1) continue;
            
                // q= for Google, p= for Yahoo
                if (qsip[0] == 'q' || qsip[0] == 'p') {
                    
                    // Decode, and convert +'s to single spaces
                    var term = unescape(qsip[1].replace(/\+/g, ' '));
                    
                    if ($.cookie) {
                        // Store it in a cookie for later use
                        $.cookie('searchTerm', term, {expires: 360, path: '/'});
                    }
                    
                    return term + ';';
                }
            }
        }        
    };
    
    // Grab the search term
    var searchTerm = detect.searchTerm();
    
    // Grab the hidden form element
    var ele = document.getElementById('searchKeywords');
    
    if (ele) {
        if (searchTerm) {
            // Store the search term
            ele.value = searchTerm;
        }
        else if ($.cookie('searchTerm')) {
            // Store the search term
            ele.value = $.cookie('searchTerm');
        }
    }
    
    /**
     * Sets the toll free number in the upper right part of the website
     * @param {string}
     */
    function setTollFreeNumber(utmSource) {
        var num;

        switch (utmSource) {
			case 'google':
				num = '888-245-6731';
			break;
			case 'yahoo': 
				num = '888-234-9718';
			break;
			case 'bing':
				num = '888-243-9253';
			break;
			case 'rm':
				num = '888-292-6546';
			break;
			case 'weblink':
				num = '888-255-0613';
			break;
			case 'email':
				num = '888-488-0499';
			break;
			case 'cons':
				num = '888-295-4635';
			break;
			case 'fb':
				num = '888-740-5850';
			break;
			case 'li':
				num = '888-808-0090';
			break;
			default:
			    num = '888-955-3520';
		}
	
		$('span.tel strong').show().text(num);
    }
    
    (function() {
        
    	var utmSource = $.getURLParam('utm_source'), num;

        if (utmSource) {
            $.cookie('_utmSource', utmSource, {expires: 360, path: '/'});
            setTollFreeNumber(utmSource);
        }
        else {
            utmSource = $.cookie('_utmSource');
            
            if (utmSource) {
                setTollFreeNumber(utmSource);
            }
        }
    	
		// Grab hawaii ref code
		var ref = $.getURLParam('ref');
		if (ref) {
			$.cookie('ref', parseInt(ref, 10), {
				expires: 360,
				path: '/',
				domain: '.freedomiq.com'
			});
		}

	})();
	
	if ($('input[name=promoReferrer]').length) {
		var ref = $.cookie('ref');
		if (ref) {
			$('input[name=promoReferrer]').val(ref);
		}
	}

});
