////////////////////////////////
// set global vars and objects //
////////////////////////////////
// Homepage namespace for various global variables
Homepage = {
    host : "www.swctc.org",
    opendrawer : "",
    set_background_image : function(){
        if(!$('background_images')){return;}
		var background_images = $('background_images').getElementsBySelector('li');
        var rand = Math.round(background_images.length * Math.random());
        rand = rand - 1 ;
        var image = ""
        if (rand == -1){
            image = $A(background_images).last().innerHTML
        }
        else{
            image = background_images[rand].innerHTML;
        }
        var montage_image_div = $$('.index_montage_image')[0]
        montage_image_div.style['backgroundImage'] = 'url(common/images/montage/'+image+ ')';
    }
}
// object with homepage utilities
var HomepageUtils = {
    /*
    * Attach a script to the head of a document.
    *
    * src = url to the JS file to import
    * id = element id to set
    */
    attach_script : 
    function (src, id){
        var head = $$('head')[0];
        // if the script source exists in the DOM then delete it
        if ( $(id) ){
            $(id).remove();
        }
        var script = document.createElement('script');
        script.setAttribute('id', id);
        script.setAttribute('src', src);
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('charset', 'utf-8')
        head.appendChild(script);
    }
}
// Drawer object
var Drawer = {
    hover : function(e){
        Event.element(e).style['backgroundPosition'] = '0px -56px'
    }
    ,
    stop_hover : function(e){
        Event.element(e).style['backgroundPosition'] = '0 0'
    }
    ,
    remove_hrefs : function(){
        $A($('drawer_buttons').getElementsBySelector('a')).each(function(a){
            a.href = '#';
        }
        );
    }
    ,
    turn_off_hover : function(){
        $A($('drawer_buttons').getElementsBySelector('a')).each(function(a){
            a.style['backgroundPosition'] = '0 0';
        }
        )
    }
    ,
    close : function(){
        Drawer.close_drawer_section();
        new Effect.toggle('drawer' , 'appear', {
            queue:{
                position:'end', scope : 'drawer', limit:2
            }
            , duration:0.3
        }
        );
    }
    ,
    close_drawer_section : function(){
        if (opendrawer != ''){
            new Effect.toggle('appear' + opendrawer, 'appear', {
                queue:{
                    position:'end', scope : 'drawer', limit:3
                }
                , duration:0.1
            }
            );
        }
    }
    ,
    /*
    Drawer.toggle(evt) fades the drawer in and out.
    evt is a click event from tools, a-z or news & events
    */
    toggle : function(e){
        // don't attach event to header logo
        if ( Event.element(e).className == 'ust_logo'){
            return;
        }
				
        // find the base name (e.g. news, azindex)
        Event.element(e).className.match(/[a-zA-Z0-9]_(\w+)/)
        var drawer_base_name = RegExp.$1

				

        // options for first half of transition
        var begin_options = {
            queue:{
                position: 'front', scope: 'drawer', limit: 3
            }
            ,
            duration:0.3
        }
        // options for the last half of transition
        // open the drawer
        if (! $('drawer').visible()){
            // opening drawer
            opendrawer = drawer_base_name;
            new Effect.toggle('drawer' , 'appear', begin_options);
            new Effect.toggle('appear' + drawer_base_name, 'appear',
            {
                queue:{
                    position:'end', scope : 'drawer', limit:3
                }
                ,
                duration : 0.3
            }
            );
            Event.element(e).style['backgroundPosition'] = '0 -56px'
					
        }
        // close the drawer or switch to another
        else{
            // close current section (e.g. 'atoz')
            Drawer.close_drawer_section();
            // if drawer was already opened
            if (opendrawer != ""){
                // if another drawer is open
                if (drawer_base_name != opendrawer){
                    // turn the element to "on" state
                    Event.element(e).style['backgroundPosition'] = '0 -56px'
                    new Effect.toggle('appear' + drawer_base_name, 'appear', {
                        queue:{
                            position:'end', scope : 'drawer', limit:3
                        }
                        ,
                        duration : 0.0
                    }
                    );
                    opendrawer = drawer_base_name;

                    return;
                }
            }
            new Effect.toggle('appear' + drawer_base_name, 'appear',{
                queue:{
                    position:'end', scope : 'drawer', limit:3
                }
                ,
                duration : 0.0
            }
            )
            new Effect.toggle('drawer' , 'appear', begin_options);
        }
    }
}