$(document).ready(function(){
	$(".clients").each(function(){
		images = $(this).find(".images");
		texts = $(this).find(".texts");
		$(this).find(".image").each(function(){
			pad_top = Math.floor(($(this).parent().height()-$(this).height()) / 2);
			$(this).css("padding-top",pad_top);
		})
		images.find(".image:eq(0)").show().addClass("active");
		texts.find(".text:eq(0)").show().addClass("active");
		$(this).find(".next").click(function(event){
			if(images.find(".active").next().length){
				images.find(".active").add(texts.find(".active")).fadeOut().removeClass("active").next().fadeIn().addClass("active");
			}
			else{
				images.find(".active").add(texts.find(".active")).removeClass("active").fadeOut();
				images.find(".image:eq(0)").add(texts.find(".text:eq(0)")).fadeIn().addClass("active");
			}
			event.preventDefault();
		})
		$(this).find(".prev").click(function(event){
			if(images.find(".active").prev().length){
				images.find(".active").add(texts.find(".active")).fadeOut().removeClass("active").prev().fadeIn().addClass("active");
			}
			else{
				images.find(".active").add(texts.find(".active")).removeClass("active").fadeOut();
				images.find(".image:last").add(texts.find(".text:last")).fadeIn().addClass("active");
			}
			event.preventDefault();
		})
	});
	
	$(".works").each(function(){
		/*$(this).find(".image img").each(function(){
			marg_top = Math.floor(($(this).parent().parent().height()-$(this).height()) / 2);
			$(this).css("margin-top",marg_top);
		})*/
		var item_size = $(this).find("li:eq(0)").outerWidth(1);
		var newPos = 0;
		ul = $(this).find("ul");
		
		ul.css("width",(ul.find("li").length+1) * item_size);
		$(this).find(".next").click(function(event){
			newPos -= item_size;
			if(Math.abs(newPos) < ul.parent().width()){
				ul.animate({left:newPos}, 700);
			}
			else newPos += item_size;
			event.preventDefault();
		});
		$(this).find(".prev").click(function(event) {
			newPos += item_size;
			if(!(newPos > 0)){
				ul.animate({left:newPos}, 700);
			}
			else newPos -= item_size;
			event.preventDefault();
		});
	});
	
	maximg=0;
	
	$("#clients_logos .item img").each(function(){
		if ($(this).height() > maximg) maximg = $(this).height();
	}).each(function(){
		$(this).css("margin",Math.floor((maximg-$(this).height())/2))+"px 0";
	})
	
	$(document).ready(function(){
		$(".banners .item .image img").each(function(){
   $(this).clone().attr("src",$(this).attr("src").substr(0,$(this).attr("src").length-4)+"_hover.jpg").hide().insertAfter($(this));
  })

		$(".banners .item .image").hover(function(){
			$(this).find("img:eq(0)").fadeOut().next().fadeIn();
		},function(){
			$(this).find("img:eq(1)").fadeOut().prev().fadeIn();
		})
	})
})

