var maxStyle = 10;

var wSeconds = wHours = wMinutes = false;
function showWatch()
{
    if (wSeconds===false)
    {
        var e = $('watch').innerHTML.split(':');
        wHours = e[0]-0;
        wMinutes = e[1]-0;
        wSeconds = e[2]-0;
    }
    else wSeconds++;
    if (wSeconds>=60)
    {
    	wMinutes++;
    	wSeconds-=60;
    }
    if (wMinutes>=60)
    {
    	wHours++;
    	wMinutes-=60;
    }
    //if (wHours>23) window.location.reload();
    $('watch').innerHTML = wHours+':'+(wMinutes>9?wMinutes:'0'+wMinutes)+':'+(wSeconds>9?wSeconds:'0'+wSeconds);
    window.setTimeout(showWatch, 1000);
}

var wStyle = wStyleTM = false;
function selectStyle(s,uid,w)
{
	if (wStyle===false) wStyle = s;
	if (w) wStyle = wStyle%2 ? wStyle+1 : wStyle-1; else wStyle+=2;
	if (wStyle>maxStyle) wStyle = wStyle%2 ? 1 : 2;
    $('index_style').href = '/css/'+wStyle+'.css';
    (new CCookie()).set('style', wStyle, '/');
    if(uid && !wStyleTM) wStyleTM = window.setTimeout(saveStyle, 1000);
}

function saveStyle()
{
	wStyleTM = false;
	(new CAjax()).req('/user/style/'+wStyle+'/');
}

function $_(elem)
{
    if (elem && elem.parentNode)
    {
        var p = elem.parentNode;
        p.removeChild(elem);
        delete(elem);
    }
}

function stripTags(s)
{
    if (!s) return '';
    return trim(s.replace(/<\/?[^>]+>/g, ''));
}

function trim(s)
{
    if (!s) return '';
    s = s.replace( /^\s+/g, '');
    return s.replace( /\s+$/g, '');
}

function quot(s)
{
	return s.replace(/\"/g, "''");
}

function getBodyScrollTop()
{
  return parseInt(self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop));
}

function getBodyScrollLeft()
{
  return parseInt(self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft));
}

function offsetPosition(element)
{
    var offsetLeft = 0, offsetTop = 0;
    do {
        offsetLeft += element.offsetLeft;
        offsetTop  += element.offsetTop;
    } while (element = element.offsetParent);
    return {left:offsetLeft, top:offsetTop};
}

function viewPort()
{
    var d = document.documentElement;
    return {width:d.clientWidth, height:d.clientHeight};
}

function scTV(el)
{
    var def = 0;
    if(window.wSettings && !window.wSettings.not_move_top && $('index_div_top'))
    {
    	def = $('index_div_top').offsetHeight;
    }

	var pos = offsetPosition(el);
    var vp = viewPort();
    var sTop = getBodyScrollTop() + def;
    var sBottom = sTop+vp.height - def;

    if(pos.top<sTop || pos.top+20>sBottom)
    {
    	window.scrollTo(getBodyScrollLeft(), pos.top - 7 - def);
    }
}

function toggleElem(id)
{
	var el = $(id);
	if (!el) return;
	el.style.display = el.style.display=='none' ? 'block' : 'none';
	windowsLineUp();
}

function toggleElement(id,t,n,b,ttl)
{
	var el = $(id);
	if (!el) return;
	var ins = inst(id);
	id = id.replace(ins,'');
    if (ttl===false)
    {
    	if (window.coo.get('toggle'+id))
    	{
    		el.style.display = 'none';
            if (t)
		    {
		        if(ttl) t.title = n; else t.innerHTML = n;
		    }
    	}
    	return;
    }
	if (el.style.display=='none')
	{
		el.style.display = 'block';
		if (t)
		{
		    if(ttl) t.title = b; else t.innerHTML = b;
		}
	    window.coo.remove('toggle'+id, '/');
	}
	else
	{
		el.style.display = 'none';
		if (t)
		{
		    if(ttl) t.title = n; else t.innerHTML = n;
		}
	    window.coo.set('toggle'+id, '1', '/');
	}
	windowsLineUp();
}

function hlErr(el,bg,tm,it)
{
    if(!el) return;
	scTV(el);
	new ChlErr(el,bg,tm,it);
	el.focus();
	return false;
}

function ChlErr(el,bg,tm,it)
{
    if(!el || !el.style) return;
	this.el = el;
	this.bg = bg ? bg : '#F00';
	this.bge = el.style.background;
	if(this.bg == this.bge) return;
	this.tm = tm ? parseInt(tm) : 50;
	if(this.tm<1) this.tm = 50;
	this.it = it ? parseInt(it) : 5;
	if(this.it<3) this.it = 3;
	var me = this;
	this.hilight = function()
	{
		if(me.el.style.background === me.bge)
		{
			if(me.it==0) return;
			me.it--;
			me.el.style.background = me.bg;
		}
		else
		{
			me.el.style.background = me.bge;
		}
	    window.setTimeout(me.hilight,me.tm);
	};
	this.hilight();
}

