/*** Create boxOpen objects ***/
function getStartedDragging()
{
	button1obj = new DraggableObject(document.getElementById("button1"));
	button2obj = new DraggableObject(document.getElementById("button2"));
	button3obj = new DraggableObject(document.getElementById("button3"));
	button4obj = new DraggableObject(document.getElementById("button4"));
	button5obj = new DraggableObject(document.getElementById("button5"));
	button6obj = new DraggableObject(document.getElementById("button6"));
}

/*** Define macro behavior of boxOpen objects ***/
function DraggableObject(myButton)
{
	draggableDIV = myButton.parentNode.parentNode; // The div to be dragged

	draggableDIV.getElementsByClassName = getElementsByClassName;
	draggableDIV.findPos = findPos;

	draggableDIV.dragButton = myButton;
		draggableDIV.dragButton.fatherDiv = draggableDIV;
	draggableDIV.closeButton = draggableDIV.getElementsByClassName("buttonClose")[0];
		draggableDIV.closeButton.fatherDiv = draggableDIV;
	draggableDIV.introDiv = draggableDIV.getElementsByClassName("intro")[0];
		draggableDIV.introDiv.fatherDiv = draggableDIV;
	draggableDIV.headerDiv = draggableDIV.getElementsByClassName("header")[0];
		draggableDIV.headerDiv.fatherDiv = draggableDIV;
	draggableDIV.innerDiv = draggableDIV.getElementsByClassName("inner")[0];
		draggableDIV.innerDiv.fatherDiv = draggableDIV;

	draggableDIV.dragButton.styleDrag = styleDrag;
	draggableDIV.dragButton.stopDrag = stopDrag;

	addEvent(document,'mousedown',blockBehavior); // Prevent Mozilla from standard behavior for OnClick event (otherwise it would try to drag the button picture rather than the DIV! Or, it would allow text selection)
	addEvent(document,'mousemove',blockBehavior); // Prevent IE from standard behavior for OnMouseMove event (otherwise it would try to drag the button picture rather than the DIV! Or, it would allow text selection)
	addEvent(myButton,'mousedown',startDrag);
	addEvent(myButton,'mouseup',stopDrag);
	addEvent(myButton.fatherDiv,'mousedown',focusMe);
}


function blockBehavior(e)
{
	functionName = (window.event) ? "event.returnValue=false;" : "e.preventDefault()";
	eval(functionName);
}

/*** Activate dragging ***/
function startDrag(e)
{
	if(!e){var e=window.event};
	//window.status=this.id;

	this.styleDrag("yes");

	// Assign default values for top and left properties
	this.fatherDiv.findPos();
	this.fatherDiv.style.left = this.fatherDiv.posLeft;
	this.fatherDiv.style.top = this.fatherDiv.posTop;

	// Define the position of the mouse, related to the Box object
	//clientX/clientY e' la posizione assoluta del mouse rispetto alla finestra (escluse barre strumenti, scorrimento, etc.)
	this.fatherDiv.mouseRelativeX = e.clientX - parseInt(this.fatherDiv.style.left);
	this.fatherDiv.mouseRelativeY = e.clientY - parseInt(this.fatherDiv.style.top);

	this.fatherDiv.dragging=true;
	addEvent(document,'mousemove',dragDivs); // In order to spare some work to the browser, the dragging function is called only when needed.
}

function dragDivs(e)
{
	//window.status+=1; // This allows to check that this function is called only during the dragging.
	for (k=0; k<document.getElementsByClassName("boxOpen").length; k++)
	{
		myDiv = document.getElementsByClassName("boxOpen")[k];
		if(myDiv.dragging)
	 	{
			if(!e){var e=window.event};
	
			// move div element
			myDiv.style.left = e.clientX - myDiv.mouseRelativeX + 'px';
			myDiv.style.top = e.clientY - myDiv.mouseRelativeY + 'px';
			return false;
		}
	}
}

/*** Bring the current Div to the top ***/
function focusMe()
{
	for (a=0; a<document.getElementsByClassName("boxOpen").length; a++)
	{
		tempDiv = document.getElementsByClassName("boxOpen")[a];
		//alert(tempDiv.id +","+ this.id);
		if (tempDiv==this) {tempDiv.style.zIndex=200;}
		else {tempDiv.style.zIndex=100;}
	}
}

/*** Change the style of DIVs according to the status ***/
function styleDrag(choice)
{
	//this.style.cursor = (choice=="yes") ? "move" : "default";
	this.fatherDiv.style.position = "absolute"; // Otherwise, while dragging, the "top" position will be added to the distance from the previous
	this.fatherDiv.style.MozOpacity = (choice=="yes") ? 0.75 : 1;
	//this.fatherDiv.style.filter = (choice=="yes") ? "alpha(opacity=80)" : "alpha(opacity=100)"; // On IE, opacity damages background transparency!
}

/*** Stop dragging ***/
function stopDrag()
{
	this.styleDrag("no");
	this.fatherDiv.dragging=false;
	removeEvent(document,'mousemove',dragDivs); // In order to spare some work to the browser, the dragging function is called only when needed.
}