sidepanels = {};
mobile_safari = false;
if ((navigator.userAgent.match(/iPhone/i)) ||
    (navigator.userAgent.match(/iPad/i))) {
    mobile_safari = true;
}

var load_func = function(url) {
    $.get(url + "/side_menu", function(data) {
        sidepanels[url] = data;
    });
}
var get_side_panels = function() {

    sidepanels['item'] = $("#sidemenu").html();
    $(".item").each(function(i, item) {
        $item = $(item);

        if (!sidepanels[$item.attr('id')]) {
            load_func($item.attr('id'));
        }
    });
}


var scroll_me_able = function() {
    i = $(".item");
    b = $("#bottom_menu");

    viewport_h = b.offset().top;
    tolerance = 20;

    i.each(
        function(i, item) {

            $item = $(item);
            $wp = $item.find(".wrapper");

            if ($wp.find('.media_entry').length) {
                lll = $wp.find('.media_entry').height();
                number = parseInt($wp.find('.media_entry').length);
                number = Math.round(number / 5 + 0.4);

                if (number > 0)
                    height = number * lll;

                height += $wp.height();
            } else {
                height = $wp.height();
            }
            height += tolerance;
            if (height > viewport_h) {

                $item.find(".item_padding").addClass("paddingston");
            } else {
                $item.find(".item_padding").removeClass("paddingston");
            }
        })
}

item_tops = [];

var set_item_height = function() {

    height = $("#item").height();
    viewport_h = $("body").height();
    min_height = 870;

    tolerance = 95;

    if (height > viewport_h && height + tolerance > min_height) {
        min_height = height + tolerance;
    } else if (viewport_h + tolerance > min_height) {
        min_height = viewport_h + tolerance;
    }

    item_bottom = min_height;
    min_height += "px";
    if (!mobile_safari)
        $(".item").css("min-height", min_height);
    else {
        $(".item").css("height", "1352px");
        $("#mask").css("height", "6760px")

    }

    $('.item').each(function(i, item) {
        item_tops.push({top:$(item).offset().top, id:$(item).attr('id')});
    });

}
var item_bottom;

$(document).ready(function() {

    get_side_panels();
    set_item_height();

    items = $(".meter");
    items.each(function(i, item) {
        if (i > 0) {
            maxwidth = 0;
            $(item).find("a").each(function(a, aitme) {
                if ($(aitme).width() > maxwidth) {
                    maxwidth = $(aitme).width();
                }

            });
            $(item).css("width", maxwidth + 20);
        }
    });

    /* setup vertical scrolling */
    $('.button').click(function (event) {

        event.preventDefault();

        var animation_time = 1000;
        var factor = 1.5;
        var $anchor = $(this).parent();

        if ($($anchor.attr('href')).offset().top > 1000) {
            animation_time = $($anchor.attr('href')).offset().top / factor;
        } else if ($("html").scrollTop() > 1000) {
            animation_time = $("html").scrollTop() / factor;
        } else if ($("body").scrollTop() > 1000) {
            animation_time = $("body").scrollTop() / factor;
        }

        //console.log(animation_time, $("html").scrollTop())


        $("html,body").animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, animation_time, 'easeInOutExpo');


        // prevent default
        return 0;
    });

    $('.button').hover(function() {
        $this = $(this);
        $this.attr("src", $this.attr('over'));
    }, function() {
        $this = $(this);
        $this.attr("src", $this.attr('out'));
    });

});
var actual_side_id = 'item';
var idx = 0;
$(window).scroll(function() {

    scroll_top = $("html,body").offset().top * -1;
    test =  $("#item").offset().top;
    if($.browser.msie)
        scroll_top = document.documentElement.scrollTop
    $back_button = $(".back_to_top");

    if (item_bottom > scroll_top) {

        if ($back_button.css("display") != 'none') {
            $back_button.fadeOut('slow');
        }
    } else {
        if ($back_button.css("display") != 'block') {
            $back_button.fadeIn('slow');
        }
    }

    for (i = item_tops.length - 1; i > 0; i--) {

        if (scroll_top + 20 < item_tops[1].top) {

            $('#sidemenu').html(sidepanels.item);
            $("#searchfield").DefaultValue("suche");
            break;
        }
        if (item_tops[i].top < (scroll_top + 20)) {
            $('#sidemenu').html($($(sidepanels[item_tops[i].id])[1]).html())
            $("#searchfield").DefaultValue("suche");
            break;

        }
    }


});
