﻿function ClickDensityCommon()
{
    this.loadScript = function(url)
	{
	    // Don't send anything unless the page is being viewed as a web page
	    if ((window.location.protocol == "http:") || (window.location.protocol == "https:"))
	    {
	        var scriptTag = document.createElement("script");
	        scriptTag.setAttribute("type", "text/javascript");
	        scriptTag.setAttribute("src", url);
	        
	        document.getElementsByTagName("head")[0].appendChild(scriptTag);
	
	        return true;
	    }
	    else
	    {
	        return false;
	    }
	}
	
    this.getCookie = function(sName)
    {
        var aCookie = document.cookie.split("; ");
        for (var i=0;i<aCookie.length;i++)
        {
            var aCrumb = aCookie[i].split("=");
            if (sName == aCrumb[0])
                return aCrumb[1];
        }
        return null;
    }
}

function clickdensity_init()
{
    try
    {
        // quit if this function has already been called
        if (arguments.callee.done) return;

        // flag this function so we don't do the same thing twice
        arguments.callee.done = true;

        // use clickdensity_siteID by default falling back to siteID
        if (typeof(clickdensity_siteID) == 'undefined')
            // No 'var' as they may be specified in the web page - we need it to be global anyway
            clickdensity_siteID = siteID;
        
        // use clickdensity_keyElement by default falling back to keyElement
        if (typeof(clickdensity_keyElement) == 'undefined')
            // No 'var' as they may be specified in the web page - we need it to be global anyway
            clickdensity_keyElement = keyElement;
        
        // use clickdensity_keyElement by default falling back to keyElement
        if (typeof(clickdensity_pageID) == 'undefined' && typeof(pageID) != 'undefined')
            // No 'var' as they may be specified in the web page - we need it to be global anyway
            clickdensity_pageID = pageID;
        
        clickdensityCommon = new ClickDensityCommon();

        if (window.location.protocol == "https:")
            clickdensityCommon.loadScript('https://j.clickdensity.com/clicks.js');
        else
            clickdensityCommon.loadScript('http://j.clickdensity.com/clicks.js');
    }
    catch (e)
    {}
}

var clickdensity_DomLoaded =
{
	onload: [],
	loaded: function()
	{
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		for (i = 0;i < clickdensity_DomLoaded.onload.length;++i)
		    clickdensity_DomLoaded.onload[i]();
	},
	load: function(fireThis)
	{
		this.onload.push(fireThis);
		if (document.addEventListener) 
			document.addEventListener("DOMContentLoaded", clickdensity_DomLoaded.loaded, null);
		if (/KHTML|WebKit/i.test(navigator.userAgent))
			var _timer = setInterval(function()
			{
				if (/loaded|complete/.test(document.readyState))
				{
					clearInterval(_timer);
					delete _timer;
					clickdensity_DomLoaded.loaded();
				}
			}, 10);
		
        /* <JasobNoObfs> */
		/*@cc_on @*/
		/*@if (@_win32)
		var proto = "javascript:void(0)";
		if (location.protocol == "https:") proto = "src=//0";
		document.write("<scr"+"ipt id=clickdensity__ie_onload defer src=" + proto + "><\/scr"+"ipt>");
		var script = document.getElementById("clickdensity__ie_onload");
		script.onreadystatechange = function() {
		    if (this.readyState == "complete")
		        clickdensity_DomLoaded.loaded();
		};
		/*@end @*/
        /* </JasobNoObfs> */
	    var oldOnLoad = window.onload;
        if (typeof oldOnLoad != 'function')
            window.onload = clickdensity_DomLoaded.loaded;
        else
            window.onload = function()
            {
                oldOnLoad();
                clickdensity_DomLoaded.loaded();
            }
	}
};

var clickdensity;
var clickdensityCommon;
var clickdensityABTest;

try
{
    clickdensity_DomLoaded.load(clickdensity_init);
}
catch (e)
{}
