/* 
 * Google analytics.
 * tracking original sources
 */

/**
 * extracts utmz cookie parameters and values.
 */
function _uGC(l,n,s) {
    // used to obtain a value form a string of key=value pairs
    if (!l || l=="" || !n || n=="" || !s || s=="") return "";
    var i,i2,i3,c="";
    i=l.indexOf(n);
    i3=n.indexOf("=")+1;
    if (i > -1) {
        i2=l.indexOf(s,i);
        if (i2 < 0) {
            i2=l.length;
        }
        c=l.substring((i+i3),i2);
    }
    return c;
}

/**
 * check if its the user's first time on mineeds
 */

function checkFirst(){
    // check if this is a first time visitor
    newVisitor = 0;
    var myCookie = "" + document.cookie + ";";
    var searchName = "__utma=";
    var startOfCookie = myCookie.indexOf(searchName)
    if (startOfCookie == -1) {   // i.e. first time visitor
        newVisitor = 1;
    }
}

/**
 * gets the original source from utmz and stores it in utmv
 *
 */
function grabReferrer(){
    // grab campaign and referrer info from the _utmz cookie
    if (newVisitor) {
        var z = _uGC(document.cookie, "__utmz=", ";");
        urchin_source = _uGC(z,"utmcsr=", "|");
        urchin_medium = _uGC(z,"utmcmd=", "|");
        urchin_keyword = _uGC(z,"utmctr=", "|");
        urchin_campaign = _uGC(z,"utmccn=", "|");
        urchin_content = _uGC(z,"utmcct=", "|");
        // add a goolge to the variables-LaryS
        var gclid = _uGC(z,"utmgclid=","|");

        // extra params to track.
        var params = ["city", "state", "c_id", "subc_id", "target_site"];

        // custom cookie contents.
        var utmv_content = "utmcsr=" + urchin_source + "|utmccn=" + urchin_campaign+ "|utmcmd=" + urchin_medium + "|utmctr=" +urchin_keyword +"|utmcct=" + urchin_content;

        // utmv special cases
        if(window.location.href.match(/weddingsoftoday/i)) {
            params = ["city", "state", "subc_id", "target_site"];
            utmv_content = "utmcsr=Affiliate|utmccn=(not set)|utmcmd=WeddingsOfToday|utmctr=" +urchin_keyword +"|utmcct=" + urchin_content+"|c_id=64";
        }



        // append the extra params to the custom cookie.
        for (var index=0;index < params.length;index++) {
            utmv_content += "|"+params[index]+"="+get_parameter(params[index]);
        }

        pageTracker._setVar(utmv_content); // expand initial referrer data to capture med,term, and source- LaryS
    }
}
/**
 * parses the url and gets the values
 * @params: name - the parameter in the URL.
 *
 */
function get_parameter(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&#]"+name+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href.toLowerCase());

    if( results == null ) {
        return "";
    } else {
        return results[1];
    }
}

checkFirst();        // checks if this is a new visitor// Moved to execute befire the _getTracker function- LaryS
var pageTracker = _gat._getTracker("UA-2472393-1");
//var pageTracker = _gat._getTracker("UA-22735230-1");
pageTracker._initData();
pageTracker._trackPageview();
grabReferrer();      // Grab referrer info

