/*
   SiteComponents version:
   6.6.4, tag SC_6_6_4, created Wed Aug 19 13:39:28 +0200 2009
   $Name:  $
   
   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.
  
*/

/*

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

    This script makes the cplib debug messages a bit nicer
*/

var Exception = Class.create({
    
    initialize: function(element) {
        this.element = $(element);
        
        this.trace = this.element.select('div.exception-trace').first();
        this.trace.hide();
        
        this.message = this.element.select('.exception-message').first();
        
        $('sc-tile-exception-container').insert({ bottom: this.element });
        
        this.element.setStyle({
            backgroundColor: '#ee5533',
            padding: '4px',
            margin: '0px',
            fontFamily: 'monospace'
        });

        this.message.setStyle({
            fontWeight: 'bold',
            fontSize: '1.1em'
        });
        
        this.message.insert({ top: '<img src="/images/expand.png"/> ' });
        this.more = this.message.down();
        
        this.trace.insert({ top: '<img src="/images/collapse.png"/> ' })
        this.less = this.trace.down();
        
        [this.more, this.less].each(function(element) {
            element.setStyle({
                cursor: 'pointer'
            })
        });
        
        this.more.observe('click', this.showTrace.bindAsEventListener(this));
        this.less.observe('click', this.hideTrace.bindAsEventListener(this));
    },
    
    showTrace: function(event) {
        event.stop();
        
        this.trace.show();
        this.message.hide();
    },
    
    hideTrace: function(event) {
        this.trace.hide();
        this.message.show();
    }
    
});

var PageDebug = Class.create({
   
    initialize: function(element) {
        this.element = $(element);
        this.hasError = false;
        
        $('sc-debug-container').insert({ bottom: '<div id="sc-page-debug-container"></div>' });
        
        this.pageDebug = $('sc-page-debug-container');
        this.pageDebug.update('<div id="sc-page-debug-expanded"></div><div id="sc-page-debug-collapsed"></div>');
        
        this.pageDebugExpanded = $('sc-page-debug-expanded');
        this.pageDebugCollapsed = $('sc-page-debug-collapsed');
        
        this.pageDebugExpanded.update(this.element);
        this.pageDebugExpanded.insert({ top: '<img src="/images/collapse.png"/> ' })
        
        this.pageDebugCollapsed.update('<img title="View page debug information" src="/images/clock.png"/> ');
        this.pageDebugCollapsed.hide();
        
        this.collapse = this.pageDebugExpanded.down();
        this.expand = this.pageDebugCollapsed.down();
        
        this.collapse.setStyle({
            float: 'left',
            marginLeft: '3px'
        });
                
        this.collapse.observe('click', this.hideDebug.bindAsEventListener(this));
        this.expand.observe('click', this.showDebug.bindAsEventListener(this));
        
        var collapsedDebugPosition = getSiteComponentsConfig('debug', 'collapsedPageInfoPosition', 'right');
        
        if(collapsedDebugPosition == 'left') {
            this.pageDebugCollapsed.setStyle({
                position: 'absolute',
                top: '0',
                left: '0',
                padding: '2px 10px 2px 10px'
            });
        } else {
            this.pageDebugCollapsed.setStyle({
                position: 'absolute',
                top: '0',
                right: '0',
                padding: '2px 10px 2px 10px'
            });
        }
        
        this.pageDebugExpanded.setStyle({
            backgroundColor: '#008822',
            borderBottom: '3px solid #003311',
            padding: '2px 10px 2px 10px',
            color: '#ffffff'
        });
        
        this.element.down().setStyle({
            listStyleType: 'none',
            margin: '0px 0px 0px 25px',
            padding: '0px'
        });
        
        this.element.select('label').each(function(element) {
            element.setStyle({
                paddingRight: '5px',
                color: '#77ff44'
            });
        });
        
        this.element.select('a').each(function(element) {
            element.setStyle({
                color: '#fff'
            });
        });
        
        $$('div.db-log').each(function(element) {
            element.setStyle({
                color: '#fff'
            });
        });
        
        this.element.select('.debug-error').each(function(element) {
            element.setStyle({
                color: '#f00',
                background: '#fff',
                fontWeight: 'bold'
            });
            
            this.hasError = true;
        }.bind(this));
        
        this.element.down().childElements().each(function(element) {
            element.setStyle({
                display: 'inline',
                paddingRight: '10px',
                marginRight: '10px',
                borderRight: '2px solid #003311'
            })
        });
        
        this.initializeState();
    },
    
    initializeState: function() {
        this.cookie = new CtCookie();
        
        if(!this.hasError && this.cookie.get('sc_debug_page_hidden')) {
            this.hideDebug();
        }
        
        if(this.hasError) {
            this.pageDebugExpanded.setStyle({
                backgroundColor: '#aa3311'
            });
            
            this.element.select('label').each(function(element) {
                element.setStyle({
                    color: '#ffbb88'
                });
            });
        }
    },
    
    hideDebug: function() {
        this.pageDebugExpanded.hide();
        this.pageDebugCollapsed.show();
        this.pageDebug.setStyle({ overflow: 'hidden',  height: '0px' });
        
        this.cookie.set('sc_debug_page_hidden', 1);
    },
    
    showDebug: function() {
        this.pageDebugExpanded.show();
        this.pageDebugCollapsed.hide();
        this.pageDebug.setStyle({ overflow: 'visible',  height: 'auto' });
        
        this.cookie.clear('sc_debug_page_hidden');
    }
    
});

document.observe('dom:loaded', function(event) {
    var exceptions = $$('div.ct-tile-exception');
    var pageDebug = $('sc-page-debug');
    
    $$('body').first().insert({ top: '<div id="sc-debug-container"></div>'});

    // ------------------------------------------------------------------------
    if(exceptions.length > 0) {
        $('sc-debug-container').insert({ top: '<div id="sc-tile-exception-container"></div>'});
        $('sc-debug-container').setStyle({
            fontFamily: 'monospace'
        });
        
        $('sc-tile-exception-container').setStyle({
            background: '#aa3311',
            color: 'white',
            padding: '10px',
            borderBottom: '3px solid #552211'
        });
        
        var postfix = exceptions.length>1?'s':'';
        $('sc-tile-exception-container').update('<h1>' + exceptions.length + ' unhandled tile exception' + postfix + ':</h1>')
        $('sc-tile-exception-container').down().setStyle({
            marginTop: '5px'
        });
    
        exceptions.each(function(element) {
            new Exception(element);
        });
    }
    
    // ------------------------------------------------------------------------
    if(pageDebug != null) {
        new PageDebug(pageDebug);
    }
    
});