//Settings
        var requestTimeout = 7000; //Timeout in milliseconds
//Declarations
        var documentAlias = document,
        windowAlias = window,
        escapeWrapper = windowAlias.encodeURIComponent || escape,

        /*
        * decode or unescape
        * - decodeURIComponent added in IE5.5
        */
			unescapeWrapper = windowAlias.decodeURIComponent || unescape,
            configUrl = documentAlias.location.href;
        var configTrackerSiteId = "";

//Cookie functions
        function setCookie(cookieName, value, daysToExpire, path, domain, secure) {
            var expiryDate;

            if (daysToExpire) {
                // time is in milliseconds
                expiryDate = new Date();
                // there are 1000 * 60 * 60 * 24 milliseconds in a day (i.e., 86400000 or 8.64e7)
                expiryDate.setTime(expiryDate.getTime() + daysToExpire * 8.64e7);
            }

            documentAlias.cookie = cookieName + '=' + escapeWrapper(value) +
					                  (daysToExpire ? ';expires=' + expiryDate.toGMTString() : '') +
					                  ';path=' + (path ? path : '/') +
					                  (domain ? ';domain=' + domain : '') +
					                  (secure ? ';secure' : '');
        }

        /*
        * Get cookie value
        */
        function getCookie(cookieName) {
            var cookiePattern = new RegExp('(^|;)[ ]*' + cookieName + '=([^;]*)'),

					cookieMatch = cookiePattern.exec(documentAlias.cookie);

            return cookieMatch ? unescapeWrapper(cookieMatch[2]) : 0;
        }

//User data functions
        function getUserKey() {
            var cookieKey = "IMPloggcookie";
            var userKey = getCookie(cookieKey);
            if (userKey) {

            } else {
                userKey = createUUID();
            }
            setCookie(cookieKey, userKey,30);
            return userKey;
        }
        function getReferrer() {
            var referrer = '';
            try {
                referrer = top.document.referrer;
            } catch (e) {
                if (parent) {
                    try {
                        referrer = parent.document.referrer;
                    } catch (e2) {
                        referrer = '';
                    }
                }
            }
            if (referrer === '') {
                referrer = documentAlias.referrer;
            }
            return referrer;
        }
        //Creates unique userid
        function createUUID() {
            // http://www.ietf.org/rfc/rfc4122.txt
            var s = [];
            var hexDigits = "0123456789ABCDEF";
            for (var i = 0; i < 32; i++) {
                s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
            }
            s[12] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
            s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01

            var uuid = s.join("");
            return uuid;
        }

		getSessionKey = function () {

        var cookieKey = "IMPSession";

        var sessKey = getCookie(cookieKey);

        if (sessKey) {



        } else {

            sessKey = createUUID();

        }

        setCookie(cookieKey, sessKey);

        return sessKey;

    	};

//Build request with parameters
        function getRequest() {
            var i, now, request;
            now = new Date();
            request = 'siteid=' + configTrackerSiteId +
				        '&url=' + escapeWrapper(configUrl) +
				        '&urlref=' + escapeWrapper(getReferrer()) +
                        '&userkey=' + escapeWrapper(getUserKey()) +
						'&impsess=' + escapeWrapper(getSessionKey()) +
				        '&rand=' + Math.random();
            for (var key in siteProperties) {
                request += '&KeyValue=' + escapeWrapper(key) + ':' + escapeWrapper(siteProperties[key]);
            }
            return loggurl + request;
        }

        var siteProperties = new Array();

//display functions


         function hide_IMPiframe() {
           //  var element = document.getElementById("imp_container");
           // if (element) {
           //     element.parentNode.removeChild(element);
           // }
            setNoDisplay();//Dont show message more in this session
        }
        //
        var displayCookieKey = "IMPDispcookie";
        function setNoDisplay() {
            setCookie(displayCookieKey, 1);
        }
        function getDisplyAllowed() {
            var stopDisplay = getCookie(displayCookieKey);
            if (stopDisplay) {
                return false;
            } else {
                return true;
            }
        }

//Public functions
         function initsite(siteId) {
             configTrackerSiteId = siteId;
         }
         function appendSiteProp(key, value) {
             siteProperties[key] = value;
         }
         // no conflict
         var $j = jQuery.noConflict();

         function trackLogging() {
             $j.ajax({ url: getRequest(), dataType: 'json', timeout: requestTimeout, success: function (data) {
                 if (data) {
                     displayInfo(data.Caption, data.HtmlBody, data.InteractionLogId, data.ResponseUrl, data.Width, data.Height,data.delaySek, data.VisualEffect);
                 }
             }
             })
         }

