//
//  Author: 001 Team
// Contact: info@001.cat
// Version: v1.2
//   Date: 05/12/2009
//

$(window).bind('resize', function() {
	resize();
});

var v_refresh;
var v_percent;

function init(t1,t2)
{	
	var colors = getColors();
	var initcol = rand(0, 10);
	
	v_refresh = t1;
	v_percent = t2;
	
	var el = document.getElementById('block_left');
	cBackcolorNow('block_left', colors[initcol]);
	el.name = initcol;
	var el = document.getElementById('block_bottom');
	cBackcolorNow('block_bottom', colors[initcol]);
	el = document.getElementById('noblock');
	cBackcolorNow('noblock', colors[initcol+1]);
	el.name = initcol+1;

	var x = 1;
	while(x < 22)
	{	
		var el = document.getElementById('v'+x);
		var ell = document.getElementById('h'+x);
		cBackcolorNow('v'+x, gradientF(colors[initcol], colors[initcol+1], 21, x));
		cBackcolorNow('h'+x, gradientF(colors[initcol], colors[initcol+1], 21, x));
		x = x + 1;
	}
	var el = document.getElementById('block_bottom');
	el.name = 0;
	
	resize();
}

function resize()
{
	var sizeH = getSizeH();
	var sizeW = getSizeW();

	var ebl = document.getElementById('block_left');
	var eblh = sizeH;
	var eblw = Math.round(sizeW * 0.228896);
	ebl.style.height = sizeH + 'px';
	ebl.style.width = eblw + 'px';

	var ebb = document.getElementById('block_bottom');
	var ebbh = Math.round(sizeH * 0.305286);
	var ebbw = (sizeW - eblw);
	ebb.style.height = ebbh + 'px';
	ebb.style.width = ebbw + 'px';

	var velw = Math.round(sizeW * 0.006835);
	var velh = Math.round(sizeH * 0.009114);
	
	var enb = document.getElementById('noblock');
	var enbh = (sizeH - ebbh - velh * 21);
	var enbw = (sizeW - eblw - velw * 21);
	enb.style.height = enbh + 'px';
	enb.style.width = enbw + 'px';

	var i = 21;
	while(i>0)
	{
		var elv = document.getElementById('v'+i);
		elv.style.height = enbh + velh * (22-i) + 'px';
		elv.style.width = velw + 'px';
		i = i - 1;
	}
	
	i = 21;
	while(i>0)
	{
		el = document.getElementById('h'+i);
		el.style.width = enbw + velw * (21-i) + 'px';
		el.style.height = velh + 'px';
		i = i - 1;
	}
	
	var img1 = document.getElementById('img1');
	img1.style.left = Math.round(sizeW * 0.04) + 'px';
	img1.style.top = Math.round(sizeH * 0.04) + 'px';
	
	var img2 = document.getElementById('img2');
	img2.style.left = Math.round(sizeW * 0.04) + 'px';
	img2.style.bottom = Math.round(sizeH * 0.05) + 'px';
	
	var img3 = document.getElementById('img3');
	img3.style.left = Math.round(sizeW * 0.43) + 'px';
	img3.style.top = Math.round(sizeH * 0.05) + 'px';

	var img4 = document.getElementById('img4');
	img4.style.left = Math.round(sizeW * 0.43) + 'px';
	img4.style.top = (Math.round(sizeH * 0.05) + 60) + 'px';
	
	var img5 = document.getElementById('img5');
	img5.style.left = Math.round(sizeW * 0.43) + 'px';
	img5.style.top = (Math.round(sizeH * 0.05) + 100) + 'px';
	
	var img6 = document.getElementById('img6');
	img6.style.left = Math.round(sizeW * 0.43) + 'px';
	img6.style.top = (Math.round(sizeH * 0.05) + 140) + 'px';
}

function getSizeW() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;
}

function getSizeH() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getColors()
{
	var colors = new Array(10);
	
	colors[0] = 'FF1C23';
	colors[1] = '622280';
	
	colors[2] = 'FFBC00';
	colors[3] = '009EE0';
	
	colors[4] = '110CE5';
	colors[5] = 'F9AFF5';
	
	colors[6] = 'FF0861';
	colors[7] = '6AC634';
	
	colors[8] = '145118';
	colors[9] = 'FF1C23';
	
	colors[10] = '9ADEE2';
	colors[11] = '622280';
	
	colors[12] = '1AD30B';
	colors[13] = '110D63';
	
	colors[14] = 'CCCCCC';
	colors[15] = '663300';
	
	colors[16] = 'F9CB08';
	colors[17] = 'FF0861';
	
	colors[18] = '33CCFF';
	colors[19] = '622280';
	
	return colors;
}

// CC
function cC1()
{
	var le1 = document.getElementById('block_bottom');
	if(le1.name == 0)
	{
		le2 = document.getElementById('v1');
		le2.name = 1;
	}
}

function cC2()
{	
	var le1 = document.getElementById('block_bottom');
	if(le1.name == 0)
	{
		le2 = document.getElementById('v1');
		if(le2.name == 1)
		{		
			le1.name = 1;
			le2.name = 0;
			
			var colors = getColors();
			var i = findColorsCC();	
			
			var lel = document.getElementById('noblock');
			var j = i;
			if(i+1==lel.name)
				j = i + 2;

			setTimeout("cCunit(" + 1 + "," + 25 + ",'"+colors[i]+"','"+colors[(j+1)%20]+"',"+21+","+((j+1)%20)+")", 25);
		}
	}
}

// CF
function cF1()
{
	var le1 = document.getElementById('block_bottom');
	if(le1.name == 0)
	{
		le2 = document.getElementById('v21');
		le2.name = 1;
	}
}

