/*
    SiteComponents version:
    6.8.0.1, tag SC_6_8_0_1, created Fri Aug 20 11:18:33 +0200 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.
    
*/

/*jslint laxbreak: true, sub: true, white: false, browser: true,
onevar: false, nomen: false, noindent: true, eqeqeq: false, plusplus: false,
forin: true */
/*global siteComponentsConfig: false, getSiteComponentsConfig: false,
Ajax: false, $$: false, $: false, Element: false, window: false, Class: false,
Effect: false, lightbox: false, PeriodicalExecuter: false, Event: false,
CtCookie: false, cpKeywords: false, Prototype: false,
cpWriteMediaObject: false, tooltip: false */

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype.
    ============================================================================

    Wraps buttons on the page in spans reqired for making rounded corners.
*/

var RoundedButton = Class.create({
    
    initialize: function(button) {
        this.button = button;
        
        this.wrapButton(button);
    },
    
    /**
     * Wraps the button within HTML-markup required for making rounded buttons.
     *
     * <span class="button-wrapper">
     *   <span class="button-left"></div>
     *   ... The Button ...
     *   <span class="button-right"></div>
     * </span>
     */
    wrapButton: function(button) {
        // To make sure we do not double wrap any buttons, we check that the
        // button is not already wrapped inside a .button-wrapper element
        if(!button.up().hasClassName('button-wrapper')) {
            var wrapper = button.wrap('span', { 'class': 'button-wrapper' });
            button.insert({ before: '<span class="button-left"></span>' });
            button.insert({ after: '<span class="button-right"></span>' });
            wrapper.insert({ after: '<span class="button-after"></span>' });
            // Click on ant part of the wrapper should trigger a form.submit()
            wrapper.observe('click', this.wrapperClickListener.bindAsEventListener(this));
        }
    },
    
    // ------------------------------------------------------------------------
    // Event listeners
    
    /**
     * Listens for click on the button wrapper. When the wrapper is clicked,
     * the event will result in either a call to the button onclick function,
     * if this exists - or if the button is a descendant of a form, a submit
     * on this form.
     */
    wrapperClickListener: function(event) {
        // We do not want to handle events triggered from the wrapped button
        if(this.button == event.element()) {
            return;
        }
        
        // Find the wrapped input element or button, trigger its onclick
        ['input', 'button'].each(function(elementType) {
            var button = event.findElement('span.button-wrapper').down(elementType);
            if(typeof button != 'undefined') {
                button.click();
                return;
            }
        });
    }
    
});

// We rewrite buttons on dom:loaded, or for lightbox content whenever the
// lightbox content in updated (lightbox:contentUpdated event)
document.observe('dom:loaded', function(event) {
    $$('div.ctform-submit input',
       'input.ctwebform-element-type-reset',
       'input.ctwebform-element-type-submit',
       'input.ctwebform-element-type-button',
       'td.ctwebform-element-type-submit input',
       'button.submit',
       'input.submit',
       'a.rounded'
    ).each(function(element) { new RoundedButton(element); });
});

document.observe('lightbox:contentUpdated', function(event) {
    lightbox.content.select(
        'div.ctform-submit input',
        'input.ctwebform-element-type-reset',
        'input.ctwebform-element-type-submit',
        'input.ctwebform-element-type-button',
        'td.ctwebform-element-type-submit input',
        'button.submit',
        'input.submit',
        'a.rounded'
    ).each(function(element) { new RoundedButton(element); });
});

