function $(id)
{
    return document.getElementById(id);
}

function getElementPosition(obj)
{
    var x = 0, y = 0;
    while(obj)
    {
        x += obj.offsetLeft;
        y += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return {x:x, y:y};
}

window.onload = function()
{
    var showTimeout = null;
    var hideTimeout = null;
    var div         = null;
    var curItem     = null;
    var curData     = [];
    var lastDataId  = null;
    
    function setDivPos()
    {
        function createLink(text, href)
        {
	        var link = document.createElement('a');
	        link.href = href;
	        link.innerHTML = text;
	        link.className = 'new';
	        div.appendChild(link);
        }
        
        function createLabel(text, capt)
        {
            var link = document.createElement('div');
            link.innerHTML = text;
            link.className = 'label';
            div.appendChild(link);
        }
        
        function createCapt(text, spaced)
        {
            var e = document.createElement('div');
            e.innerHTML = text;
            e.className = 'capt';
            div.appendChild(e);
            
            if (spaced)
            {
                e.style.paddingTop = '10px'
            }
        }
    
        while (div.childNodes.length)
        {
            div.removeChild(div.childNodes[0]);
        }
        
        createCapt('Новые');
        
        if (!curData.unread && !curData.unread2)
        {
            createLabel('Новых нет');
        }
        else
        {
	        if (curData.unread)
	        {
	            createLink('Видимые: ' + curData.unread, 'read.php?action=unread&cid=' + curData.id);
	        }
	        if (curData.unread2)
	        {
	            createLink('Скрытые: ' + curData.unread2, '#');
	        }
        }
        
        createCapt('Всего', true);
        
        if (curData.hidden)
        {
            createLink('Скрытых: ' + curData.hidden, 'read.php?action=hidden&cid=' + curData.id);
        }
        else
        {
            createLabel('Скрытых нет совсем');
        }
        
        createCapt('Разное', true);
        createLink('Настройки', 'category.php?cid=' + curData.id);
        
        var pos = getElementPosition(curItem);
        div.style.left = (pos.x - div.offsetWidth - 5) + 'px';
        div.style.top  = (pos.y) + 'px';
        
        div.onmouseover = curItem.onmouseover;
        div.onmouseout  = curItem.onmouseout;
    }
    
    var ShowDiv = function()
    {
	    div = document.createElement('div');
	    div.className = 'catListHint';
	    
	    document.body.appendChild(div);
	    setDivPos();
	    
	    ItemOver = St2Over;
	    ItemOut  = St2Out;
    }
    
    var St1Over = function()
    {
        showTimeout = setTimeout(ShowDiv, 500);
    }
    
    var St1Out = function()
    {
        clearTimeout(showTimeout);
    }
    
    var St2Over = function()
    {
        if (lastDataId !== curData.id)
        {
            setDivPos();
            lastDataId = curData.id;
        }
    }
    
    var St2Out = function()
    {
        if (null !== hideTimeout)
        {
            clearTimeout(hideTimeout);
        }
        
        hideTimeout = setTimeout(function()
        {
            if (!curItem)
            {
			    ItemOver = St1Over;
			    ItemOut  = St1Out;
			    document.body.removeChild(div);
			    div = null;
            }
        }, 100);
    }
    
    var ItemOver = St1Over;
    var ItemOut  = St1Out;
    
    function SetItemEvents(item, data)
    {
        item.onmouseover = function()
        {
            curData = data;
            curItem = item;
            ItemOver();
        }
        
        item.onmouseout = function()
        {
            curItem = null;
            ItemOut();
        }
    }
    
    for (var i = 0; i < CatList.length; i++)
    {
        if ($('clink' + CatList[i].id))
        {
            SetItemEvents($('clink' + CatList[i].id), CatList[i]);
        }
    }
}

function Ajax_createRequest()
{
    var Request = false;

    if (window.XMLHttpRequest)
    {
        Request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        Request = new ActiveXObject("Microsoft.XMLHTTP");

        if (!Request)
        {
            HRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }

    if (!Request)
    {
        alert("Cannot create XMLHttpRequest");
    }

    return Request;
}

function deleteListEntry(iid)
{
	if ($('entry' + iid))
	{
        var div     = $('entry' + iid);
	    var opacity = 0.9;
	    var k       = 0.08;
	    var t       = 25;
	    var height  = div.offsetHeight;
	    var hk      = Math.round(height / 8);
	    var block   = 1000;
	    var func    = function()
	    {
            var div = $('entry' + iid);
		    if (opacity > k && height > hk && --block > 0)
		    {
		        div.style.opacity = opacity;
		        opacity -= k;
		        
		        height -= hk;
		        div.style.height = height + 'px';
		        
		        setTimeout(func, t);
		    }
		    else if (div.parentNode)
		    {
                div.parentNode.removeChild(div);
		    }
		}
		
		div.style.overflow = 'hidden';
		setTimeout(func, t);
	}
}

function rateItem(iid, cid, rid, page, act)
{
    $('rb' + iid).className += rid > 0 ? ' gf' : ' bf';
    $('rb' + iid).onclick = function(){};
    
    var req = Ajax_createRequest();
    req.open("GET", RootUri + '/read.php?action=rate&cid=' + cid + '&iid=' + iid
        + '&rid=' + rid + '&act=' + act + '&page=' + page, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    req.onreadystatechange = function()
    {
        if (req.readyState == 4)
        {
            try
            {
                var resp = eval('(' + req.responseText + ')');
                // Add new entry
                var div = $('entry' + iid);
                if (div)
                {
                    var parent = div.parentNode;
                    deleteListEntry(iid);
                    
                    if (resp.html)
                    {
                        parent.innerHTML += resp.html;
                    }
                    if ((!resp.nonext || resp.nonext == 'true')
                        && $('nextPage'))
                    {
                        $('nextPage').style.display = 'none';
                    }
                }
            }
            catch (e)
            {
                alert('Error: ' + req.responseText);
            }
        }
    }
    req.send(null);
}
