function showYears(){
$("#yearsForArhive").toggle("slow");
}
function showMonths(){
$("#monthesForArhive").toggle("slow");
}

var calTopURL = "/_site/src/inc/ajax/calendarajax.aspx";

var toCell = null;
var dateForShow = null;

var overCell = false;
var flowID = -30000;

var currentRow = null;

function rowDisplayProp(r){
    return r.cells[0].getElementsByTagName("div")[0].style.display;
}

function slideHideRow(r){
    $(r.cells[0].getElementsByTagName("div")[0]).slideUp("slow");
}

function slideShowRow(r){
    var div = r.cells[0].getElementsByTagName("div")[0];
    if(div.style.display=="none")
        $(div).slideDown("slow");
}

function setRowHTML(r,data){
    r.cells[0].getElementsByTagName("div")[0].innerHTML = data;
}

function getRowForShow(e){
    var row = e.parentNode;
    while(row.tagName!="TR")
        row = row.parentNode;
    var index = row.rowIndex;
    return $id("calTable").rows[index+1];
}

function loadPopular(e){
    date = e.id;
    overCell=true;
    if(e!=toCell){
        flowID++;
        toCell = e;
        dateForShow = date;
        setTimeout("loadNow("+flowID+",'"+date+"')","1000");
    }
    return false;
}

function loadNow(n,date){ 
    if(flowID!=n)return;
    if(!overCell)return;
    if(toCell==null)return;
    if(date!=dateForShow)return;
    
    var row = getRowForShow(toCell);
    if(row != currentRow && currentRow!=null)
        slideHideRow(currentRow);
    
    currentRow = row;
    row.id = date;
    
    slideShowRow(row);

    var data = $(toCell).attr("innerData");
    if(data==null){
        setRowHTML(row,$id("preloading").innerHTML);
        var text = 
        eval("$.get(calTopURL+\"?date=\"+dateForShow,null,function(data){gotTopArticles(data,'"+dateForShow+"')});");
    }
    else
        setRowHTML(row,data);
}

function gotTopArticles(data,d){
    if(toCell==null) return;
    if(d!=toCell.id)return;
    toCell.setAttribute("innerData",data);
    var row = getRowForShow(toCell);
    
    if(row.id==dateForShow)
        setRowHTML(row,data);
}

function hidePopular(){
    if(toCell==null) return;
    overCell = false;
    setTimeout("preHideNow()",0);
}

function preHideNow(){
    if(toCell==null) return;
    var row = getRowForShow(toCell);
    
    if(row!=currentRow && currentRow!=null){ // если над ячейкой другого ряда
        setTimeout("hideNow()","2000");
        return;
    }

    if(!overCell){ // если над популярными
        if(rowDisplayProp(row) == "none"){ // если уже спрятан
            toCell = null;
            return;
        }
        setTimeout("hideNow('"+row.id+"')","2000");
    }
}

function hideNow(date){
    if(toCell==null) return;
    var row = getRowForShow(toCell);

    if(row!=currentRow && currentRow!=null){
        slideHideRow(currentRow);
        return;
    }
    
    if(date!=row.id) return; // не закрывать не свои статьи

    slideHideRow(row);
    currentRow = null;
    toCell = null;
}