/**
 * Miscellaneous JavaScript goodies for TEAMZIEREIS website.
 */

/**
 * Initialize the menu by adding an animated hover effect and a fade effect for not-selected menu items on level 2.
 */
function initMenu() {
    // Add overlays to fade the inactive items.
    $('#nav ul.level2 > li').append('<div class="haze"></div>');

    // Hover effect with slide and fade.
    $('#nav ul.level2 > li').removeClass('hover');
    $('#nav ul.level2 > li').hoverIntent(
        function() {
            $(this).children('ul.level3:first').animate({
                width:190,
                height:190
            });
            $(this).siblings().addClass('hazed');
            $(this).removeClass('haze');
        },
        function() {
            $(this).children('ul.level3:first').animate({
                width:0,
                height:0
            });
            $(this).siblings().removeClass('hazed');
        }
    );
}

/**
 * Initialize the scrollbars in the main area.
 */
function initScrollbar() {
    // Adjust setting for overflow.
    $('.withScrollbar').css('overflow', 'hidden');

    // Call scrollbar plugin.
    $('.withScrollbar').jScrollPane({
        scrollbarWidth:5,
        scrollbarMargin:10
    });
}

/**
 * Initialize the login and search boxes.
 */
function initBox() {
    // Disable links.
    $('.box-header a.nolink').click(function() {
        return false;
    });

    // Add hover effect for the login box.
    $('.box-header:not([class~=nohover])').hoverIntent({
        over:function() {
            $(this).siblings('.box-content').show();
        },
        out:function() {
        }
    });
    $('.box').hoverIntent({
        over:function() {
        },
        out:function() {
            $(this).find('.box-content').hide();
        }
    });
}

/**
 * Initialize the title section with switchable images.
 */
function initTitle() {
    // Return if not title section was found.
    if (!$('#pagetitle')) {
        return;
    }

    // Return if only one image was found.
    if ($('.pagetitle .images img').size() == 1) {
        return;
    }

    // Make the links section visible.
    $('.pagetitle .links').show();

    // Build up the links to the other images.
    var links = [];
    $('.pagetitle .images img').each(function() {
        // Add a link item for every image in the list.
        var id = $(this).attr('id');
        links.push('<a href="#" rel="#' + id + '">' + id.replace(/^pagetitle_image_/, '') + '</a>');
    });
    $('.pagetitle .links').html(links.join('|'));

    // Make first item active.
    $('.pagetitle .links a:first').addClass('active');

    // Add a click action for the links.
    $('.pagetitle .links a').click(function() {
        // Remember the current item.
        var anchor = $(this);

        // Switch over the active item.
        $('.pagetitle .links a.active').removeClass('active');
        anchor.addClass('active');

        // Switch over the images.
        $('.pagetitle .images img:visible').hide();
        $('.pagetitle .images img' + anchor.attr('rel')).show();
    });
}

/**
 * Show and hide hints on product information.
 */
function initHints() {
    // Adjust position of hint boxes relative to the trigger link.
    $('form.product div.line div.hint').each(function() {
        var $hint = $(this);
        var $position = $hint.siblings('a').position();
        $hint.css('top', $position.top + 11);
        $hint.css('left', $position.left + 11);
    });
    $('div.products .subcr div.hint').each(function() {
        var $hint = $(this);
        var $position = $hint.prev('p').children('a').position();
        $hint.css('top', $position.top + 12);
        $hint.css('left', $position.left + 12);
    });

    // Event handler for trigger links.
    $('form.product div.line a').click(function() {
        $(this).siblings('div.hint').toggle();
        return false;
    });
    $('div.products .subcr a').click(function() {
        $(this).parent().siblings('div.hint').toggle();
        return false;
    });
}

/**
 * Handle the search rules and details.
 */
function initSearch() {
    // Hide the hint boxes initially.
    $('div.tx-indexedsearch-hint, dl.tx-indexedsearch-hint').hide();

    // Event handler for trigger link to show the boxes.
    $('a.tx-indexedsearch-hint').click(function() {
        $(this).parent().siblings('div.tx-indexedsearch-hint, dl.tx-indexedsearch-hint').toggle();
    });
}

/**
 * Execute the above scripts when the page is loaded and the DOM is ready for work.
 */
$(document).ready(function() {
    // Call the various initialization functions defined above.
    initMenu();
    initScrollbar();
    initBox();
    initTitle();
    initHints();
    initSearch();

    // Disable links if needed.
    $('a.nolink').addClass('disabled');
    $('a.nolink').click(function() {
        return false;
    });

    // Enable placeholder for the search box as a label replacement.
    $('#sword').placeholder();

    // Fill the quantity box on the product page with a default value.
    var $quantity = $('form.product input[type=text]');
    if ($quantity) {
        if ($quantity.attr('value') == '') {
            $quantity.attr('value', 1);
        }
    }

    // Handle clicks on the 'continue shopping' button.
    $('#basket_continue_shopping').click(function() {
        if (window.history.length > 1) {
            window.history.back();
        }
        else {
            window.location.href = $('#logo').attr('href');
        }
    });
});
