//$(document).ready(function(){
    function liwil_seo_init(){
        //START variables data
        var flash = new Array();
        flash['default'] = '/design/default/images/shapka.swf';
        flash['alt'] = '/design/default/images/shapka-alt.swf';
        var text = new Array();
        text['default'] = '<strong>Наш адрес: </strong>Мичуринский проспект д.7 корп. 1<br><strong>Телефон: </strong>760-3300<br><strong>E-mail</strong>: <a href="mailto:vulevu09@mail.ru">vulevu09@mail.ru</a>';
        text['alt'] = '<strong>Наш адрес: </strong>Мичуринский проспект д.7 корп. 1<br><strong>Телефон: </strong>984-0451<br><strong>E-mail</strong>: <a href="mailto:vulevu09@mail.ru">vulevu09@mail.ru</a>';
        //text['alt'] = '(495) 984-04-51';

        var keywords = new Array("tvoyglyanec", "глянец");
        
        var engines = new Array('google','yandex','rambler','mailru','bing','yahoo','ask','qip');
        //END of variables
        
        //changing the text    
        function change_all(cookie){
            jQuery('#liwil-swf-val').attr('value',flash['alt']);
            jQuery('#liwil-swf-src').attr('src',flash['alt']);
            jQuery(document).ready(function(){
                jQuery('#content p:first').html(text['alt']);     
            });
            
            //jQuery('#content p:first').html('<strong>Наш адрес: </strong>Мичуринский проспект д.7 корп. 1<br><strong>Телефон: </strong>984-0451<br><strong>E-mail</strong>: <a href="mailto:vulevu09@mail.ru">vulevu09@mail.ru</a>');
            //alert(text['alt']);
            //var time = (1 / 24 / 60 / 60) * 160 //in seconds
            var time = 365;  //in days
            if(cookie != 'seo'){
                SetCookie('liwil_visitor_source','seo',time);
            }
        }
        
        
        //just testing
        var ref = referer();
        var query = ref['query'];
        if(query){query = query.toLowerCase();}
        if(getCookie('liwil_visitor_source') == 'seo'){
            change_all(getCookie('liwil_visitor_source'));
        }
        else if(query){
            var check = 'not found';
            for(i=0;i<keywords.length;i++){
                var result = query.indexOf(keywords[i])
                if(result != -1){           //found
                    check = 'found';
                    break;
                }
                else if(result == -1){      //not found
                    continue;
                }
            }
            if(check == 'not found'){
                change_all();    
            }
        }
    }

    //meta - this function returns array(engine,query)
    function referer(){
        engines = [
            {start:'http://www.google.', query:'q', name:'google'},
            {start:'http://yandex.', query:'text', name:'yandex'},
            {start:'rambler.ru/search', query:'query', name:'rambler'},
            {start:'http://go.mail.ru/', query:'q', name:'mailru', cp1251:true},
            {start:'http://www.bing.com/', query:'q', name:'bing'},
            {start:'search.yahoo.com/search', query:'p', name:'yahoo'},
            {start:'http://ru.ask.com/', query:'q', name:'ask'},
            {start:'http://search.qip.ru/search', query:'query', name:'qip'}
        ];
     
        var ref=document.referrer,req="",engine="", start, cp1251;
     
        for (var i in engines){
            if  (!engines.hasOwnProperty(i))
                continue;
            if (ref.indexOf(engines[i].start)==-1)
                continue;
            start = ref.indexOf('?' + engines[i].query + '=') 
            if (start == -1){
                start = ref.indexOf('&' + engines[i].query + '=');
                if (start == -1)
                    return false;
            }
            engine = engines[i].name;
            req = engines[i].query;
            cp1251 = engines[i].hasOwnProperty('cp1251');
        }
        if (!engine)
            return false;
        ref = ref.substr(start + req.length + 2);
        var end = ref.indexOf('&');
        if (end != -1)
                ref = ref.substr(0, end);
        if (cp1251){
            function win2unicode(str) {
                var charmap   = unescape(
                "%u0402%u0403%u201A%u0453%u201E%u2026%u2020%u2021%u20AC%u2030%u0409%u2039%u040A%u040C%u040B%u040F"+
                "%u0452%u2018%u2019%u201C%u201D%u2022%u2013%u2014%u0000%u2122%u0459%u203A%u045A%u045C%u045B%u045F"+
                "%u00A0%u040E%u045E%u0408%u00A4%u0490%u00A6%u00A7%u0401%u00A9%u0404%u00AB%u00AC%u00AD%u00AE%u0407"+
                "%u00B0%u00B1%u0406%u0456%u0491%u00B5%u00B6%u00B7%u0451%u2116%u0454%u00BB%u0458%u0405%u0455%u0457")
                var code2char = function(code) {
                   if(code >= 0xC0 && code <= 0xFF) return String.fromCharCode(code - 0xC0 + 0x0410)
                   if(code >= 0x80 && code <= 0xBF) return charmap.charAt(code - 0x80)
                   return String.fromCharCode(code)
                }
                var res = ""
                for(var i = 0; i < str.length; i++) res = res + code2char(str.charCodeAt(i))
                return res
            }
            ref = unescape(ref);
            ref = win2unicode(ref);
        }
        else
            ref = decodeURIComponent(ref);
            ref = ref.replace(/[+]+/g, ' ')
            return {"engine": engine, "query": ref};
    }


    //meta - cookie functions
    function SetCookie(cookieName,cookieValue,nDays) {
         var today = new Date();
         var expire = new Date();
         if (nDays==null || nDays==0) nDays=1;
         var seconds = nDays * 24 * 60 * 60 * 1000;
         expire.setTime(today.getTime() + seconds);
         document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString() + ";path=/";
        }

    //meta - cookie functions     
    function getCookie(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        } 
        else {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
    }

    //initiating function
    //liwil_seo_init();

    //promo_code
    //var code = Math.floor(Math.random()*9) + 1;
    var code = '';
    var cookie = getCookie('__utmv');
    if(cookie == null){
        var rand = '';
        for(i=1;i<=6;i++){
          rand = Math.floor(Math.random()*9) + 1;
          code += rand.toString();     
        }
        _gaq.push(['_setCustomVar',
            1,                   // This custom var is set to slot #1.  Required parameter.
            'promo_code',     // The name acts as a kind of category for the user activity.  Required parameter.
            code,               // This value of the custom variable.  Required parameter.
            1                    // Sets the scope to session-level.  Optional parameter.
        ]);

        
    }
    else{
        var code_name = 'promo_code=';
        var code_position = cookie.indexOf(code_name, 0);
        if(cookie.indexOf(code_name, 0) == -1){ //means that there is __utmv cookie, but no promo code
            for(i=1;i<=6;i++){
              rand = Math.floor(Math.random()*9) + 1;
              code += rand.toString();     
            }
            _gaq.push(['_setCustomVar',
                1,                   // This custom var is set to slot #1.  Required parameter.
                'promo_code',     // The name acts as a kind of category for the user activity.  Required parameter.
                code,               // This value of the custom variable.  Required parameter.
                1                    // Sets the scope to session-level.  Optional parameter.
            ]);    
        } 
        else{
            code_position += code_name.length;
            code = cookie.substr(code_position, 6);
        }    
    }
//});
