var QP = {};

QP.Styles = function(){
	jQuery(".client:nth-child(3n),.caseStudy:nth-child(3n)").css("margin-right","0");
	jQuery(".client:nth-child(3n+1),.caseStudy:nth-child(3n+1)").css("clear","left");
}

QP.Carousel = function(){
	this.targets='.carouselItem';
	this.items = [];
	this.currentItem = 0;
};

QP.Carousel.prototype={
	init : function(){
		if(jQuery(".carousel").length>0){
			this.items=jQuery(this.targets);
			if(this.items.length>0){
				this.build();
			}
		}
		
	},
	build : function(){
		var scope = this;
		for(i=0; i< this.items.length; i++){
			var divider = (i <=this.items.length-2) ? " |" : "";
			i>0? jQuery(this.items[i]).hide() : "";
			jQuery("#carouselControls .last").before('<li><a href="#" class="clink">'+(i+1)+divider+'</a></li>');
		}
		
		jQuery("#carouselControls .clink").each(function(){
			jQuery(this).click(function(){
				return scope.transition(this.innerHTML.replace("|","")-1);
			});
		});
		
		jQuery("#carouselControls .first,#carouselControls .last").each(function(){
			jQuery(this).click(function(){
				var itemNumber;
				if(this.className=="first"){
					itemNumber = scope.currentItem-1;
					(itemNumber<0)?itemNumber=scope.items.length-1:itemNumber=itemNumber;
				}
				if(this.className=="last"){
					itemNumber = scope.currentItem+1;
					(itemNumber>(scope.items.length-1))?itemNumber=0:itemNumber=itemNumber;
				}
				return scope.transition(itemNumber);
			});
		});
	},
	transition : function(itemNumber){
		jQuery(this.items).hide();
		jQuery(this.items[itemNumber]).fadeIn(1200);
		this.currentItem=itemNumber;
		return false;
	}
}

QP.round = function(){
	$("img").each(function(){
		var newEl = $("<span class='roundMe'><span class='tl'></span><span class='tr'></span><span class='bl'></span><span class='br'></span></span>");
		newEl.append($(this).clone());
		$(this).replaceWith(newEl);
	});
}

jQuery(document).ready(function(){
	new QP.Carousel().init();
	new QP.Styles();
	QP.round();
})