if (typeof __LOGGER_JS_TIMEOUT == "undefined"){
	var __LOGGER_JS_TIMEOUT = null;

	/*function logStudy(itemType, itemId, courseOpenId){
		if (__LOGGER_JS_TIMEOUT != null){
	    	clearTimeout(__LOGGER_JS_TIMEOUT);
            __LOGGER_JS_TIMEOUT = null;
	    }


	    if ((itemType != 10 && itemType != 20)){
	    	var key = new UUID().id.toLowerCase();
	    	var url = Logger.getLogUrl(itemId) + "&vs="+key;
	    	var position = 0;
	    	var completed = false;
	    	var evt = {key:key, url:url, position:position, completed:completed};
	    	studyStart(url);
	    	setLogStudy(evt);
	    }
	}*/

	function setLogStudy (evt){
		Logger.heart(evt);
        __LOGGER_JS_TIMEOUT = setTimeout(function() {setLogStudy(evt)}, 100);
	}

	function studyStart(url){
		url = url+"&at=10&sp=0";
		$.ajax({
			type:'get',
			url:url
		});
	}

	var Logger = {
	    getCookies : function() {
	        var logCookies = {};
	        var cookies = document.cookie.split('; ');
	        for (var i=0; i<cookies.length; i++) {
	            var split = cookies[i].split('=');
	            logCookies[split[0]] = unescape(split[1]);
	        }
	        return logCookies;
	    },
	    queue : [],
	    get : function(key) {
	        for (var i = 0; i < this.queue.length; i++) {
	            if (this.queue[i].key == key) {
	                return this.queue[i];
	            }
	        }
	        return null;
	    },
	    put : function(item) {
	        for (var i = 0; i < this.queue.length; i++) {
	            if (this.queue[i].key == item.key) {
	                this.queue[i].completed = item.completed;
	                this.queue[i].position = item.position;
	                this.queue[i].url = item.url;
	                return this.queue[i];
	            }
	        }
	        this.queue.push({key: item.key, url: item.url, position: item.position, completed: item.completed, timerId: null});
	        return this.queue[this.queue.length-1];
	    },
	    remove : function(obj) {
	        for (var i = 0; i < this.queue.length; i++) {
	            if (this.queue[i].key == obj.key) {
	                this.queue.splice(i, 1);
	                return;
	            }
	        }
	    },
	    start: function(item) {
	        if (typeof item == "undefined") return;
	        item.timerId = window.setTimeout(function() {
	            Logger.write([{key: item.key, url: item.url, position: item.position, completed: item.completed}])
	        }, 500);
	    },
	    stop : function(item) {
	        var id;
	        if (item) {
	            id = item.timerId;
	            if (id!=null) window.clearTimeout(id);
	            item.timerId = null;
	        } else {
	            for (var i = 0; i < this.queue.length; i++) {
	                id = this.queue[i].timerId;
	                if (id!=null) window.clearTimeout(id);
	                this.queue[i].timerId = null;
	            }
	        }
	    },
	    debug: function(item) {
	        //console.info(["key:",item.key,"pos:",item.position,"timerId:",item.timerId].join(","))
	    },
	    heart : function(evt) {
	        var item = Logger.put(evt);
	        Logger.stop(item);
	        if (item.completed) return;
	        Logger.start(item);
	    },
	    write : function(params, async) {
	        if (typeof(params) == "boolean") {
	            async = params;
	            params = Logger.queue;
	        } else if (typeof(params) == "undefined") {
	            params = Logger.queue;
	        }
	        if (typeof(async)=="undefined") async=true;

	        for (var i = params.length - 1; i >= 0; i--) {
	            this.stop(params[i]);
	            this.debug(params[i]);
	            if (! params[i].completed && params[i].url != null) {
	                $.ajax({
	                    type: 'GET',
	                    url: params[i].url+"&at=50&sp=" + params[i].position+"&prventcache="+(new Date()).getTime(),
	                    async:async
	                });
	            }
	            this.queue.splice(i, 1);
	        }
	    },
	    getLogUrl : function(itemId) {
	        var url = CONTEXTPATH + '/log/study.mooc?';
	        var p = {};
	        var cookies = this.getCookies();

	        //p.si = openCourseId;
	        p.ii = itemId;
	        //p.it = itemType;
	        p.ut = cookies["moocsk"] || "";
	        p.vk = cookies["moocvk"] || "";

	        p.ds = (window.screen.width || 0) + "x" + (window.screen.height || 0);
	        p.dc = window.screen.colorDepth || 0;
	        p.cs = navigator.cookieEnabled?1:0;
	        p.js = navigator.javaEnabled()?1:0;
	        p.ln = navigator.language || navigator.browserLanguage || navigator.systemLanguage || navigator.userLanguage || "";
	        p.ua = (new Base64()).encode(navigator.userAgent);
	        p.ru = location.href;

	        var fv = "";
	        if (navigator.plugins && navigator.mimeTypes.length) {
	            var s =navigator.plugins["Shockwave Flash"];
	            s && s.description && (fv = s.description.replace(/^.*\s+(\S+)\s+\S+$/, "$1"))
	        } else if (window.ActiveXObject)
	            try {
	                var s = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
	                s && (fv = s.GetVariable("$version")) && (fv = fv.replace(/^.*\s+(\d+),(\d+).*$/, "$1.$2"))
	            } catch (xa) {
	            }
	        p.fv = fv;

	        var q = [];
	        for (var k in p) {
	            q.push(k+"="+encodeURIComponent(p[k]));
	        }
	        url += q.join("&");
	        $(window).unbind('beforeunload');
	        $(window).bind('beforeunload',function() {
	            Logger.write(false);
	        });
	        return url;
	    }
	}
}
