// Get IP to Geolocation / Region from Google API
// Matches a sampling of surrounding regions and cities
// author: mcn 1/6/2009 -- updated for cochranfirm 6/26
// copyright 2009: page1solutions, llc
//

// -- Configuration --

//var debug = true;
var debug       = false;
var suffix      = '-texas';

// -- Functions --

// Gets document name from path
// for non-CMS urls - 
//   returns String
//
function get_docname (ref) {

  if (typeof(ref) == 'undefined' || ref == '') {
    return '';
  }  
  return ref.substr(ref.lastIndexOf('/')+1);
}

//
// Converts *.* document name to *suffix.*
// returns string.
// example : given suffix == '-texas'
//           index.html   => index-texas.html
//           oranges.html => oranges-texas.html
// @todo: nts- chris use regex next time please.
// 
function add_suffix (docname) {

  return docname.substr(0,docname.lastIndexOf('.')) + suffix + docname.substr(docname.lastIndexOf('.'));
}

//
// Google maps api object handler, 
// sets client City and State as terms
//
function initialize() {
    
    if (google.loader.ClientLocation && google.loader.ClientLocation.address.city && google.loader.ClientLocation.address.region) {
        clientCity  = google.loader.ClientLocation.address.city.toUpperCase();
        clientState = google.loader.ClientLocation.address.region.toUpperCase();
    }
}

// -- Script ----

google.load("maps", "2");

var clientCity  = ""; 
var clientState = ""; 

initialize();

var terms       = [clientCity, clientState];
var locations   = { 'texas':  ['TX', 'TEXAS'], 
                    'denver': ['DENVER', 'CO', 'COLORADO']};   
           
//
// Match a term in array against the given array of words, 
//     Return true if found, false otherwise.
//                  
function match_within (terms, words) {

    var matched = false;
    var wsize   = words.length;
    var tsize   = terms.length;

    if (wsize == 0 || tsize == 0) {
        return false;
    }
        
    for ( var i = 0; i < wsize; i++ ) {
    
        for ( var j = 0; j < tsize; j++ ) {
            var pat = new RegExp( words[i] );
            matched = pat.test( terms[j] );
            
            if (debug) {
                alert("match word pattern: " + words[i] + " with term: " + terms[j] + " result: " + matched); //IE
                //console.log("match word pattern: " + words[i] + " with term: " + terms[j] + " result: " + matched);
            }
            if (matched == true) return true;
        }
    }
    return false;
}

//
// If a term is matched within the words for a webclient's city/state, 
//    redirect the client to the appropriate page
//
switch (true) {
    
    case ( match_within(terms, locations['texas']) ) :
        //
        // @mcn 7/27 - bugfix for texans going to http://www.cochranfirm.com/-texas.html - Change hardcoded hostname to generic when reusing the code. Dont' have time to do it now, sorry!
        //
        if (window.location.pathname == '/' || window.location.href == 'http://www.cochranfirm.com/' || window.location.href == 'http://cochranfirm.com/') {
          window.location.replace('http://www.cochranfirm.com/index-texas.html');  
        }
        else {
          window.location.replace(add_suffix(get_docname(window.location.href))); 
        }
        break;
    
    // test case    
    //case ( match_within(terms, locations['denver']) ) :    
        // do nothing, for now
        
    default: 
        //do nothing.
}