var activeInfoBox = '';
var Bloque= new Array(5) 
	Bloque[0]="Bloque1"; 
	Bloque[1]="Bloque2"; 
	Bloque[2]="Bloque3"; 
	Bloque[3]="Bloque4"; 
	Bloque[4]="Bloque5"; 
	Bloque[5]="Bloque6"; 
	Bloque[6]="Bloque7"; 
var x=0; 
var locked = 'unlocked';


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
addLoadEvent(function () {
	for (x=0; x<7; x++) { 
		var titleObj = getObject(Bloque[x] + 'Title');
		//titleObj.innerHTML = projectNameArray[relArray[Bloque[x]]];
		var newText = document.createTextNode(projectNameArray[relArray[Bloque[x]]]);
		titleObj.appendChild(newText);
	}
})
addLoadEvent(function ()
{
	expand(relArray['Bloque4'] + 'Desc');
})

function loadTitles() {
	for (x=0; x<7; x++) { 
		titleObj = getObject(Bloque[x] + 'Title');
		titleObj.removeChild(titleObj.childNodes[0]);
		newText = document.createTextNode(projectNameArray[relArray[Bloque[x]]]);
		titleObj.appendChild(newText);
	}
}


/*var relArray= new Array()
	relArray["Bloque1"]="Client1";
	relArray["Bloque2"]="Client2";
	relArray["Bloque3"]="Client3";
	relArray["Bloque4"]="Client4";
	relArray["Bloque5"]="Client5";
	relArray["Bloque6"]="Client6";
	relArray["Bloque7"]="Client7";

var clientArray= new Array()
	clientArray[0]="Client1";
	clientArray[1]="Client2";
	clientArray[2]="Client3";
	clientArray[3]="Client4";
	clientArray[4]="Client5";
	clientArray[5]="Client6";
	clientArray[6]="Client7";
	clientArray[7]="Client8";
	clientArray[8]="Client9";
	clientArray[9]="Client10";
	clientArray[10]="Client11";
	clientArray[11]="Client12";
	clientArray[12]="Client13";
*/

function expand(infoBox){
	var box = getObject(infoBox);
	box.style.display = 'block';
	if(activeInfoBox != '') {
		var hideBox = getObject(activeInfoBox);
		hideBox.style.display = 'none';
	}
	activeInfoBox = infoBox;
}

function getObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId);
	} else {
		return false;
	}
}
function incrementColumn(method){
	if(locked == 'locked') {
		return;
	}
	for (x=0; x<7; x++) 
	{ 
		var BloqueObj = getObject(Bloque[x]);
		if(BloqueObj.style.left == '333px') {
			if(x != 6 && method == 'previous') {
				var column = Bloque[x + 1];
			}
			else if(x != 0 && method == 'next') {
				var column = Bloque[x - 1];
			}
			else if(method == 'previous') {
				var column = Bloque[0];
			}
			else if(method == 'next') {
				var column = Bloque[6];
			}
		}	
	}
	moveColumn(column);

}

