var _gaq = _gaq || [];
(function(){
    var domain = "M";
    
    // read cookie function
    function read_cookie(cookie_name){
        var my_cookie = "" + document.cookie;
        var ind = my_cookie.indexOf(cookie_name);
        if (ind == -1 || cookie_name == "") 
            return "";
        var ind1 = my_cookie.indexOf(';', ind);
        if (ind1 == -1) 
            ind1 = my_cookie.length;
        return unescape(my_cookie.substring(ind + cookie_name.length + 1, ind1));
    };
    //given the name of the variable finds value inside the __utmv cookie
    function read_value(name){
        var utmv = read_cookie("__utmv");
        var start = utmv.indexOf(name + "=");
        if (start == -1) {
            return "";
        };
        var tail = utmv.substring(start + 3);
        var end = tail.indexOf("=");
        var segments = tail.substring(0, end);
        return segments;
    };
    // assigning the 'loyalty score' to the visitor
    function score(){
        var old = read_value("CR");
        var start = old.indexOf(domain);
        var d = new Date();
        var curr_date = Math.floor((d.getTime() / 86400000) - 13835); //!!!
        if (start == -1) {
            return (old + domain + curr_date + "s1");
        };
        var diff = curr_date - old.substring(start + 1, start + 5);
        var tail = old.substring(start + 6);
        var end = tail.search("([^0-9]|$)");
        var score = tail.substring(0, end);
        var l = score.length;
        if ((diff < 6) && (diff > 0)) {
            var scoreN = parseInt(score) + 1;
            return (old.substring(0, start + 1) + curr_date + "s" + scoreN + old.substring(start + 6 + l));
        }
        if (diff > 5) {
            return (old.substring(0, start + 1) + curr_date + "s1" + old.substring(start + 6 + l));
        }
    };
    //mark as a visitor of the site
    function newVal(){
        var old = read_value("SG");
        if (old.indexOf(domain) == -1) {
            return (old + domain);
        }
        else {
            return old;
        }
    };
    //mark as a specific visitor of the site
    function newVal2(key){
        var old = newVal();
        if (old.search(domain + "[a-z]*" + key + "[a-z]*([A-Z]|$)") == -1) {
            var start = old.indexOf(domain);
            return (old.substring(0, start + 1) + key + old.substring(start + 1));
        }
        else {
            return old;
        }
    };
        
    _gaq.push(['_setAccount', 'UA-1176753-1']);
    _gaq.push(['_addOrganic', 'nova.rambler.ru', 'query'], ['_addOrganic', 'search.qip.ru', 'query'], ['_addOrganic', 'meta.ua', 'q'], ['_addOrganic', 'nigma.ru', 's'], ['_addOrganic', 'google.com.ua', 'q'], ['_addOrganic', 'gogle.com.ua', 'q'], ['_addOrganic', 'googl.com.ua', 'q'], ['_addOrganic', 'google.ru', 'q'], ['_addOrganic', 'search.ukr.net', 'search_query']);
    _gaq.push(['_addIgnoredRef', 'control.avtomagazine.ua']);
	try {
    	_gaq.push(['_setCustomVar', 4, 'CR', score(), 1]);
    } 
    catch (e) {
    }
	
    try {
        var _gaqPar = '';
        if (window.location.pathname.indexOf('keywords=') != -1) {
            _gaqPar = window.location.pathname.match(/\/@am\/search\/(.*)\/@html\/html\/?(page=\d+)?/)
            _gaqPar = '/?' + _gaqPar[1].replace(/\//g, '&') + (_gaqPar[2] ? ('&' + _gaqPar[2]) : '')
        }
        if (!_gaqPar) {
            _gaq.push(['_trackPageview']);
        }
        else {
            _gaq.push(['_trackPageview', _gaqPar]);
        }
    } 
    catch (e) {
        _gaq.push(['_trackPageview']);
    }
    _gaq.push(['_setAccount', 'UA-1571561-14']);
	_gaq.push(['_trackPageview']);
    try {
        var hrefP = window.location.href;
        if (hrefP.indexOf('/@am/delivery/@html/m?action=confirm') != -1) {
            _gaq.push(['_setCustomVar', 5, 'SG', newVal2("s"), 1]);
        }
        else {
            if (hrefP.search(/\/html\/php\/(show_messages\.php\?msg_id=(14|22|28|28\.1)$|rich_form\.php\?id=\d+&edited=t)/) != -1) {
                _gaq.push(['_setCustomVar', 5, 'SG', newVal2("m"), 1])
            }
            else {
                _gaq.push(['_setCustomVar', 5, 'SG', newVal(), 1])
            }
        }
    } 
    catch (e) {
    }
})();

(function(){
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

