/* Carousel */
function carousel(carousel) {
	var divs = carousel.children[0].children;
	
	var count = 0;
	var speed = 0.01;
	var radiusx = 17.5;
	var radiusy = 6.25;
	
	divs[0].style.position = 'absolute';
	divs[0].style.left = (carousel.offsetWidth / 2 - divs[0].offsetWidth / 2) / getFontSize() + "em";
	divs[0].style.top =  (carousel.offsetHeight / 2 - divs[0].offsetHeight / 2) / getFontSize() + "em";
	divs[0].onmousedown = function() {return false;}
	divs[0].onselectstart = function() {return false;}
	
	/* Initialize Carousel */
	carousel.onmousemove = carmousemove;
	carousel.onmousedown = function() {
		this.mousedown = true;		
		return false;	
	};
	carousel.onmouseup = function() {
		this.mousedown = false;		
	};
	carousel.mousedown = false;
	carousel.onselectstart = function() {return false;}	
	carousel.parentNode.onmouseover = carmouseover;
	
	/* Initialize and Rounded Corders */
	var corners = [1,1,2,3,4,6];	
	for (var i=1; i< divs.length; i++) {
		divs[i].style.position = 'absolute';
		divs[i].onmouseover = function() {
			this.active = true;	
		};
		divs[i].onmouseout = function() {
			this.active = false;	
		};	
		divs[i].children[1].style.display = 'none';
		//divs[i].children[2].style.display = 'none';
		
		divs[i].clicked = false;
		divs[i].active = false;
		divs[i].full = false;
		
		inner = divs[i].innerHTML;
		bgcolor = divs[i].style.backgroundColor;
				
		divs[i].innerHTML = "";
		divs[i].style.backgroundColor = "";
		
		cdiv = document.createElement('div');
		for (var j=5; j >= 0; j--) {
			div = document.createElement('div');
			div.style.marginLeft = corners[j] + "px";
			div.style.marginRight = corners[j] + "px";		
			div.style.backgroundColor = bgcolor;
			div.style.height = "1px";
			div.style.clear = "both";
			div.style.overflow = "hidden";
			
			cdiv.appendChild(div);
		}
		divs[i].appendChild(cdiv);
		
		div = document.createElement('div');
		divs[i].innercontent = div;
		div.innerHTML = inner;
		div.style.backgroundColor = bgcolor;
		div.style.paddingLeft = corners.length + "px";
		div.style.paddingRight = corners.length + "px";
		div.children[0].style.cursor = 'pointer';		
		div.children[0].container = divs[i];
		
		/* The X */
		div.children[0].children[0].style.display = 'none';
		div.children[0].children[0].style.fontSize = '1.25em';
		div.children[0].children[0].style.fontWeight = 'bold';
		div.children[0].children[0].style.cssFloat = 'left';
				
		div.children[0].children[0].onclick = function () {			
			this.style.display = 'none';
			this.parentNode.parentNode.children[1].style.display = 'none';
			
			this.parentNode.container.clicked = false;
			this.parentNode.container.full = false;
		};
		
		/* The Title */
		div.children[0].children[1].onclick = function () {
			for (var i=1; i< divs.length; i++) {			
				if (divs[i].innercontent == this.parentNode.parentNode) {
					if (!divs[i].clicked && !divs[i].full) {					
						divs[i].innercontent.children[0].children[0].style.display = '';
						divs[i].innercontent.children[1].style.display = '';
						divs[i].clicked = true;
					}
				} else {
					divs[i].innercontent.children[0].children[0].style.display = 'none';
					divs[i].innercontent.children[1].style.display = 'none';
					divs[i].clicked = false;
					divs[i].full = false;
				}
			}
		};
		
		/* The Abstract */
		div.children[1].style.display = 'none';
		div.children[1].container = divs[i];
		div.children[1].onclick = function() {
			this.style.display = 'none';
			this.container.full = true;
		};
		
		/* The Content */
		var tmphtml = div.children[2].innerHTML;
		div.children[2].innerHTML = "";
		div.children[2].style.cursor = 'pointer';
		
		div2 = document.createElement('div');
		div2.innerHTML = tmphtml;
		div2.style.display = "none";
		div.children[2].appendChild(div2);
		
		
		divs[i].appendChild(div);
		
		cdiv = document.createElement('div')
		for (var j=0; j <= 5; j++) {
			div = document.createElement('div');
			div.style.marginLeft = corners[j] + "px";
			div.style.marginRight = corners[j] + "px";		
			div.style.backgroundColor = bgcolor;
			div.style.height = "1px";
			div.style.clear = "both";
			div.style.overflow = "hidden";
			cdiv.appendChild(div);
		}
		divs[i].appendChild(cdiv);
	}
			
	function carmousemove(event) {
		var centery = carousel.offsetHeight / 2;
		var centerx = carousel.offsetWidth / 2;
		
		if (!event) event = window.event;
		
		x = event.clientX - centerx;
		y = -1* (event.clientY - centery);
				
		if (!this.lastx) {
			this.lastx = x;
			this.lasty = y;
			
			return;
		}
		
		for (var i=1; i< divs.length; i++) {
			if(divs[i].clicked || divs[i].full) {
				return;
			}
		}
		
		dist = 150 * Math.sqrt(Math.pow(this.lastx - x, 2) + Math.pow(this.lasty - y, 2)) / Math.sqrt(Math.pow(radiusx * getFontSize(), 2) + Math.pow(radiusy * getFontSize(), 2));
		angle = Math.acos((this.lasty - y) / dist);
		dist = Math.round(dist);
		
		if (this.mousedown) {
			if (x > 0 && y > 0) {
				if (x > this.lastx || y < this.lasty) {
					count -=dist;
				} else {
					count +=dist;	
				}
			} else if (x > 0) {
				if (x > this.lastx || y > this.lasty) {
					count +=dist;	
				} else {
					count -=dist;
				}
			} else if (x < 0 && y > 0) {
				if (x > this.lastx || y > this.lasty) {
					count -=dist;	
				} else {
					count +=dist;	
				}
			} else {
				if (x < this.lastx || y > this.lasty) {
					count -=dist;	
				} else {
					count +=dist;
				}
			}
		}
		
		this.lastx = x;
		this.lasty = y;
	}
	
	function carmouseover(e) {
		//var tg = (window.event) ? e.srcElement : e.target;
		if (!e) var e = window.event;
		var tg = e.relatedTarget || e.fromElement;
		
		if (this === tg) {
			carousel.mousedown = false;
		}
	}	
		
	function startCarousel() {
		var centery = carousel.offsetHeight / 2;
		var centerx = carousel.offsetWidth / 2;
		var accel = 0;
		var full = false;
		var newtime = new Date().getTime();
		
		for (var i=1; i< divs.length; i++) {
			if(divs[i].clicked) {
				var angle = i * ( Math.PI * 2) / (divs.length - 1);
				
				if (Math.sin( count * speed + angle) > 0.05) {
					accel = -8;
				} else if (Math.sin( count * speed + angle) < -0.05) {
					accel = 8;	
				} else {
					accel = 0;	
					if(divs[i].full) {
						full = divs[i];
					}
				}
				break;
			} else if (divs[i].active) {
				accel = 0;
			}
		}
				
		for (var i=1; i< divs.length; i++) {
			var angle = i * ( Math.PI * 2) / (divs.length - 1);
			var tmp_cos = Math.cos(count * speed + angle);
			var tmp_sin = Math.sin(count * speed + angle);
			var font_size = getFontSize();
			
			if (divs[i].originalwidth == null) {
				divs[i].originalwidth = divs[i].offsetWidth / getFontSize();
			}
			
			/*if (tmp_cos < 0) {
				divs[i].style.fontSize = 1 + Math.round(tmp_cos * 1000) / 4000 + "em";
			} else {
				divs[i].style.fontSize = "1em";
			}*/
						
			divs[i].style.left = Math.round(tmp_sin * radiusx * font_size + centerx - (divs[i].offsetWidth / 2)) / font_size + "em";
			
			if (tmp_cos > 0) {
				divs[i].style.zIndex = 200 - Math.round(Math.abs(tmp_sin * 100));
			} else {
				divs[i].style.zIndex = Math.round(Math.abs(tmp_sin * 100));
			}
			
      if (divs[i].clicked || divs[i].active) {
       carousel.setOpacity(divs[i],100);
      } else {
       carousel.setOpacity(divs[i],Math.round((tmp_cos + 1) * 1000) / 2000);
      }
			
			
				
			if (full != divs[i]) {
				if (carousel.getOpacity(divs[i].innercontent.children[2].children[0]) > 0) {
					carousel.setOpacity(divs[i].innercontent.children[2].children[0],null,-.1);
				} else {	
					divs[i].innercontent.children[2].children[0].style.display = 'none';
					//carousel.setOpacity(divs[i].innercontent.children[2],0);
					
					if (divs[i].innercontent.children[2].offsetHeight > 0) {						
						divs[i].innercontent.children[2].style.height = Math.max(0,divs[i].innercontent.children[2].offsetHeight - 8) + "px";						
					}
					
					var top = Math.round(tmp_cos * radiusy * font_size + centery - 0);
					
					if (carousel.mousedown) {
						divs[i].style.top = top + "px";	
					} else if (divs[i].offsetTop + 8 < top) {
						divs[i].style.top = (divs[i].offsetTop + 8) + "px";							
					} else if (divs[i].offsetTop - 8 > top) {
						divs[i].style.top = (divs[i].offsetTop - 8) + "px";
					} else {
						divs[i].style.top = top + "px";	
					}
																				
					divs[i].style.width = Math.max(divs[i].originalwidth * font_size, divs[i].offsetWidth - 8) / font_size + "em";
				}
			} else {
				if (divs[i].offsetWidth < carousel.offsetWidth || divs[i].offsetHeight < carousel.offsetHeight - 15) {
					carousel.setOpacity(divs[i].innercontent.children[2].children[0],0);
					divs[i].innercontent.children[2].style.backgroundColor = divs[i].innercontent.children[2].parentNode.style.backgroundColor;
					
					if (divs[i].innercontent.children[2].style.display != '') {
						divs[i].innercontent.children[2].style.display = '';
						
						divs[i].innercontent.children[2].style.height = 0;
					}
										
					if (divs[i].offsetWidth < carousel.offsetWidth) {
						divs[i].style.width = (divs[i].offsetWidth + 8) / getFontSize() + "em";	
					}
															
					if (divs[i].offsetHeight < carousel.offsetHeight - 15) {
						divs[i].innercontent.children[2].style.height = (divs[i].innercontent.children[2].offsetHeight + 8) + "px";	
					}
					
					if (divs[i].offsetTop > 0) {
						divs[i].style.top = Math.max(0, divs[i].offsetTop - 8) + "px";
					}
				} else {
					divs[i].innercontent.children[2].children[0].style.display = '';
					if (carousel.getOpacity(divs[i].innercontent.children[2].children[0]) < 1) {
						carousel.setOpacity(divs[i].innercontent.children[2].children[0],null,.1);
					}
				}
			}									
		}
		
		var ratio = 1;	
		var newtime2 = new Date().getTime();
		
		
		this.lastduration = this.duration - Math.min(newtime2 - newtime, this.duration - 20);
		setTimeout(startCarousel,this.lastduration);
				
		//$('debug').innerHTML = newtime2 - newtime;
		
		if (!carousel.mousedown) {			
			count += accel * ratio;
		}
	}
	
	this.duration = 40;
	this.startTime = new Date().getTime();
	this.lastduration = this.duration; 
	this.lastindex = 1;
	
	//setTimeout(startCarousel, this.duration);
	
	carousel.getOpacity = function(obj) {
		if (obj.opacity == null) {
			obj.opacity = 1;
		}
		//$('debug').innerHTML = obj.opacity;
		return obj.opacity;
	}
	
	carousel.setOpacity = function(obj, value, change) {
		if (obj.opacity == null) {
			obj.opacity = 1;
		}
		
		if (change == null) {
			change = 0;
		} else {
			value = obj.opacity;	
		}
		
		value = Math.round((value + change) * 100) / 100;
		if (value < 0) {
			value = 0;	
		} else if (value > 1) {
			value = 1;	
		}
		
		if(obj.opacity === 1){
		  obj.style.filter = "";
		}
	  
	  if (value != obj.opacity) {
			obj.style.filter = "alpha(opacity:"+value*100+")";
			obj.style.KHTMLOpacity = value;
			obj.style.MozOpacity = value;
			obj.style.opacity = value;
		}
		obj.opacity = value; 
	}
	
	startCarousel();
}
