//adds width and height to divs (ie bug)
window.onload = resizeDivs;
function resizeDivs()
{
    $('.buttonImageSlider').each(function ()
  {     
    $(this).width($(this).find("img").width());
    $(this).height($(this).find("img").height()); 
  });
}
$(document).ready(function() 
{
  var flashElement = $('.overlay').length;

  $('.buttonImageSlider').hover(
        function()
    {
            var currentBtn = $(this);
            $('.selectedBtn').removeClass('selectedBtn');
            $(currentBtn).addClass('selectedBtn');
            
            $('#container div img.fade').each(function(){
                $(this).stop().animate({opacity:0.3}, 500);
            });   
            
          if(flashElement)
          {
            $('#container div img.overlay').each(function(){
              $(this).stop().show().animate({opacity:0.7}, 500);
            });
            }
            
            currentBtn.find('img.fade').stop().animate({opacity:1}, 500);
            
            if(flashElement)
             currentBtn.find('img.overlay').stop().animate({opacity:0}, 500).delay(1,function(){$(this).hide().css({'cursor':'pointer'})});
        },
        function()
    {
            $(this).find('img.fade').stop().animate({opacity:0.3}, 500);  
            
            if(flashElement)
             $(this).find('img.overlay').stop().animate({opacity:0.7}, 500);
        }
    );
    
    var currentPos;
    
    $('#slider').bind("mouseenter", function(e){
        move = true;
    });
    
    $('#slider').bind("mouseleave", function(e){
        move = false;
    });
    
    var speed = 15;
    var maxScroll = $('#slider').attr('scrollWidth') - 972;
    var currentX;
    var middleWidth = 150;
    var move = false;
    var ease = 1;
    var currentDirection;
    
    $('#slider').mousemove(function(e){
        currentX = e.pageX - $(this).offset().left;
    });
    
    var timer = setInterval(enterFrame, 1000/30);
    function enterFrame(){
        if(move){
            if(currentX < 972/2 - middleWidth){
                ease = 1;
                currentDirection = "left";
                scrollContainer(-1);
            }
            else if(currentX > 972/2 + middleWidth){
                ease = 1;
                currentDirection = "right";
                scrollContainer(1);
            }
            else{
                if(currentDirection == "left"){
                    ease = ease/3;
                    scrollContainer(-ease);
                }
                else if (currentDirection == "right"){
                    ease = ease/3;
                    scrollContainer(ease);
                }
            }
        }
    }
    
    function scrollContainer(direction){
        if($('#slider').attr('scrollLeft') < 100 || $('#slider').attr('scrollLeft') > (maxScroll - 100)){
            $('#slider').attr('scrollLeft', $('#slider').attr('scrollLeft') + (direction * speed) * ease );
            
        }
        else{
            $('#slider').attr('scrollLeft', $('#slider').attr('scrollLeft') + (direction * speed));
        }
    }
    
    function stopContainer(){
        
    }
    
    $('#slider').hover(
        function(e){
        
        },
        function(){
            $('#container div').each(function(){
                $(this).find('img.fade').stop().animate({opacity:1}, 500);
                
                if(flashElement)
                  $(this).find('img.overlay').animate({opacity:0}, 500);
            });		
        }
    );

});

$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
};
