var cwindow_obj = null;
var cwindow_id = 1;

function class_cwindow(div_id,div_content,x_decal,y_decal)
{
	this.id = cwindow_id++;
	this.debug_id = 0;
	this.div_id = div_id;				/* identifiant du bloc */
	this.content = div_content;			/* identifiant du bloc texte */

	this.div_posx = 0;				/* position x */
	this.div_posy = 0;				/* position y */
	
	this.margin = 6;
	
	this.x_decal = x_decal;
	this.y_decal = y_decal;
	
	this.div_width = 0;				/* largeur du bloc */
	this.div_height = 0;			/* hauteur du bloc */
	
	this.div_defaultWidth = 300;
	this.div_defaultHeight = 50;
	
	this.moving_timer = null;
	
	this.is_moving = false;				/* variable d'état - déplacement en cours ? */

	this.div_parent = null;
}

class_cwindow.prototype.setup = function()
{
	this.div_parent = _div_getParent(this.div_id);
	//this.drawResized();
}

class_cwindow.prototype.debug = function()
{
	div_setContent('debug',"width="+this.div_width+' height='+this.div_height);
	this.debug_id++;
}

class_cwindow.prototype.rmove = function(byX,byY)
{
	this.div_posx += byX;
	this.div_posy += byY;
	//div_setContent(this.div_id,'x '+this.div_posx+' '+'y '+this.div_posy);
	this.draw();
}

class_cwindow.prototype.resize = function(width,height)
{
	this.div_width = width;
	this.div_height = height;
	//div_setContent(this.div_id,'x '+this.div_posx+' '+'y '+this.div_posy);
	this.drawResized();
}

class_cwindow.prototype.move = function(posX,posY)
{
	this.div_posx = posX;
	this.div_posy = posY;
	//div_setContent(this.div_id,'x '+this.div_posx+' '+'y '+this.div_posy);
	this.draw();
}

class_cwindow.prototype.draw = function()
{
	div_setParameter(this.div_id,'left',parseInt(this.div_posx)+"px");
   	div_setParameter(this.div_id,'top',parseInt(this.div_posy)+"px");
}

class_cwindow.prototype.drawResized = function()
{
	_div_resize(this.div_id,this.div_width,this.div_height);
	
	posx = parseInt(document.body.clientWidth/2) - parseInt(this.div_width/2);	
	posy = parseInt(getWindowHeight()/2) - parseInt(this.div_height/2) + _page_scroll_position();
	
	this.move(posx,posy);
}


class_cwindow.prototype.appear = function(content)
{
	_div_unhide(this.div_id,'block');
	//div_unhide(this.div_id);
}


class_cwindow.prototype.disappear = function()
{
	_div_hide(this.div_id,false);
}



class_cwindow.prototype.startDrag = function()
{
	if(!this.is_moving && cwindow_obj==null)
	{
		cwindow_obj = this;

		this.moving_timer = setInterval( "cwindow_obj.moveAtMousePosition()", 30);
		this.is_moving = true;	
	}
	else
	{
		//this.stopDrag();
	}
	
	//alert('commence le drag');
}

class_cwindow.prototype.stopDrag = function()
{
	if( this.is_moving && cwindow_obj!=null )
	{
		clearInterval(this.moving_timer);
		cwindow_obj=null;
		this.is_moving = false;	
	}
	else
	{
		//alert('erreur');
	}
}

class_cwindow.prototype.setText = function(text)
{	
	cwindow_obj = this;
	
	width = this.div_defaultWidth;
	height = this.div_defaultHeight;
	
	var textLength = text.length;
	
	height = Math.ceil((textLength/100) * this.div_defaultHeight);
		
	div_setContent(this.div_id,'<div class="text">'+text+'</div>');
			
	_div_setBackground(cwindow_obj.div_id,'');
	
	cwindow_obj.resize(width,height);
	cwindow_obj.appear();
		
	cwindow_obj = null;
}


class_cwindow.prototype.setPicture = function(path)
{	
	myImage = new Image;
	
	cwindow_obj = this;

	cwindow_obj.resize(300,60);

	_div_setBackground(cwindow_obj.div_id,'');
	div_setContent(cwindow_obj.div_id,"<div class=\"texte\" style=\"text-align: center;\"><br/><br/>chargement / loading</div>");
	cwindow_obj.appear();
		
	myImage.onload = function() 
	{
		width = this.width;
		height = this.height;
			
		div_setContent(cwindow_obj.div_id," ");
	
		cwindow_obj.resize(width+(cwindow_obj.margin*2),height+(cwindow_obj.margin*2));
		_div_setBackground(cwindow_obj.div_id,path);
	
		cwindow_obj = null;
		
		this.onload = null;
	}
	
	myImage.src = path;
}


class_cwindow.prototype.pictureLoaded = function()
{

}


class_cwindow.prototype.moveAtMousePosition = function()
{
	//alert(navigator.appName);

	if(navigator.appName == "Microsoft Internet Explorer")
	{
		this.move(mouse_x+this.x_decal,mouse_y+this.y_decal);
		
	}
	else
	{
		this.move(mouse_x-this.div_parent.offsetLeft+this.x_decal,mouse_y-this.div_parent.offsetTop+this.y_decal);
	}
	
	//div_setContent('debug',mouse_x+'  -  '+this.div_parent.offsetLeft);	
}





