// Malone 2003
// Some crazy javascript ive cooked up
// Its a bit hacky but it should work




//first some handy little functions we might need
hexa = new Array(16); 
for(var i = 0; i < 10; i++){
  hexa[i] = i; 
}
hexa[10]="a"; hexa[11]="b"; hexa[12]="c"; 
hexa[13]="d"; hexa[14]="e"; hexa[15]="f"; 

function hex(i) { 
  
  if (i < 0){
   return "00"; 
  }else if (i > 255){
   return "ff"; 
  }else{
    return "" + hexa[Math.floor(i/16)] + hexa[Math.floor(i%16)];
  }
} 

// Converts #XXXXXX style color to RGB
function dehexize(Color){ 
  var colorArr = new Array(0,0,0); 
  for (i=1; i<7; i++){ 
    for (j=0; j<16; j++){ 
      if (Color.charAt(i) == hexa[j]){ 
        if (i%2 !=0) 
          colorArr[Math.floor((i-1)/2)]=eval(j)*16; 
        else 
          colorArr[Math.floor((i-1)/2)]+=eval(j); 
      } 
    } 
  } 
  return colorArr; 
} 

// Converts int,int,int style color to RGB
function dergb(Color){
  var colorArr = endColor;
  if(Color.length > 0){
    colorArr = Color.substring(4).split(",");
    for (var i=0;i < colorArr.length;i++){
      colorArr[i] = parseInt(colorArr[i]);
    }
  }
  return colorArr; 
} 

// IE  - #XXXXXX
// Moz - int,int,int
function colorarray(Color){
  if (document.all){ 
    return dehexize(Color);
  }else{
    return dergb(Color);
  }
}

// Debug function for showing an objects properties
function dump(object) {
  for (var i in object) {
   window.document.write('Property[' + i +'] Value['+object[i]+']<br>');
  }
}


function getcookie(name) {
  var start = document.cookie.indexOf(name+"=");
  var len = start+name.length+1;
  if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
  if (start == -1) return null;
  var end = document.cookie.indexOf(";",len);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
}

function loginbar(){
  var username = getcookie('csbname');
  if(username != null && getcookie('csbsesh') != null){
    window.document.write('Welcome back '+username.replace(/\+/g,' ')+' - <a href="http://fxhome.com/users/user_management/intro.php">Click to edit options</a> - <a href="http://fxhome.com/users/login/logout.php">Click to logout</a>');
    if(username == 'malone' || username == 'schwar' || username == 'Tarn'){
      //yes, it is password protected :P
      window.document.write(' - <a href="http://fxhome.com/admin/">admin</a>');
    }
  }else{
    window.document.write('<a href="http://fxhome.com/users/login/userlogin.php">Login</a> - <a href="http://fxhome.com/users/signup/signuppage.html">Signup with website</a>');
  }
}



//Fading background colors
startColor = "#3377FF";
endColor = "#143066";

stepIn = 30; // delay when fading in 
stepOut = 200; // delay when fading out 

startColor = dehexize(startColor.toLowerCase()); 
endColor = dehexize(endColor.toLowerCase()); 

var celement = null;
var showpops = true;
var fadeList = new Array();

// This function goes through the fadelist and updates the color for each item
function fade() {
  var continueloop = false;
  for (var i=0;i < fadeList.length;i++){
    if(fadeList[i] != null){
    
      fadeList[i][2] = fadeList[i][2] + fadeList[i][3];
      fadeList[i][4] = fadeList[i][4] + fadeList[i][5];
      fadeList[i][6] = fadeList[i][6] + fadeList[i][7];
    
      fadeList[i][0].style.backgroundColor = "#"+hex(fadeList[i][2])+hex(fadeList[i][4])+hex(fadeList[i][6]); 
    
      //fadeList[i][0].style.backgroundColor = "rgb("+fadeList[i][2]+","+fadeList[i][4]+","+fadeList[i][6]+")"; 
    
      fadeList[i][1]--;
      if(fadeList[i][1] < 1){
        fadeList[i] = null;
      }else{
        continueloop = true;
      }
    }
  }
  
  if(continueloop){
    setTimeout("fade()",10);
  }
}

