// "substitutes" fade animation functions //////////////////////////////


// called on page load - starts the "throb" animation on substituted values
function substitutesAnimationStart(){

  // find all the elements that need fading and store them as a global array
  window.substituteElements = getElementsByClassName( document, "*", "substitute" );

  // create two fades
  var fade1 = colourCreateFade( {r:251,g:177,b:28}, {r:255,g:255,b:255}, 10 );
  var fade2 = colourCreateFade( {r:255,g:255,b:255}, {r:251,g:177,b:28}, 10 );

  // join the two fades together and store them in a global variable
  window.substitutesAnimationSteps = $A( [ fade1, fade2 ] ).flatten();

  // start at the first step
  window.substitutesAnimationStepCount = 0;
  window.substitutesAnimationStepTime = 50;

  // start the animation
  substitutesAnimationStep();
}


// perform a single step in the fade
function substitutesAnimationStep(){
  // if there are still steps left to do in the fade
  if( window.substitutesAnimationStepCount < window.substitutesAnimationSteps.length ){
    // find out what the next colour should be and apply it to all the substitutes
    var newColour = window.substitutesAnimationSteps[window.substitutesAnimationStepCount];
    substitutesSetBackground( newColour );

    window.substitutesAnimationStepCount++;

    // schedule the next step
    setTimeout( substitutesAnimationStep, window.substitutesAnimationStepTime );
  }
}



// set the background colour of all the substituted elements
function substitutesSetBackground( hexColour ){
  for( var i = 0; i < window.substituteElements.length; ++i ){
    var element = window.substituteElements[i];
    element.style.backgroundColor = hexColour;
  }
}


////////////////////////////////////////////////////////////////////////

// set calls handler for sorting search results
function sortSearchResults(element){
  var sortUrl = "/fcfalcon/page/search/sortsearchresults.page?sortOption="+ element.value;
  document.location.replace( sortUrl );
}

//generate content for a alternate flight sticky via ajax call and displays sticky
function stickyShowAlternateFlights( owner, elementId, url) {
  // make sure our arguments are nodes, not just IDs
    var sticky = $( elementId );
  var alternativeFlightGenerated = "alternativeFlightGenerated_" + elementId;

  //TODO can we generically register a content generator?
  // if sticky does not exist then create a new one
 // if( sticky == null ){
 // 	generateAlternateFlightsContent(sticky, url);
//  }


  if( $( alternativeFlightGenerated ) == null ){
   generateAlternateFlightsContent(sticky, url);
  }

  stickyShow(owner, elementId);

}

//generate content for a alternate flight sticky via ajax
//elementId is the id for the new div container that will be used in the Ajax call.
function generateAlternateFlightsContent(sticky, url) {
  //create new div using the given elemntId as the div id
//	var tmpElem1 = document.createElement('div');



// 	tmpElem1.setAttribute('class','alternativeFlightsOverlay');
// 	tmpElem1.style.zIndex = "999";
   //tmpElem1.style.position= "absolute";
//    document.body.appendChild(tmpElem1);

  // make Ajax call, provide the container id which is the the new div created and target url


  containerUpdater(sticky,url);

}


