//------cpRotation---------------------------
cpRotation = function(name){
	this.name=name;
	this.div=name;
	this.picArray=Array();
	this.status='null';
	this.winkel=0;
	this.radius=250;
	this.xoff = parseInt(document.getElementById(this.name).style.width)/2;
	this.yoff = parseInt(document.getElementById(this.name).style.height)/9*4;
	this.speed=-0.5;
	this.initSpeed=-0.5;
	this.maxSpeed=3;
	this.newSpeed=-0.5;
	this.maxWait=5000;
	this.picCount;
	var self=this;
	if (document.addEventListener) { document.getElementById(this.name).addEventListener("mousemove", function(e){ self.setSpeed(e); }, true); }
	else { document.getElementById(this.name).onmousemove = function(){self.setSpeed();}; }

	var ua = navigator.userAgent.toLowerCase();
	this.netscapebrowser = (ua.indexOf("mozilla") != -1 && ua.indexOf("compatible") == -1 && ua.indexOf("opera") == -1);
	this.iebrowser = (ua.indexOf("msie") > -1 && ua.indexOf("opera") == -1);

	var av = navigator.appVersion;
	var version = parseFloat(av.substr(0, av.indexOf(" ")));
	var version2 = parseFloat(av.substr(0, av.indexOf(".")));
	if (this.netscapebrowser) {
	  this.versionsnummer = version2;
	} else {
	  if (this.iebrowser) {
	    if (version2 < 4) {
	      this.versionsnummer= 3;
	    } else if (version2==4 && ua.indexOf("msie 4") > -1) {
	      this.versionsnummer = 4;
	    } else if (version2==4 && ua.indexOf("msie 5") > -1) {
	      this.versionsnummer = 5;
	    } else if (version2==4 && ua.indexOf("msie 6") > -1) {
	      this.versionsnummer = 6;
	    } else if (version2==4 && ua.indexOf("msie 7") > -1) {
	      this.versionsnummer = 7;
	    } else if (version2==4 && ua.indexOf("msie 8") > -1) {
	      this.versionsnummer = 8;
	    } else this.versionsnummer = 9;
	  }
	}

	this.setSpeed=function(e){
		if(e==undefined) e=event;
		var tmp_left=0;
		var obj=document.getElementById(this.name);
		if (obj.offsetParent) do{
			tmp_left+=obj.offsetLeft;
			obj=obj.offsetParent;
		} while (obj = obj.offsetParent);
		var speed=Math.max(Math.min(e.clientX - tmp_left - document.getElementById(this.name).offsetWidth/2,250),-250);
		this.newSpeed=speed/180;
		this.speed=this.speed*0.9+(speed/80)*0.1;
		window.status=speed;
	}

	this.create=function(){
		var pics=document.getElementById(this.name).getElementsByTagName('div');
		for(var i=0;i<pics.length;i++){
			this.picArray[i]=Array();
			this.picArray[i]['div']=pics[i];
			this.picArray[i]['dwidth']=parseInt(pics[i].style.width);
			this.picArray[i]['dheight']=parseInt(pics[i].style.height);
			this.picArray[i]['dratio']=this.picArray[i]['dwidth']/this.picArray[i]['dheight'];
			this.picArray[i]['pic']=pics[i].getElementsByTagName('img')[0];
			this.picArray[i]['div'].style.position='absolute';
		}
		this.picCount=pics.length;
		this.status='wait4load';
		this.wait4load();
	}

	this.wait4load=function(){
		var loading=false;
		if( this.status=='wait4load' ){
			for(var i=0;i<this.picCount;i++) {
				if(this.picArray[i]['pic'].width<1) loading=true;
				else {
					this.picArray[i]['pwidth']=this.picArray[i]['pic'].width;
					this.picArray[i]['pheight']=this.picArray[i]['pic'].height;
					this.picArray[i]['pratio']=this.picArray[i]['pwidth']/this.picArray[i]['pheight'];
					if( this.picArray[i]['pratio']>this.picArray[i]['dratio'] ) {
						this.picArray[i]['prwidth']=this.picArray[i]['dwidth']-6;
						this.picArray[i]['prheight']=this.picArray[i]['prwidth']/this.picArray[i]['pratio'];
						this.picArray[i]['prtop']=(this.picArray[i]['dheight']-this.picArray[i]['prheight'])/2;
					} else {
						this.picArray[i]['prheight']=this.picArray[i]['dheight']-6;
						this.picArray[i]['prwidth']=this.picArray[i]['prheight']*this.picArray[i]['pratio'];
						this.picArray[i]['prtop']=3;
					}
				}
			}
			this.maxWait=this.maxWait-20;
			if(this.maxWait<0) loading=false;
			if(loading) setTimeout("cpRotationItem['"+this.name+"'].wait4load();",20);
			else {
				this.status='rotate';
				this.rotate();
			}
		}
	}

	this.setRadius=function(radius){
		this.radius=radius;
	}
	this.setPicSize=function(width,height){
		for(var i=0;i<this.picArray.length;i++){
			this.picArray[i]['dwidth']=parseInt(width);
			this.picArray[i]['dheight']=parseInt(height);
			this.picArray[i]['prwidth']=parseInt(width);
			this.picArray[i]['prheight']=parseInt(height);
		}
	}

	this.rotate=function(){
		if(this.status=='rotate'){
			this.speed=this.speed*0.9+(this.newSpeed)*0.1;
			for(var i=0;i<this.picCount;i++){
				var localwinkel=( this.winkel - (360*i/this.picCount) ) /180*Math.PI;
				var ratio=(100+20*(Math.sin(localwinkel)-1))/100;

				if( this.iebrowser && this.versionsnummer<100 ){
					this.picArray[i]['div'].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src='"+this.picArray[i]['pic'].src+"')";
					this.picArray[i]['pic'].style.display='none';
				}
				else {
					this.picArray[i]['div'].style.filter = "alpha(opacity:"+Math.min(ratio*100+10,100)+")";
					this.picArray[i]['div'].KHTMLOpacity = this.picArray[i]['div'].style.MozOpacity = this.picArray[i]['div'].style.opacity = Math.min(ratio+0.1,1);
					this.picArray[i]['pic'].style.marginTop=ratio*this.picArray[i]['prtop']+'px';
					this.picArray[i]['pic'].style.height=ratio*this.picArray[i]['prheight']+'px';
					this.picArray[i]['pic'].style.width=ratio*this.picArray[i]['prwidth']+'px';
				}

				this.picArray[i]['div'].style.height=ratio*this.picArray[i]['dheight']+'px';
				this.picArray[i]['div'].style.width=ratio*this.picArray[i]['dwidth']+'px';

				this.picArray[i]['div'].style.left=Math.round((this.radius * Math.cos(localwinkel)) + this.xoff - parseInt(this.picArray[i]['div'].style.height)/2) + 'px';
				this.picArray[i]['div'].style.top=Math.round((0.2*this.radius * Math.sin(localwinkel)) + this.yoff  - parseInt(this.picArray[i]['div'].style.width)/2) + 'px';
				this.picArray[i]['div'].style.zIndex=Math.round( (Math.sin(localwinkel)+1)*20 );
			}
			this.winkel=(this.winkel+this.speed) % 360;
			setTimeout("cpRotationItem['"+this.name+"'].rotate();",40);
		}
	}

}


var cpRotationItem=Array();

function addCpRotation(id){
	if(cpRotationItem[id]==null) {
		cpRotationItem[id]=new cpRotation(id);
		cpRotationItem[id].create();
	}
}
