﻿// Define Kpw object only if it's not already defined.

if ("undefined" == typeof Kpw) {Kpw = {};}

Kpw.Analytics = {
    
    // Log the data associated with a navigation event
    // (such as a link click).
    
    logNavigation: function(href) {
        Kpw.Analytics.publish('navigation', href);
    },
    
    // Log a content hit (image click, article view, etc.)
    
    logContent: function(id) {
        Kpw.Analytics.publish('content', id);
    },
    
    // Publishes analytics data mined during the course
    // of the user session.
    
    publish: function(type, data) {
        if(type != null){
            var publishUrl = 'analytics.kpw?type=' + type;
            $.post(publishUrl, data);
        }
    },
    
    // The following functions support the display of
    // the analytics dashboard.
    // -----------------------------------------------
    
    pageViewsByDateData: null,
    contentViewsByDateData: null,
    siteViewsByDateData: null,
    graphPlotArea: null,
    
    // Initialize the analytics graph display.
    
    initialize: function(pageViewsData, contentViewsData, siteViewsData, boundingElement) {
        Kpw.Analytics.pageViewsByDateData = pageViewsData;
        Kpw.Analytics.contentViewsByDateData = contentViewsData;
        Kpw.Analytics.siteViewsByDateData = siteViewsData;
        Kpw.Analytics.graphPlotArea = boundingElement;
    },
    
    // Render the specified graph data on the page.
    
    displayGraph: function(graphData) {
       
        Kpw.Analytics.graphPlotArea.css("class", "span-23 last");
        Kpw.Analytics.graphPlotArea.css("height", "250px"); 
        Kpw.Analytics.graphPlotArea.css("border", "");
        Kpw.Analytics.graphPlotArea.css("padding", "");
       
        if(graphData != null)
        {                       
            var options = {
                legend: {
                    show: true,
                    margin: 10,  
                    backgroundOpacity: 0.5
                },
                points: {
                    show: true,
                    radius: 4
                },  
                lines: {
                    show: true,
                    fill: true,
                    fillColor: "rgba(0,163,204,0.1)"
                },
                grid: {
                    hoverable: true,
                    borderWidth: 1,
                    borderColor: "silver"
                },
                xaxis: {
                    mode: "time",
                    timeformat: "%m/%d/%y",
                    minTickSize: [1, "day"]
                },
                yaxis: {
                    minTickSize: 1
                },        
               colors: ["#00a3cc"]
            };
            
            $.plot(Kpw.Analytics.graphPlotArea, graphData, options);
        }
        else {
            Kpw.Analytics.graphPlotArea.css("border", "solid 1px silver");
            Kpw.Analytics.graphPlotArea.css("padding", "5px");
            Kpw.Analytics.graphPlotArea.text("This data is not available at this time. Please check back later.");
        }
        
        var previousPoint = null;            
        
        Kpw.Analytics.graphPlotArea.bind("plothover", function (event, pos, item) {
            if (item) {
            
                // If the previous point is equal to the current point,
                // there is no need to re-render the tooltip.
                
                if (previousPoint != item.datapoint) {
                                        
                    previousPoint = item.datapoint;
                
                    // Remove any pre-existing tooltip, prior to adding the new one.
                        
                    $("#analyticsGraphTooltip").remove();
                    
                    // Display the tooltip, indicating the corresponding y-value.
                    
                    var count = item.datapoint[1].toFixed(0);
                    $('<div id="analyticsGraphTooltip">Views ' + count + '</div>').css( {
                        top: item.pageY - 30,
                        left: item.pageX + 5
                    }).appendTo("body").fadeIn(200);
                }
            }
        });
    },
    
    displayProfileGraph: function() {
        
        var graphData = null;
        
        if(Kpw.Analytics.pageViewsByDateData != null){
            graphData = 
                [
                    { label: "Profile Views", data: Kpw.Analytics.pageViewsByDateData }
                ]        
        }
        
        Kpw.Analytics.displayGraph(graphData);
        
    },
    
    displayContentGraph: function() {
        
        var graphData = null;
        
        if(Kpw.Analytics.contentViewsByDateData != null){
            graphData = 
                [
                    { label: "Content Views", data: Kpw.Analytics.contentViewsByDateData }
                ]        
        }
        
        Kpw.Analytics.displayGraph(graphData);
        
    },
    
    displaySiteGraph: function() {
        
        var graphData = null;
        
        if(Kpw.Analytics.siteViewsByDateData != null){
            graphData = 
                [
                    { label: "myKPW Visitors", data: Kpw.Analytics.siteViewsByDateData }
                ]        
        }
        
        Kpw.Analytics.displayGraph(graphData);
    },
    
    displayOverlayGraph: function() {
    
        var graphData = null;
        
        if(Kpw.Analytics.pageViewsByDateData != null && Kpw.Analytics.siteViewsByDateData != null){
            graphData = 
                [
                    { label: "Profile Visitors", data: Kpw.Analytics.pageViewsByDateData }, 
                    { label: "Content Views", data: Kpw.Analytics.contentViewsByDateData },
                    { label: "myKPW Visitors", data: Kpw.Analytics.siteViewsByDateData }
                ]        
        }
        
        Kpw.Analytics.displayGraph(graphData);
    }
}
