// main function to process the fade request //
/*function errorMessage(text)
{
	//alert("errorMessage");
	//alert(document.getElementById("error"));
	//document.getElementById("error").value = text + "\n";
}*/

function colorFade(end) {
 
  changeAColor(end);

  var id = 'bgcolour';
  var element = 'background';
 
  //remove the rgb() from the colour property
  
  var newstart = document.getElementById('bgcolour').style.backgroundColor.slice(4);  
  newstart = newstart.slice(0, newstart.length - 1);  
  
  //split the R G B
  var newstartarray = newstart.split(',');
  
  var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
  var target = document.getElementById(id);
  
  target.r = newstartarray[0];
  target.g = newstartarray[1];
  target.b = newstartarray[2];
  
  var steps = 10;
  var speed = 10;
  if(target.timer) 
	clearInterval(target.timer);
  
  endrgb = colorConv(end);
  
  er = endrgb[0];
  eg = endrgb[1];
  eb = endrgb[2];
  
  rint = Math.round(Math.abs(target.r-er)/steps);
  gint = Math.round(Math.abs(target.g-eg)/steps);
  bint = Math.round(Math.abs(target.b-eb)/steps);

  if(rint == 0) { rint = 1 }
  if(gint == 0) { gint = 1 }
  if(bint == 0) { bint = 1 }
  target.step = 1;
  target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
  
  
  
}

// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
	
  var target = document.getElementById(id);
  var color;
  
  if(target.step <= steps) {
    var r = target.r;
    var g = target.g;
    var b = target.b;
    if(r >= er) {
      r = r - rint;
    } else {
      r = parseInt(r) + parseInt(rint);
    }
    if(g >= eg) {
      g = g - gint;
    } else {
      g = parseInt(g) + parseInt(gint);
    }
    if(b >= eb) {
      b = b - bint;
    } else {
      b = parseInt(b) + parseInt(bint);
    }
   
    color = 'rgb(' + r + ',' + g + ',' + b + ')';    
	
	if(element == 'background') {      
	  if(color.length > 8){
		target.style.backgroundColor = color;	 
		changeLineColor(color);
	  }
    }else {
      if(color.length > 8){
		target.style.color = color;
	  }
    }
    target.r = r;
    target.g = g;
    target.b = b;
    target.step = target.step + 1;
  } else {
    clearInterval(target.timer);
    color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
	  
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
  }
  
  //changeLineColor(color);
}

// convert the color to rgb from hex
function colorConv(color) {	
  var rgb = [parseInt(color.substring(0,2),16), 
    parseInt(color.substring(2,4),16), 
    parseInt(color.substring(4,6),16)];
  return rgb;  
}

//Add fade to any element
function changeLineColor(color){

	var newColor = color; 	//'rgb(' + colorConv(color) + ')';  <-- need this for manual change		
	// Change UL border color
	var theTags = document.getElementsByTagName("ul");	
	for(var i=0; i<theTags.length; i++){
		var nodeObj = theTags.item(i);
		nodeObj.style.borderColor = newColor;
	}
	// Change P border color
	var theTags = document.getElementsByTagName("p");	
	for(var i=0; i<theTags.length; i++){
		var nodeObj = theTags.item(i);
		nodeObj.style.borderColor = newColor;
	}	
	// Change title colors
	var theTags = document.getElementsByTagName("div");		
	for(var i=0; i<theTags.length; i++){		
		var nodeObj = theTags.item(i);
		//alert(nodeObj.className);
		if(nodeObj.className == "transparentTtlBorder"){
			nodeObj.style.backgroundColor = newColor;
		}
		if(nodeObj.className == "transparentTtl ttlWhatsNew" || nodeObj.className == "transparentTtl ttlLatestWork" || nodeObj.className == "transparentTtl ttlContactUs"){
			nodeObj.style.backgroundColor = newColor;
		}
		//Change request a quote color
		if(nodeObj.className == "RequestQuote"){
			nodeObj.style.backgroundColor = newColor;
		}		
	}
    theTags = null;
}

//Change the a:hover color	
function changeAColor(color){	
	if(color == "CC0000"){	
		var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			//nodeObj.setAttribute("class", "aTagCC0000");
			nodeObj.className = "aTagCC0000";			
		}
	} 
	if(color == "017CB3"){		
		var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			nodeObj.className = "aTag017CB3";
		}
	}
	if(color == "FFFFFF"){	
		var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			nodeObj.className = "aTagAmia";			
		}
	}
	if(color == "af663d"){	
		var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			nodeObj.className = "aTagVast";			
		}
	}
	if(color == "181B4C"){	
		var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			nodeObj.className = "aTagXtreme";			
		}
	}
        if(color == "933f75"){	
		var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			nodeObj.className = "aTagMWD";			
		}
	}
       if(color == "50ace9"){
                var theTags = document.getElementsByTagName("a");	
		for(var i=0; i<theTags.length; i++){						
			var nodeObj = theTags.item(i);	
			nodeObj.className = "aAest";			
		}
       }

	theTags = null;
}






