// 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-ticker span.container-item').html('Error loading the news.');
		$('#news-ticker 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>/, '');
	if (item.title.length > 80) {
		item.title = item.title.substr(0,80) + '...';
	}
    var itemHtml = '<a href="' + item.id + '">' + item.title + '</a>';
    $('#news-ticker span.container-item').html(itemHtml);
    setTimeout("com_doctorsofusc_home_feed_timer(" + this.cursor + ")", 5000);
} };

$(document).ready(function() {
    jQuery.getFeed({
        url: '/uscurology/about/press-room/rss/78001/10',
        success: function(feed) {
        	if (feed.items.length > 5) {
        		feed.items = feed.items.slice(0,5);
        	}
            com_doctorsofusc_home_feed.items = feed.items;
            com_doctorsofusc_home_feed.next();
        }
    });
    $('#front-body .content .item-list').attr('id', 'scientific-slider');
    //Scientif Papers News Carousel
    $('#scientific-slider').easySlider({
    	continuous: true,
    	nextId: 'nextBtn',
    	prevId: 'prevBtn',
    	numeric: true,
    	numericId: 'numericControl'
    });

    // Next link
	$('#news-ticker a.next').click(function() {
		com_doctorsofusc_home_feed.next();
		return false;
	});

	// If Mac Firefox
	if( $.browser.mozilla && navigator.platform == "MacPPC" ) {
	    $('#hero').append('<div id="flashcontainer-backup"></div>');
	    com_srausc_contactus_macfirefox();
	}
	
	
	//scientific paper
	$('#scientific-slider ul li').each(function(){
		var isThereImage = $(this).find('.view-item-scientific-papers-news .view-data-node-data-field-scientific-thumbnail-field-scientific-thumbnail-fid').html();
		if(isThereImage == ""){
			var img = '<img src="/files/usc.png" alt="USC Urology" title="USC Urology" class="imagefield imagefield-field_scientific_thumbnail" height="150" width="100" />';
			$(this).find('.view-item-scientific-papers-news .view-data-node-data-field-scientific-thumbnail-field-scientific-thumbnail-fid').html(img);
		}
	});
	
	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) {
	            index = itemCount; // go forward to the next duplicate, but look like we are moving backward
	            self.goTo(index-1, 0);
	        } else if (index >= itemCount) {
	            index = 0;
	            self.goTo(index, 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');
            var cntNewImg = '#cnt' + newImg;
	        var cnt = parseFloat($(cntNewImg).val());
	        var rand = Math.floor(Math.random()*cnt);
	        var relName = newImg + "-" + rand;
	        //alert(relName);
            //console.log(relName);
	        
	        // the class 'active' removes the display:none
	        $('#docsHomeScrollerImage img').removeClass('active').stop().fadeTo(500, 0);
	        //$('#docsHomeScrollerImage img.'+newImg ).addClass('active').stop().fadeTo(500, 1);
	        $('#docsHomeScrollerImage img[rel="' + relName + '"]').addClass('active').stop().fadeTo(500, 1);
	        
	        $('#docsHomeScrollerNav .images .image').addClass('inActive').eq(index).removeClass('inActive');
	        $('#docsHomeScrollerNav .images li.closed').addClass('inActive').removeClass('isActive')
	        	.eq(index).removeClass('inActive').addClass('isActive');
	        
	        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;
	        
	        
	        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');
				rel = rel.substring(0, rel.length-2);
	            window.location = $('#docsHomeScrollerNav a[rel="'+rel+'"]').attr('href');
	            return false;
	        });
	        
	        $('#docsHomeScrollerNav #specialty_list span.button').unbind('click').click(function(){
	        	$('div.image a', $(this).parent().parent()).click();
	        	return false;
	        });
	        
	        self.goTo(0);
	    };
	    
	    
	    self.init();
	    return self;
	};
	
	docsHomeScroller = new docsHomeScroller();
	docsHomeScroller.play();
});

