/**
 * IgnitionWeb 4.0
 *
 * The main JavaScript file.
 *
 * This file contains the definition of IW class,
 * which encapsulates the basic client-side functionality
 * found in IgnitionWeb
 * 
 * (C) 2007 High-Touch Communications Inc.
 *
 * @author Artem Ploujnikov
 */
 
 IW = function(){};
 
 IW.prototype = {
    
    handleExternalLinks : function() {
        if (!window._externalLinksHandled) {
            var links = document.documentElement.getElementsByTagName('a');
            for (var i=0;i<links.length;i++) {
                if (links[i].getAttribute('rel') == 'external') {                
                    Event.observe(links[i], 'click', function(e) {
                        Event.stop(e);
                        try {
                            var link = Event.element(e);
                            window.open(link.href); 
                        } catch (ex) {
                            alert(ex.message);
                        }
                    });               
                }
            }
            window._externalLinksHandled = true;
        }
    },
    
    
    initialize : function() {
        Event.observe(window, 'load', this.handleExternalLinks.bindAsEventListener());
        // Legacy script compatibility
        if (!document.body) {
            //document.body = document.getElementsByTagName('body')[0];
        }
    },
    
    toDebugString : function(object, full) {
        var debugString = '';
        switch(typeof(object)) {
            case 'object':
                for (var i in object) {
                    debugString += i + ' = ' + object[i] + "\n";
                }
                break;
            default:                
                debugString = object.toString();
                break;
        }
        if (!full && debugString.length > 255) {
            debugString = debugString.substring(0, 252) + '...';
        }
        return debugString;
    },
    
    handleError: function(error) {
        alert(this.toDebugString(error));
        
        $('js-errors').appendChild(document.createTextNode(this.toDebugString(error, true)));
    },    
    handleAJAXError : function(transport, error) {
        this.handleError(error);
    },
    
    debug: function(message) {
        $('js-errors').appendChild(document.createTextNode(this.toDebugString(message, true)));
        $('js-errors').appendChild(document.createElement('br'));
        if (window.console) {
            console.log(message);
        }
    },
    copyToClipboard: function(elementId) {
        var inElement = $(elementId);
        IW.debug(inElement);
        if (inElement.createTextRange) {
            var range = inElement.createTextRange();
            if (range) {
                if (range.execCommand('Copy')) {
                     alert(IW.getText('fe_url_copied_ok'));
                } 
            }
        }
    },
    getText: function(identifier) {
        var url = Paths.www + '/ignitionweb/getText/' + identifier + '?echo=1';
        var handler = {
            success: function(transport) {
                this.text = transport.responseText;
            },
            failure : function() {
                this.text = '???';
            }
        };
        new Ajax.Request(
            url,
            {
                method: 'get',
                onSuccess : handler.success.bindAsEventListener(handler),
                onFailure : handler.failure.bindAsEventListener(handler),
                asynchronous: false
            }                      
        );
        return handler.text;
    }    
 };
 
 AjaxIndicator = Class.create();
 
 AjaxIndicator.prototype = {
    id: 'ajax-indicator',
    
    state: 'hidden',
    
    show : function() {
        if (this.state != 'visible') {
            var indicator = $(this.id);
            Effect.Grow(indicator);         
            this.state = 'visible';
        }
    },
    
    hide : function() {
        if (this.state != 'hidden') {
            var indicator = $(this.id);
            Effect.Puff(indicator);  
            this.state = 'hidden';
        }
    },
    
    initialize: function(id) {
        this.id = id;
    }
 };
  
var indicatorElement = document.getElementById('ajax-indicator');
if (indicatorElement && indicatorElement.indicator) {
	Ajax.Responders.register(
		{
		    onCreate: function() {
		        $('ajax-indicator').indicator.show();   
		    },
		    onFailure: function() {
		        $('ajax-indicator').indicator.hide();           
		    },
		    onComplete: function() {
		        $('ajax-indicator').indicator.hide();           
		    },
		    
		    onSuccess : function() {
		        $('ajax-indicator').indicator.hide();           
		    }
		}
	); 
}
 
IW = new IW();
IW.initialize();    

Event.observe(window, 'load', 
    function() {
        if ($('root-url')) {
            IW.imageDir = $('root-url').innerHTML + 'img/';
        } else if (Paths && Paths.www) {
            IW.imageDir = Paths.www + 'img/';
        }
     }
);
