function makeScrollable(wrapper, scrollable){
  // Get jQuery elements
  var wrapper = $(wrapper), scrollable = $(scrollable);

  // Hide images until they are not loaded
  scrollable.hide();
  var loading = $("<div class='loading'></div>").appendTo(wrapper);

  // Set function that will check if all images are loaded
  var interval = setInterval(function(){
    var images = scrollable.find("img");
    var completed = 0;

    // Counts number of images that are succesfully loaded
    images.each(function(){
      if (this.complete) completed++;   
    });

    if (completed == images.length){
      clearInterval(interval);
      // Timeout added to fix problem with Chrome
      setTimeout(function(){

        loading.hide();
        // Remove scrollbars    
        wrapper.css({overflow: "hidden"});

        scrollable.slideDown("slow", function(){
          enable(); 
        });                 
      }, 1000); 
    }
  }, 100);

  function enable(){
    // height of area at the top at bottom, that don't respond to mousemove
    var inactiveMargin = 99;					
    // Cache for performance
    var wrapperWidth = wrapper.width();
    var wrapperHeight = wrapper.height();
    // Using outer height to include padding too
    var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
    // Do not cache wrapperOffset, because it can change when user resizes window
    // We could use onresize event, but it's just not worth doing that 
    // var wrapperOffset = wrapper.offset();
    
    // Save menu titles
    scrollable.find('a').each(function(){				
    	$(this).data('tooltipText', this.title);				
    });
    
    // Remove default tooltip
    scrollable.find('a').removeAttr('title');		
    // Remove default tooltip in IE
    scrollable.find('img').removeAttr('alt');	
    
    var lastTarget;
    //When user move mouse over menu			
    wrapper.mousemove(function(e){
    	// Save target
    	lastTarget = e.target;
    
    	var wrapperOffset = wrapper.offset();
        	
    	// Scroll menu
    	var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
    	if (top < 0){
    		top = 0;
    	}			
    	wrapper.scrollTop(top);
    });
    
    // Setting interval helps solving perfomance problems in IE
    var interval = setInterval(function(){
    	if (!lastTarget) return;	
    								
    }, 200);              
  }
}


$(function() {
    $('.preventDefault').click(function(e){
        e.preventDefault();
    });
    
    Cufon.replace('.menu-header li a, .rockwell, .widget h3, .comment-title, .blog-sidebar a', {
        fontFamily: 'Rockwell',
        hover: true
    });
    
    Cufon.replace('.extra-info p', {
        fontFamily: 'Avenir'
    });
    
    $('#side-intro').html($('#text-3').html());
    
	$("#slideshow").css("overflow", "hidden");
	
    $('.searchform .search').focus(function() {
        if($(this).val() == 'Search...') {
            $(this).val('');
        }
    });
    
    $('.searchform .search').blur(function() {
        if($(this).val() == '') {
            $(this).val('Search...');
        }
    });
    
	makeScrollable("div.portfolio-sidebar-wrapper", "div.portfolio-sidebar-menu");

    $(".gallery br, .portfolio-content-wrapper style").remove();
    
    $('.portfolio-content-wrapper img, .testimonial-wrapper img, .press-wrapper img').first().remove();
    $('.portfolio-content-wrapper img').last().remove();


    $('.portfolio-content-wrapper').cycle({ 
        timeout: 5000,
        speed: 1500,
        pager:  '#mycarousel',
        pause: 1,
        // callback fn that creates a thumbnail to use as pager anchor 
        pagerAnchorBuilder: function(idx, slide) { 
            return '<li><a href="#"><img src="' + slide.src + '" width="72" /></a></li>'; 
        } 
    }); 
    
    $('.testimonial-wrapper').cycle({
        timeout: 0,
        speed: 600,
        prev: '#testimonial-prev',
        next: '#testimonial-next'
    });
    $('.press-wrapper').cycle({
        timeout: 0,
        speed: 600,
        prev: '#press-prev',
        next: '#press-next'
    });

    //jCarousel 
    if ($.browser.webkit) {
        setTimeout(function(){
            $('#mycarousel').jcarousel({
                scroll: 8,
                visible: 8
            });
        }, 1000);
	$('.testimonial-sender').next().remove();
    } else {
        $('#mycarousel').jcarousel({
            scroll: 8,
            visible: 8
        });
    }

    // prevent default clicking behavior on the image
    $('.gallery a').click(function(e) {
        e.preventDefault();
    });
    
	$("#slides").cycle({
		fx: 'fade',
		pause: 1,
		prev: '#prev',
		next: '#next',
        before: function () {
            // Retrieve and show title and alt
            console.log(this);
            var alt = $(this).attr('alt');
            var title = $(this).attr('title');

            $('.photocredit').text('photographed by ' + title);
            $('.photolink').attr('href', alt);
        },
	});
});

