﻿/* (c) copyright Bluesulphur Ltd. 2010. All rights reserved. */

/******* User-configurable settings ******/

var mainContentLoadDelayStandard = 200; // initial value  200
var mainContentLoadDelayLong = 1000;    // initial value 1000

var infoPanelsOpenAfter = 300;  // initial value 300. Best left at initial value.
var infoPanelsRemainFor = 3000; // initial value 3000

/*** End of user-configurable settings ***/


// Other required global variables
var infoPanelOpenTimer;
var infoPanelCloseTimer;
var currentProduct = null;

// Functions
function showMainContent() {
    $('.mainContent').slideDown('normal');
}

function showInfoPanel(product) {
    var $line = $('.line.' + product);
    var $speechBubble = $('.speechBubble.' + product);
    var lineLength = $line.data('width');
    var speechBubbleWidth = $speechBubble.data('width');

    setCurrentProduct(product);

    $line.stop().css({
        width: 0,
        height: 2,
        opacity: 1
    })
    .animate(
        { width: lineLength },
        'fast',
        '',
        function() {
            $speechBubble.stop().css({
                width: 0,
                opacity: 1
            })
            .animate(
                { width: speechBubbleWidth },
                'fast'
            );
        }
    );
}

function hideInfoPanels() {
    setCurrentProduct(null);
    $('.line, .speechBubble').stop().hide();
}

function fadeOutInfoPanel() {
    var product = currentProduct;
    setCurrentProduct(null);
    $('.line.' + product + ', .speechBubble.' + product).stop().fadeOut('slow');
}

function showSpeechBubble(product) {
    setCurrentProduct(product);
    $('.speechBubble.' + product).stop()
    .css(
        {
            height: 'auto',
            opacity: 1
        }
    )
    .slideDown('fast');
}

function hideSpeechBubbles() {
    setCurrentProduct(null);
    $('.speechBubble').stop().hide();
}

function fadeOutSpeechBubble() {
    var product = currentProduct;
    setCurrentProduct(null);
    $('.speechBubble.' + product).stop()
    .animate(
        {
            opacity: 0,
            height: 0
        },
        'slow',
        '',
        hideSpeechBubbles
    );
}

function setCurrentProduct(product) {
    currentProduct = product;
}

// on document ready...
$(function() {

    // Store the line lengths and info panel widths for use in the animation
    $('.header .line, .header .speechBubble').each(
        function() {
            $(this).data('width', $(this).css('width'));
        }
    );

    // Remove titles from links to product websites
    $('.products ul li a').removeAttr('title');

    // Product icon rollovers
    // Group site (info panels with lines etc.)
    $('.header .products li>a')
    .mouseover(
        function(e) {
            var product = e.target.className.split(' ')[0];
            if (product == currentProduct) {
                window.clearTimeout(infoPanelCloseTimer);
            } else {
                infoPanelOpenTimer = window.setTimeout(
                    function() {
                        window.clearTimeout(infoPanelCloseTimer);
                        hideInfoPanels();
                        showInfoPanel(product);
                    },
                    infoPanelsOpenAfter
                )
            }
        }
    )
    .mouseout(
        function(e) {
            var product = e.target.className.split(' ')[0];
            window.clearTimeout(infoPanelOpenTimer);
            if (product == currentProduct) {
                infoPanelCloseTimer = window.setTimeout(
                    function() {
                        fadeOutInfoPanel();
                    },
                    infoPanelsRemainFor
                );
            }
        }
    );
    // Product site (speech bubbles)
    $('.sidebar .products li>a')
    .mouseover(
        function(e) {
            var product = e.target.className.split(' ')[0];
            if (product == currentProduct) {
                window.clearTimeout(infoPanelCloseTimer);
            } else {
                infoPanelOpenTimer = window.setTimeout(
                    function() {
                        window.clearTimeout(infoPanelCloseTimer);
                        hideSpeechBubbles();
                        showSpeechBubble(product);
                    },
                    infoPanelsOpenAfter
                )
            }
        }
    )
    .mouseout(
        function(e) {
            var product = e.target.className.split(' ')[0];
            window.clearTimeout(infoPanelOpenTimer);
            if (product == currentProduct) {
                infoPanelCloseTimer = window.setTimeout(fadeOutSpeechBubble, infoPanelsRemainFor);
            }
        }
    );
    // Keep speech bubbles open if the mouse is over them
    $('.header .speechBubble')
    .mouseenter(
        function() {
            window.clearTimeout(infoPanelCloseTimer);
        }
    )
    .mouseleave(
        function(e) {
            infoPanelCloseTimer = window.setTimeout(fadeOutInfoPanel, infoPanelsRemainFor);
        }
    );
    $('.sidebar .speechBubble')
    .mouseenter(
        function() {
            window.clearTimeout(infoPanelCloseTimer);
        }
    )
    .mouseleave(
        function(e) {
            infoPanelCloseTimer = window.setTimeout(fadeOutSpeechBubble, infoPanelsRemainFor);
        }
    );
});

// on page load...
$(window).load(
    function() {
        var delay;
        if ($('.mainContent.showAfterDelay').length > 0) {
            delay = mainContentLoadDelayLong;
        } else {
            delay = mainContentLoadDelayStandard;
        }
        window.setTimeout(showMainContent, delay);
    }
);