//This function starts a fade
function addfade(element,fadefrom,fadeto,step){ 
  var running = false;
  var lastnull = fadeList.length;

  for (var i=0;i < fadeList.length;i++){
    celement = fadeList[i];
    if(celement != null){
      running = true;
      if(celement[0] == element){
        //update
        fadeList[i] = new Array(element,step,fadefrom[0],((fadeto[0]-fadefrom[0])/step),fadefrom[1],((fadeto[1]-fadefrom[1])/step),fadefrom[2],((fadeto[2]-fadefrom[2])/step));
    	return;
      }
    }else{
      lastnull = i;
    }
  }

  fadeList[lastnull] = new Array(element,step,fadefrom[0],((fadeto[0]-fadefrom[0])/step),fadefrom[1],((fadeto[1]-fadefrom[1])/step),fadefrom[2],((fadeto[2]-fadefrom[2])/step));
  
  if(!running){
    setTimeout("fade()",10);
  }
  return;

}


// POP UP MENUS

var popList = new Array();

function positionpopupmenu(menuname,parentname,xoffset,yoffset){
  var parentElement = document.getElementById(parentname);
  var popupmenu = document.getElementById(menuname);

  if(parentElement != null && popupmenu != null){
    var el = parentElement;
    do {
      xoffset += el.offsetLeft
    }while ((el = el.offsetParent));
  
    el = parentElement;
    do{
      yoffset += el.offsetTop;
    }while ((el = el.offsetParent))

    popupmenu.style.left = xoffset; //left-1;
    popupmenu.style.top = yoffset; //top + 18;
  }
}

function positionallpopupmenus(){
  for (var i=0;i < popList.length;i++){
    positionpopupmenu(popList[i][0],popList[i][1],popList[i][2],popList[i][3]);
  }
}

function addpopupmenu(menuname,parentname,xoffset,yoffset){
  popList[popList.length] = new Array(menuname,parentname,xoffset,yoffset);
}


//MOUSE OVER FUNCTIONS

function domouseover(element) { 
  while(element){
    if (element.className == "menu") {
   
      
	  addfade(element,colorarray(element.style.backgroundColor),startColor,stepIn);
      return;
    }
   
    element = element.parentNode;
  }

}

function domouseout(element) { 
  while(element){
    if (element.className == "menu") {
      
      addfade(element,colorarray(element.style.backgroundColor),endColor,stepIn);
      return;
    }
    
    element = element.parentNode;
  }
}

// IE and MOZ have different style event handlers routines
// These stubs format them properly for the real functions above
function domouseoverMOZ(event) { 
  domouseover(event.target);
} 

function domouseoutMOZ(event) { 
  domouseout(event.target);
} 

function domouseoverIE() { 
  domouseover(event.srcElement);
}

function domouseoutIE() { 
  domouseout(event.srcElement);
}

// HOOK into appropriate events
document.onmouseover = domouseoverMOZ; 
document.onmouseout = domouseoutMOZ; 

if (document.all){ 
  document.onmouseover = domouseoverIE; 
  document.onmouseout = domouseoutIE;
}


window.onresize = positionallpopupmenus;
document.onload = setTimeout('positionallpopupmenus()',100);


//MAIN POPUPS

//This probably shouldnt be in this file, but it is for the following reasons
// 1) Having it in a js means the client browser should cache it
// 2) If javascript is disabled the client wont even bother downloading it
// 3) Putting it here instead of a seperate js gets rid of one server request

function drawpopup(menuname,items){
  window.document.write('<table class="popupmenu" id="'+menuname+'pop" cellpadding="0" cellspacing="0">');
  
  for (var i=0;i < items.length - 1;i++){
    if(items[i][0] == '-spacer'){
      window.document.write('<tr><td class="menuspaceh"></td></tr>');
    }else{
      if(items[i][1].length > 1){
        window.document.write('<tr><td><a href="'+items[i][1]+'" class="mi"><div class="mi">'+items[i][0]+'</div></a></td></tr>');
      }else{
        window.document.write('<tr><td><div class="mi">'+items[i][0]+'</div></td></tr>');
      }
    }
  }
  window.document.write('<tr><td><a href="'+items[items.length - 1][1]+'" class="mi"><div class="bottommi">'+items[items.length - 1][0]+'</div></a></td></tr>');

  window.document.write('</table>');
}

//a fun wittle hack
//keeps positioning the pop ups for the first 60 seconds of page life
var keepposcount = 0;
function keeppositioningpopups(){
  keepposcount = keepposcount + 1;
  positionallpopupmenus();
  if(keepposcount < 60){
    setTimeout('keeppositioningpopups()',1000);
  }
}




//safari is a no show
if(navigator.userAgent.indexOf('afari') >= 0){
  showpops = false;
}



