var ContentHeight = 194;
var TimeToSlide = 250.0;

var openAccordion = '';
var openHdr = '';
var prevMktTab = '';
var prevCommTab = '';
var prevCurrencyTab = '';
var prevType = '';
var prevTab = '';

function runAccordion(index)
{
  var hdrId = "Accordion" + index;
  var cntID = hdrId + "Content";

  $(hdrId).className = 'cnn_mtpactive';

  if(openAccordion == cntID)
	  cntID = '';

  if(openHdr) {
	  $(openHdr).className = '';
  }

  setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'" 
      + openAccordion + "','" + cntID + "','" + hdrId + "')", 33);
  
  openAccordion = cntID;
  openHdr = hdrId;
  
// Handles setting/clearing input box for currency converter
  var formInputs = document.getElementsByTagName('input');
  for (var i = 0; i < formInputs.length; i++) {
      var theInput = formInputs[i];
      
      if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
          /* Add event handlers */          
          addEvent(theInput, 'focus', clearDefaultText, false);
          addEvent(theInput, 'blur', replaceDefaultText, false);
          
          /* Save the current value */
          if (theInput.value != '') {
              theInput.defaultText = theInput.value;
          }
      }
  }
  
}

function tabChange(tab,type)
{

  var tabId = tab + type;
  var tabBtn = tabId + "Btn";
  
  // Initializes & Stores sub tabs
  switch (type) {
  case 'Market':
	  prevTab = prevMktTab;
	  if (prevMktTab) {
		  prevMktTab = tab;
	  } else {
		  prevMktTab = tab;
		  prevTab = 'asia';
	  }
  break;
  case 'Comm':
	  prevTab = prevCommTab;
	  if (prevCommTab) {
		  prevCommTab = tab;
	  } else {
		  prevCommTab = tab;
		  prevTab = 'energy';
	  }	  
  break;
  case 'Currency':
	  prevTab = prevCurrencyTab;
	  if (prevCurrencyTab) {
		  prevCurrencyTab = tab;
	  } else {
		  prevCurrencyTab = tab;
		  prevTab = 'euro';
	  }
  break;
  }
   
  // Sets previous tabid & button id to disable buttons and show content
  var prevTabId = prevTab + type;
  var prevTabBtn = prevTabId + "Btn";

  // Set new tabs to 'On'
  $(tabBtn).className = 'toggle togOn';
  $(tabId).className = 'tab pmOn';
  
  // Set previous tabs to 'Off'
  $(prevTabBtn).className = 'toggle togOff';
  $(prevTabId).className = 'tab pmOff';
 
  prevTab = tab;
  prevType = type;

}

function animate(lastTick, timeLeft, closingId, openingId, titleId)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var opening = (openingId == '') ? null : document.getElementById(openingId);
  var closing = (closingId == '') ? null : document.getElementById(closingId);
	
  if(timeLeft <= elapsedTicks)
  {
    if(opening != null)
    {
        opening.style.height = ContentHeight + 'px';
    }
    
    if(closing != null)
    {
      closing.style.display = 'none';
      closing.style.height = '0px';
    }
    return;
  }
 
  timeLeft -= elapsedTicks;
  var newClosedHeight = Math.round((timeLeft/TimeToSlide) * ContentHeight);

  if(opening != null)
  {
    if(opening.style.display != 'block')
      opening.style.display = 'block';
    	opening.style.height = (ContentHeight - newClosedHeight) + 'px';
  }
  
  if(closing != null)
    closing.style.height = newClosedHeight + 'px';

  setTimeout("animate(" + curTick + "," + timeLeft + ",'" 
      + closingId + "','" + openingId + "','" + titleId + "')", 33);
}

function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    target.style.color = '#000';
    
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;

    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.style.color = '#999';
        target.value = target.defaultText;
    }
}
