﻿var globalBreadcrumb = [];
function increaseFontSize() {
    var currentSize = getFontCookie("oakhamsize");
    if (currentSize == "") {
        currentSize = 0;
    }
    if (currentSize == 3) return;
    currentSize++;
    document.cookie = "oakhamsize=" + currentSize + ";path=/";
    var tmp = $("*",document.body);
    $.each($(tmp),function(i, elem) {
        var s = $(elem).css("font-size");
        if(s != null && s != "") {
            $([elem]).css("font-size",parseInt(s) + 1 + "px");
        }
    });
    setBreadcrumb();
}

function decreaseFontSize() {
    var currentSize = getFontCookie("oakhamsize");
    if (currentSize == "") {
        currentSize = 0;
    }
    if (currentSize == -2) return;
    currentSize--;
    document.cookie = "oakhamsize=" + currentSize + ";path=/";
    var tmp = $("*",document.body);
    $.each($(tmp),function(i, elem) {
        var s = $(elem).css("font-size");
        if(s != null && s != "") {
           if(parseInt(s) > 1) 
                $([elem]).css("font-size",parseInt(s) - 1 + "px");
        }    
    });
    setBreadcrumb();
}

function setFontSize() {
    var currentSize = getFontCookie("oakhamsize");
    if (currentSize == "" || currentSize == 0) {
        $("#skin_wrapper").show();
        return;
    }
    if ((currentSize < -2) || (currentSize > 3)) {
        currentSize = 0;
        document.cookie = "oakhamsize=" + currentSize + ";path=/";
        return;
    }
    var tmp = $("*",document.body);
    $.each($(tmp),function(i, elem) {
        var s = $(elem).css("font-size");
        if(s != null && s != "") {
            if(parseInt(s) > 1)
                $([elem]).css("font-size",parseInt(s) + parseInt(currentSize) + "px");
        }    
    });
    setBreadcrumb();
    $("#skin_wrapper").show();
}

function setBreadcrumb() {
    if(globalBreadcrumb.length == 0) {
        return;
    }
    var currentBreadcrumb = [];
    var textLen = 0;
    for (var i = 0;i < globalBreadcrumb.length; i++) {
        //console.log(globalBreadcrumb[i]);
        currentBreadcrumb.push(globalBreadcrumb[i]);
        textLen += globalBreadcrumb[i].length;
    }
    textLen += globalBreadcrumb.length * 3;
    var currLen = getMaxBreadLength();
    //console.log(textLen + ">" + currLen);
    
    if (textLen > currLen) {
        while (textLen > currLen) {
            var howMany = textLen - currLen;
            var run15 = true;
            for (var i = 0;i < currentBreadcrumb.length;i++) {
                //console.log("Len: " + currentBreadcrumb[i].length);
                if (currentBreadcrumb[i].length > 20) {
                    //console.log("in if");
                    var chopLength = currentBreadcrumb[i].length - 20;
                    //console.log("chop: " + chopLength);
                    if (chopLength < 3)
                        continue;
                    if (chopLength > howMany) {
                        chopLength = howMany;
                    }
                    textLen -= chopLength
                    //console.log("zzzzxvvv " + currentBreadcrumb[i].substr(0,currentBreadcrumb[i].length - chopLength - 2) + "...")
                    currentBreadcrumb[i] = currentBreadcrumb[i].substr(0,currentBreadcrumb[i].length - chopLength - 2) + "..."
                    run15 = false;
                    break;
                    
                }
            }
            if (run15) {
                for (var i = 0;i < currentBreadcrumb.length;i++) {
                    if (currentBreadcrumb[i].length > 15) {
                        var chopLength = currentBreadcrumb[i].length - 15
                        if (chopLength < 3)
                            continue;
                        if (chopLength > howMany) {
                            chopLength = howMany;
                        }
                        textLen -= chopLength
                        currentBreadcrumb[i] = currentBreadcrumb[i].substr(0,currentBreadcrumb[i].length - chopLength - 2) + "..."
                        run10 = false;
                        break;
                        
                    }
                }
            }
            
        }
        
        for(var i = 0;i < currentBreadcrumb.length - 1;i++) {
            //console.log("ZZzz: " + currentBreadcrumb[i]);
            $("#breadcrumb_" + i + " a:first").text(currentBreadcrumb[i]);
        }
        //console.log("KRYSTIAN: " + $("#breadcrumb_last a:first").text());
        $("#breadcrumb_last").text(currentBreadcrumb[currentBreadcrumb.length - 1]);
    }
}

function getMaxBreadLength() {
    var currentSize = parseInt(getFontCookie("oakhamsize"));

    switch (currentSize) {
        case -2: return 103;
        case -1: return 95;
        case  1: return 81;
        case  2: return 75;
        case  3: return 70;
        default: return 87;
    }
}

function getFontCookie(name) {//Required only for GL Details
    //	alert(document.cookie);
    name = name + "=";
    if (document.cookie.length > 0) {
        // There are Cookies
        offset = document.cookie.indexOf(name);
        if (offset != -1) {
            // Cookie exists
            offset += name.length;
            // set index to beginning of the value
            end = document.cookie.indexOf(";", offset);
            // set index to end of the value
            if (end == -1)
                end = document.cookie.length;
            return unescape(document.cookie.substring(offset, end));
        }
    }
    return "";
}
