Cloud=function(parent){
	this.parent=parent;
	parent.style.position="relative";
	this.height=parseInt(parent.offsetHeight);
	this.width=parseInt(parent.offsetWidth);
	this.elements=new Array();
};

Cloud.prototype={
	addElement:function(element){
		var thisp=this;
		element.element.onclick=function(){thisp.showElement(element);}
		this.parent.appendChild(element.element);

		if (element.params.random){
			var col=1;
			while(col){
				col=0;
				var x=parseInt(Math.random()*(this.width-element.element.offsetWidth));
				var y=parseInt(Math.random()*(this.height-element.element.offsetHeight));
				for(var i=0;i<this.elements.length;i++){
					var ex=parseInt(this.elements[i].element.style.left);
					var ey=parseInt(this.elements[i].element.style.top);
					var ew=parseInt(this.elements[i].element.offsetWidth);
					var eh=parseInt(this.elements[i].element.offsetHeight);
					if ((x>=ex && x<=ex+ew) && (y>=ey && y<=ey+eh )) col=1;
					if ((x+element.element.offsetWidth>=ex && x+element.element.offsetWidth<=ex+ew) && (y>=ey && y<=ey+eh )) col=1;
					if ((x+element.element.offsetWidth>=ex && x+element.element.offsetWidth<=ex+ew) && (y+element.element.offsetHeight>=ey && y+element.element.offsetHeight<=ey+eh )) col=1;
					if ((x>=ex && x<=ex+ew) && (y+element.element.offsetHeight>=ey && y+element.element.offsetHeight<=ey+eh )) col=1;
				}
				element.element.style.top=y+"px";
				element.element.style.left=x+"px";
			}
		}
		this.elements.push(element);
		element.nimp=element.imp;
		element.setSize();
		element.vx=Math.random()*10;
		element.vx=(5-element.vx)/Math.abs(5-element.vx);
		element.vy=Math.random()*10;
		element.vy=(5-element.vy)/Math.abs(5-element.vy);
		
		element.timerKos=setInterval(function(){element.startKos();},50);
	},
	showElement:function(element){

		var pthis=this;
		if (element.expanded){
			for (var i=0;i<pthis.elements.length;i++){
				pthis.elements[i].startKosAnim();
			}
			return;
		}
		var x=0;
		var y=0;
		var minx=0;
		var maxx=this.width;
		for (var i=0;i<this.elements.length;i++){
			var elw=this.elements[i].element.offsetWidth;
			var elh=this.elements[i].element.offsetHeight;
			if (this.elements[i].expanded){
				elw=this.elements[i].realW;
				elh=this.elements[i].realH;
			}
			if (this.elements[i]!=element){
				if (y+(elh)<this.height){
					this.elements[i].move(x,y);
					y+=elh;
					if (elw>minx)minx=elw;
				}else{
					x=this.width-elw;
					y=0;
					this.elements[i].move(x,y);
					y+=elh;
					if (maxx>x)maxx=x;
				}
			}
		}
		element.moveExpand(minx+5,0+5,maxx-minx-10,this.height-13);
	}
	
};

CloudElement=function(params){
	var thisp=this;
	this.element=document.createElement("div");
	this.element.style.position="absolute";
	this.element.style.cursor="pointer";
	this.params=params;
	this.params.createElement(this);
	this.imp=this.nimp=params.important;
	this.nimp+=5;
	this.setSize();

	this.element.onmouseover=function(){thisp.mouseover();};
	this.element.onmouseout=function(){thisp.mouseout();};
};

