addDOMLoadEvent(addckeditor);
addDOMLoadEvent(setupTabs);
addDOMLoadEvent(ajaxloaders);

function ajaxloaders()
{
    if($('homelive')) {
        new PeriodicalExecuter(function() {
            new Ajax.Updater('homelive', 'maps/live?ajax=1', {
              method: 'post'
            });
        }, 30);
    }

    if($('homefeatured')) {
        new PeriodicalExecuter(function() {
            new Ajax.Updater('homefeatured', 'maps/featured?ajax=1', {
              method: 'post'
            });
        }, 30);
    }
}

function setupTabs()
{
    if ($('horizontaltabs'))
    {
        $('horizontaltabs').insert({before: '<ul id="tabnav"></ul>'});
        var x = $$('#horizontaltabs .tab');
        for (var i=0; i<x.length;i++)
        {
            title = $(x[i]).down("h3").innerHTML;
            id = $(x[i]).identify();
            $('tabnav').insert({bottom: '<li><a href="#" rel="' + id + '">' + title + '</a></li>'});
            $(x[i]).hide();
        }

        var y = $$('#tabnav li');
        for (var p=0; p<y.length;p++)
        {
            Event.observe($(y[p]).down("a"), 'click', function(e)     {
                Event.stop(e);
                this.up("ul").select("li.active").each(function(s){
			s.removeClassName("active");
		});
                $('horizontaltabs').select("div.tab").each(function(s){
			s.hide();
		});
                id = this.readAttribute('rel');
                this.up("li").addClassName("active");
                $(id).show();

            }, false);
        }

        if (location.hash!="")
        {
            selectedOption = location.hash.replace("#","");
            var y = $('horizontaltabs').select('h3');
            for(var i=0; i < y.length; i++)
            {
                if ($(y[i]).readAttribute('name')==selectedOption)
                {
                    $("tabnav").select("li.active").each(function(s){
			s.removeClassName("active");
                    });
                    $('horizontaltabs').select("div.tab").each(function(s){
                            s.hide();
                    });
                    $(y[i]).up("div.tab").show();
                    checkid = $(y[i]).up("div.tab").identify();
                }
            }
        } else {            
            $(x[0]).show();
            checkid = $(x[0]).identify();
        }
        
        $("tabnav").select("li a").each(function(s){
            if(s.readAttribute('rel')==checkid)
                s.up("li").addClassName("active");
        });
    }
}

function addckeditor() {
    var x = $$('.fhEditor textarea');
    for(var i=0; i < x.length; i++)
    {
        CKEDITOR.addStylesSet( 'my_styles',
        [
            // Block Styles
            { name : 'Page Header', element : 'h2', styles : {  } },
            { name : 'Page Sub-Header' , element : 'h3', styles : {  } },
        ]);

        CKEDITOR.replace($(x[i]).identify(), {
            forcePasteAsPlainText: true,
            stylesSet: 'my_styles',
            toolbar:
                [
                    ['Source','-'],
                    ['Cut','Copy','Paste','SpellChecker'],
                    ['Undo','Redo','-','RemoveFormat'],
                    ['Bold','Italic','Strike'],
                    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                    ['Link','Unlink'],
                    '/',
                    ['Image','MediaEmbed','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
                    ['Styles'],
                    ['Maximize']
                ]
        });
    }
}
