// Hack for Mac FF
function com_srausc_contactus_macfirefox() {
    if( $('#TB_window').length > 0 ) {
        $('#hero object').hide();
        $('#hero #flashcontainer-backup').show();
    }
    else {
        $('#hero object').show();
        $('#hero #flashcontainer-backup').hide();
    }
    setTimeout("com_srausc_contactus_macfirefox()", 500);
}

// News Ticker
function com_doctorsofusc_home_feed_timer(cursor) {
    if (cursor != com_doctorsofusc_home_feed.cursor) return;
    com_doctorsofusc_home_feed.next();
}
var com_doctorsofusc_home_feed = { "cursor" : -1, "items" : [], "next" : function(){
    if (typeof this.items == 'undefined' || this.items.length == 0) {
        $('#news span.container-item').html('Error loading the news.');
		$('#news a.next').remove();
        return;
    }
    this.cursor++;
    if (this.cursor >= this.items.length) this.cursor = 0;
    var item = this.items[this.cursor];
	var itemDesc = item.description.replace(/<p>/, '');
    var itemHtml = '<a href="' + item.id + '">' + item.title + '</a>';
    $('#news span.container-item').html(itemHtml);
    setTimeout("com_doctorsofusc_home_feed_timer(" + this.cursor + ")", 5000);
} };



var docsHomeScroller = function(){
    var slideshowTtl = 6000;
    
    var self = this;
    
    var currentIndex = 0;
    var itemCount;
    var activeItem;
    var originalCount;
    var paused = false;
    
    self.next = function(){
        var next = 1*currentIndex+1;
        self.goTo(next);
    };
    
    self.previous = function(){
        var previous = 1*currentIndex-1;
        self.goTo(previous);
    };
    
    var playInterval = false;
    self.play = function(){
        if (! playInterval) {
            playInterval = window.setInterval(play, slideshowTtl)
        }
    };
    
    var play = function(){
        if (!paused && !$('#docsHomeScroller').hasClass('hover') ) {
            self.next();
        }
    };
    
    self.goTo = function(index, duration) {
        if (typeof duration == 'undefined') {
            duration = 300; // * Math.abs(index - currentIndex);
        }
        
        if (index < 0) {
            // console.log('Skip forward: '+index+' -> '+(index + originalCount));
            
            index = index + originalCount; // go forward to the next duplicate, but look like we are moving backward
            self.goTo(index+1, 0);
            
        } else if (index >= itemCount-3) {
            
            
            
                // go back a to the previous duplicate, but look like we are moving forward
            //index = index - originalCount;
            //self.goTo(index-1, 0);
            
            var offset = 1;
            offset = Math.abs(currentIndex - index);
            
            // console.log('Skip back: '+index+' -> '+(index - originalCount)+ ', offset: ' + offset);
            
            index = index - originalCount;
            self.goTo(index - offset, 0);
        } else {
            
            // console.log('Go to: '+index);
        }
                
        $('#docsHomeScrollerNav .images').stop(true, true).scrollTo('.image:eq('+index+')', duration, {axis:'y'});
        
        var newImg = $('#docsHomeScrollerNav .images .image:eq('+index+') a').attr('rel');
            // the class 'active' removes the display:none
        $('#docsHomeScrollerImage img').removeClass('active').stop().fadeTo(500, 0);
        $('#docsHomeScrollerImage img.'+newImg ).addClass('active').stop().fadeTo(500, 1);
        
        $('#docsHomeScrollerNav .images .image').addClass('inActive').eq(index).removeClass('inActive');
        
        currentIndex = index*1;
    };
    
    self.init = function(){
        $('#docsHomeScrollerNav .nav a.previous').click(function(){
            paused = true;
            self.previous();
            return false;
        });
        $('#docsHomeScrollerNav .nav a.next').click(function(){
            paused = true;
            self.next();
            return false;
        });
        
        originalCount = $('#docsHomeScrollerNav .images .image').length;
        
            // add 'extra' copies to fill in
            // make sure we have at least 2 dupes and at least 2 screenfulls (2screen x 4high)
        var $clone = $('#docsHomeScrollerNav .images .image').clone();
        var failsafe = 0;
        
        $('#docsHomeScrollerNav .images').append($clone);
        while ($('#docsHomeScrollerNav .images .image').length < 8  && failsafe < 12) {
            failsafe++;
            $('#docsHomeScrollerNav .images').append(
            	$('#docsHomeScrollerNav .images .image').clone()
            );
        }
        
        itemCount = $('#docsHomeScrollerNav .images .image').length;
        
        var tmpIndex = 0;
        $('#docsHomeScrollerNav .images .image a').each(function(){
            $(this).attr('uscimgindex', tmpIndex);
            tmpIndex++;
        });
        
        $('#docsHomeScrollerNav .images .image a').click(function(){
            paused = true;
            var newIndex = $(this).attr('uscimgindex');
            self.goTo(newIndex);
            return false;
        });
        
        $('#docsHomeScrollerImage img').addClass('loaded');
        
        
        if (! $.browser.msie) {
            $('#docsHomeScrollerNav .images .image').hover(function(){
                if ($(this).hasClass('inActive')) {
                    $('img', this).stop().animate({'opacity' : 1}, 300);
                }
            }, function(){
                var imgRef = $('img', this);
                
                imgRef.stop().css({'opacity' : ''}); 
                return;
                
                var callback = function(){
                    imgRef.stop().css({'opacity' : ''});    
                };
                
                if ($(this).hasClass('inActive')) {
                    imgRef.stop().animate({'opacity' : .25}, 300, callback);
                } else {
                    imgRef.stop().animate({'opacity' : 1}, 300, callback);
                }
            });
        }
            
        
        
        $('#docsHomeScroller').hover(function(){
            $(this).addClass('hover');
        }, function(){
            $(this).removeClass('hover');
        });
        
        $('#docsHomeScrollerImage').click(function(){
            var rel = $('img.active', this).attr('rel');
            window.location = $('#docsHomeScrollerNav a[rel="'+rel+'"]').attr('href');
            return false;
        });
        
        self.goTo(0);
    };
    
    
    self.init();
    return self;
};

