﻿var loaded = 0;
var $j = $;
var loadFirstEntry = 0;
var _scroll = 0;
var _scroolTableOfContent = 0;
var bookmark = 0;
var moveToStart = 0;

function gotoTitle(title, id) {
    bookmark = title;
    if (!(id == loaded)) {
        $j.history.load(id + '/' + title);
        window.location.hash = id + '/' + title;
    } else {
        Scroll();
        window.location.hash = id + '/' + title;
    }
    loaded = id;
    return false;
}

function getSearch() {
    var obj = document.getElementById("ctl00_search_searchString")
    if (obj == null) {
        return ''
    } else {
        return encodeURI(obj.value);
    }
}

/*
### tiek izsakts, lai ielādētu fragmentu
1.SAdala ids pa daļām - škirkļa id un bookmarka ID
2.Ja šads fragments jau nav ielādets, tad dara to
*/
function pageLoad(ids) {
    var id = 0;
    if (ids.toString().indexOf('/') > 0) {
        bookmark = ids.toString().substring(ids.lastIndexOf('/') + 1);
        id = ids.toString().substring(0, ids.lastIndexOf('/'));
    }
    else {
        id = ids;
        bookmark = 0;
    }
    if (id > 0) {
        if (!(id == loaded)) {
            $j("p[@id ^='entry']").css("font-weight", "normal");
            $j("#entry" + id).css("font-weight", "bold");
            $j("#entry" + id + " > img[@src *='pluss']").trigger("click");
            if ($j("#entry" + id).is(":hidden")) expandThis($j("#entry" + id).attr('class').replace('class', 'img'));
            $j("#ctl00_vidus_resTable").html("<img class='loading' src='img/ieladesana.gif' />").load("getEntry.aspx?id=" + id + "&q=" + getSearch() + "&sid=" + Math.random(), function() {
                entryLoaded(id);
            });
        }
        else {
            if (moveToStart == 1) Scroll();
        }
    }
}

//Izsauc fragmneta atvēršanas klikšķi
function trigerClick(id) {
    $j("#entry" + id + " > a").trigger("click");
}

//Izvērš/sakļauj apakšnodaļas, ja izvērš, tad izsauc klikšķi
function showChildren(id, obj) {
    if ($j("p[@class='class" + id + "']").is(":hidden")) {
        if ($j("p[@class='class" + id + "']").length > 20) {
            $j("p[@class='class" + id + "']").show();
        }
        else {
            $j("p[@class='class" + id + "']").show("fast");
        }
        $j("p[@class='class" + id + "']").slideDown("fast");
        $j(obj)[0].src = "images/minus1.png";
    } else {
        $j(".class" + id + " > img[@src *='minus']").trigger("click");
        if ($j("p[@class='class" + id + "']").length > 20) {
            $j("p[@class='class" + id + "']").hide();
        }
        else {
            $j("p[@class='class" + id + "']").hide("fast");
        }
        $j(obj)[0].src = "images/pluss1.png";
    }
}

//show children without loading entry
function onlyShowChildren(id, obj) {
    if ($j("p[@class='class" + id + "']").is(":hidden")) {
        if ($j("p[@class='class" + id + "']").length > 20) {
            $j("p[@class='class" + id + "']").show();
        }
        else {
            $j("p[@class='class" + id + "']").show("fast");
        }
        $j("p[@class='class" + id + "']").slideDown("fast");
        $j(obj)[0].src = "images/minus1.png";
    }
    else {
        Scroll();
    }
}

//document ready
$j(document).ready(function() {   
    resizeTOC();
    $j.history.init(pageLoad);
    $j("a[@type='linkPubl']").click(function() {
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        moveToStart = 1;
        // moves to a new page. 
        // pageload is called at once.
        if (hash.length > 0) {
            $j.history.load(hash);
            loaded = hash;
        }
        return false;
    });
    if (window.location.hash.length == 0) {
        if (loadFirstEntry == 1) {
            loadFirstEntry = 0;
            readNext(0);
        }
    }
});

//entry loaded
function entryLoaded(id) {
    bindClick($('a[type="link_"]'));   
    if ($j(".highlightEntry").length > 0) $j(".paintBrush").show();
    highlighted = -1;
    initCookie();
    loaded = id;
    resize();
    Scroll();
    if (_scroolTableOfContent == 1) {
        _scroolTableOfContent = 0;
        var $obj = $j("#entry" + loaded + "");
        if ($obj != null) {
            $j(".tableOfContents").scrollTo($obj, 1000);
        } 
    } 
}

