$(document).ready(function() {
		
	$('.gallery ul').after('<div id="gallery" />');
	
	
	$('.gallery ul li').each(function(i,e){
		var imglink = $(this).children('a').attr('href');
		var imgsrc = $(this).children('a').attr('rel');
		var imgtitle = $(this).children('a').attr('title');
		var imgtext = $(this).children('div').html();
		$(this).children('a').removeClass('pirobox_gall');
		$('#gallery').append('<a class="pirobox_gall" href="'+imglink+'" title="'+imgtitle+'"><img src="'+imgsrc+'" alt="'+imgtitle+'" title="'+imgtitle+'"/><div>'+imgtext+'</div></a>');
	});
	
	$('#gallery').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 0, 
	    pager:  '.gallery ul',
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '.gallery ul li:eq(' + idx + ') a'; 
	    } 
	});
	
	$('.gallery ul').wrap('<div id="gallery_wrap" />');
	$('.gallery ul li div').css('display', 'none');
	$('.gallery ul li').css({
		'width': 'auto'	
	});
	
	
	$('#gallery_wrap').css({
		'width': '600px',
		'height': '120px',
		'overflow': 'hidden',
		'margin' : '0 auto'
	});
	
	$('.gallery ul').css({
		'width': '10000px'
	});
	
	var lastLi = $('.gallery ul').find('li:last-child');
	var lastOffset = $('.gallery ul').children().size()*100;
	var lastWidth = 100;

	var divWidth = $('#gallery_wrap').width();
	
	$('.gallery ul').css('width', lastOffset+lastWidth);
	
	$('#gallery_wrap').mousemove(function(e){
		
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();
	
	    var left = (e.pageX - $('#gallery_wrap').offset().left) * (ulWidth-divWidth) / divWidth;
	    $('#gallery_wrap').scrollLeft(left);
	});
  
	$('.gallery ul li').fadeTo(0, 0.4);
  
  	$('.gallery ul li').live('mouseover', function(){
  		$(this).fadeTo('fast', 1);
  		return false;
  	})
  	
  	$('.gallery ul li').live('mouseout', function(){
  		$(this).fadeTo('fast', 0.4);
  		return false;
  	})

		
	$().piroBox({
      my_speed: 300, //animation speed
      bg_alpha: 0.5, //background opacity
      radius: 4, //caption rounded corner
      scrollImage : false, // true == image follows the page _|_ false == image remains in the same open position
                           // in some cases of very large images or long description could be useful.
      slideShow : 'true', // true == slideshow on, false == slideshow off
      slideSpeed : 3, //slideshow
      pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
      pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
      close_all : '.piro_close,.piro_overlay' // add class .piro_overlay(with comma)if you want overlay click close piroBox
     });
    		
});