var images;
var lastMover;
var hiderTO;
var moverTO;



$(function() {
    images=$("#docImages").attr("value");

    //egeresemenyek inicializalasa a dokumentumban
    initNextPos();
    initLayerHide();
    initSyncToc();
    initFootnotePos();
    initHideFootnote();
    initLayerShow();
    initHideParaLayer();
    initGetConnectedDocs();
    initResetMoverTimer();
    initSetMoverTimer();
    initShowMoverTimer();
    initHideMoverTimer();
    initDelTo();
    initAddTo();


   
});

function initNextPos(){
    $(".moveInDoc").click(function(){
        var aname = $(this).attr("aname");
        var pos = parseInt($(this).attr("pos"));
        var fwd = parseInt($(this).attr("fwd"));
        var fajl = parseInt($(this).attr("fajl"));
        MoveInDoc(aname,fwd,pos, fajl);
        return false;
    });
}

function initLayerHide(){
    $(".layerHide").click(function(){
        var pos = parseInt($(this).attr("position"));
        layerHide('L_',pos);
        return false;
    });
}

function initSyncToc(){
    $(".syncToc").click(function(){
        var calcid = $(this).attr("calcid");
        syncTOC(calcid);
        return false;
    });
}

//megjeleniti a labjegyzet szamra kattintva a kis info dobozt
function initFootnotePos(){
    $(".footnotePos").click(function(){
        var markerId = $(this).attr("markerId");
        footnotePos(markerId);
        return false;
    });
}

//eltunteti a labjegyzet szamra kattintva a kis info dobozt
function initHideFootnote(){
    $(".hideFootnote").click(function(){
        var markerId = $(this).attr("markerId");
        hide_footnote(markerId);
        return false;
    });
}

function initLayerShow(){
    $(".layerShow").click(function(){
        layerShow('documenthourglass');
        return true;
    });
}

function initHideParaLayer(){
    $(".hideParaLayer").click(function(){
        hide_paralayer();
        return false;
    });
}

//kapcsolodo dokumentumok ablak megjelenitese
function initGetConnectedDocs(){
    $(".getConnectedDocs").click(function(){
        var docid = $(this).attr("docid");
        var fajl = $(this).attr("fajl");
        var number = $(this).attr("number");
        xajax_getConnected(docid,fajl,number);
        return false;
    });
}


function initResetMoverTimer(){
    $(".resetMoverTimer").mouseover(function(){
        ResetMoverTimer();
        return false;
    });
}

function initSetMoverTimer(){
    $(".setMoverTimer").mouseout(function(){
        SetMoverTimer();
        return false;
    });
}

function initShowMoverTimer(){
    $(".showMoverTimer").mouseover(function(){
        var position =  parseInt($(this).attr("position"));
        ShowMoverTimer('L',position);
        return false;
    });
}

function initHideMoverTimer(){
    $(".hideMoverTimer").mouseout(function(){
        HideMoverTimer();
        return false;
    });
}

function initDelTo(){
    $(".delTo").mouseover(function(){
        delto();
        return false;
    });
}

function initAddTo(){
    $(".addTo").mouseout(function(){
        addto();
        return false;
    });
}


function MoveInDoc2(id, first) {
    target = document.getElementById(id + '_1');
    if (!first) {
        ii = 2;
        next = document.getElementById(id + '_' + ii);
        while (next) {
            target = next;
            ii++;
            next = document.getElementById(id + '_' + ii);
        }
    }
    if (target) {
        moveToAnchor(target.name);
    }
}

function MoveInDoc(id, fwd, pos, fajl) {

    parameter = -1;
    if (fwd)
        target = document.getElementById(id + '_' + (pos + 1));
    else
        target = document.getElementById(id + '_' + (pos - 1));
    if (target) {
        //alert(target.name);
        moveToAnchor(target.name);
    }
    else {
        if (id == 'SZ') {
            act = 'moveparagraph';
            if (fwd) {
                var lastpos = parseInt($("#doc_lastpos").attr("value"));
                parameter = (lastpos + 1);
            }
            else {
                var firstpos =  parseInt($("#doc_firstpos").attr("value"));
                parameter = (firstpos - 1);
            }
        }
        else if (id == 'VF') {
            act = fwd ? 'movevffw' : 'movevfbw';
            parameter = fajl;
        }
        else if (id == 'VT') {
            act = fwd ? 'movevtfw' : 'movevtbw';
            parameter = fajl;
        }
        if (parameter != -1)
        {
            var docdb = $("#doc_dbid").attr("value");
            url = 'doc.php?docid=' + document.getElementById('docgroupid').innerHTML + '&docdb='+docdb+'&action=' + act + '&param=' + parameter;
            layerShow('documenthourglass');
            //document.location.href = url;
            xajax_foundInDoc(document.getElementById('docgroupid').innerHTML,act,parameter);
        }
    }
    return false;
}



function ShowMover(id, pos) {
    clearTimeout(moverTO);
    if (lastMover) {
        lastMover.style.display = "none";
    }
    lastMover = document.getElementById(id + '_' + pos);
    if (lastMover) {
        lastMover.style.display = "inline";
    }
    return false;
}

function HideMover() {
    if (lastMover) {
        lastMover.style.display = "none";
        lastMover = null;
    }
    return false;

}

function ShowMoverTimer(id, pos) {
    cmd = "ShowMover('" + id + "', " + pos + ")";
    moverTO = setTimeout(cmd, 350);
    return false;
}

function HideMoverTimer() {
    if (lastMover == null) {
        clearTimeout(moverTO);
    }
    return false;
}

function SetMoverTimer() {
    hiderTO = setTimeout("HideMover()", 1500);
    return false;
}

function ResetMoverTimer() {
    clearTimeout(hiderTO);
    return false;
}

function footnotePos (id){
    IE6=(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);

    show_footnote(id);

    if (!IE6){
        var htmlWidth = document.body.clientWidth;
        var markerDiv = document.getElementById(id + "_MARKER");
        var contentDiv = document.getElementById(id + "_CONTENT");
        divposx = markerDiv.offsetLeft + 303;
        if (htmlWidth-divposx < 250){
            contentDiv.style.width = 300 + "px";
            contentDiv.style.left = markerDiv.offsetLeft - 280 + "px";
        }
    }
}










