var tpCarousel = {
	imageUrl : SITE_ROOT + '/get_carousel_images.php?id=1',
	targetDiv : 'carousel',
	imageUrls : [], images : [], preloaders : [],
	preload : true,
	pointer : 0,		
	totalNum : 0,
	delay : 6000,
	fadeSpeed : 1500,
	
	imgCss : {
		'top' : '0px',
		'left' : '0px',
		'position' : 'absolute',
		'display' : 'none',
		'width' : 600,
		'height' : 400
	},
	
	divCss : { 'position' : 'relative' },
	
	init : function() {
		var that = this;
		this.$targetDiv = $('#'+this.targetDiv);
		this.$targetDiv.css(this.divCss);
		$.getJSON(this.imageUrl, function(data) { 
			that.imageUrls = data; 
			that.loop();
		});
	},
	
	loop : function() {
		var that = this;
		if(this.preload) {
			this.preloaders[this.pointer] = new Preloader(this.imageUrls[this.pointer], function() { that.show() });
			this.preloaders[this.pointer].load();
		}	
		else {
			this.show();	
		}
	},
	
	show : function() {
		var that = this;
		$img = this.images[this.pointer] = $('<img />').css(this.imgCss);
		$img.attr('src', this.imageUrls[this.pointer]).attr('id', 'img_'+this.totalNum);
		this.$targetDiv.append($img);
		$img.fadeIn(this.fadeSpeed, function() {
			if(that.totalNum > 0) $('#img_'+(that.totalNum-1)).remove();
			that.totalNum++; 
		});
		if(this.pointer + 1 == this.imageUrls.length) {
			this.preload = false;
			this.pointer = 0;
		} 
		else {
			this.pointer++;
		}
		setTimeout(function() { that.loop() }, this.delay);
	}	
}
