autodemo = new (function()
{
    var eventListeners = {};
    var debug = window.location.host.indexOf('localhost') === 0;
    
    this.sendEvent = function(ownerID, eventName)
    {
        if (typeof(eventListeners[ownerID]) == 'undefined') return;
        var listeners = eventListeners[ownerID];
        for(var i = 0, l = listeners.length; i < l; i++)
        {
            if (typeof(listeners[i]['handleEvent']) == 'function')
            {
                listeners[i]['handleEvent'](ownerID, eventName);
            }
        }
    }
    
    this.addEventListener = function(listener, ownerID)
    {
        if (typeof(eventListeners[ownerID]) == 'undefined')
        {
            eventListeners[ownerID] = [];
        }
        eventListeners[ownerID].push(listener);
    }
    
    this.launch = function(url, width, height, name, scroll, resize, ret)
    {
        var w;
        // Temporary fix for Google Chrome on OS X
        width = parseInt(width, 10);
        height = parseInt(height, 10);
        
        if (/Macintosh.+Chrome.+Safari/.test(navigator.userAgent))
        {
            height += 50;
        }
        
        if (width && height)
        {
            var params = ''
            var x = Math.round((screen.availWidth - width) / 2);
            var y = Math.round((screen.availHeight - height) / 2);
            params += 'left=' + x;
            params += ',top=' + y;
            params += ',screenX=' + x;
            params += ',screenY=' + y;
            params += ',width=' + width;
            params += ',height=' + height;
            params += ',resizable=' + (!!resize ? 'yes' : 'no');
            params += ',scrollbars=' + (!!scroll ? 'yes' : 'no');
            params += ',toolbar=no';
            params += ',menubar=no';
            params += ',status=no';
            params += ',location=no';
            params += ',directories=no';
            w = window.open(url, (!name ? '' : name), params);
        }
        else
        {
            if (!name)
            {
                w = window.open(url);
            }
            else
            {
                w = window.open(url, name);
            }
        }

        if (ret) return w;
        return false;
    }
    
    this.reCenterForms = function()
    {
        var f;
        if ($('#register').is(':visible'))
        {
            f = $('#register');
        }
        else if ($('#contact-form').is(':visible'))
        {
            f = $('#contact-form');
        }
        
        if (f) f.centerToWindow();
    }
    
    this.freezeScroller = function(b)
    {
        if (b == null) b = true;
        if (typeof(this.flashScroller) != 'undefined')
        {
            this.flashScroller.freeze(b);
        }
    }
    
    this.trackPageView = function(url)
    {
        if (typeof(pageTracker) == 'object')
        {
            if (typeof(pageTracker._trackPageview) == 'function')
            {
                pageTracker._trackPageview(url);
            }
        }
    }
    
    this.trackConversion = function(campaign)
    {
        if (debug) return;
        var i = $('iframe[name="google-adwords"]');
        if (i.length) i.remove();
        i = $(document.createElement('iframe'));
        i.attr('name', 'google-adwords');
        i.attr('class', 'google-adwords');
        i.attr('src', '/static/html/adwords/' + campaign + '.html');
        $('body').append(i);
    }
    
    this.handleEvent = function(ownerID, eventName)
    {
        switch(ownerID)
        {
            case 'window':
                switch(eventName)
                {
                    case 'ready':
                        if (typeof(this.contact) == 'object') this.contact.close();
                        if (typeof(this.register) == 'object') this.register.close();
                        $('a.new-window').click(function()
                        {
                            return autodemo.launch(this.getAttribute('href'));
                        });
                        $('a.pdf').click(function()
                        {
                            autodemo.trackPageView($(this).attr('href'));
                        });
                        break;
                    
                    case 'scroll':
                    case 'resize':
                        this.reCenterForms();
                        break;
                }
                break;
        }
    }
    
    this.addEventListener(this, 'window');
    
    $(window).load(function()
    {
        autodemo.sendEvent('window', 'load');
    });

    $(window).ready(function()
    {
        autodemo.sendEvent('window', 'ready');
    });

    $(window).scroll(function()
    {
        autodemo.sendEvent('window', 'scroll');
    });

    $(window).resize(function()
    {
        autodemo.sendEvent('window', 'resize');
    });
});