﻿var calculator;

(function ($) {
    $(function () {
        calculator.init();
    });

    calculator = {
        options: {
            listElementHeight: 32,
            extraChannelsMinHeight: 55,
            listExtraHeight: 20
        },
        definitions: {
            extraChannelsRealHeight: function ($element) {
                var result = 0;
                if ($element.length > 0) {
                    var $listElements = $element.find('li');
                    if ($listElements.length > 0) {
                        return $listElements.length * calculator.options.listElementHeight;
                    }
                }
                return result;
            },
            extraChannelHeight: function ($element) {
                var result = 0;
                if ($element.length > 0) {
                    return $element.height();
                }
                return result;
            },
            calculatorRealHeight: function ($calculator, $channels) {
                var result = 0;
                if ($calculator.length > 0) {
                    if ($channels.length > 0) {
                        return $calculator.height() - calculator.definitions.extraChannelHeight($channels) + calculator.definitions.extraChannelsRealHeight($channels);
                    }
                    else {
                        return $calculator.height();
                    }
                }
                return result;
            },
            containerHeight: function ($calculator) {
                var result = 0;
                if ($calculator.length > 0) {
                    if ($calculator.parents('#content').height() > $(window).height()) {
                        result = $(window).height();
                    }
                    else {
                        $calculator.parents('#content').height();
                    }
                }
                return result;
            }
        },
        init: function () {
            var $calculator = $('#calculator');

            $calculator.parents('.column25').css('position', 'static');

            calculator.setHeight();

            $('#calculator button, span.info').livequery(function () {

                $(this).tipTip({
                    defaultPosition: 'right',
                    delay: 500//
                    //                enter: function () {
                    //                    $('.tooltip').addClass('ttcalculator').find('span.tooltiparrow').remove();
                    //                    $('.tooltip').append('<span class="tooltiparrow"></span>');
                    //                }
                });

            });

            $('.calculatorbutton.locked').livequery(function () {
                $(this).removeAttr('href');

                $(this).tipTip({
                    defaultPosition: 'right',
                    delay: 500//,
                    //                enter: function () {
                    //                    $('.tooltip').addClass('ttcalculator').find('span.tooltiparrow').remove();
                    //                    $('.tooltip').append('<span class="tooltiparrow"></span>');
                    //                }

                });
            }, function () {
                // this is a pretty bad hack - the tooltip won't come back.
                $(this).attr('href', '#').unbind('mouseover').unbind('mouseout');


            });
        },
        float: function () {
            var $calculator = $('#calculator');
            var $parentElement = $('#flowcontainer');

            if (!$calculator.hasClass('basic') && $parentElement.height()) {
                var containerHeight = $parentElement.height();
                var topPos = 0;
                if ($parentElement.offset() != null) {
                    topPos = $parentElement.offset().top;
                }
                var maxBottom = topPos + containerHeight;

                if ($(document).scrollTop() > topPos) {
                    $('#calculator').removeClass('fixedbottom').addClass('floating');
                }
                else {
                    $('#calculator').removeClass('floating');
                };

                if (($(document).scrollTop() + $('#calculator').height()) > maxBottom) {
                    $('#calculator').removeClass('floating').addClass('fixedbottom');
                }
                else {
                    $('#calculator').removeClass('fixedbottom');
                }
            }
        },
        setHeight: function () {
            var $calculator = $('#calculator');
            var $extraChannelList = $('#calcExtraChannels');

            $('.tooltip').hide();

            //If calculatorHeight exceeds the viewerHeight
            if ((calculator.definitions.calculatorRealHeight($calculator, $extraChannelList) + calculator.options.listExtraHeight) > calculator.definitions.containerHeight($calculator)) {

                //If extraChannelList exists
                if ($extraChannelList.length > 0) {

                    //Try to minimize the extraChannelList
                    var diff = ($calculator.height() + calculator.options.listExtraHeight) - calculator.definitions.containerHeight($calculator);

                    var newHeight = $extraChannelList.height() - diff;

                    if (newHeight > 0) {

                        if (calculator.definitions.extraChannelsRealHeight($extraChannelList) > calculator.options.extraChannelsMinHeight) {
                            newHeight = (newHeight > calculator.options.extraChannelsMinHeight) ? newHeight : calculator.options.extraChannelsMinHeight;
                            $extraChannelList.height(newHeight);
                            //$extraChannelList.css("max-height", newHeight);
                            $calculator.addClass('minimize');
                        }
                    } else {
                        calculator.cancelFloat($calculator, $extraChannelList);
                    }
                    /*
                    //If calculatorHeight still exceeds the containerHeight/windowHeight
                    if (($calculator.height() + calculator.options.listExtraHeight) > calculator.definitions.containerHeight($calculator)) {
                    //Nothing to do, cancel the float
                    calculator.cancelFloat($calculator, $extraChannelList);
                    }
                    //Else: everything is OK, do the floating stuff
                    else {
                    $calculator.removeClass('basic');
                    calculator.float();
                    }
                    */
                }
                //Else: Nothing to do, cancel the float
                else {
                    calculator.cancelFloat($calculator, $extraChannelList);
                }
            }
            //Else: everything is OK, do the floating stuff
            else {
                if ($extraChannelList.length > 0) {
                    $extraChannelList.height(calculator.definitions.extraChannelsRealHeight($extraChannelList));
                }
                $calculator.removeClass('basic');
                calculator.float();
            }
        },
        cancelFloat: function ($calculator, $extraChannelList) {
            $calculator.removeClass().addClass('basic');
            $calculator.css({ "overflow-y": "hidden" });
            $extraChannelList.height('auto');
            $extraChannelList.css({ "max-height": 3000 });
        }
    };

    $(window).scroll(function () {
        calculator.float();
    });

    $(window).resize(function () {
        calculator.setHeight();
    });
})(jQuery);
