jQuery(document).ready(function($) {
   $(".work li .excerpt").hide()
   $(".work li").hover(function () {
    	$(".excerpt", this).fadeIn();
  	}, function () {
    	$(".excerpt", this).fadeOut();
  	})
  	//enlarge the window
  	$(".work li a").click(function(){
  		var url = $(this).attr("rel");
  		//$(".work li:has(.full)").find('.intro').slideDown()
		//$(".work li:has(.full)").find('.full').slideUp(function(){$(this).remove()})
  		$(this).parent().parent().append("<div class='full'></div>");
  		$(this).parent().parent().addClass("loading")
  		$(this).parent().parent().append("<span class='loader'></span>");
  		
  		$(this).parent().parent().find(".full").load(url, function() {
  			$(this).hide();
  			$(this).parent().removeClass("loading");
  			$(this).parent().find(".loader").hide();
  			$("img:eq(0)",this).remove();
  			$('.wp-caption',this).wrapAll("<div class='carousel' rel='1'><div class='carousel-container'></div></div>")
  			var n = $('.wp-caption', this).size();
  			var w = 700;//$('.carousel', this).width();
  			//console.log(n)
  			$('.carousel-container',this).width(w*n);
  			$('.carousel img',this).each(function(i){
  				$(this).removeAttr('width').hide();
  				$(this).load(function(){
  					$(this).fadeIn();
  				})  				
  			})
  			$('.carousel-container a',this).removeAttr('href')
  			$('.carousel .wp-caption',this).removeAttr('style')
  			$(this).parent().prepend("<a class='collapse'> &uarr;</a>")
  			if(n>1){
	  			$('.carousel',this).prepend("<a class='arrow left disabled'>&lsaquo;</a><a class='arrow right'>&rsaquo;</a>")
  			}
  			$(this).slideDown()
  			$(this).parent().find(".intro").slideUp();
		});
		//return false
  	})
  	// prevent images from open in an external window
  	
  	
  	//pagination on carousel
  	$(".arrow").live('click', function(){
  		
		 if($(this).hasClass('disabled')) return false;
		 
		 var currentPage = $(this).parent().attr('rel')
  		 var totalPages = $(this).parent().find(".carousel-container .wp-caption").size()
		 var blocksContainer = $(this).parent().find(".carousel-container")
		 
		 if($(this).hasClass('left')) {
		 	currentPage--
		 	blocksContainer.animate({ marginLeft: '+='+blocksContainer.parent().width()}, 500);
		 }
		 if($(this).hasClass('right')) {
		 	currentPage++
		 	blocksContainer.animate({ marginLeft: '-='+blocksContainer.parent().width()}, 500);
		 }
		 //console.log(currentPage+", "+totalPages)
		 if(currentPage>1){
		 	$(this).parent().find(".left").removeClass("disabled")
		 }else{
		 	$(this).parent().find(".left").addClass("disabled")
		 }
		 if(currentPage<totalPages){
		 	$(this).parent().find(".right").removeClass("disabled")
		 }else{
		 	$(this).parent().find(".right").addClass("disabled")
		 }
		 $(this).parent().attr('rel',currentPage)
	});	
	
	$(".collapse").live('click', function(){
		$(this).parent().find('.intro').slideDown()
		$(this).parent().find('.full').slideUp(function(){$(this).remove()})
		$(this).remove()
	})
	
	//smooth scroll 
    $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });

});  
 