function showScheduleDetail(owner, target) {
		$(owner).addClassName('schedule-box-hover');
		$(target).show();
}

function hideScheduleDetail(owner, target) {
		$(owner).removeClassName('schedule-box-hover');
		$(target).hide();
}

function showZipSearchForm() {
		$('zip-search').addClassName('visible');
		$('zip-search-form').show();
}

function hideZipSearchForm() {
		$('zip-search').removeClassName('visible');
		$('zip-search-form').hide();
}

function clearMe(obj, text) {
				if(obj.value == text) {
								obj.value = '';
				}
}

function restoreMe(obj, text) {
				if(obj.value == '') {
								obj.value = text;
				}
}

function adjustTextarea(textarea, collapsed) {
  var lines = textarea.value.split("\n");
  var count = lines.length;
  lines.each(function(line) { count += parseInt(line.length / 70); });

  var rows = parseInt(collapsed / 20);

  if (count > rows) {
    textarea.style.height = (collapsed * 2) + 'px';
  }

  if (count <= rows) {
    textarea.style.height = collapsed + 'px';
  }
}

function setMonth(month) {
	$('month-link').removeClassName('january');
	$('month-link').removeClassName('february');
	$('month-link').removeClassName('march');
	$('month-link').removeClassName('april');
	$('month-link').removeClassName('may');
	$('month-link').removeClassName('june');
	$('month-link').removeClassName('july');
	$('month-link').removeClassName('august');
	$('month-link').removeClassName('september');
	$('month-link').removeClassName('october');
	$('month-link').removeClassName('november');
	$('month-link').removeClassName('december');
	$('month-link').addClassName(month);
}

function setSearchOption(option, option_input) {
	$('search-new').removeClassName('selected');
	$('search-refine').removeClassName('selected');
	$('search-web').removeClassName('selected');
	$('search_new').value = 0;
	$('search_refine').value = 0;
	$('search_web').value = 0;
	$(option).addClassName('selected');
	$(option_input).value = 1;
}

document.observe('click', function(e) {
	var target = $(e.target);
	
  // first check if an element exists on the page, then hide the element if we're clicking outside of it
	if ($('timezone-picker') && !target.up('#timezones')) 
	  Element.hide('timezone-picker');
	
	if ($('month-picker') && !target.up('#months')) 
	  Element.hide('month-picker');
	
	if ($('genre-picker') && !target.up('#genres-div')) 
	  Element.hide('genre-picker');
  	  
  if ($('zip-search')) {
    if (!(target.up('#zip-search'))) {
      hideZipSearchForm();
    }
  }
	if ($('minischedule')) {
    if (!(target.up('#minischedule'))) {
      $("minischedule-options").hide();
    }
  }
})

// http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/

function getElementsByClassName(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 searchCloudSelectGenre(genre, subgenre) {
	var children = $$('a.searchcloud');
	children.each(function(child) {
		if (child.hasClassName('genre-' + genre)) {
	 		child.addClassName("selected");
		} else {
			child.removeClassName("selected");
		}
	});
	var children = $$('li.subnav-links');
	children.each(function(child) {
		child.removeClassName("selected");
	});
	$('genre-' + genre + '-link').addClassName('selected');
	if (genre == 'all') {
		searchCloudSetVisibleResults('', '');
	} else {
		searchCloudSetVisibleResults(genre, subgenre);
	}
	// scrollbarGlobal comes from from scroll.js
	dw_Scrollbar_Co.resetBars(scrollbarGlobal)
	scrollbarGlobal.shiftTo(0,0)
}

// If the subgenre is specified, then the person must have the subgenre; if just 
// the genre is specified, then the person must have the genre; otherwise show everyone.
function searchCloudSetVisibleResults(genre, subgenre) {
  var resultsBox = $('programming-box-list-ul');
  if( resultsBox ) {
    if( subgenre ) {
      resultsBox.childElements().each( function(child) {
        if( child.hasAttribute( 'subgenre' ) && ( child.getAttribute( 'subgenre' ) == subgenre ) ) {
          child.show();
        } else {
          child.hide();
        }
      });
    } else if( genre ) {
      resultsBox.childElements().each( function(child) {
        if( child.hasAttribute( 'genre' ) && ( child.getAttribute( 'genre' ) == genre ) ) {
          child.show();
        } else {
          child.hide();
        }
      });
    } else {
      resultsBox.childElements().each( function(child) {
        child.show();
      });
    }
  }
}

function displayMinischeduleMenu() {
	$("minischedule-options").show();
}

function selectMinischeduleOption(option) {
	$('minischedule-selected').innerHTML = option;
	$("minischedule-options").hide();
}

// Silly workaround for the IE "Press OK to active content" when inserting flash.
function insertHTML(id, html) {
  $(id).innerHTML = html;
}

// Observe mouseEnterLeave on mouseover/mouseout
var mouseEnterLeave = function(e) {
    var rel = e.relatedTarget, cur = e.currentTarget;
    if (rel && rel.nodeType == 3) {
        rel = rel.parentNode;
    }
    if(
        // Outside window
        rel == undefined ||
        // Firefox/other XUL app chrome
        (rel.tagName && rel.tagName.match(/^xul\:/i)) ||
        // Some external element
        (rel && rel != cur && rel.descendantOf && !rel.descendantOf(cur))
    ) {
        e.currentTarget.fire('mouse:' + this, e);
        return;
    }
};

var curtainAd = {
    timeout : null,
    showCurtain : function(){
        clearTimeout(this.timeout);
        if($('ad-curtain-feature').style.display == 'none'){
            this.timeout = setTimeout(function() {new Effect.Appear('ad-curtain-feature', { duration: 0.75, queue: { position: 'end', scope: 'curtainscope', limit: 2 } }); },1000);
        }
    },
    hideCurtain : function(){
        if($('ad-curtain-feature').style.display == 'none'){
            clearTimeout(this.timeout);
        } else {
            this.timeout = setTimeout(function(){ new Effect.Fade('ad-curtain-feature', { duration: 0.75, queue: { position: 'end', scope: 'curtainscope', limit: 2 } }); },300);
        }
    }    
}