/** Animation Codes */
var animation_rate = 50;
var animation_map = new Array();
var global_animation_timer = null; // self.setInterval("process_animation()", animation_rate);

/**
  Using one timer now. This function is to play all animations
**/
function process_animation() {
  try {
    //timer
    for (key in animation_map) {
      var a_obj = animation_map[key];
      if (a_obj !=null ) {
        if (a_obj[0] == "slide") {
          slide(document.getElementById(key), a_obj[1], a_obj[2], a_obj[3], a_obj[4], a_obj[5]);
        } else if (a_obj[0] == "fade") {
          fade_action(document.getElementById(key), a_obj[1], a_obj[2], a_obj[3], a_obj[4], a_obj[5]);
        }
      }
    }
  } catch (err) {}
}

/**
  Easing function: regular easeout
**/
function easeout(t,b,c,d){
  var k = t/d;
  return -c * k * (k - 2) + b;
}

/**
  Update animation obj alpha/Opacity for each frame
**/
function fade_action(elm, start_time, start, target, duration, callback) {
  //console.log ('fade action');
  var d = new Date();
  var current_time = d.getTime();
  var final_time = start_time+duration;
  var done = false;
  if (current_time >= final_time) {
    current_time = final_time;
    done = true;
  }

  updateOpacity(elm, easeout(current_time-start_time, start, target, duration));
  if (done) {
    animation_map[elm.id] = null;
    self.clearInterval(global_animation_timer)
    global_animation_timer = null;
    //animation_map.splice(elm.id, 1);
    if (callback)
      callback();
    callback = null;
  }
}


/** Slide an object to a different position
    elm:    the element to move
    direction: fadeIn, fadeOut
    duration: the animation duration
    callback: the callback function when slide is completed
*/
function fade(elm, start_time, direction, duration, callback) {
  try {
  	
    var start = getInitialOpacity(elm, direction);
    var target = 0;
    
    //console.log('calling fade:' + direction + "\t" + "\t" + duration + "\t" + start + "\t" + target + "\t" + elm);
    //console.log('calling fade');
     
    if (direction == "fadeIn")
      target = 1;
     
    // A new fade request as come in, we stop the previous fade animation on elm
    if (animation_map[elm.id] != null)
      animation_map[elm.id]= null;

                             //t, b, c, d
    animation_map[elm.id] = ["fade", start_time, start, target-start, duration, callback];
    if(global_animation_timer ==null)
	    global_animation_timer = self.setInterval("process_animation()", animation_rate);
  } catch(err) {//console.log('fade error');
  }
} // function fade
   
function parseIEOpacity(filterString) {
  try {
    var start = filterString.indexOf("(opacity=");
    var end = filterString.indexOf(")");
    var numString = filterString.substring(start+9, end);
    return parseFloat(numString) / 100;
  } catch(err) {}
} // parseIEOpacity
 
function getInitialOpacity(elm, direction) {
  var start;
  try {        
    if (elm.filters) { // IE
      if (elm.style.filter == "") { 
        //If style opacity value is missing, we initalize it
        if (direction == "fadeIn") {
          setIEOpacity(elm, 0);
          start = 0;
        } else {
          setIEOpacity(elm, 1);
          start = 1;
        } // if (direction == "fadeIn")
      } else {
        start = parseIEOpacity(elm.style.filter);
      } // if (elm.style.filter == "")
    } else { // Other browsers
      if (elm.style.opacity == "") { 
        //If style opacity value is missing, we initalize it
        if (direction == "fadeIn") {
          elm.style.opacity = 0;
          start = 0;
        } else { 
          elm.style.opacity = 1;
          start = 1;
        } // if (direction == "fadeIn") 
      } else {
        start = parseFloat(elm.style.opacity);
      } // if (elm.style.opacity == "")
    } // if(elm.filters)
    
    return start;
  } catch(err) {}
} //function getInitialOpacity  

function setIEOpacity(elm, value) {
  try {
    var elem = elm.style;
    // IE has trouble with opacity if it does not have layout
    // Force it by setting the zoom level
    elem.zoom = 1;

    // Set the alpha filter to set the opacity
    elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
                  (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
  } catch(err) {}
} // function setIDOpacity
      
function updateOpacity(elm, value) {
  try {
    if (elm.filters) { //IE
      setIEOpacity(elm, value);
    } else {
      elm.style.opacity = value;
    }
  } catch(err) {//console.log ('error in update opacity')
  }
} // function play_animation