$(document).ready(function(){
	$('#slides').hide();
	var images = [];
	$('.images').each(function(){
		images.push($(this).attr('href'));
	});
    	$(images).each(function(index,value){
    		var list = $('<div id="portfolio_'+index+'"></div>');
			$('#gallery').append(list);
    		var img = new Image();
        	$(img)
        	.load(function(){
        		$('#portfolio_'+index).append(this);
        		if(index == (images.length)-1){
        			$('#preloader').fadeOut(400,function(){
        			$('#slides').show();
        			$('#gallery div').css({opacity: 0});
					$('#gallery div:first').animate({opacity: 1}, 400);
					$('#gallery div:first').addClass('show');
        			setInterval('gallery()',4000);
        			});
        		}
        	})
        	.attr('src', value).attr('width', 700).attr('height', 340);	
    	});
        
});

function gallery(){
	var current = ($('#gallery div.show')?  $('#gallery div.show') : $('#gallery div:first'));
	var next = (current.next().length) ? current.next() : $('#gallery div:first');
	current.delay(300)
    .animate({opacity: 0.0}, 1000)  
    .removeClass('show');	
	next.addClass('show')  
    .animate({opacity: 1.0}, 1000);  
}
