$(document).ready( function() {
    
    try {
        $('#accordion').accordion();
    }
    catch (e) {}
    
    window.setInterval( function() {
        $('#prayerRotate').fadeOut(400, function() {
            if ( (currentQuote + 1) >= quotes.length ) {
                currentQuote = 0;
            }
            else {
                currentQuote++;
            }
            $(this).html( quotes[currentQuote] );
        }).fadeIn(400);
    }, 6000 );
    
    jQuery.fn.supersleight = function(settings) {
        settings = jQuery.extend({
            imgs: true,
            backgrounds: true,
            shim: '../img/x.gif',
            apply_positioning: true
        }, settings);
        
        return this.each(function(){
            if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 && parseInt(jQuery.browser.version) > 4) {
                jQuery(this).find('*').each(function(i,obj) {
                    var self = jQuery(obj);
                    // background pngs
                    if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
                        var bg = self.css('background-image');
                        var src = bg.substring(5,bg.length-2);
                        var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
                        var styles = {
                            'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
                            'background-image': 'url('+settings.shim+')'
                        };
                        self.css(styles);
                    };
                    // image elements
                    if (settings.imgs && self.is('img[src$=png]')){
                        var styles = {
                            'width': self.width() + 'px',
                            'height': self.height() + 'px',
                            'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
                        };
                        self.css(styles).attr('src', settings.shim);
                    };
                    // apply position to 'active' elements
                    if (settings.applyPositioning && self.is('a, input') && self.css('position') === ''){
                        self.css('position', 'relative');
                    };
                });
            };
        });
    };
    
    $('.carousel, #globalHeader').supersleight();
    
    
    /*
     * handles switching videos on the nominee detail page
     *
     */
    $('ul#candidateQuestions li a').click(function(e) {
        
        // don't actually follow the link
        e.preventDefault();
        
        // empty the existing flowplayer FLASH object/embed
        // and switch the link's HREF to the new vid
        $('a#player').empty().attr( 'href', $(this).attr('href') );
        
        // call flowplayer like it's the maiden voyage
        flowplayer('player', 'flowplayer/flowplayer-3.1.1.swf', {
            clip: { 
                autoPlay: true,
                onStart: function(clip) {             
                    google._trackEvent("Videos", "Play", clip.url);
                },
                onPause: function(clip) {             
                    google._trackEvent("Videos", "Pause", clip.url, parseInt(this.getTime()));         
                },                 
                onStop: function(clip) {             
                    google._trackEvent("Videos", "Stop", clip.url, parseInt(this.getTime())); 
                },                  
                onFinish: function(clip) {             
                    google._trackEvent("Videos", "Finish", clip.url);         
                }
            },
            plugins: {
                controls: {
                    bottom: 0,
                    opacity: 0.75,
                    fullscreen: false,
                    autoHide: 'always',
                    hideDelay: 500,
                    stop: true
                }
            }
        });
        
        // make the list highlight the link so it's active/current
        $('ul#candidateQuestions li.currentQuestion').removeClass('currentQuestion');
        $(this).parent().addClass('currentQuestion');
        
    });
    
});

