﻿function ClosedThread(_threadId, _expired) {
    this.threadId = _threadId;
    this.expired = _expired;
}
function ConfClientManager(_cnfCodeName) {
    this.cnfCodeName = _cnfCodeName;
    this.daysCount = 1;
    this.maxCookieSize = 4000;
    this.dateKey = new Date(2009, 0, 1);
    this.timeSlice = 1000*60*10; // 10 мин
    this.closedThreads = new Array();
}
ConfClientManager.prototype.addClosedThread = function(tid) {
    var exists = false;
    for (var i=0; i<this.closedThreads.length; i++) {
        if (this.closedThreads[i] == null) continue;
        if (this.closedThreads[i].threadId == tid) {exists = true; break;}
    }
    if (exists) return;
    var d = new Date(); 
    d = new Date(d.valueOf() + 1000*60*60*24*this.daysCount); 
    this.closedThreads.push(new ClosedThread(tid, d));
}
ConfClientManager.prototype.removeClosedThread = function(tid) {
    var exists = false;
    for (var i=0; i<this.closedThreads.length; i++) {
        if (this.closedThreads[i] == null) continue;
        if (this.closedThreads[i].threadId == tid) {this.closedThreads.splice(i,1); break;}
    }
}
ConfClientManager.prototype.makeCookieValue = function() {
    var result = "";
    for (var i=0; i<this.closedThreads.length; i++) {
        if (this.closedThreads[i] == null) continue;
        var exp = Math.round((cnfMgr.closedThreads[i].expired.valueOf() - this.dateKey.valueOf())/this.timeSlice)
        var txt = ((result.length > 0) ? "|" : "") 
            + this.closedThreads[i].threadId
            + "," + exp;
        if ((result + txt).length > this.maxCookieSize) break;
        result += txt;
    }
    //alert(result.length + "; " + this.closedThreads.length + "; \r\n" + result + "; ");
    return result;
}
ConfClientManager.prototype.initFromCookieValue = function(value) {
    if (value == "") return;
    var aItems = value.split("|");
    for (var i=0; i<aItems.length; i++) {
        var aValues = aItems[i].split(",");
        if (aValues.length < 2) continue;
        if ((aValues[0] == "") || (aValues[1] == "")) continue;
        var tid = parseInt(aValues[0]);
        var exp = parseInt(aValues[1]);
        var expires = new Date(exp*this.timeSlice + this.dateKey.valueOf());
        var d = new Date();
        if (expires.valueOf() - (new Date()).valueOf() > 1000*60*60*24*30) {
            expires = new Date(d.valueOf() + 1000*60*60*24*this.daysCount); 
        }
        if (expires > d) {this.closedThreads.push(new ClosedThread(tid, expires));}
    }
}

function setCookieValue(name, value, expiresDays, domain) {
    var val = "";
    if (value == null || value == "") {
        if (domain == null || domain == "") {
            removeCookieValue(name, ".7ya.ru");
            removeCookieValue(name, domain);
        } else {
            removeCookieValue(name, domain);
        }
        return;
    }
    var d = new Date(); 
    d  = new Date(d.valueOf() + 1000*60*60*24*expiresDays);
    if (domain == null || domain == "") {
        removeCookieValue(name, ".7ya.ru");
        //val = name + "=" + value + "; max-age=" + (60*60*24*expiresDays) + ";";
        val = name + "=" + value + "; expires=" + d.toGMTString() + ";";
        document.cookie = val;
    } else {
        //val = name + "=" + value + "; max-age=" + (60*60*24*expiresDays) + "; domain=" + domain + ";";
        val = name + "=" + value + "; expires=" + d.toGMTString() + "; domain=" + domain + ";";
        document.cookie = val;
    }
}

function getCookieValue(name) {
    var allCookies = document.cookie;
    var pos = allCookies.indexOf(name+"=");
    if (pos == -1) return "";
    var start = pos + (name+"=").length;
    var end  = allCookies.indexOf(";", start);
    if (end == -1) end = allCookies.length;
    var result = allCookies.substring(start, end);
    return decodeURIComponent(result);
}

function removeCookieValue(name, domain) {
    var d = new Date(); 
    d  = new Date(2000, 0, 1);
    var val = "";
    if (domain == null || domain == "") {
        //val = name + "=; max-age=0;";
        val = name + "=; expires=" + d.toGMTString() + ";";
        document.cookie = val;
    } else {
        //val = name + "=; max-age=0; domain=" + domain + ";";
        val = name + "=; expires=" + d.toGMTString() + "; domain=" + domain + ";";
        document.cookie = val;
    }
}

function addListItem(item, val) {
    if (val.indexOf("."+item+".") >= 0) return val;
    val += ((val=="")?".":"")+item+".";
    return val;
}

function removeListItem(item, val) {
    var prefix = "."+item+".";
    var ptn = new RegExp("\\."+item+"\\.", "gi");
    val = val.replace(ptn, ".");
    return val;
}

function addListItemToCookie(item, cookiename, expiresDays) {
    if (expiresDays == null) expiresDays = 30;
    var RubrCookie = getCookieValue(cookiename);
    RubrCookie = addListItem(item, RubrCookie);
    setCookieValue(cookiename, RubrCookie, expiresDays);
}

function removeListItemFromCookie (item, cookiename, expiresDays) {
    if (expiresDays == null) expiresDays = 30;
    var RubrCookie = getCookieValue(cookiename);
    RubrCookie = removeListItem(item, RubrCookie);
    setCookieValue(cookiename, RubrCookie, expiresDays);
}

function formatDate(d) { 
    return "" + ((d.getDate()<10)?"0":"") + d.getDate()
    + "." + ((d.getMonth()<9)?"0":"") + (d.getMonth()+1)
    + "." + d.getFullYear();
}
function formatTime(d) { 
    return ((d.getHours()<10)?"0":"") + d.getHours()
    + ":" + ((d.getMinutes()<10)?"0":"") + d.getMinutes()
    + ":" + ((d.getSeconds()<10)?"0":"") + d.getSeconds();
}
function formatDateTime(d) { 
    return formatDate(d) + " " + formatTime(d);
}
function formatDateS(d) { 
    var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());
    var dif  = d - today;
    if ((0<=dif)&&(dif<1000*60*60*24)) return formatTime(d);
    else return formatDateTime(d);
}

function wsReciver() {
    var boundArgs = arguments;
    return function() {
        var args = [];
        for(var i = 0; i < boundArgs.length; i++) args.push(boundArgs[i]);
        for(var i = 0; i < arguments.length; i++) args.push(arguments[i]);
        return wsBinder.apply(this, args);
    }
}

function wsBinder(bindMethod, target, data) {
    return bindMethod(target, data);
}

function setUpdating(elem, updating) {
    var cssClass = elem.className;
    if (updating) {
        if (cssClass.indexOf("updating") != -1) return;
        cssClass += ((cssClass!="")?" ":"") + "updating";
    } else {
        cssClass = cssClass.replace(/\s*updating/i, "");
    }
    elem.className = cssClass;
}