/* Note: This is a very smart JS */

/* Constants */
var minFloatingHeader = 600;
var minFloatingFooter = 0;
var minFloatingSidebar = 600;

var fontSizeMobiles = 10;
var fontSizeTabs = 12;
var fontSizeStandard = 14;
var fontSizeLarge = 16;

var htmlLoader = '<div id="loader">Laden...</div>';

/* Globals */
var pageHeight;
var viewHeight;
var headerHeight;
var footerHeight;
var panelHeight;
var sidebarHeight;
var sidebarOrigHeight;
var sidebarCount;
var sidebarTop;
var sidebarBottom;
var sidebarFactor;
var sidebarBlockHeights;
var pageTop;
var urls;

/* Check if jQuery is loaded */
if (!jQuery) {
    console.log('Error: jQuery not loaded');
    throw ('jQueryNotLoadedException'); /* Throw to stop execution */
} else {
    $ = jQuery;
}

/* Verify that the body tag is loaded */
if ($('body').length == 0) {
    console.log('Error: Body tag not found. Place the JS inside the body tag to fix this.');
    throw ('BodyTagNotFound'); /* Throw to stop execution */
}

/* Triggers */
$(window).bind('blur', preInitHost);
$(window).bind('load', initHost);
$(window).bind('load', postInitHost);
$(window).bind('resize', cacheHost);
$(window).bind('load scroll resize', actionHost);

/* Trigger pre-initialization actions */
$(window).trigger('blur');

/* Timer */
setInterval(actionHost, 500);

/* Pre-initialization handler */
function preInitHost() {
    try {
        if ($('#loader').length == 0) {
            $('body').append(htmlLoader);
            $('#loader').css({
                left: (($(document).width() / 2) - 50).toString() + 'px'
            });
        }
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Initialization handler */
function initHost() {
    try {
        urls = new Array();
        sidebarBlockHeights = new Array();
        sidebarBlock = $('#selector-pane > ul');

        for (var idx = 1; idx <= $('#selector-pane > ul').children().length; idx++) {
            $('#selector-' + idx.toString()).css('zIndex', idx.toString());
        }

        cacheHost();
        doShowLoader();
        doFocusSidebar();
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Post-initialization handler */
function postInitHost() {
    try {
        $('#loader').fadeOut(1500);
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Cache handler */
function cacheHost() {
    try {
        getScreenData(true);
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Action handler */
function actionHost() {
    try {
        getScreenData(false);
        doAdjustFontSize();
        doFloatingHeader();
        doFloatingPanel();
        doFloatingSidebar();
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Fetch element sizes on the screen */
function getScreenData(cache) {
    try {
        pageHeight = $(document).height();
        viewHeight = $(window).height();
        headerHeight = $('#header').outerHeight();
        footerHeight = $('#footer').outerHeight();
        panelHeight = $('#panel').outerHeight();
        pageTop = $(window).scrollTop();

        if ($('#selector-pane').length > 0) {
            sidebarHeight = $('#selector-pane').outerHeight();
            sidebarCount = $('#selector-pane > ul').children().length;
            sidebarTop = $('#selector-pane').offset().top;
            sidebarFactor = parseInt($('body').css('fontSize')) * 3;

            if (cache) {
                sidebarOrigHeight = $('#selector-pane').outerHeight() + panelHeight;
            }

            for (var idx = 1; idx <= sidebarCount; idx++) {
                sidebarBlockHeights[idx] = $('#selector-' + idx.toString()).height() - 2 - $('#selector-' + idx.toString() + ' > a > h1').outerHeight() + parseInt($('#selector-' + idx.toString()).css('paddingTop'));

                if (cache) {
                    sidebarOrigHeight -= sidebarBlockHeights[idx];
                }
            }
        }
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Show a loading box when navigating through pages */
function doShowLoader() {
    try {
        $('a')
        .unbind('click')
        .bind('click', function () {
            if ($(this).attr('href').indexOf('#') != 0) {
                $('#loader').slideDown("slow");
                
                setTimeout(function() {
                    $('#loader').fadeOut(1500);
                }, 15000);
            }
            
            return true;
        });
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

/* Adjust font size as per available view area */
function doAdjustFontSize() {
    try {
        if (viewHeight <= 300) {
            $('body').css('fontSize', fontSizeMobiles.toString() + 'px');
        } else if (viewHeight <= 500) {
            $('body').css('fontSize', fontSizeTabs.toString() + 'px');
        } else if (viewHeight <= 700) {
            $('body').css('fontSize', fontSizeStandard.toString() + 'px');
        } else if (viewHeight <= 900) {
            $('body').css('fontSize', fontSizeLarge.toString() + 'px');
        }

        getScreenData(false);
    } catch (e) {
        console.log('Error: ' + e.message);
    }
}

