﻿$(document).ready(function() {
            
    //start session GUID cookie block
//    var sessionGuid = readCookie("LifetimeSessionGUID");
//    if (sessionGuid == null)
//    {
//        $.getJSON("/ClickTracker/GuidJson", function(data){
//            document.cookie = 'LifetimeSessionGUID=' + data + ';path=/';
//            sessionGuid = data;
//        });
//    }
    //end session GUID cookie block

    //begin click tracking block
    //code within this block calls public EmptyResult LogClick(string sessionID, string referringURL, string destinationURL, string locationID, int? coordX, int? coordY)

    //log external clicks.
//    if (document.referrer != null && document.referrer.indexOf(document.domain) < 0)
//    { //if the document referrer is not the current document domain then it will be logged as an external click.

//        //get the dynamic product page links from the menus into an array
//        var productLinks = $("#products ~ .DropDownMenu").children().children("a").map(function(){
//                            return $(this).attr("href").replace(/\//g, ""); //despite the fact that VS sees the // in the replace statement as the beginning of a comment, this is the correct syntax and compiles correctly.
//                            }).get();
//        productLinks.push($("#products").attr("href").split("?")[0].replace("/", "")); //add the products landing page.
//        
//        //get the locationID querystring if it exists.
//        var locID = getQueryString("locationID", document.URL);
//        
//        if (locID != null || inArray(productLinks, document.URL.split("?")[0].split("/")[3])) { //currently only tracking external clicks with the "locationID" query string or targeted at the products pages.
//            $.post("/ClickTracker/LogClick", { s2essionID: sessionGuid,
//                                                referringURL: document.referrer,
//                                                destinationURL: document.URL,
//                                                locationID: locID,
//                                                isExternalClick: true});
//        }
//    }

    //log internal clicks.
    $("a").live("click", function(event) {
        internalClickLogger(this, event);
    });
    //end click tracking block

    //start google KW block.
    if (document.referrer.indexOf("http://www.google.com") > -1)
    {//Saves google KW to cookie.
        document.cookie = 'LifetimeGoogleKW=' + getQueryString("q", document.referrer.substring(1)) + ';path=/';
    }

    var kw = readCookie("LifetimeGoogleKW");
    if (kw != null)
    {
       $("a[href^='http://www.buylifetime.com']").each(function() {  //for each link pointed at buylifetime.com....
                  if (this.href.indexOf('?') > -1) this.href = this.href + "&LifetimeGoogleKW=" + kw; //...append the LifetimeGoogleKW query string to existing query strings or,...
                  else this.href = this.href + "?LifetimeGoogleKW=" + kw; //...add new query string block with LifetimeGoogleKW.
               });
    }
    //end google KW block.
});

function internalClickLogger(element, event)
{
    var locID = ""; //location IDs should be followed by a , so that multiple IDs can be included for each click.
    
    if($(element).attr("href").search("locationID=") != -1) locID += getQueryString("locationID", $(element).attr("href")) + ","; //get the locationID query string value if it exists.
    if(($(element).parents("#divNavMenu").length ? true : false)) locID += "TopMenu,"; //set location ID to "TopMenu" if the link is in divNavMenu
    if(($(element).parents("#LeftNavigation").length ? true : false)) locID += "LeftMenu,"; //set location ID to "LeftMenu" if the link is in LeftNavigation
    if($(element).attr("href").search("icovia.aspx") != -1) locID += "Icovia,"; //set location ID to Icovia for the Icovia space planner.
    if(($(element).parents(".feedPanel1").length ? true : false)) locID += $(element).closest("div[id*=adPanel]").attr("id") + ","; //add locationID for the ad panels.
    if(($(element).parents("#ARFoldTeaserDiv").length ? true : false)) locID += "MainAdRotator,";
    if(($(element).parents("#blogPanel").length ? true : false)) locID += "BlogPanel,";
    if(($(element).parents("#tipsandsolutionsPanel").length ? true : false)) locID += "TipsAndSolutionsPanel,";
    if(($(element).parents("#videosPanel").length ? true : false)) locID += "VideosPanel,";
    if(($(element).parents("#adPanel").length ? true : false)) locID += "BottomAdPanel,";
    if(document.URL.indexOf("/international/") != -1) locID += "International,";

    $.post("/ClickTracker/LogClick", { sessionID: sessionGuid,
                                        referringURL: document.URL,
                                        destinationURL: element.href,
                                        locationID: locID,
                                        coordX: event.pageX,
                                        coordY: event.pageY,
                                        isExternalClick: false
                                    });

}

function flashClickLogger(sourceURL,destURL, pageX,pageY, isExtenernal) {

    $.post("/ClickTracker/LogClick", { sessionID: sessionGuid,
    referringURL: sourceURL,
    destinationURL: destURL,
        locationID: "Flash",
        coordX: pageX,
        coordY: pageY,
        isExternalClick: isExtenernal
    });
}

var sessionGuid = readCookie("LifetimeSessionGUID");
if (sessionGuid == null)
{
    $.post("/ClickTracker/GuidJson", function(data){
        document.cookie = 'LifetimeSessionGUID=' + data + ';path=/';
        sessionGuid = data;
    });
}

function inArray(array, val)
{
    for (var i=0;i<array.length;i++)
    {
        if (array[i] == val) return true;
    }
    return false;
}

function getQueryString(queryStringName, url)
{
    var arrURL = url.split("?"); //create array containing the base URL and query strings.
    if (arrURL[1] != null) //if there are query strings...
    {
        var qStrings = arrURL[1].split("&"); //create new array of individual query strings.
        for (var i=0;i<qStrings.length;i++) //for each element in the qString array...
        {
            var pair = qStrings[i].split("="); //create  a new array containing the name and value.
            if (pair[0] == queryStringName) return pair[1]; //if the query string name matches the name specified return the value.
        }
    }
    return null; //return null if all else fails.
}

function readCookie(name)
{
    var arrCookies = document.cookie.split(';'); //create a new array of all cookies which will look like {Cookie1=foo, Cookie2=bar}.
    for(var i=0;i < arrCookies.length;i++) //for each cookie in the array...
    {
        var cookie = arrCookies[i].replace(/^\s+/,"").split("="); //trim the space(s) off the left and make a new array containing the name and the value.
        if (cookie[0] == name) return cookie[1]; //if the name matches the one supplied, return the value.
    }
    return null; //if no matches, return null.
}
