﻿var choiceThumbs;
var curThumb = 0;
var thumbClicked = false;
var delay = 4000;

var sliding = false;


function moveTopics( multiplier )
            {
                var toSlide = $('#topicSlider');
                
                
                if( !sliding )
                {
                    sliding = true;
                    
                    var distance = 130;

                    var newTop = parseInt($(toSlide).css("top")) + ( distance * multiplier );

                    
                    $(toSlide).animate(
                        {
                        top: newTop
                        } , 
                        function(){
                            if( multiplier > 0 )
                            {
                                var moveChild = $(toSlide).children(':last');

                                moveChild.prependTo(toSlide);
                            }else{
                                var moveChild = $(toSlide).children(':first');

                                   moveChild.appendTo(toSlide);

                            }                                  
                            $(toSlide).css("top", newTop - ( distance * multiplier  ) )      
                            sliding = false;                        
                        }
                    );        
                }
            }
            
            function moveThumbs( button, multiplier )
            {
                var toSlide = $(button).parent(".thumbs").children(".thumbDisplay").children(".thumbSlider");
                if( !sliding )
                {
                    sliding = true;              
                    var distance = 80;
                    var newLeft = parseInt($(toSlide).css("left")) + ( distance * multiplier );
                    $(toSlide).animate(
                        {
                        left: newLeft
                        } , 
                        function(){
                            if( multiplier > 0 )
                            {
                                var moveChild = $(toSlide).children(':last');

                                moveChild.prependTo(toSlide);
                            }else{
                                var moveChild = $(toSlide).children(':first');

                                   moveChild.appendTo(toSlide);

                            }                                
                            $(toSlide).css("left", newLeft - ( distance * multiplier  ) )       
                            sliding = false;      
                        }
                    );
                    curLeft = newLeft;          
                }
            }
            
function showChoice( choice )
{
        var id = "#choice" + $(choice).attr("docId");
 
        var position = $(choice).position();
        
        $('#choiceBorder').animate({
        left: position.left
        }, 500, function(e)
        {
            if( $(id + ":visible").length == 0 )
           {
                $(".choiceApp:visible").hide();
                $(id ).fadeIn();
            }
        }); 
}

function showNextThumb()
{

    if( !thumbClicked )
    {
 
        curThumb = ( curThumb + 1 ) % choiceThumbs.length;
       
        showChoice(choiceThumbs[curThumb]);
         setTimeout(function(){ showNextThumb(); }, delay );
     }
}

function setHolderSize()
{
    var left = $('#left:visible');
    
    if( left.length > 0 )
    {    
        var height = $(window).height() - 95;
        var width = $(window).width();
        $('#holder').css('height', height );
        $('#holder').css('width', width );
        
        if( $.browser.msie ){
            $('#right').css('width', width - 170 );
        }
    }
        
}

$(function(){

    setHolderSize();
   $(window).bind('resize', function(){ setHolderSize(); } );   
    $('#right').scroll( function(){ $('.popularityTip').hide(); } );  
  
    
      choiceThumbs = $(".choiceThumb");

    if( choiceThumbs.length > 0 )
    {
        var position = $(choiceThumbs[0]).position();

        $('#choiceBorder').css('left', position.left );
        $('#choiceBorder').show();

        $('#choiceBorder').click(function(e){
            window.location = $(".choiceApp:visible").children(':first').children('a').attr('href');
        });        

        $(".choiceApp:first").show();

        $(".choiceThumb").bind("click", function(ex){
            thumbClicked = true;
            showChoice(this);
         });
         
        setTimeout(showNextThumb, delay );
    }  
    
   
     var topics = $('#topicSlider>div');
    topics.clone().appendTo('#topicSlider');
    
    var moveExtra = 0;
    
    if( $.browser.msie ){
        moveExtra = -50;
        }
        
    $('#topicSlider').css("top", ( topics.length * -130  ) + moveExtra );
                
    var leftButton = $('.thumbLeft');
                
                 
                if( leftButton.length > 0 )
                {
                    var thumbs = $('.thumbSlider').children();
                                  
                      jQuery.each(thumbs, function(){
                        $(this).clone().appendTo($(this).parents(':first'));                            
                      });
                      
                       jQuery.each(thumbs, function(){
                        $(this).clone().appendTo($(this).parents(':first'));                            
                      });
                   
                   if( $.browser.msie )
                   {           
                     $('.thumbSlider').css("left", -280 );
                     }else{
                         $('.thumbSlider').css("left", -262 );
                     }
               
                    $('.thumbLeft').bind('click', function(event)
                    {
                        moveThumbs($(this), 1);
                    
                   
                      
                    });

                    $('.thumbRight').bind('click', function(event)
                    {
                        moveThumbs($(this), -1);
                 
                    }); 
                    
                    
                
                }
 
                 $('.topicLink').hover( function(e){
                $('#topicTip').css('left', e.pageX-200);
                $('#topicTip').html('<span class="title">' + $(this).attr('Title') + '</span><br/><span class="author">' + $(this).attr('Author') + '</span>');
                $('#topicTip').show();
            },
            function(){
                $('#topicTip').hide();
                }
                );
                
                
                 
      
                     $('#topicsUp').bind('click', function(event)
                    {
             
                        moveTopics( 1);
                    
                   
                      
                    });

                    $('#topicsDown').bind('click', function(event)
                    {
                        moveTopics(-1);
                 
                    });  
                    
                   
            
});


