﻿//SET GLOBALS 
var origRowColor
var origBorderColor

function set_inner_html(id,value)
{
  if(document.getElementById(id)) 
  {
    document.getElementById(id).innerHTML = value; 
  } 
}

function remove_elem(id)
{
	//watch for DUPLICATE id names on the page!!!!!!!
	if(document.getElementById(id))
	{
		var item = document.getElementById(id);
		item.parentNode.removeChild(item);
	}
}

function set_border_color(id,color)
{
  // REQUIRES that the ID element has a VALID class in the CSS AND needs border=1+
	if(document.getElementById(id))
	{
  	document.getElementById(id).style.borderColor = color;  
	}
}

function set_bg_color(id,color)
{
	if(document.getElementById(id))
	{
	  document.getElementById(id).style.backgroundColor = color;  
	}
}

/* ONLY works if style is set INSIDE THE ELEM, NOT in CSS FILE */
function store_row_color(id)
{
	if(document.getElementById(id))
	{
		origRowColor = document.getElementById(id).style.backgroundColor;	
	}
}

/* ONLY works if style is set INSIDE THE ELEM, NOT in CSS FILE */
function store_border_color(id)
{
	if(document.getElementById(id))
	{
		origBorderColor = document.getElementById(id).style.borderColor;	
	}
}

function pos_sub_div_under_parent_div(parId,newId)
{
 	//let's determine WHERE to put X coordinates...
	var L = getLeftPos(parId);
	var R = getRightPos(parId);
	var W = getWidth(parId);
	var w = getWidth(newId);
	var bodyW = getWidth('body');
	
	var x;
	var y;
	
	var diff = eval(bodyW - w - L);
	//if Lpos + elem width exceeds body, move it left until it fits
	if(diff >= 0)
	{
		x = L + 5;
	}
	else
	{
		x = eval(L + diff)- 5;
	}
	//alert('L '+ L + 'R '+R + 'W '+W + 'w '+w+' body '+bodyW+' diff '+diff+' new pos '+x);	
	y = getBottomPos(parId)+3;
	document.getElementById(newId).style.left = x + 'px';
	document.getElementById(newId).style.top = y + 'px';
	//position_div(newId,x,y);
}

function pos_sub_div_right_parent_div(parId,newId)
{
	var x = getRightPos(parId);
	var y = getTopPos(parId);
	document.getElementById(newId).style.left = x + 'px';
	document.getElementById(newId).style.top = y + 'px';
}

function create_new_float_div(id)
{
	var newDiv;
	var doc = document.getElementsByTagName("body")[0];
	if(!doc.id)
	{
		doc.id = 'body';
	}	
	//safety check to see if this ID already exists 
	remove_elem(id);
	
	newDiv = document.createElement("div");
	newDiv.id=id;
	
	doc.appendChild(newDiv); 

	//set up some key items, basically hide it for now, it has no info yet anyway
	document.getElementById(newDiv.id).style.visibility='hidden';
	document.getElementById(newDiv.id).style.overflow='hidden';
	document.getElementById(newDiv.id).style.zIndex=95;
	document.getElementById(newDiv.id).style.position='absolute';
	document.getElementById(newDiv.id).style.border='1px';
}

function grow_div_by_height(id)
{
	//REMEMBER:  only works if the div has overflow : hidden, either set in STYLE SHEET, OF if this 
	//is a NEW FLOAT DIV, then we SET when created IT !!!!!!!!!!
	
	//set std growth rate
	var delay = 3;
	var inc = 1;
	var h = getHeight(id);
	//alert('ht: '+h);
	set_height(id,1);
	//if building a large div, over 100px, speed up delay var, and increase inc by factor of 1x per 50
	var scope = (h / 100);
	if(scope >= 3)	//at least 300px high
	{
		inc = Math.round(inc * scope);
		delay = 1;
	}
	else if(scope >= 2)//at least 200px
	{
		inc = Math.round(inc * scope);
		delay = 2;
	}
	else if(scope >= 1)//at least 100px
	{
		//no change to delay var, only to inc var
		inc = Math.round(inc * scope);
	}

	//make visible (do not use toggle, just in case we are growing an already visible div)
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).style.display = 'block';
	
	//expand div to correct size, we use a std speed, but a variable increment amt.
	for(i=inc; i <= (h+inc+inc); i+=inc)
	{
		setTimeout("set_height('"+id+"',"+i+");",eval(delay*i));
	}
}

