$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len = elems.length;
      
  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }
  });

  return this;
};

$(document).ready(function (){

	if ( $("#post.vrt").length > 0 )
	{
		//$("#post.vrt").css("display","block").css("visibility","hidden");
		
		$("#post.vrt ul#gallery li:last").after("<li id='title'>" + $("#post.vrt #title").html() + "</li>");
					$("#post.vrt div#title").css("display","none");
		
	// on attend que les images soient chargées avant de faire les calculs
		$('#post.vrt img', this).imagesLoaded( function () {
			
			$('#loader').remove();
			$('#post.vrt li#title p:first').remove();
			
					
					$("#overflow").height( $(window).height() );
					//$("#post.vrt").css("display","none").css("visibility","visible");
					$("#post.vrt").fadeTo(3000,1);
					
	
	
    $('#overflow').mousedown(function (event) {
            $(this)
                .data('down', true)
                .data('y', event.clientY)
                .data('scrollTop', this.scrollTop);
            return false;
        }).mouseup(function (event) {
            $(this).data('down', false);
        }).mousemove(function (event) {
            if ($(this).data('down') == true) {
                this.scrollTop = $(this).data('scrollTop') + $(this).data('y') - event.clientY;
            }
        }).mousewheel(function (event, delta) {
            this.scrollTop -= (delta * 30);
        }).css({
            'overflow' : 'hidden'
        });
    });
    
    $(window).mouseout(function (event) {
        if ($('#overflow').data('down')) {
            try {
                if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
                    $('#overflow').data('down', false);
                }                
            } catch (e) {}
        }	
	
	
	
	
	
			
			});
		
	}
	
	if ( $("#post.hrz").length > 0 )
	{
		$("#post.hrz").css("display","block").css("visibility","hidden");
		
		//alert ( $(document).width() )
		
		$("#overflow").width( $(window).width() );
		margenegative = $("#global").offset().left;
		
		$("#overflow").css( "left", -(margenegative));
		
		// on attend que les images soient chargées avant de faire les calculs
		$('#post.hrz img', this).imagesLoaded( function () {
			
			$('#loader').remove();
			$('#post.hrz #title p:first').remove();
			
			
			var wdthGallery = 0;
				$("#post.hrz #gallery li").each( function()
				{
					wdthGallery = wdthGallery + $(this).width();
					//console.log("boucle " + $(this).width() );
				});
				//console.log("fin de boucle " + wdthGallery);
				wdthGallery = wdthGallery + ( $("#post.hrz #gallery li").length * 18);
				//console.log("avec marge " + wdthGallery);
				$("#post.hrz").width(wdthGallery + 300);
				//$("#post.hrz #gallery").width(wdthGallery);
				$("#post.hrz").css("display","none").css("visibility","visible");
				var postX = $("#menu").scrollLeft();
				$("#post.hrz").css("left",214+margenegative);
				$("#post.hrz").fadeTo(3000,1);

				
				
				
				
				
				
	    $('#overflow').hover(function (){
			 $(this).css('cursor','url(http://www.thibaudcartigny.com/portfolio/wp-content/themes/thibaudcartigny/images/cursors_over.gif), move');
		}).mousedown(function (event) {
            $(this)
                .data('down', true)
                .data('x', event.clientX)
                .data('scrollLeft', this.scrollLeft)
				.css('cursor','url(http://www.thibaudcartigny.com/portfolio/wp-content/themes/thibaudcartigny/images/cursors_release.gif), crosshair');
            return false;
        }).mouseup(function (event) {
            $(this).data('down', false);
			//$(this).css('cursor','url(http://www.thibaudcartigny.com/portfolio/wp-content/themes/thibaudcartigny/images/cursors_over.gif), move');
        }).mousemove(function (event) {
            if ($(this).data('down') == true) {
                this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
				$('#overflow').css('cursor','url(http://www.thibaudcartigny.com/portfolio/wp-content/themes/thibaudcartigny/images/cursors_release.gif), crosshair');
            }
        }).mousewheel(function (event, delta) {
            this.scrollLeft -= (delta * 30);
        }).css({
            'overflow' : 'hidden'
        })
    });
    
    $(window).mouseout(function (event) {
        if ($('#overflow').data('down')) {
            try {
                if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
                    $('#overflow').data('down', false);
                }                
            } catch (e) {}
        }
		
        

				
				
				
				
				
				
				
			
			}); // fin load
		
		
	
			
			
	
	}
	
	
	if ( $("#post.page").length > 0 )
	{
		//$("#post.page").css("display","block").css("visibility","hidden");
		$('#loader').remove();
		$("#post.page").fadeTo(3000,1);
		
	}
	

		
});