//expands node
function expandThis(id) {
    if ($j("p:has(#" + id + ")").is(":hidden")) {
        expandThis($j("p:has(#" + id + ")").attr('class').replace('class', 'img'));
    }
    $j('#' + id).click();
}

//read next entry
function readNext(id) {
    highlighted = -1;
    if (id == 0) {
        $j("a[@type='linkPubl']:first").trigger("click");
        $j(".tableOfContents").scrollTo({ top: 0, left: 0 }, 500);
        $j("#entryScroll").scrollTo({ top: 0, left: 0 }, 500);
    }
    else {
        _scroolTableOfContent = 1;
        $j.history.load(id);
        window.location.hash = id;
        //pageLoad(id);
        _scroll = 1;
    }
}

//resize entry layout
function resize() {
    var size = $j(window).height() - 240;
    if (size < 400) size = 400;
    if ($j("#entryScroll").height() != size) {
        var zise = $j(".tableOfContentHeader").height();
        $j("#entryScroll").css("height", size).css("overflow-x", "auto");
    }
    resizeTOC()
}

//resize entry layout
function resizeTOC() {
    var size = $j(window).height() - 250;
    if (size < 400) size = 400;
    size = size - $j(".tableOfContentHeader").height() + 45;
    if ($j(".tableOfContents").height() != size) {
        $j(".tableOfContents").css("height", size);
    }
}

//scroolls to next highlighted phrase
var highlighted = -1
function scrollToHighlight(nr) {
    if (highlighted > -1) $j(".highlightEntry:eq(" + highlighted + ")").css("text-decoration", "none");
    highlighted += nr;
    if (highlighted > $j(".highlightEntry").length - 1) highlighted = $j(".highlightEntry").length - 1
    if (highlighted < 0) highlighted = 0
    $j("#entryScroll").stop();

    var $obj = $j(".highlightEntry")[highlighted];
    $j(".highlightEntry:eq(" + highlighted + ")").css("text-decoration", "underline");
    if ($obj != null) {       
        $j("#entryScroll").scrollTo($obj, 600);
    };
}

//highlights all matched words
function hightlight() {
    if ($j("#high").attr("src").indexOf("/mark.png") > 0) {
        $j(".highlightEntry").css("background-color", "#ffffff");
        $j("#high")[0].src = "img/mark.jpg";
    }
    else {
        $j(".highlightEntry").css("background-color", "#f9dcaa");
        $j("#high")[0].src = "img/mark.png";
    }
}

//zoom in text
function zoomIn() {
    if ($j("#entryScroll")) {
        var siz;
        var size = getCookie('textSize');
        if (size != null && size != "") {
            siz = parseInt(size) + 1;
        }
        else {
            var size = $j("#entryScroll").css("font-size");
            siz = parseInt(size.replace('px', '')) + 1;
        }
        if (siz > 36) siz = 36;
        $j("#entryScroll").css("font-size", siz + "px");
        setCookie('textSize', siz, 365);
    }
    return false;
}

function initCookie() {
    if ($j("#entryScroll")) {
        var siz;
        var size = getCookie('textSize');
        if (size != null && size != "") {
            $j("#entryScroll").css("font-size", size + "px");
        }
    }
    return false;
}

//zoom out text
function zoomOut() {
    if ($j("#entryScroll")) {
        var siz;
        var size = getCookie('textSize');
        if (size != null && size != "") {
            siz = parseInt(size) - 1;
        }
        else {
            var size = $j("#entryScroll").css("font-size");
            siz = parseInt(size.replace('px', '')) - 1;
        }
        if (siz < 6) siz = 6;
        $j("#entryScroll").css("font-size", siz + "px");
        setCookie('textSize', siz, 365);
    }
    return false;
}

//iegūst no sīkfaila info
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
//iestata sīkfailu
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

//Aizritina līdz bookmarkam tekstā
function scrollToFav(id) {
    bookmark = id;   
    Scroll();
    window.location.hash = loaded + '/' + id;
}
//scrolls entry
function Scroll() {
    $j("#entryScroll").stop();   
    if (bookmark > 0) {
        var $obj = $j("#fav" + bookmark)[0];
        if ($obj != null) {
            $j("#entryScroll").scrollTo($obj, 500);
        };
        bookmark = -1;
    } else {
            $j("#entryScroll").scrollTo({ top: 0, left: 0 }, 500);
    }
    return false;
}
