/*
    SiteComponents version:
    6.7.0, tag SC_6_7_0, created Thu Jan 07 10:17:49 +0100 2010

    Disclaimer
    
    While we make every effort to ensure that this code is fit for its intended
    purpose, we make no guarantees as to its functionality. CoreTrek AS will
    accept no responsibility for the loss of data or any other damage or
    financial loss caused by use of this code.


    Copyright
    
    This programming code is copyright of CoreTrek AS. Permission to run this
    code is given to approved users of CoreTrek's publishing system CorePublish.
    
    This source code may not be copied, modified or otherwise repurposed for use
    by a third party without the written permission of CoreTrek AS.
    
    Contact webmaster@coretrek.com for information.
    
*/

/**
 * Configuration for site components javascript
 *
 * Configuration array structure:
 * {<component>: {<parameter>: value, <parameter>: value, ... }, ...}
 */
var siteComponentsConfig = {
    
    // Configuration for the keyword module.
    
    keywords: {
        elements: ['placeholder-content'],
        skiptags: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
        usetooltip: false
    },
    // Remember to add a comma above when enabling more options
    
    /*
    // Valid keys:
    //    disable: true/false (default enabled)
    //    collapsedPageInfoPosition: Use this to move the collapsed debug
    //                               position to the left, instead of the
    //                               default right.
    debug: {
        disable: true,
        collapsedPageInfoPosition: 'right'
    }
    */
    
    /*
    // Configuration for tooltip.
    // 
    // Valid keys:
    //    disable: true/false (default enabled)
    //    positionby: Use this to override the default position for tooltip.
    //                Valid values is:
    //                  mouse (default), tooltip follows mouse
    //                  element, tooltip is attached to the title element
    */
    tooltip: {
        disable: true,
        positionby: 'mouse'
    }

    /*
    // Use this to override the default fontsizes in the fontsize selector
    // (html/lib/fontsize.js)
    
    fontsize: {
        sizes: ["10pt", "15pt", "24pt"]
    }
    */
    
};

document.observe("dom:loaded", function() {
    autoLabel();
    passwordMask();
    drawer();
    linkMakeup();
    spinCarousel();
});

function autoLabel() {
    $$('.autolabel').each( function(field) {
        defaultValue = field.value;

        field.observe('focus', function(event) {
            field.setValue('');
            field.removeClassName('blurred');
            field.addClassName('focused');
        });
    
        field.observe('blur', function(event) {
            if (field.value.length == 0) {
                field.setValue(defaultValue);
                field.removeClassName('focused');
                field.addClassName('blurred');
            }
        });
    });
}

function passwordMask() {
    if ($('password-mask') != null && $('password-field') != null) {

        $('password-mask').observe('focus', function(event) {
            $('password-mask').hide();
            $('password-field').show();
            $('password-field').focus();
        });

        $('password-field').observe('blur', function(event) {
            if ($('password-field').value.length == 0) {
                $('password-field').hide();
                $('password-mask').show();
            }
        });

    }
}

function drawer() {
    if ($('drawer') != null) {
        $$('.drawer-handle').each( function(element) {
            element.observe('click', function(event) {
                Effect.toggle('drawer', 'blind', { duration: 0.5 });
                Effect.ScrollTo('extra-8');
            });
        });
    }
}

function linkMakeup() {
    $$('a.button').each( function(link) {
        var span = new Element('span').update(link.innerHTML);
        link.update();
        link.replace(span.wrap(link));
    });
}

function spinCarousel() {

    var carousel = new PeriodicalExecuter( function(pe) {

        // Change the article
        $$('.multimedia-presentation ul.articles li.active').each( function(currentItem) {
            var nextItem = (currentItem.next() == null) ? currentItem.up().down(): currentItem.next();
            transition(currentItem, nextItem);
        });

        // Update the links
        $$('.multimedia-presentation ul.links li.active').each( function(li) {
            li.removeClassName('active');

            var nextItem = (li.next() == null) ? li.up().down(): li.next();

            nextItem.addClassName('active');

        });

    }, 5);

    // Respond to pointer hovering and clicking on page links
    $$('.multimedia-presentation ul.links a').each( function(pageLink) {
        pageLink.observe('click', function(event) {
            // Prevent the browser from trying to send an HTTP request to the
            // link's href URL. The URL is simply used as an identifier.
            event.stop();

            // Since the user explicitly clicked on a page link, stop the
            // carousel so the user can take his/her time to read the
            // corresponding article. There is no way the user can start the
            // carousel again, except by reloading the web page.
            carousel.stop();

            // Don't do anything if the active link has been clicked
            if (event.element().up().hasClassName('active')) {
                return;
            }

            // Find the page link whose corresponding article is currently being
            // showed, called the active page link.
            var activePageLink = null;
            $$('.multimedia-presentation ul.links li.active a').each( function(a) {
                // In practice there is only one of these
                activePageLink = a;
            });

            // Remove the active page link's "active" status
            activePageLink.up().removeClassName('active');

            // Mark the clicked page link as the new active page link
            pageLink.up().addClassName('active');

            // Find the articles corresponding to the previously and the new
            // active page links.
            var selectedItem = $(pageLink.getAttribute('href'));
            var currentItem = null;
            $$('.multimedia-presentation ul.articles li.active').each( function(li) {
                // Again, in practice there is only one of these
                currentItem = li;
            });

            // Finally, do the rotation
            transition(currentItem, selectedItem);

        });

        // Stop the carousel if the pointer hovers over the active link,
        // something that may indicate that the user wants to read the currently
        // displayed article.
        pageLink.observe('mouseover', function(event) {
            if (pageLink.up().hasClassName('active')) {
                carousel.stop();
            }
        });

    });

}

function transition(fromElement, toElement) {
    var nextHeight = toElement.getHeight();
    var currentHeight = fromElement.getHeight();

    // Crop the height of the toElement if it stretches below the border of the
    // fromElement.
    if (toElement.getHeight() > fromElement.getHeight()) {
        toElement.style.height = fromElement.getHeight() + 'px';
    }

    // Slide to the height of toElement
    fromElement.morph('height: ' + nextHeight + 'px;', {duration: 0.5});
    toElement.morph('height: ' + nextHeight + 'px;', {duration: 0.5});

    // Fade in toElement and fade out fromElement
    fromElement.fade({duration: 0.5, afterFinish: function() {
        fromElement.removeClassName('active').addClassName('inactive');
        toElement.addClassName('active').removeClassName('inactive');
        toElement.style.height = nextHeight + 'px';
        fromElement.style.height = currentHeight + 'px';
    }});
    toElement.appear({duration: 0.5});
}

