﻿/* Copyright © Jon Howson 2011
/*
/**********************************************************************************************************/
$(document).ready(function ()
{
    $.localScroll();
});



(function (sunshine, $, undefined)
{
    /* Menu
    *************************************************************************************************************/
    (function (menu, $, undefined)
    {

        $(document).ready(function ()
        {
            /* Menu
            *************************************************************************************************************/
            $(".main-menu .top-highlight, .main-menu .bottom-highlight").remove();
            var topHighlight = $('<div class="menu-highlight"></div>').appendTo(".main-menu");
            var highlight = new highlights();

            $(".main-menu").hover(function ()
            {
            },
            function ()
            {
                if ($(".main-menu .selected").length == 0)
                {
                    $(topHighlight).stop().animate({
                        left: -$(topHighlight).outerWidth()
                    }, 500, function ()
                    {
                        $(topHighlight).css({
                            "left": 0,
                            "width": 0
                        });
                    });
                }
            });

            $(".main-menu li").hover(function ()
            {
                $(topHighlight).stop().animate({
                    width: $(this).outerWidth(),
                    left: $(this).position().left
                }, 500, function ()
                {
                    highlight.animate($(this).position().left, $(this).outerWidth());
                });
            },
            function ()
            {
                if ($(".main-menu .selected").length > 0)
                {
                    $(topHighlight).stop().animate({
                        width: $(".main-menu .selected").outerWidth(),
                        left: $(".main-menu .selected").position().left
                    }, 500, function ()
                    {
                        highlight.animate($(".main-menu .selected").position().left, $(".main-menu .selected").outerWidth());
                    });
                }
                else
                    highlight.end();
            });

            // On load - if an item is selected then set the highlights in place.
            if ($(".main-menu .selected").length > 0)
            {
                $(topHighlight).css({
                    width: $(".main-menu .selected").outerWidth(),
                    left: $(".main-menu .selected").position().left
                });
                highlight.static($(".main-menu .selected").position().left, $(".main-menu .selected").outerWidth());
            }
        });

    } (window.sunshine.menu = window.sunshine.menu || {}, jQuery))

} (window.sunshine = window.sunshine || {}, jQuery))
/* Highlights
*************************************************************************************************************/
function highlights()
{
    var _this = this;
    _this.colours = new Array();
    _this.classes = new Array();
    _this.widths = new Array();
    _this.minColourWidth = 5;

    _this.initialise();
}
highlights.prototype.initialise = function ()
{
    var _this = this;
    _this.colours[0] = $('<div></div>').appendTo(".main-menu");
    _this.colours[1] = $('<div></div>').appendTo(".main-menu");
    _this.colours[2] = $('<div></div>').appendTo(".main-menu");
    _this.colours[3] = $('<div></div>').appendTo(".main-menu");
    _this.colours[4] = $('<div></div>').appendTo(".main-menu");

    _this.classes[0] = "colour-highlight colour-blue";
    _this.classes[1] = "colour-highlight colour-green";
    _this.classes[2] = "colour-highlight colour-red";
    _this.classes[3] = "colour-highlight colour-light-blue";
    _this.classes[4] = "colour-highlight colour-light-green";
    _this.classes[5] = "colour-highlight colour-light-red";
};
highlights.prototype.animate = function (left, maxWidth)
{
    var _this = this;
    _this.randomiseWidths(maxWidth);
    _this.randomiseColours(function ()
    {
        _this.animateColour(left, 0);
    });
};
highlights.prototype.static = function (left, maxWidth)
{
    var _this = this;
    _this.randomiseWidths(maxWidth);
    _this.randomiseColours(function ()
    {
        _this.staticColour(left, 0);
    });
};
highlights.prototype.animateColour = function (left, index)
{
    var _this = this;
    var h = 20 + Math.floor(Math.random() * 81);
    var w = _this.widths[index];

    $(_this.colours[index]).removeClass()
    .addClass(_this.classes[index])
    .css({
        "top": -h,
        "left": left,
        "width": w,
        "height": h
    }).animate({
        top: $(".main-menu").outerHeight() - 10
    }, 250,
    function ()
    {
        index++;
        if (index < _this.colours.length)
            _this.animateColour(left + w, index);
    });
};
highlights.prototype.staticColour = function (left, index)
{
    var _this = this;
    var h = 20 + Math.floor(Math.random() * 81);
    var w = _this.widths[index];

    $(_this.colours[index]).removeClass()
    .addClass(_this.classes[index])
    .css({
        "top": $(".main-menu").outerHeight() - 10,
        "left": left,
        "width": w,
        "height": h
    });
    index++;
    if (index < _this.colours.length)
        _this.staticColour(left + w, index);
}
highlights.prototype.randomiseColours = function (fn)
{
    var _this = this;
    _this.classes.sort(function () { return (Math.round(Math.random()) - 0.5) });
    if (fn)
        fn();
};
highlights.prototype.randomiseWidths = function (width, fn)
{
    var _this = this;
    var total = 0;

    // Randomise the widths.
    for (var x = 0; x < _this.colours.length; x++)
    {
        var w = _this.minColourWidth + Math.floor(Math.random() * (width / 4));
        total += w;
        _this.widths[x] = w;
    }

    // Adjust to fit the width.
    // -- Possibly too expensive. More efficient randomisation?.
    var index = 0;
    if (total > width)
    {
        while (total > width)
        {
            if (_this.widths[index] > _this.minColourWidth)
            {
                _this.widths[index]--;
                total--;
            }

            index++;
            if (index == _this.widths.length) index = 0;
        }
    }
    else if (total < width)
    {
        while (total < width)
        {
            _this.widths[index]++;
            total++;
            index++;
            if (index == _this.widths.length) index = 0;
        }
    }

    if (fn)
        fn();
};
highlights.prototype.end = function (left)
{
    var _this = this;
    _this.endNext(0);
};
highlights.prototype.endNext = function (index)
{
    var _this = this;

    $(_this.colours[index]).animate({
        top: $(".main-menu").outerHeight()
    }, 150,
    function ()
    {
        index++;
        if (index < _this.colours.length)
            _this.endNext(index);
    });
};
