function BCarouselSelection(width,heigth) {
    this.classname="BCarouselSelection";
    this.createView();
    this.idcounter = 0;
    //this.items = new Object();
    this.itemsMap = new Object();
    this.items = new Array();
    this.listener = new Array();
    this.selected = null;
    this.math = new BCarouselSelectionMath(this.items);
    this.angle=90;
}
BCarouselSelection.prototype.createView = function() {
    var div = document.createElement("div");
    div.style.position="relative";
    div.style.left="0px";
    div.style.top="0px";
    div.style.width="740px";
    div.style.height="150px";
    //div.style.height="145px";
    //div.style.overflow="auto";
    //div.style.whiteSpace="nowrap";
    //div.style.border="1px solid blue";
    this.view = div;
}
BCarouselSelection.prototype.getView = function() {
    return this.view;
}
BCarouselSelection.prototype.getSelected = function() {
    return this.selected;
}
BCarouselSelection.prototype.clear = function() {
    for (var i=0;i<this.items.length;i++) {
	this.view.removeChild(this.items.view);
    }
    this.itemsMap = new Object();
    this.items = new Array();
    this.listener = new Array();
}
BCarouselSelection.prototype.add = function(item) {
    item.bcsid = this.idcounter++;
    var bssitem = new BCarouselSelectionItem(this,item)
    if (this.selected==null)
	this.selected=bssitem;
    this.itemsMap[""+item.bcsid]=bssitem;
    this.items.push(bssitem);
    this.view.appendChild(bssitem.view);
    this.math.add();
    this.rot(0);
}
BCarouselSelection.prototype.remove = function(item) {
    var bssitem = this.itemsMap[""+item.bcsid];
    this.view.removeChild(bssitem.view);
    this.itemsMap[""+item.bcsid]=null;
    var found=false;
    for (var i=0;i<this.items.length;i++) {
	if (found) {
	    this.items[i]=this.items[i-1];
	} else if (this.items[i]==bssitem) {
	    found = true;
	}
    }
    this.items.pop();
    this.math.remove();
    this.rot(0);
}
BCarouselSelection.prototype.addItems = function(items) {
    for (var i=0;i<items.length;i++){
	this.add(items[i]);
    }
}
BCarouselSelection.prototype.addSelectionListener = function(listener,update) {
    this.listener.push(listener);
    if (update)
	listener.selectionChanged(this.selected);
}
BCarouselSelection.prototype.clicked = function(item) {
    if (item!=this.selected) {
	if (this.selected!=null)
	    this.selected.deselect();
	this.selected = item;
	this.selected.select();
	for (var i=0;i<this.listener.length;i++)
	    this.listener[i].selectionChanged(this.selected);
	this.doRot(item.angle);
    }
}
BCarouselSelection.prototype.rot = function(angle) {
    this.angle=(this.angle+angle)%360;
    this.math.doSet();
    this.math.rotateY(this.angle);
    this.math.rotateX(355);
    this.math.doProj(600, 1.5, 370, 40);
    for (var i=0;i<this.items.length;i++) {
	this.items[i].redraw(this.math.proj[i]);
    }
}
BCarouselSelection.prototype.doRot = function(angle) {
    var gap = (630+this.angle-this.selected.angle)%360;
    var s = 1;//Math.round(10/this.items.length);
    if (s<1) s=1;
    if (gap!=0) {
	if (gap<s) s=gap;
	this.rot(gap>180?s:360-s);
	//this.doRot(angle);
	window.setTimeout("selection.doRot("+angle+")",1);
    }
}