function cF2()
{
	var le1 = document.getElementById('block_bottom');
	if(le1.name == 0)
	{
		le2 = document.getElementById('v21');
		if(le2.name == 1)
		{		
			le1.name = 1;
			le2.name = 0;
	
			var colors = getColors();
			var i = findColorsCF(colors);
	
			setTimeout("cFunit(" + 21 + "," + 25 + ",'"+colors[i]+"','"+colors[(i+1)%20]+"',"+21+","+((i+1)%20)+")", 25);
		}
	}
}

function cFunit(x, t, ci, cf, max,c)
{	
	var el = document.getElementById('v'+x);
	var ell = document.getElementById('h'+x);
	var grad = gradientF(cf,ci,max,x);
	
	if(x == 1)
	{
		var lel = document.getElementById('block_left');
		cBackcolor('block_left', cf);
		lel.name = c;
		
		var lel = document.getElementById('block_bottom');
		lel.name = 0;
		cBackcolor('block_bottom', cf);
	}
	
	if(x > 0)
	{
		cBackcolor('v'+x, grad);
		cBackcolor('h'+x, grad);
		setTimeout("cFunit("+(x-1)+","+t+",'"+ci+"','"+cf+"',"+max+","+c+")", t);
	}
}

function cCunit(x, t, ci, cf, max, c)
{	
	var el = document.getElementById('v'+x);
	var ell = document.getElementById('h'+x);
	var grad = gradientF(ci,cf,max,x);
	if(x == max)
	{
		var lel = document.getElementById('noblock');
		cBackcolor('noblock', cf);
		lel.name = c;
		var lel = document.getElementById('block_bottom');
		lel.name = 0;
	}
	
	if(x < (max+1))
	{
		cBackcolor('v'+x, grad);
		cBackcolor('h'+x, grad);
		setTimeout("cCunit("+(x+1)+","+t+",'"+ci+"','"+cf+"',"+max+","+c+")", t);
	}
}

function findColorsCF(colors)
{	
	var lel = document.getElementById('noblock');
	return lel.name;
}

function findColorsCC()
{	
	var lel = document.getElementById('block_left');
	return lel.name;
}


// Gradient functions
function separateRBG(color)
{
	var col = new Array(3);
	col[0] = parseInt(color.substring(0,2),16);
	col[1] = parseInt(color.substring(2,4),16);
	col[2] = parseInt(color.substring(4,6),16);
 
 	return col;
}

function gradientFG(color_init, color_fin, perc)
{
	var deb = separateRBG(color_init);
 	var fin = separateRBG(color_fin);

 	// calculate the red, the bleu, and the green gradient
 	var i = 0;
 	var j = 0;
 	var enc = new Array(3);
 	var num;
 	while(i < 3)
 	{
 		enc[i] = Math.abs((fin[i] - deb[i])) / 100;
 		if(deb[i] > fin[i])
 			num = Math.floor(deb[i] - (enc[i] * perc));
 		else
 			num = Math.floor(deb[i] + (enc[i] * perc));
 		enc[i] = pad(num.toString(16),2);
 		i = i + 1;
 	}
 	
  	// build the final gradient array
  	var gradientColor = enc[0] + enc[1] + enc[2];
  	
 	return gradientColor;
}

function gradientF(color_init, color_fin, n, act)
{
	var deb = separateRBG(color_init);
 	var fin = separateRBG(color_fin);

 	// calculate the red, the bleu, and the green gradient
 	var i = 0;
 	var j = 0;
 	var enc = new Array(3);
 	var num;
 	while(i < 3)
 	{
 		enc[i] = Math.abs((fin[i] - deb[i])) / n ;
 		if(deb[i] > fin[i])
 			num = Math.floor(deb[i] - (enc[i] * act));
 		else
 			num = Math.floor(deb[i] + (enc[i] * act));
 		enc[i] = pad(num.toString(16),2);
 		i = i + 1;
 	}
 	
  	// build the final gradient array
  	var gradientColor = enc[0] + enc[1] + enc[2];
  	
 	return gradientColor;
}

// Random number
function rand(min, max)
{
	var aleatorio = Math.floor(Math.random()*(max-(min-1))) + min;  
	return aleatorio;
}

// Change backgorund color for all browsers
function cBackcolor(elt, colorf)
{ 
	el = document.getElementById(elt);
	var col = el.style.backgroundColor;
	var colori;
	if(col.indexOf('#') == -1)
		colori = rgbConvert(col);
	else
		colori = col.replace("#","");

	setTimeout("cBackcolorD('"+elt+"','"+colori+"','"+colorf+"',"+v_percent+")", 1);
}

function rgbConvert(str) {
   str = str.replace(/rgb\(|\)/g, "").split(",");
   str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
   str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
   str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
   str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
   str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
   str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
   return (str.join(""));
}

function cBackcolorD(elt, colori, colorf, perc)
{
	if(perc < 100)
	{
		el = document.getElementById(elt);
		
		var c = '#' + gradientFG(colori, colorf, perc);
		el.style.backgroundColor = c;
	//	el = document.getElementById("block_bottom");
		//el.innerText = el.innerText + '#' + gradientFG(colori, colorf, perc);
		// '#' + gradientFG(colori, colorf, perc);
		
		setTimeout("cBackcolorD('"+elt+"','"+colori+"','"+colorf+"',"+(perc+v_percent)+")", v_refresh);
	}
	else
	{
		el = document.getElementById(elt);
		el.style.backgroundColor = '#' + colorf;
	}
}

function cBackcolorNow(elt, color)
{
	el = document.getElementById(elt);
	el.style.backgroundColor = '#' + color;
}

// Lpad for string, with 0 left
function pad(number, length) 
{   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

// Sleep
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}