function generalizeDomain(){var domainArray = document.domain.split(".");var domainArrayLength = domainArray.length;if (domainArrayLength >= 2) {document.domain = domainArray[domainArrayLength - 2] + "." + domainArray[domainArrayLength - 1];}}
function getCookie(name){var value='';var posName=document.cookie.indexOf(escape(name)+'=');if(posName!=-1){var posValue=posName+(escape(name)+'=').length;var endPos=document.cookie.indexOf(';',posValue);if(endPos!=-1){value=unescape(document.cookie.substring(posValue,endPos))}else{value=unescape(document.cookie.substring(posValue))}}return value}function setCookie(name,value,expires,path,domain,secure){document.cookie=name+"="+escape(value)+((expires)?"; expires="+expires.toGMTString():"")+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"")}
var JSON={stringify:function(v){var a=[];function e(s){a[a.length]=s}function g(x){var c,i,l,v;switch(typeof x){case'object':if(x){if(x instanceof Array){e('[');l=a.length;for(i in x){v=x[i];if(typeof v!='undefined'&&typeof v!='function'){if(l<a.length){e(',')}g(v)}}e(']');return}else if(typeof x.valueOf=='function'){e('{');l=a.length;for(i in x){v=x[i];if(typeof v!='undefined'&&typeof v!='function'&&(!v||typeof v!='object'||typeof v.valueOf=='function')){if(l<a.length){e(',')}g(i);e(':');g(v)}}return e('}')}}e('null');return;case'number':e(isFinite(x)?+x:'null');return;case'string':l=x.length;e('"');for(i=0;i<l;i+=1){c=x.charAt(i);if(c>=' '){if(c=='\\'||c=='"'){e('\\')}e(c)}else{switch(c){case'\b':e('\\b');break;case'\f':e('\\f');break;case'\n':e('\\n');break;case'\r':e('\\r');break;case'\t':e('\\t');break;default:c=c.charCodeAt();e('\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16))}}}e('"');return;case'boolean':e(String(x));return;default:e('null');return}}g(v);return a.join('')},parse:function(text){return(/^(\s+|[,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text))&&eval('('+text+')')}};

////////////////////////////////////////////////////////////////////////////////
//
// clock functions
//
function show() {
var Digital=new Date();
var UTChours=Digital.getUTCHours();
var LAhours=UTChours+17;
var TYhours=UTChours+4;
var SYhours=UTChours+3;
var minutes=Digital.getUTCMinutes();
var seconds=Digital.getUTCSeconds();
//var dn="AM" 
if (LAhours>24) {
  LAhours=LAhours-24;
}

if (TYhours>24) {
  TYhours=TYhours-24;
}

if (SYhours>24) {
  SYhours=SYhours-24;
}

//dn="PM"

//this is so the hours written out is 
//in 12-hour format, instead of the default //24-hour format.

if (LAhours==24) LAhours=00;
if (TYhours==24) TYhours=00;
if (SYhours==24) SYhours=00;

//this is so the hours written out 
//when hours=0 (meaning 12a.m) is 12
if (minutes<=9) minutes="0"+minutes;
if (seconds<=9) seconds="0"+seconds;

document.getElementById('Clockla').innerHTML = LAhours+":"+minutes+":"+seconds+" ";
document.getElementById('Clockty').innerHTML = TYhours+":"+minutes+":"+seconds+" ";
document.getElementById('Clocksy').innerHTML = SYhours+":"+minutes+":"+seconds+" ";
setTimeout("show()",1000);
}

//
////////////////////////////////////////////////////////////////////////////////

function jsHover() {
  var hEls = document.getElementById("topm");
  if (hEls == null) {
      return false;
  }
  hEls = hEls.getElementsByTagName("li");   
  for (var i=0, len=hEls.length; i<len; i++) {
       hEls[i].onmouseover=function() { this.className="jshover"; }
       hEls[i].onmouseout=function() { this.className=""; }
   }
}

if (window.attachEvent && navigator.userAgent.indexOf("Opera")==-1) window.attachEvent("onload", jsHover);

startList = function() {
  if (document.all&&document.getElementById) {
    navRoot = document.getElementById("leftmenu");
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover = function() {
          this.className+=" over";
        }
        node.onmouseout = function() {
          this.className=this.className.replace(" over", "");
        }
      }
    }
  }
}

window.onload = startList;

var garage = {
    con: null,
    conCnt: null,
    list: null,
    exp: new Date("January 01, 2020 00:00:01"),

    init: function() {
        var cars = this.get();

        this.con    = document.getElementById('garage');
        this.conCnt = document.getElementById('garageCnt'); 
        this.list   = document.getElementById('garageList');

        if (this.con && this.conCnt) {
            if (cars.length) {
                this.conCnt.innerHTML = cars.length + ' авто';
                this.con.style.display = 'block';
            } else {
                this.conCnt.innerHTML = 'пусто';
            }
        }
    },

    get: function() {
        return JSON.parse(getCookie('garage')) || [];
    },

    add: function(id, name) {
        if (!id || !name) {
            return false;
        }

        var car = { id: id, name: name },
            cars = this.get(),
            exists = false;

        for (var i = 0; i < cars.length; i++) {
            if (cars[i].id == id) {
                exists = true;
                break;
            }
        }

        if (!exists) {
            cars.push(car);

            setCookie('garage', JSON.stringify(cars), this.exp, '/', document.domain);

            this.init();

            alert('Автомобиль ' + name + ' добавлен в ваш гараж!');
        } else {
            alert('Автомобиль ' + name + ' уже добавлен в ваш гараж!');
        }
    },

    remove: function(id) {
        var cars = this.get();

        for (var i = 0; i < cars.length; i++) {
            if (cars[i].id == id) {
                delete cars[i];

                setCookie('garage', JSON.stringify(cars), this.exp, '/', document.domain);

                this.init();
                this.hide();

                break;
            }
        }
    },

    show: function() {
        if (!this.list) {
            return false;
        }

        var cars = this.get(),
            html = '<a href="javascript: garage.hide();" style="float: right;">Закрыть</a><ul>';

        for (var i = 0; i < cars.length; i++) {
            html += '<li><a style="float: right;" href="javascript: garage.remove(\'' + cars[i].id + '\');">Удалить</a>';
            html += cars[i].name.indexOf('(Copart)') > 0 ? '<a href="/auctcop/' : '<a href="/auctman/';
            html += 'lot/' + cars[i].id + '">' + cars[i].name + '</a></li>';
        }

        this.list.innerHTML = html + '</ul>';
        this.list.style.display = 'block';
    },

    hide: function() {
        this.list.style.display = 'none';
    }
}

generalizeDomain();
