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.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;
	
	//alert('posx='+this.div_posx+'  posy='+this.div_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()
{
	//alert('draw resized()');
	_div_resize(this.div_id,this.div_width,this.div_height);
	
	var browserName = _getBrowser();
	var version = _browserVersion(browserName);
	
	if(browserName=='ie' && version<9)
	{
		//alert('ie détecté');
		
		
		var windowWidth = document.body.offsetWidth;
		var windowHeight = document.body.offsetHeight;
		
		posx = parseInt(windowWidth/2) - parseInt(this.div_width/2);
		posy = parseInt(windowHeight/2) - parseInt(this.div_height/2);
		
	}
	else
	{
		//alert('window.innerWidth='+window.innerWidth+' window.innerHeight='+window.innerHeight);
	
		posx = parseInt(window.innerWidth/2) - parseInt(this.div_width/2);
		posy = parseInt(window.innerHeight/2) - parseInt(this.div_height/2);
	
	}
	
	//alert(posx+','+posy);
	
	this.move(posx,posy);
	
	//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.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.setPicture = function(path)
{	
	myImage = new Image;
	
	cwindow_obj = this;
	
	myImage.onload = function() 
	{
		width = this.width;
		height = this.height;
		
		//alert(width);
		
		cwindow_obj.resize(width+(cwindow_obj.margin*2),height+(cwindow_obj.margin*2));
		
		_div_resize(cwindow_obj.content,width,height);
		
		//div_setContent(this.content,'hello');
		
		div_setParameter(cwindow_obj.content,'background','url('+path+')');
		
		cwindow_obj.appear();
		
		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);	
}





