var imagecarousel = {
	itemVisibleInCallback: function(carousel, item, i, state, evt) {
		if (state == 'prev') {
			var idx = carousel.index(i);
			carousel.add(i, carousel.get(idx).html());
		}
	},
	itemVisibleOutCallback: function(carousel, item, i, state, evt) {
		if (state == 'next') {
			var indexes = imagecarousel.getCarouselIndexes(carousel);
			var idx = carousel.index(i);
			carousel.add((indexes[1] + 1), carousel.get(idx).html());
		}
	},
	getCarouselIndexes: function(carousel) {
		var first_index = carousel.prevFirst;
		var last_index = first_index;
		var ok = false;
		while (ok == false) {
			if (carousel.get(last_index).html() == null) {
				return [first_index, last_index-1];
			}
			last_index = last_index + 1;
		}
	},
	randomize: function(carousel, childElem) {
		var $this = jQuery(carousel);
		var elems = $this.children(childElem);
		elems.sort(function() { return (Math.round(Math.random())-0.5); });
		$this.remove(childElem);
		for(var i=0; i < elems.length; i++) {
			$this.append(elems[i]);
		}
	},
	
	
	/**
	 * Normal initCallback
	 */
	initCallback: function(carousel, state) {
		var childs = carousel.clip.context.children;
		var parent = childs[ 0 ].parentNode;
		var carousel = document.createElement("div");
		carousel.id = "carousel";
		carousel.style.width = parent.style.width;
		parent.appendChild( carousel );
		parent.style.width = "252px";
		parent.style.height = "77px";
		parent.style.overflow = "hidden";
		var length = childs.length;
		var i = 0;
		while( i < length){
			ele = parent.childNodes[ i ];
			i++;			
			if(ele){
				if(ele.nodeName){
					if(ele != carousel){
						carousel.appendChild( ele );
						i--;
					}
				}
			}
		}
		window.setTimeout("moving( 'carousel', 0)", 2000);
		
		/*carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});*/
	},
	/**
	 * Mouseover initCallback
	 */
	initCallbackMouseover: function (carousel, state) {
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	}
};
function moving( parentid, left ){
	var parent = document.getElementById(parentid);
	
	var child = parent.children[0]; 
	width = child.scrollWidth + 36;
	left += 5;
	if(left >= width ){
		left = left - width;
		parent.removeChild( child );
		child.style.marginLeft = "0px";
		parent.appendChild( child );	
		window.setTimeout("moving( '" +parentid+ "', "+0+")", 2000);
	}else{   
		parent.children[0].style.marginLeft = -left+ "px"; 
		window.setTimeout("moving( '" +parentid+ "', "+left+")", 20);
	}
}