function moveColumn(column){
	if(locked == 'locked') {
		return;
	}
	locked = 'locked';
	var columnObj = getObject(column);
	var columnLeft = parseFloat(columnObj.style.left);
	if(columnObj.offsetWidth != 115) {
		for (x=0; x<7; x++) 
		{ 
			var BloqueObj = getObject(Bloque[x]);
			var BloqueLeft = parseFloat(BloqueObj.style.left);
			if(column == Bloque[x]) {
				transformLayer (333,113,115,215,column,200,30);
				transformLayer (333,79,115,0,column + 'Title',200,30);
			}
			else if(columnLeft > 333) {
				if (BloqueObj.offsetWidth != '80' && BloqueObj.offsetWidth != '81') {
					transformLayer ((BloqueLeft - (columnLeft - 367)),171,80,157,Bloque[x],200,30);
					transformLayer ((BloqueLeft - (columnLeft - 367)),99,80,0,Bloque[x] + 'Title',200,30);
				}
				else {
					transformLayer ((BloqueLeft - (columnLeft - 350)),171,80,157,Bloque[x],200,30);
					transformLayer ((BloqueLeft - (columnLeft - 350)),99,80,0,Bloque[x] + 'Title',200,30);
				}
			}
			else {
				if (BloqueObj.offsetWidth != '80' && BloqueObj.offsetWidth != '81') {

					transformLayer ((BloqueLeft + (367 - columnLeft)),171,80,157,Bloque[x],200,30);
					transformLayer ((BloqueLeft + (367 - columnLeft)),99,80,0,Bloque[x] + 'Title',200,30);
				}
				else {
					transformLayer ((BloqueLeft + (350 - columnLeft)),171,80,157,Bloque[x],200,30);
					transformLayer ((BloqueLeft + (350 - columnLeft)),99,80,0,Bloque[x] + 'Title',200,30);
				}
			}
		}
	}
	else {
		locked = 'unlocked';
		return;
	}
}

function transformLayer (x,y,w,h,id,duration,steps) {
	stepDuration = Math.round(duration/steps) ;
	obj = dd.elements[id];
	x1 = obj.x;	
	y1 = obj.y;	
	w1 = obj.w;	
	h1 = obj.h;	
	x = (x)?x:x1;
	y = (y)?y:y1;
	w = (w)?w:w1;
	h = (h)?h:h1;
	difX = (x - x1)/steps;	
	difY = (y - y1)/steps;	
	difW = (w - w1)/steps;	
	difH = (h - h1)/steps;	

	var displayObj = getObject(obj.id);
	obj.moveBy(difX,difY);
	obj.resizeBy(difW,difH);
	var ugh = getObject(id);

	if(x1 > 700) {
		displayObj.style.display = 'none';
		obj.moveBy(-700 + (700 - parseFloat(ugh.style.left)),0);
		x = x-700;
		x1 = x1-700;
		obj.x = 0;
		var index = getIndex(Bloque,id);
		var leftGuy = getNext(index);
		var clientIndex = getIndex(clientArray,relArray[Bloque[leftGuy]])
		if(clientIndex == 0) {
			var temp = clientArray.length - 1;
			relArray[id]=clientArray[temp];
		}
		else {
			temp = getIndex(clientArray,relArray[Bloque[leftGuy]]);
			relArray[id]=clientArray[temp-1];
		}
		loadTitles();

	}
	else if(x1 < 0) {
		displayObj.style.display = 'none';
		obj.moveBy((700 - parseFloat(ugh.style.left)),0);
		x = x+700;
		x1 = x1+700;
		obj.x = 700;

		var index = getIndex(Bloque,id);
		var rightGuy = getPrev(index);
		var clientIndex = getIndex(clientArray,relArray[Bloque[rightGuy]])
		var temp = clientArray.length - 1;
		if(clientIndex == temp) {			
			relArray[id]=clientArray[0];
		}
		else {
			relArray[id]=clientArray[clientIndex+1];
		}
		loadTitles();
	}
	else {
		displayObj.style.display = 'block';
	}
	duration = (duration - stepDuration);
	steps --;
	if(steps>=0){
		setTimeout('transformLayer('+x+','+y+','+w+','+h+',"'+id+'",'+duration+','+steps+')',stepDuration);
	}
	else {
		if(h == 215) {
			expand(relArray[id] + 'Desc');
		}
		locked = 'unlocked';
	}
	return;
}

function getIndex(arrayName,value) {
	for (x=0; x<arrayName.length; x++) { 
		if(value == arrayName[x]) {
			return x;
		}
	}
}

function getNext(index) {
	if(index < 6) {
		index++;
	}
	else {
		index = 0;
	}
	return index;
}

function getPrev(index) {
	if(index > 0) {
		index--;
	}
	else {
		index = 6;
	}
	return index;
}