var urlList = '';
var count = 0;
var loaded = 0;




$(document).ready(function(){
		
	resizeContainers();
	
	$(window).resize(resizeContainers);
	
	if($('.portfolio_container').length > 0) {
		$.get(urlList,function(response){
			$('.portfolio_container').html(response);
			count = $(response).find('img').length;
			loaded = 0;
			$(response).find('img').each(function(){
			obj = new Image();
			$(obj).attr('src',$(this).attr('src')).load(function(){
				loaded++;
				if(count == loaded)
					$('.portfolio_index').fadeIn();
				
			})
			})
			//$('.list_container_parent').height($(window).height() - 219);
		});
	}
	
	$('.work_item img').click(function(){
		var elems_count = $('.work_item').length;
		var index = $('.work_item').index($(this).parent().first());
		
		var position = 0;
		
		if(index == elems_count - 1) 
			index = 0;
		else {
			index = index + 1;
			var position = $('.work_item:eq('+index+')').position().top;
		}
		
//		$('.work').animate({top: -position});
//		$(window).scrollTop(position);
		if(index == elems_count - 1 && $('.work_item:eq('+index+')').height() < $(window).height() - 159) {
			$('.work_item:eq('+index+')').height($(window).height() - 149);
		}
		$('html,body').animate({
			scrollTop: position
			}, 500);
		
	});
	
	$('.image_container img.over').live('mouseover',function(){
		if($(this).width() < 250)return;
//		$(this).animate({left : "-239px"},200);
//		$(this).siblings('img:[class!="over"]').live('mouseout',function(){
//			$(this).unbind('mouseout');
//			$(this).siblings('.over').fadeIn();
//			alert('dupa');
//		});
		$(this).fadeTo('fast',0.01);
		
	});
	$('.image_container img.over').live('mouseout',function(){
		if($(this).width() < 250)return;
//		$(this).animate({left : "-239px"},200);
//		$(this).siblings('img:[class!="over"]').live('mouseout',function(){
//			$(this).unbind('mouseout');
//			$(this).siblings('.over').fadeIn();
//			alert('dupa');
//		});
		$(this).fadeTo('fast',1);
		
	});
	
	if($('.work')) {
		count = $('.work').find('img').length;
		loaded = 0;
		$('.work').find('img').each(function(){
		obj = new Image();
		$(obj).attr('src',$(this).attr('src')).load(function(){
			loaded++;
			if(count == loaded)
				$('.work').fadeIn();
		})
		})
	}

	$('.article').fadeIn();
//	$('.item').live('mouseout',function(){
//		if($(this).find('img.over').width() < 250)return;
////		$(this).animate({left : "0"},200);
//		$(this).find('img.over').show();
//	});
});

function resizeContainers() {
	var wh = $(window).height();
	$('.res').each(function(){
		$(this).height(wh - 59);
	});
	
	//239
	$('.portfolio_index').each(function(){
		$(this).height(wh - 149);
	});	
	$('.portfolio_container').each(function(){
		$(this).height(wh - 239);
	});	
	
	$('.list_container_parent').each(function(){
		$(this).height(wh - 219);
	});	
}

function showWork(url) {
	$.get(url,function(response){
		$('.portfolio_index').append(response);
		
		$('.work_item img').click(function(){
			var elems_count = $(this).parent().siblings().length;
			var index = $('.work_item').index($(this).parent().first());
			
			var position = 0;
			
			if(index == elems_count - 1) 
				index = 0;
			else {
				index = index + 1;
				var position = $('.work_item:eq('+index+')').position().top;
			}
				
			$('.work').animate({top: -position});
			$(window).scrollTop(position);
			
		});
	});
}

function hideWork() {
	$('.work').detach();
}
