var this_scroller = {
    active_block: "",
    active_distance: 0,
    standard_distance: 0,
    time_to_scroll: 0,
    scroll_forward: function(active_distance) {
        $direction = "forward";
        if (active_distance != 0) {
            this_scroller.time_to_scroll = active_distance * 18;
        } else if (this_scroller.standard_distance > 900) {
            this_scroller.time_to_scroll = this_scroller.standard_distance * 18;
        } else {
            return false;
        }
        jQuery(this_scroller.active_block).animate({
            left: -(this_scroller.standard_distance)
        }, jQuery.extend(true, [], { duration: this_scroller.time_to_scroll, complete: function() {
            this_scroller.scroll_backward(0);
        }})); 
    },
    scroll_backward: function(active_distance) {
        $direction = "backward";
        if (active_distance != 0) {
            this_scroller.time_to_scroll = active_distance * 18;
        } else if (this_scroller.standard_distance > 900) {
            this_scroller.time_to_scroll = this_scroller.standard_distance * 18;
        } else {
            return false;
        }
        jQuery(this_scroller.active_block).animate({
            left: "0"
        }, jQuery.extend(true, [], { duration: this_scroller.time_to_scroll, complete: function() {
            this_scroller.scroll_forward(0);
        }}));
    }
};

jQuery(document).ready(function() {
//remove tabs that should not be active
    jQuery("a.sport_tags").each(function() {
        $tag = jQuery(this);
        $tag_link_pound = jQuery(this).attr("href"); 
        $tag_link = $tag_link_pound.slice(1);
        $actual_blocks = new Array();
        jQuery("div.ticker_sport").each(function() {
           $actual_blocks.push(jQuery(this).attr("id"));
        });
        if ($actual_blocks.indexOf($tag_link) == -1) {
            $tag.addClass("inactive_link");
        }
    });
    jQuery("a.inactive_link").parent("li").css("background", "#002D81");
//set up initial page
    //find block that matches first tab
    $first_block_name = "div" + jQuery("a.sport_tags:not(.inactive_link)").first().attr("href"); 
    //hide all blocks 
    jQuery('div.ticker_sport').css("display", "none");
    //display block that matches first tab
    this_scroller.active_block = $first_block_name;
    jQuery($first_block_name).css("display", "block");
    //make first tab active
    jQuery("a.sport_tags").first().parent("li").addClass("active");
    //count divs within block, assign width to block 
    $active_length = 0;
    $active_boxes = jQuery(this_scroller.active_block).children("div.champ_box");
    $active_boxes.each(function(){
    	$active_length = $active_length + jQuery(this).width() + 14;
    });
    $active_length = $active_length + 200;
    if ($active_length > 900) {
        this_scroller.standard_distance = $active_length - 900;
    } else {
        this_scroller.standard_distance = 900;
    }
    jQuery(this_scroller.active_block).css("width", $active_length + 100);  
//click on tabs
    jQuery("a.sport_tags").click(function() {
        if (jQuery(this).hasClass("inactive_link")) {
            return(false);
        } else {
            //stop animation in other blocks
            jQuery("div.ticker_sport").stop();
            //change active class
            jQuery("a.sport_tags").parent("li").removeClass("active");
            jQuery(this).parent("li").addClass("active");
            //change which block is visible
            this_scroller.active_block = "div" + jQuery(this).attr("href");
            jQuery('div.ticker_sport').css("display", "none");
            jQuery(this_scroller.active_block).css("display", "block");
            //change width of block
            $active_length = 0;
            $active_boxes = jQuery(this_scroller.active_block).children("div.champ_box");
            $active_boxes.each(function(){
            	$active_length = $active_length + jQuery(this).width() + 14;
            });
            $active_length = $active_length + 200;
            this_scroller.standard_distance = $active_length - 900;
            jQuery(this_scroller.active_block).css("width", $active_length);
            //push scroll to beginning
            jQuery(this_scroller.active_block).css("left", 0);
            //tell block to scroll
            this_scroller.scroll_forward(0);
            return(false);
        }
    });
//set up autoscroll
    //push scroll to beginning
    jQuery(this_scroller.active_block).css("left", 0);
    //start scroll
    this_scroller.scroll_forward(0);
//stop action of scroll if hover on boxes
    jQuery("div.champ_scores").hover(function() {
        jQuery(this_scroller.active_block).stop();
    }, function() {
        $adjusted_active_position = jQuery(this_scroller.active_block).position();
        if ($direction == "forward") {
            this_scroller.scroll_forward(this_scroller.standard_distance + $adjusted_active_position.left); 
        } else {
            this_scroller.scroll_backward(this_scroller.standard_distance - $adjusted_active_position.left);
        }
    });
//click on left arrow
    jQuery("div.arrow-left").click(function() {
        //check position of block, if far left, do nothing, otherwise move left
        $active_position = jQuery(this_scroller.active_block).position();
        if ($active_position.left == 0) {
            return(false);
        } else if ($active_position.left > -385) {
            jQuery(this_scroller.active_block).stop().animate({
                left: "0"
            }, 2500, function() {
               this_scroller.scroll_forward(0); 
            });
            return(false);
        } else {
            jQuery(this_scroller.active_block).stop().animate({
                left: '+=385'
            }, 1500, function() {
                $adjusted_active_position = jQuery(this_scroller.active_block).position();
                this_scroller.scroll_backward(this_scroller.standard_distance + $adjusted_active_position.left);
            });
            return(false); 
        }    
    });
//click on right arrow
    jQuery("div.arrow-right").click(function() {
        //check position of block
        $active_position = jQuery(this_scroller.active_block).position();
        //calculate far right limit
        $active_negative = -this_scroller.standard_distance;
        //if far right, do nothing, otherwise move right
        if ($active_position.left == $active_negative) {
            return(false);
        } else if (this_scroller.standard_distance + $active_position.left < 385) {
            jQuery(this_scroller.active_block).stop().animate({
                left: $active_negative
            }, 2500, function() {
                this_scroller.scroll_backward(0);
            });
            return(false);           
        } else {
            jQuery(this_scroller.active_block).stop().animate({
                left: '-=385'
            }, 1500, function() {
                $adjusted_active_position = jQuery(this_scroller.active_block).position();
                this_scroller.scroll_forward(this_scroller.standard_distance + $adjusted_active_position.left);
            });
            return(false);
        }
    });
});