/* ONLY works if style is set INSIDE THE ELEM, NOT in CSS FILE */
function get_div_bg_color(id)
{
	if(document.getElementById(id))
	{
		return document.getElementById(id).style.backgroundColor;	
	}
}

//just the standard yellow fade
function yellow_splash(id)
{
	if(document.getElementById(id))
	{
		elem = document.getElementById(id)
		var origColor = get_div_bg_color(id);
  	var letter = "F";
  	var factor = 50;
		var time;
		
  	var i=1;
  	for(i=0;i<10;i++)
  	{
    	color = "#FFFF" + i + i;  
    	time = i * factor; 
     	setTimeout("set_bg_color('"+id+"','"+color+"')",time);
   	}

  	i=1;
  	while(i<=6)
  	{
    	if(i == 1) {letter = "A";}
    	else if(i == 2) {letter = "B";}
    	else if(i == 3) {letter = "C";}
    	else if(i == 4) {letter = "D";}
    	else if(i == 5) {letter = "E";}
    	else if(i == 6) {letter = "F";}
    	color = "#FFFF" + letter + letter; 
    	time = time + factor; 
     	setTimeout("set_bg_color('"+id+"','"+color+"')",time);  //fading white
    	i++;
  	}
   	setTimeout("set_bg_color('"+id+"','"+origColor+"')",(time+25));
	}//end if ID
}	//end func


function fade_color(id,type,speed)
{
	if(document.getElementById(id))
	{
  	var letter = "F";
  	var color = "";
  	var time = 0; //this gets added by every factor in between color shifts 
  	var factor = 65;  //delay between color shifts.  65ms*15colors = appx 1000ms (1 sec)
		//optional param is the speed... if not F,S,L, then use normal of 65
		if(speed == 'F')
		{
			factor = 35;
		}
		else if (speed == 'S')
		{
			factor = 200;
		}
		else if (speed == 'L')
		{
			//lightning
			factor = 10;
		}			
  	if(type == "") { type = "bg";}
  	var i=1;
  	while(i <= 9)
  	{
    	color = "#" + i + i + "FF" + i + i;  
    	time = time + factor; 
    
    	if(type == "border")
    	{
      	setTimeout("set_border_color('"+id+"','"+color+"')",time);
    	}
    	else
    	{
      	setTimeout("set_bg_color('"+id+"','"+color+"')",time);
    	}
    	i+=1;
  	}
  	i=1;
  	while(i<=6)
  	{
    	if(i == 1) {letter = "A";}
    	else if(i == 2) {letter = "B";}
    	else if(i == 3) {letter = "C";}
    	else if(i == 4) {letter = "D";}
    	else if(i == 5) {letter = "E";}
    	else if(i == 6) {letter = "F";}
    	color = "#" + letter + letter + "FF" + letter + letter; 
    	time = time + factor; 
    	if(type == "border")
    	{
      	setTimeout("set_border_color('"+id+"','"+color+"')",time);  //fading to white
    	}
    	else
    	{
      	setTimeout("set_bg_color('"+id+"','"+color+"')",time);  //fading white
    	}	
    	i+=1;
  	}
		if(type=="bg")
		{
			//return to the original color, if possible
			if(origRowColor != "")
			{
		 		//set AHEAD of the time factor else it gets overwritten
      	setTimeout("set_bg_color('"+id+"','"+origRowColor+"')",(time+25));
			}
		}
		if(type=="border")
		{
			//return to the original color, if possible
			if(origBorderColor != "")
			{
		 		//set AHEAD of the time factor else it gets overwritten
      	setTimeout("set_border_color('"+id+"','"+origBorderColor+"')",(time+25));
			}
		}
	}//end if ID
}	//end func