/**
 * Carousel
 */

function Carousel ( pimages, element_id, count, onpage ) {
    
	this.Index		= null;
    this.Index_next	= null;
    this.id			= null;
    this.onpage		= (onpage ? onpage : 4)
    this.count		= count;
    this.sb			= [];
    
    // Start of private
    // Private vars
    var element		= element_id;
	var to_onpage	= this.onpage - 1;
    // Private methods
    /**
     * Normalize
     *
     */	
	var normalize = function () {
		var imageContainer = _( element ).getElementsByTagName("li");
		var className = '';
		for (var i = 0; i < imageContainer.length; i++) {
			className = '';
			if( i == (imageContainer.length - 1)) {
				className = this.carousel_css_normal;
			}
			_( imageContainer[i].id ).className = className;
		}
	}
	// End of private
	
	if( count > 0 ) {
		for (var i = 0; i < this.count; i++) {
			if( pimages[ i ] ) {
		    	this.sb[ i ] = pimages[ i ];
			}
		}
	}
	
	_( element ).innerHTML = '';
	
	el = new Array();
	
	this.main_index = ((this.sb.length >= this.onpage) ? this.onpage : this.sb.length);
	
	for (var i=0; i < this.main_index; i++) {
	    el = document.createElement('li');
	    el.innerHTML = '<a onclick="return showthisimage(' + i + '); return false;" href="' + this.sb[i].href + '" title=""><img src="' + this.sb[i].sm + '" class="ajaks_img" height="90" width="120" alt="" title="" /></a>';
	    el.id = this.carousel_block_name + '[' + i + ']';
	    _( element ).appendChild(el);
	}
	
	normalize();
	
	this.clLeft = function() {
		this.Index = this.sb.shift();
		_(this.carousel_block_name + '[' + this.Index.index + ']').parentNode.removeChild(_(this.carousel_block_name + '[' + this.Index.index + ']'));
		this.sb.push(this.Index);
		this.Index_next = document.createElement('li');
		this.id = this.sb[ to_onpage ].index;
		this.Index_next.innerHTML = '<a onclick="return showthisimage(' + this.sb[3].index + '); return false;" href="' + this.sb[3].href + '" title=""><img src="' + this.sb[ to_onpage ].sm + '" class="ajaks_img" height="90" width="120" alt="" title="" /></a>';
		this.Index_next.id = this.carousel_block_name + '[' + this.id + ']';
		_( element ).appendChild(this.Index_next);
		normalize();
		return false;
	}
	
	this.clRight = function (){
		this.Index = this.sb.pop();
		_(this.carousel_block_name + '[' + this.sb[ to_onpage ].index + ']').parentNode.removeChild(_(this.carousel_block_name + '[' + this.sb[ to_onpage ].index + ']'));
		this.Index_next = document.createElement('li');
		this.id = this.sb[0].index;
		this.Index_next.innerHTML = '<a onclick="return showthisimage(' + this.Index.index + '); return false;" href="' + this.Index.href + '" title=""><img src="' + this.Index.sm + '" class="ajaks_img" height="90" width="120" alt="" title="" /></a>';
		this.Index_next.id = this.carousel_block_name + '[' + this.Index.index + ']';
		_(this.carousel_block_name + '[' + this.id + ']').parentNode.insertBefore(this.Index_next,_(this.carousel_block_name + '[' + this.sb[0].index + ']'));
		this.sb.unshift(this.Index);
		normalize();
		return false;
	}
	
}

Carousel.prototype = new Base();