function contGetElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function controlEffects(className,openHeight,secondary,replacementControlText,isGalleryControl)
{
  var secondaryControl = (secondary) ? Dom.get(secondary) : false;
  var contentHeight = openHeight;
  var imgExt = (isLTEIE7) ? '.gif' : '.png';
  var replacementText = replacementControlText;
  var controls = contGetElementsByClassName(className,'div',document);
  
  var arrowUp = '/images/arrows/upArrow' + imgExt;
  var arrowDown = '/images/arrows/downArrow'  + imgExt;
  document.createElement('img').setAttribute('src',arrowUp);

    var control = contGetElementsByClassName("controls",'div',controls[0])[0];
	var controlText = control.firstChild.firstChild.nodeValue;
    var content = contGetElementsByClassName("theContent",'div',controls[0])[0];
    if(isGalleryControl) YAHOO.util.Dom.setStyle(content,'opacity','0.99');
    var controlImage = control.getElementsByTagName('img')[0];
    if(isLTEIE7) controlImage.src = controlImage.src.replace(/.png/, ".gif");
    var thisFade = {'in' : new YAHOO.util.Anim(controlImage, {opacity: {to: 0.99}}, 0.7, YAHOO.util.Easing['easeOut']), 'out': new YAHOO.util.Anim(controlImage, {opacity: {to: 0}}, 0.7, YAHOO.util.Easing['easeIn']), 'running': false};
    var thisMove = {'open': new YAHOO.util.Anim(content, {height: {to: contentHeight}}, 2.02, YAHOO.util.Easing['easeIn']), 'close': new YAHOO.util.Anim(content, {height: {to: 0}}, 2.02, YAHOO.util.Easing['easeOut']), 'opened': false, 'moving': false};
    thisFade['out'].onComplete.subscribe(function() { thisFade['in'].animate();});
    thisFade['in'].onComplete.subscribe(function() { if(thisFade['running']) setTimeout(function(){var fadeOut = thisFade['out']; fadeOut.animate();}, 700);});
    thisMove['open'].onComplete.subscribe(function() { thisMove['moving'] = false;thisMove['opened'] = true;updateControl(true);});
    thisMove['close'].onComplete.subscribe(function() { thisMove['moving'] = false;thisMove['opened'] = false;updateControl(false);});
    Event.add(control,'mouseover',function() {Style.set(control,'backgroundColor','#FFFFFF');thisFade['running'] = true;setTimeout(function(){if(thisFade['running']) thisFade['out'].animate();},200);});
    Event.add(control,'mouseout',function() {Style.set(control,'backgroundColor','#FACACB');thisFade['running'] = false;});
    Event.add(control,'click',function() { if(!thisMove['moving']){thisMove['moving'] = true;toggle(thisMove,controlImage[0]);}});
    if(secondaryControl)
    {
      Event.add(secondaryControl,'click',function() { if(!thisMove['moving']){thisMove['moving'] = true;toggle(thisMove,controlImage[0]);}});
      Event.add(secondaryControl,'mouseover',function() { Style.set(secondaryControl,'color','#FFFFFF');});
      Event.add(secondaryControl,'mouseout',function() { Style.set(secondaryControl,'color','#FACACB');});
      Style.set(secondaryControl,'cursor','pointer');
      
    }
    
    var fadeContentOut = new YAHOO.util.Anim(content, {opacity: {to: 0}}, 1.8, YAHOO.util.Easing['easeOut']);
    fadeContentOut.onComplete.subscribe(function(){YAHOO.util.Dom.setStyle(content,'height','0');YAHOO.util.Dom.setStyle(content,'opacity','0.99');updateControl(false);thisMove['opened']=false;thisMove['moving']=false;});
  
  function toggle(who,image)
  {
    if(who['opened'] )
    {
      who['close'].animate();
    }
    else
    {
      who['open'].animate();
    }
  }

  function updateControl(which)
  {
    Style.set(control.firstChild,'visibility','hidden');
    if(which)
    {
      changeImage(controlImage,arrowUp);
      control.firstChild.firstChild.nodeValue = replacementText;
    }
    else
    {
      changeImage(controlImage,arrowDown);
      control.firstChild.firstChild.nodeValue = controlText;
    }
    Style.set(control.firstChild,'visibility','visible');
  }

  function changeImage(elm,source)
  {
    elm.src = source;
  }

  this.isOpen = function(callback)
  {
    return(thisMove['opened']);
  }

  this.fadeOut = function()
  {
    if(thisMove['opened'] && !thisMove['moving'])
    {
      thisMove['moving'] = true;
      fadeContentOut.animate();
    }
  };
  return this;
}
