OnlineHub = Class.create({});
OnlineHub.Overlay = Class.create({});
OnlineHub.Overlay.prototype = 
{
    initialize: function(opts)
    {
        this.cl = opts['class'];
        this.on_hide = opts.onClick;
        this.transparency = opts.transparency || 0.9;
        
        this.hide_on_click = false;
        if(opts.hideOnClick != undefined)
        {
            this.hide_on_click = opts.hideOnClick;
        }
        this.container = opts.container || $(document.body);
        this.overlay = new Element('div', { 'class': this.cl}).hide();
        
        if(this.hide_on_click)
        {
           this.overlay.observe('click', this.hide.bindAsEventListener(this));
        }
        this.container.insert(this.overlay);
    },
    
    show: function()
    {
        var oh = OnlineHub.Utilities.get_max_height() + 'px';
        this.overlay.setStyle({'height': oh});
        this.overlay.setStyle({'zIndex': OnlineHub.Utilities.get_max_z_index()});
        new Effect.Appear(this.overlay, { duration: 0.5,  to: this.transparency });
        this.overlay.show();
        this.overlay.absolutize();
    },
    
    hide: function(e)
    {
        this.overlay.hide();
        this.overlay.setStyle({'zIndex': 0});
        if((this.on_hide != undefined) && (e != undefined))
        {
            this.on_hide();
        }
    },
    
    remove: function()
    {
        this.overlay.remove();
    },
    
    get_z_index: function()
    {
        return parseInt(this.overlay.getStyle('zIndex'));
    }
};

OnlineHub.Rotator = Class.create();
OnlineHub.Rotator.prototype = 
{
    initialize: function(i, c, m)
    {
        this.interval = i;
        this.container = $(c);
        this.messages  = this.container.select(m);
        
        this.number_of_messages = this.messages.length;
        if (this.number_of_messages == 0)
        {
                return false;
        }
        
        var ind, max_height = 0, max_width = 0;
        for(ind = 0; ind < this.messages.length; ind++)
        {
            if (this.messages[ind].getHeight() > max_height)
            {
                max_height = this.messages[ind].getHeight();
            }
            
            if (this.messages[ind].getWidth() > max_width)
            {
                max_width = this.messages[ind].getWidth();
            }
        }
        
        var rotator_width = Math.min(this.container.getWidth(), max_width);
        var ratio = rotator_width / max_width;
        
        var rotator_height = ratio * max_height;
        this.container.setStyle({'height': rotator_height + 'px', 'width': rotator_width + 'px'});
        this.current_message = 0;
        this.previous_message = null;
        this.slide_show = false;
        setTimeout(this.start_show.bind(this), this.interval);
    },
    
    show_message: function()
    {
        if(this.slide_show)
        {
            Effect.Appear(this.messages[this.current_message]);
            setTimeout(this.fade_message.bind(this), this.interval-1000);
            this.next_message();
            setTimeout(this.show_message.bind(this), this.interval);
        }
    },
    
    fade_message: function()
    {
        if(this.slide_show)
        {
            Effect.Fade(this.messages[this.previous_message]);
        }
    },
    
    hide_messages: function()
    {
        this.messages.each(function(message)
        {
                Element.hide(message);
        })
    },
        
    stop_show: function()
    {
        this.slide_show = false;
    },
        
    start_show: function()
    {
        this.hide_messages();
        this.slide_show = true;
        this.next_message();
        this.show_message();
    },
        
    next_message: function()
    {
        if (this.current_message < this.number_of_messages-1)
        {
                this.previous_message = this.current_message;
                this.current_message = this.current_message + 1;
        } else {
                this.current_message = 0;
                this.previous_message = this.number_of_messages - 1;
        }
    },
    
    prev_message: function()
    {
        if (this.current_message == 1)
        {
            this.current_message = 0;
            this.previous_message = this.number_of_messages - 1;
        }
        else if(this.current_message == 0)
        {
                this.current_message = this.number_of_messages - 1;
                this.previous_message = this.number_of_messages - 2;
        } else {
                this.current_message = this.previous_message;
                this.previous_message = this.previous_message - 1;
        }
    },
        
    show_next: function()
    {
        if(this.slide_show)
        {
            this.slide_show = false;
            Effect.Fade(this.messages[this.previous_message]);
            setTimeout(this.show_current.bind(this), 1000);            
        }
        else
        {
            Effect.Fade(this.messages[this.current_message]);
            this.next_message();
            setTimeout(this.show_current.bind(this), 1000);
        }
    },
        
    show_prev: function()
    {
        if(this.slide_show)
        {
            this.slide_show = false;
            Effect.Fade(this.messages[this.previous_message]);
            this.prev_message();
            this.prev_message();
            setTimeout(this.show_current.bind(this), 1000);            
        }
        else
        {
            Effect.Fade(this.messages[this.current_message]);
            this.prev_message();
            setTimeout(this.show_current.bind(this), 1000);
        }
    },
    
    show_current: function()
    {
        Effect.Appear(this.messages[this.current_message]);
    }
};

