var QsCarouselTimer = null;

QsCarousel = function(config) {
	
	var MINIMUM_HEIGHT = 500;
	
	this.actualElement = 1;
	this.elemsLength = 1;
	this.timer = null;
	
	this.id = config.id;
	
	this.scroll = function(what) {
	
		var current = this;
		this.recountHeight();
	
		current.actualElement = parseInt(current.actualElement);
		current.elemsLength = parseInt(current.elemsLength);
	
		if (typeof(what) == 'undefined' || what < 0) {
			what = ((current.actualElement + 1) % (current.elemsLength + 1));
			if (what == 0) {
				what = 1;
			}
		}
		
		$j(current.id).children('li[rel="' + current.actualElement + '"]').fadeOut('fast', function() {
			$j(current.id).children('li[rel="' + what + '"]').fadeIn();
		});
		
		$j('#carousel_navigation' + current.actualElement).removeClass('nav_active');	
		$j('#carousel_navigation' + what).addClass('nav_active');
		
		current.actualElement = what;	
		
		QsCarouselTimer = setTimeout(function() {current.scroll.call(current, -1);}, 5000);	
	};
	
	this.recountHeight = function() {
	
		var current = this;
		var _height = Math.max($j(current.id).children('li:first').height(), MINIMUM_HEIGHT);
		
		var _actHeight = 0;
		
		$j(current.id).children('li').each(function() {
			_actHeight = Math.max($j(this).height(), _actHeight);
		});
		
		if (_actHeight > MINIMUM_HEIGHT) _height = _actHeight;	
		
		$j(current.id).css('height', _height);
	};
	
	this.init = function() {
	
	    clearTimeout(QsCarouselTimer);
		QsCarouselTimer = null;	
	
		var current = this;
		var _height = Math.max($j(current.id).children('li:first').height(), MINIMUM_HEIGHT);
		
		var _actHeight = 0;
		
		$j(current.id).children('li').each(function() {
			_actHeight = Math.max($j(this).height(), _actHeight);
		});
		
		if (_actHeight > MINIMUM_HEIGHT) _height = _actHeight;
	
		this.elemsLength = $j(current.id).children('li').length; 
	
		$j(current.id).css('list-style-type', 'none');
		$j(current.id).children('li').hide();
		$j(current.id).children('li:first').fadeIn();
		
		$j(current.id).css('height', _height);
		
		
		$j('.carousel_navigation a').unbind('click');
		$j('.carousel_navigation a').bind('click', function() {
			clearTimeout(QsCarouselTimer);
			QsCarouselTimer = null;
			var relValue = $j(this).attr('rel');
			current.scroll.call(current, relValue);
			return false;
		});		
		
	    $j('.nav_active').each(function() {
	    	$j(this).removeClass('nav_active');
	    	$j(this).addClass('nav_inactive');
	    });
	    
	    $j('#carousel_navigation' + current.actualElement).addClass('nav_active');
	
		QsCarouselTimer = setTimeout(function() {current.scroll.call(current, -1);}, 5000);
	}

};

function reInitCarousel() {		
	
	var qsCarousel = new QsCarousel({id : "#mycarousel"});
	qsCarousel.init();
	
	/*
	$j('#mycarousel').jcarousel({
			wrap: 'circular',
			scroll: 1,
			auto: 5,
			initCallback: function (carousel) {
				$j('.carousel_navigation a').unbind('click');
				$j('.carousel_navigation a').bind('click', function() {

					var relValue = $j(this).attr('rel');
				
			        carousel.scroll(jQuery.jcarousel.intval(relValue));
			        return false;
			    });
			},
			itemFirstInCallback: function (carousel, item, idx, state) {

				if (!carousel.navigBar) {
					carousel.navigBar = $j('#carousel_navigation');
					carousel.navigSize = $j('#carousel_navigation').children().length;
				}

				$j('.nav_active').each(function() {$j(this).removeClass('nav_active');});
				
				$j('#carousel_navigation' + $j(item).attr('rel')).addClass('nav_active');
			}
	});
	*/
}
