/*
* global variables
*/
var naworkCarrouselCount = 0;
var NaworkCarrousel = new Class({
	init:function(carrousel){
		this.carrousel = new Element(carrousel);
		/*
		* setting general information and constants
		*/
		this.carrousel.imgRotationInterval = null;
		this.carrousel.stepsToDo = 0;
		this.carrousel.intervalActive = false;
		this.carrousel.intervalGlobalActive = false;
		this.carrousel.animationDuration = 1000;
		this.carrousel.animationStepDuration = 50;
		this.carrousel.animationSteps = 1000;
		this.carrousel.steps = this.carrousel.animationDuration / this.carrousel.animationStepDuration;
		this.carrousel.RAD = Math.PI/180;
		this.carrousel.currentPos = 0;
		this.carrousel.viewAngle = ($('naworkCarViewAngle').innerHTML) * 1;
		/*
		* setting button's and other events
		*/
		car = this.carrousel;
		this.carrousel.addEvent('mousewheel', this.wheel.bindWithEvent(car) );
		this.carrousel.leftButton = this.carrousel.getElement("[id=buttonLeft]");
		this.carrousel.leftButton.addEvent('mousedown',this.startTurnLeft.bindWithEvent(this));
		this.carrousel.leftButton.addEvent('mouseup',this.stopTurnLeft.bindWithEvent(this));
		this.carrousel.leftButton.addEvent('mouseout',this.stopTurnLeft.bindWithEvent(this));
		this.carrousel.leftButton.setStyle('z-index',5000);
		this.carrousel.rightButton = this.carrousel.getElement("[id=buttonRight]");
		this.carrousel.rightButton.addEvent('mousedown',this.startTurnRight.bindWithEvent(this));
		this.carrousel.rightButton.addEvent('mouseup',this.stopTurnRight.bindWithEvent(this));
		this.carrousel.rightButton.addEvent('mouseout',this.stopTurnRight.bindWithEvent(this));
		this.carrousel.rightButton.setStyle('z-index',5000);
		/*
		* setting properties and styles
		*/
		this.carrousel.setProperty('id','carrousel'+naworkCarrouselCount);
		//this.carrousel.setStyle('zindex',-10000);
		this.carrousel.setStyles({
			zindex: -10000,
			left: 0,
			top: 0
		});
		this.carrousel.width = this.carrousel.getStyle('width').toInt();
		this.carrousel.height = this.carrousel.getStyle('height').toInt();
		this.carrousel.middleX = this.carrousel.getStyle('left').toInt()+this.carrousel.getStyle('width').toInt()/2;
		this.carrousel.middleY = this.carrousel.getStyle('top').toInt()+this.carrousel.getStyle('height').toInt()/2 + 25;
		this.carrousel.images = new Array();
		images=this.carrousel.getElements("div[class=image]");
		for(j=0;j<images.length;j++) {
			this.carrousel.images[j] = new Array();
			this.carrousel.images[j].img = images[j];
			this.carrousel.images[j].pos = this.carrousel.animationSteps/(images.length)*j;
		}
		this.carrousel.constants = new Object();
		this.carrousel.constants.imgWidth = images[0].getElement("img").getProperty('width').toInt();
		this.carrousel.constants.imgHeight = images[0].getElement("img").getProperty('height').toInt();
		this.carrousel.constants.imgMorphFac = 0.3;
		this.carrousel.constants.degreesPerImg = this.carrousel.animationSteps/this.carrousel.images.length;
		this.carrousel.radius = this.carrousel.width/3;
		this.carrousel.width -= 2*(this.carrousel.constants.imgWidth-this.carrousel.constants.imgWidth*(((1-this.carrousel.constants.imgMorphFac))/2));
		this.carrousel.height -= 2*(this.carrousel.constants.imgHeight-this.carrousel.constants.imgHeight*(((1-this.carrousel.constants.imgMorphFac))/2));
//		this.carrousel.middleX -= this.carrousel.constants.imgWidth/2 - this.carrousel.leftButton.getStyle('width').toInt()/2;
//		this.carrousel.middleY -= this.carrousel.constants.imgHeight/2;
//		//this.carrousel.middleY *= (this.carrousel.height / this.carrousel.width);
		this.carrousel.positions = this.initAttributes(this.carrousel);
		this.positionImages(this.carrousel);
		var height = Math.abs((this.carrousel.positions[0] + this.carrousel.positions[0].height) - this.carrousel.positions[this.carrousel.animationSteps/2]);
//		var width = Math.abs(this.carrousel.positions[steps/4])
		this.carrousel.setStyles({
//			width: Math.abs(this.carrousel.positions[this.carrousel.images[(this.carrousel.images.length - 1)/4*3].pos].x - this.carrousel.positions[this.carrousel.images[(this.carrousel.images.length - 1)/4].pos].x),
			height: Math.abs((this.carrousel.positions[0].y + this.carrousel.positions[0].height) - this.carrousel.positions[(this.carrousel.animationSteps/2) - 1].y)
		});
		var heightDiff = Math.abs((this.carrousel.positions[this.carrousel.animationSteps/2].y + this.carrousel.positions[this.carrousel.animationSteps/2].height) - this.carrousel.getPosition().y)*13/10;
		//for(i = 0;i<this.carrousel.animationSteps;i++) {
		//	this.carrousel.positions[i].y -= heightDiff;
		//}
		this.carrousel.middleY -= heightDiff;
		this.carrousel.positions = this.initAttributes(this.carrousel);
		this.positionImages(this.carrousel);
		this.carrousel.leftButton.setStyles({
			position: 'absolute',
			left: 0, //this.carrousel.leftButton.getStyle('width').toInt()/2,
			top: (this.carrousel.middleY - this.carrousel.leftButton.getStyle('height').toInt()/2)// * (this.carrousel.height / this.carrousel.width * 2)
		});
		this.carrousel.rightButton.setStyles({
			position: 'absolute',
			left: this.carrousel.getStyle('width').toInt() - this.carrousel.rightButton.getStyle('width').toInt(), //this.carrousel.middleX + this.carrousel.radius + this.carrousel.constants.imgWidth/2 - this.carrousel.rightButton.getStyle('width').toInt(),
			top: (this.carrousel.middleY - this.carrousel.rightButton.getStyle('height').toInt()/2)// * (this.carrousel.height / this.carrousel.width * 2)
		});
		naworkCarrouselCount++;
		
	},
	wheel:function(event){
	
	},
	round:function(number, length) {
		multi = "1";
		for(i=0;i<length;i++) {
			multi += "0";
		}
		newNumber = Math.round(number * multi.toInt()) / multi.toInt();
		return newNumber;
	},
	initAttributes:function(carrousel) {
		viewDistance = 500; // the distance between the "eye" and the origin
		distance = Math.cos(carrousel.viewAngle * carrousel.RAD) * viewDistance; // the horizontial distance between the "eye" and the origin
		height = Math.sin(carrousel.viewAngle * carrousel.RAD) * viewDistance; // the height above the origin
//		console.debug(distance);
//		console.debug(height);
		imageCount = carrousel.images.length;
		carWidth = carrousel.width;
		carHeight = carrousel.height;
		heightMod = carHeight / carWidth;
		constants = carrousel.constants;
		animationSteps = carrousel.animationSteps;
		var attributes = new Array();
		for(j=0;j<animationSteps;j++) {
			attributes[j] = new Object();
			pos = 360 / animationSteps * (j + animationSteps * 3 / 4) * carrousel.RAD;
			// viewer's position
			v = new Object();
			v.x = 0;
			v.y = -distance;
			v.z = height;
			// target point
				// reference point (middle)
				a = new Object();
				a.x = 0;
				a.y = this.round(carrousel.radius,3);
				a.z = 0;
				// point top-left from p
				b = new Object();
				b.x = this.round(Math.cos(pos) * carrousel.radius,3);
				b.y = this.round(Math.sin(pos) * carrousel.radius,3);
				b.z = 0;
				t = -v.y / (b.y - v.y);
			// projection point
			w = new Object();
			w.x = this.round(v.x + t * (b.x - v.x),3);
			w.y = -this.round(v.z + t * (b.z - v.z),3);
			// difference between projected point and original point and scaling factor
			if(j == animationSteps / 2) {
				scalingFactor = attributes[j-1].scale;
			} else  {
				scalingFactor = this.round(((w.x - a.x)/2) / (b.x - a.x),3);
			}
			attributes[j].scale = scalingFactor
			attributes[j].width = constants.imgWidth * scalingFactor;
			attributes[j].height = constants.imgHeight * scalingFactor;
			attributes[j].x = w.x + carrousel.middleX - attributes[j].width/2; // place the images centered
			attributes[j].y = w.y + carrousel.middleY - attributes[j].height/2; // above the point on the circle
			attributes[j].zIndex = -b.y + carrousel.radius;
		}
		attributes[0] = attributes[1];
		return attributes;
	},
	positionImages:function(carrousel) {
		/*
		* starting with the image positioning
		*/
		images=carrousel.images;
		imgPosition = images[0].pos;
		images[0].img.setStyle('position','absolute');
		images[0].img.setStyle('z-Index',carrousel.positions[Math.round(imgPosition)].zIndex);
		img=images[0].img.getElement("img"); // get the img tag in the div
		img.setProperty('width',carrousel.positions[Math.round(imgPosition)].width);
		img.setProperty('height',carrousel.positions[Math.round(imgPosition)].height);
		img.setProperty('position',imgPosition);
		images[0].img.setStyle('left',carrousel.positions[Math.round(imgPosition)].x);
		images[0].img.setStyle('top',carrousel.positions[Math.round(imgPosition)].y);
		images[0].img.setStyle('width',carrousel.positions[Math.round(imgPosition)].width);
		images[0].img.setStyle('height',carrousel.positions[Math.round(imgPosition)].height);
		for(j=1;j<images.length;j++) {
			/*
			* first set the right image
			*/
			imgPosition = images[j].pos;
			images[j].img.setStyle('position','absolute');
			images[j].img.setStyle('z-Index',carrousel.positions[Math.round(imgPosition)].zIndex);
			img=images[j].img.getElement("img"); // get the img tag in the div
			img.setProperty('width',carrousel.positions[Math.round(imgPosition)].width);
			img.setProperty('height',carrousel.positions[Math.round(imgPosition)].height);
			images[j].img.setStyle('left',carrousel.positions[Math.round(imgPosition)].x);
			images[j].img.setStyle('top',carrousel.positions[Math.round(imgPosition)].y);
			images[j].img.setStyle('width',carrousel.positions[Math.round(imgPosition)].width);
			images[j].img.setStyle('height',carrousel.positions[Math.round(imgPosition)].height);
			imgPosition+=carrousel.constants.degreesPerImg;
		}
	},
	startTurnLeft:function(event) {
		this.carrousel.intervalGlobalActive = true;
		this.turnLeft(this);
	},
	stopTurnLeft:function(event) {
		this.carrousel.intervalGlobalActive = false;
	},
	startTurnRight:function(event) {
		//console.debug(this);
		this.carrousel.intervalGlobalActive = true
		this.turnRight(this);
	},
	stopTurnRight:function(event) {
		this.carrousel.intervalGlobalActive = false;
	},
	turnLeft:function(car) {
		carrousel = car.carrousel;
		//console.debug(car);
		duration=carrousel.animationDuration;
		stepDuration=carrousel.animationStepDuration;
		steps=duration/stepDuration;
		if(!carrousel.intervalActive) {
			carrousel.intervalActive = true;
			carrousel.imgRotationInterval=car.rotateLeft.periodical(stepDuration,null,car);		
		}
	},
	rotateLeft:function(car) {
		carrousel = car.carrousel;
		images=carrousel.images;
		degreesPerImg=carrousel.constants.degreesPerImg;
		degreesPerStep=degreesPerImg/carrousel.steps;
		animationSteps = carrousel.animationSteps;
		attributes = carrousel.positions;
		stepsToDo = carrousel.stepsToDo;
		steps = carrousel.steps;
		addPos = (carrousel.stepsToDo+1)*degreesPerStep+(carrousel.currentPos)*degreesPerImg;
		//console.debug(addPos);
		for(imgCnt=0;imgCnt<images.length;imgCnt++) {
			position = Math.round(images[imgCnt].pos-addPos);
			//console.debug(images[imgCnt].pos,position, animationSteps);
			if(position < 0) { position+=animationSteps; }
			//console.debug(position);
			if(position>=animationSteps) { position-=animationSteps; }
			//console.debug(position);
			img = images[imgCnt].img.getElement("img");
			img.setProperty('width',attributes[position].width);
			img.setProperty('height',attributes[position].height);
			images[imgCnt].img.setStyles({
				left: attributes[position].x,
				top: attributes[position].y,
				width: attributes[position].width,
				height: attributes[position].height,
				zIndex: attributes[position].zIndex
			});
		}
		car.carrousel.stepsToDo++;
		if(stepsToDo>=steps-1) {
			//console.debug(carrousel.intervalActive);
			if(!carrousel.intervalGlobalActive) {
				$clear(car.carrousel.imgRotationInterval);
				car.carrousel.intervalActive = false;
			}		
			car.carrousel.currentPos++;
			if(carrousel.currentPos>=images.length) { car.carrousel.currentPos-=images.length; }
			car.carrousel.stepsToDo = 0;
		}
	},
	turnRight:function(car) {
		//console.debug(car);
		carrousel = car.carrousel;
		duration=car.carrousel.animationDuration;
		stepDuration=car.carrousel.animationStepDuration;
		steps=duration/stepDuration;
		if(!carrousel.intervalActive) {
			carrousel.intervalActive = true;
			carrousel.imgRotationInterval=car.rotateRight.periodical(stepDuration,null,car);		
		}
	},
	rotateRight:function(car) {
		carrousel = car.carrousel;
		images=carrousel.images;
		degreesPerImg=carrousel.constants.degreesPerImg;
		degreesPerStep=degreesPerImg/carrousel.steps;
		attributes = carrousel.positions;
		stepsToDo = carrousel.stepsToDo;
		steps = carrousel.steps;
		addPos = (stepsToDo+1)*degreesPerStep-(carrousel.currentPos)*degreesPerImg;
		for(imgCnt=0;imgCnt<images.length;imgCnt++) {
			position = Math.round(images[imgCnt].pos+addPos);
			if(position<0) { position+=animationSteps; }
			if(position>=carrousel.animationSteps) { position-=carrousel.animationSteps; }
			img = images[imgCnt].img.getElement("img");
			img.setProperty('width',attributes[position].width);
			img.setProperty('height',attributes[position].height);
			images[imgCnt].img.setStyles({
				left: attributes[position].x,
				top: attributes[position].y,
				width: attributes[position].width,
				height: attributes[position].height,
				zIndex: attributes[position].zIndex
			});
		}
		carrousel.stepsToDo++;
		if(stepsToDo>=steps-1) {
			if(!carrousel.intervalGlobalActive) {
				$clear(carrousel.imgRotationInterval);
				carrousel.intervalActive = false;
			}
			carrousel.currentPos--;
			if(carrousel.currentPos<0) { carrousel.currentPos+=images.length; }
			carrousel.stepsToDo = 0;
		}
	}
});
/*
* initializing all carrousels on page and start arrangement
*/
window.addEvent('domready', function(){
	var carrousels = document.getElements('.tx-naworkimagecarrousel-pi1_car');
	cars = new Object();
	for(var i=0; i<carrousels.length;i++){
		cars.naworkCarrousel = new NaworkCarrousel();
		cars.naworkCarrousel.init(carrousels[i]);
	} 
});