jQuery.noConflict();
jQuery(document).ready( function() {
    /* Add fade in/out for top level descriptions */
    if ( jQuery( '#home' ) )  fx_fade_in_descriptions(); // only run fade on home page
    
    /* Add class switcher to text size buttons */
    fx_range_of_classes(
        'body', 
        ['text-small', 'text-medium', 'text-large'], 
        '#decrease_text', 
        '#increase_text',
        {'expires':30, /* expires in 30 days */ 
         'path': '/'}
    ); 
    
    /*   
     * Start large main image rotating 
     * 
     * Note: the 'image_paths' array is created in scripts/effects.js via a MODx snippet that
     *       reads the content of a chunk to get the list of images to use
     */
    // EFFECT NOT WANTED: fx_main_image_rotator(jQuery("#Rotating"), image_paths, 3500);

});


function fx_fade_in_descriptions() {
    var screen_element = jQuery('#section_desc div');
    var output_element = jQuery('#section_desc p');
    jQuery.each( ['tab_2', 'tab_11', 'tab_18'], function(){
        var tab_element = jQuery( '#' + this );        
        var anchor_element = jQuery( '#' + this + ' a' ); 
        
        /* Store the 'title' attribute in another property   */
        /* so that normal tooltips don't show up in FF or IE */
        anchor_element.attr('_title', function(){ return this.title; }); 
        anchor_element.attr('title', '');
        
        tab_element.bind('mouseover', function() { 
            output_element.html( anchor_element.attr('_title') ); 
            screen_element.hide("fast");
        });
        tab_element.bind('mouseout', function() { 
            screen_element.show();
        });
    });
}

function fx_range_of_classes(elements, classes, prev_el, next_el, options) {
    /*  elements: elements to which the classes will be applied;
        classes: the list of classes which will be applied to the elements
        prev_el: elements to which an onclick will be applied to move backwards through the classes
        next_el: elements to which an onclick will be applied to move forwards throught the classes
        */
    /* Apply click handlers to switch the class on the BODY to adjust font sizes */
    var elements = jQuery(elements);
    var text_size_classes = classes;
    var cookiename = 'fx_range_of_classes-' + elements[0].tagName;
    
    /* set the body text size based on a pre-existing cookie */
    var saved_class = jQuery.cookie(cookiename); /* get the cookie value, which could possibly be null */
    if ( !saved_class ) { saved_class = classes[0]; } /* if no cookie, set to smallest size */
    
    elements.addClass( saved_class );
    
    function body_class_index () {
        /*  Match the text-size class set on the on the BODY tag with one 
         *  of the text sizes in the list classes; return its index
         *  or 0 if there is no class set.
         */
        var i = 0; /* counter */
        while ( i < classes.length ) {
            /* if one of the text size classes is among the body's classes... */
            /* ... break out of the loop; we'll use i */
            if ( elements.hasClass( classes[i] ) ) { break; }
            i++;
        }
        if ( i >= classes.length ) { i = 0 ; } /* no style assumes smallest text size */
        
        return i;
    }
    
    jQuery(prev_el).bind('click', function(){
            var i = body_class_index();
            if ( i > classes.length ) { /* do nothing; class wasn't in body and default is small size; no decrease necessary */ }
            else if ( i == 0 ) { /* also do nothing; already at smallest size */ }
            else { /* if there's a smaller size, switch the body to that one */
                elements.removeClass( classes[ i ] );
                elements.addClass( classes[ --i ] ); 
            jQuery.cookie(cookiename, classes[ i ], options );
        }
        return false;
    });
    
    jQuery(next_el).bind('click', function(){
            var i = body_class_index();
            if ( i >= classes.length-1 ) { /* do nothing; already at largest size */ }
            else { /* if there's a larger size, switch the body to that one */
                elements.removeClass( classes[ i ] );
                elements.addClass( classes[ ++i ] ); 
            jQuery.cookie(cookiename, classes[ i ], options );
        }
        return false;
    });    
}



function fx_main_image_rotator (element, paths, timeout) {
    index = -1;
    element = jQuery(element);
    function set_interval ( timeout ) {
        return window.setInterval( function() {
            element.attr('src', paths[ ( ++index ) % paths.length ]);
        }, timeout );
    }
    element.attr('clearvalue', set_interval( timeout ) );
    element.bind('click', function() {
        window.clearInterval( element.attr( 'clearvalue' ) );
    });
}






