jQuery.carousel = function() {
	
	this.contents = jQuery('.carouselContent');
	this.wrapper = jQuery('.carouselWrapper');
	this.caroNext = jQuery('.carouselNext');
	this.caroPrev = jQuery('.carouselPrev');
	this.index = 0;
	this.hover = false;
	this.cut = false;
	
	this.initialize = function() {
		
		this.caroNext.bind('click',[this],this.dnext);
		this.caroPrev.bind('click',[this],this.dprev);
		
		this.wrapper.hover(function(){
			homeCarousel.cut = true;
			homeCarousel.hover = true;
		},function() {
			homeCarousel.hover = false;
		});
		this.rotate(0);
	}
	
	this.remoteRotate= function() {
		if (this.hover) {return;}
		if (this.cut) {this.cut = false; return;}
		this.rotate(this.index);
	}
	
	this.dnext = function(data) {
		data.data[0].next();
	}
	
	this.dprev = function(data) {
		data.data[0].previous();
	}
	
	
	this.previous = function() {
		var content = jQuery(this.contents[this.index]);
		var item = jQuery('.newsItem:last',content);
		var newsWrap = jQuery('.newsWrap', content);
		newsWrap.prepend(item);
		var item = jQuery('.newsItem:last',content);
		
		var limit = 3; //Where to cut the unneeded
		this.transferToTheBig(item,content);
		
		/*var count = jQuery('.newsItem',content).length;
		if (count <= 3) {
			console.log(count);
			limit = count-1;
		}*/
		
		if (limit != 0) {
			jQuery('.newsItem:lt('+(limit+1)+')',newsWrap).show();
			jQuery('.newsItem:gt('+(limit-1)+')',newsWrap).hide();
		} else {
			jQuery('.newsItem',newsWrap).hide();
		}		
		this.cut = true;
	}
	
	this.next = function() {
		this.rotate(this.index);
		this.cut = true;
	}
	
	
	this.rotate = function(index) {
		var content = jQuery(this.contents[index]);
		var item = jQuery('.newsItem:first',content);
		var newsWrap = jQuery('.newsWrap', content);
		newsWrap.append(item);
		
		var limit = 3; //Where to cut the unneeded
		this.transferToTheBig(item,content);
		
		/*var count = jQuery('.newsItem',content).length;
		if (count <= 3) {
			console.log(count);
			limit = count-1;
		}*/
		
		if (limit != 0) {
			jQuery('.newsItem:lt('+(limit+1)+')',newsWrap).show();
			jQuery('.newsItem:gt('+(limit-1)+')',newsWrap).hide();
		} else {
			jQuery('.newsItem',newsWrap).hide();
		}
		
		
	}
	
	this.transferToTheBig = function(item,content) {
		jQuery('.bigNews .bigNewsTitle',content).html(jQuery('.newsTitle',item).html());
		jQuery('.bigNews .bigNewsText',content).html(jQuery('.newsText',item).html());
		jQuery('.bigNews .imageHolder',content).replaceWith(jQuery('.imageHolder',item).clone());
		jQuery('.bigNews .imageHolder',content).show();
		jQuery('.bigNews .imageHolder',content).parent().attr('href', jQuery('a:first',item).attr('href'));
		var br = jQuery("<br />");
		jQuery('.bigNews .newsCommentsNumber',content).before(br);
	}
	
	this.initialize();
	
}

var homeCarousel = new jQuery.carousel();
window.setInterval('homeCarousel.remoteRotate()',6000);