OnlineHub.VideoPlayer = Class.create({
    initialize: function(el)
    {
        var id = el.identify();
        
        var playlist = new Array();
        var splash = el.down('img');
        if(splash)
        {
            playlist.push(splash.readAttribute('src'));
            splash.remove();
        }
        
        var movies = el.select('a');
        var i, movie, autoplay;
        for(i = 0; i < movies.length; i++)
        {
            autoplay = true;
            if(i == 0)
            {
                autoplay = false;
            }
            
            playlist.push({'url': movies[i].readAttribute('href'), 'autoPlay': autoplay, 'scaling': 'fit'});
            movies[i].remove();
        }
        
        var w = parseInt(el.getWidth());
        var h = parseInt(el.getHeight());
                
        if(h == 0 || h == undefined || h == null || OnlineHub.Utilities.is_IE6() )
        {
            h = parseInt( w/1.777 ); //Default to 16/9
            el.setStyle({'height': h + 'px'});
        }
                
        flowplayer(id,  {   'src': '/themes/javascript_lib/video_player_1/flowplayer-3.2.7.swf',
                            'wmode': 'transparent'
                        },
                        {   'clip': {'autoPlay': true, 'autoBuffering': true, 'scaling': 'fit'},
                            'playlist': playlist,
                            'play': {'opacity': 0},
                            'plugins': {'controls': {'autoHide': 'fullscreen', 'playlist': true}}
                        }
                  );
    }
});

OnlineHub.Cookies = Class.create({
    initialize: function(path, domain) {
        this.path = path || '/';
        this.domain = domain || null;
    },
    // Sets a cookie
    set: function(key, value, days) {
        if (typeof key != 'string') {
            throw "Invalid key";
        }
        if (typeof value != 'string' && typeof value != 'number') {
            throw "Invalid value";
        }
        if (days && typeof days != 'number') {
            throw "Invalid expiration time";
        }
        var setValue = key+'='+escape(new String(value));
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var setExpiration = "; expires="+date.toGMTString();
        } else var setExpiration = "";
        var setPath = '; path='+escape(this.path);
        var setDomain = (this.domain) ? '; domain='+escape(this.domain) : '';
        var cookieString = setValue+setExpiration+setPath+setDomain;
        document.cookie = cookieString;
    },
    // Returns a cookie value or false
    get: function(key) {
        var keyEquals = key+"=";
        var value = false;
        document.cookie.split(';').invoke('strip').each(function(s){
            if (s.startsWith(keyEquals)) {
                value = unescape(s.substring(keyEquals.length, s.length));
                throw $break;
            }
        });
        return value;
    },
    // Clears a cookie
    clear: function(key) {
        this.set(key,'',-1);
    },
    // Clears all cookies
    clear_all: function() {
        document.cookie.split(';').collect(function(s){
            return s.split('=').first().strip();
        }).each(function(key){
            this.clear(key);
        }.bind(this));
    },

    get_all_keys: function()
    {
        var all = document.cookie.split(';').collect(function(s){ return s.split('=').first().strip();})
        return all;
    }
});

OnlineHub.Utilities = Class.create();
OnlineHub.Utilities.get_max_z_index = function(element)
{
    var el = element;
    if(el == undefined)
    {
        el = document.body;
    }
    el = $(el);
    var all = el.descendants();
    var all_z = all.collect( function(s){ var zind = s.getStyle('zIndex'); return parseInt(zind) || 0; })
    var max_z = all_z.max();
    return parseInt(max_z) + 1;
}

OnlineHub.Utilities.get_max_height = function (element)
{
    var el = $(element);
    
    if(el == undefined)
    {
        el = $('oh_site_wrapper') || $('site_wrapper');
    }
    
    if(el == undefined)
    {
        el = document.body;
        el = $(el);
    }
    return Math.max(el.getHeight(), document.viewport.getHeight());
}

OnlineHub.Utilities.get_max_width = function (element)
{
    var el = element;
    if(el == undefined)
    {
        el = document.body;
    }
    el = $(el);
    return Math.max(el.getWidth(), document.viewport.getWidth());
}

OnlineHub.Utilities.IE6_fixes = function()
{
    var is_the_awfull_browser = OnlineHub.Utilities.is_IE6();
    if(is_the_awfull_browser)
    {
        var images = $('oh_site_wrapper').select('img');
        images.each(function(i){
                var iw = i.getWidth();
                var p = $(i.parentNode);
                if(p != undefined)
                {
                    var pw = p.getWidth();
                    if(iw > pw)
                    {
                        if(i.hasClassName('oh_border'))
                            pw = pw - 6;
                        i.setStyle({'width': pw + 'px', 'height': 'auto'});
                    }
                }
            });
    }
}

OnlineHub.Utilities.is_IE6 = function()
{
    return Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
}

/* OnLoad Event Handlers */
window.onload = OnlineHub.Utilities.IE6_fixes;
Event.observe(window, "load", function(){ $$('div.oh_image_rotator').each(function(r){new OnlineHub.Rotator(7000, r, 'img')})}, false);
Event.observe(window, "load", function(){ $$('div.oh_video_player').each(function(r){new OnlineHub.VideoPlayer(r)})}, false);