CloudElement.prototype={
	imp:0,
	nimp:0,
	timerMove:0,
	timerKos:0,
	expanded:0,
	realH:0,
	realW:0,
	vx:1,
	vy:1,

	setSize:function(){
		this.params.setSize(this);
	},
	mouseover:function(){
		this.nimp=this.imp+5;
		this.setSize();
	},
	mouseout:function(){
		this.nimp=this.imp;
		this.setSize();
	},
	move:function(x,y){
		var thisp=this;
		this.nimp=this.imp+5;
		this.setSize();
		if (this.timerMove)clearInterval(this.timerMove);
		if (this.timerKos){ 
			clearInterval(this.timerKos);
			this.timerKos=0;
		}
		this.timerMove=setInterval(function(){thisp.moveAnim(x,y)},50);
	},
	moveExpand:function(x,y,maxx,maxy){
		var thisp=this;
		if (this.timerMove)clearInterval(this.timerMove);
		if (this.timerKos){ 
			clearInterval(this.timerKos);
			this.timerKos=0;
		}
		if (!this.realW)
			this.realW=this.element.offsetWidth-2;
		if (!this.realH)
			this.realH=this.element.offsetHeight-2;
		this.nimp=this.imp+5;
		this.setSize();
		this.timerMove=setInterval(function(){thisp.moveExpandAnim(x,y,maxx,maxy)},50);
	},
	moveAnim:function(x,y){
		var r=0;
		var elx=parseInt(this.element.style.left);
		var ely=parseInt(this.element.style.top);
		if (this.expanded){
			var elw=parseInt(this.element.offsetWidth);
			var elh=parseInt(this.element.offsetHeight);
			if (elh>this.realH+10){r=1;this.element.style.height=(elh+(this.realH-elh)/2)+"px";}
			if (elw>this.realW+10){r=1;this.element.style.width=(elw+(this.realW-elw)/2)+"px";}
			if (!r){
				this.expanded=false;
				this.element.style.height=this.realH+"px";
				this.element.style.width=this.realW+"px";
			}
		}

		if (!r){
			if (!(elx-x<5 && elx-x>-5)){r=1; this.element.style.left=parseInt(elx+((x-elx)/2))+"px"}
			if (!(ely-y<5 && ely-y>-5)){r=1; this.element.style.top=parseInt(ely+((y-ely)/2))+"px"}
		}
		if (!r){
			this.nimp=this.imp;
			this.setSize();
			this.element.style.top=y+"px";
			this.element.style.left=x+"px";
			clearInterval(this.timerMove);
		}
	},
	moveExpandAnim:function(x,y,maxx,maxy){
		var r=0;
		var elx=parseInt(this.element.style.left);
		var ely=parseInt(this.element.style.top);
		var elw=parseInt(this.element.offsetWidth);
		var elh=parseInt(this.element.offsetHeight);

		if (!(elx-x<5 && elx-x>-5)){r=1; this.element.style.left=parseInt(elx+((x-elx)/2))+"px"}
		if (!(ely-y<5 && ely-y>-5)){r=1; this.element.style.top=parseInt(ely+((y-ely)/2))+"px"}
		if (!r){
			this.element.style.top=y+"px";
			this.element.style.left=x+"px";
			if (elh<maxy-10){r=1;this.element.style.height=parseInt(elh+(maxy-elh)/2)+"px";}
			if (elw<maxx-10){r=1;this.element.style.width=parseInt(elw+(maxx-elw)/2)+"px";}
		}
		if (!r){
			this.element.style.height=(maxy)+"px";
			this.element.style.width=(maxx)+"px";
			this.expanded=true;
			this.nimp=this.imp;
			this.setSize();
			clearInterval(this.timerMove);
		}
	},
	startKos:function(){
		var r=0;
		if (this.expanded){
			var elw=parseInt(this.element.offsetWidth);
			var elh=parseInt(this.element.offsetHeight);
			if (elh>this.realH+10){r=1;this.element.style.height=(elh+(this.realH-elh)/2)+"px";}
			if (elw>this.realW+10){r=1;this.element.style.width=(elw+(this.realW-elw)/2)+"px";}
			if (!r){
				this.expanded=false;
				this.element.style.height=this.realH+"px";
				this.element.style.width=this.realW+"px";
			}
		}else{
			var dx=1;
			var dy=1;
			dx=Math.random()*4;
			dy=Math.random()*4;
			this.element.style.left=parseInt(this.element.style.left)+parseInt(this.vx*dx)+"px";
			this.element.style.top=parseInt(this.element.style.top)+parseInt(this.vy*dy)+"px";
			if (this.element.parentNode.offsetHeight<=parseInt(this.element.style.top)+this.element.offsetHeight || parseInt(this.element.style.top)<=0)this.vy*=-1;
			if (this.element.parentNode.offsetWidth<=parseInt(this.element.style.left)+this.element.offsetWidth || parseInt(this.element.style.left)<=0)this.vx*=-1;
		}
	},
	startKosAnim:function(){
		var pthis=this;

		this.timerKos=setInterval(function(){pthis.startKos();},50);
	}
};
