/* -- body class -- */

// adds class to body (for CSS styles that depend on JavaScript)
function initBodyClass(){
	var thebody = document.getElementsByTagName("body")[0];	
	thebody.className += " js";
}


/* -- compact form labels -- */

function hideLabel(field_id, hide){
  var field_for, labels, i;
  labels = document.getElementsByTagName('label' );
  for (i=0; i < labels.length; i++){
    field_for = labels[i].htmlFor || labels[i].getAttribute('for' );
    if(field_for == field_id){
      labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
      return true;
    }
  }
}

function initOverLabels(){
  var labels, id, field, i;

  // Set focus and blur handlers to hide and show labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label' );
  for (i=0; i < labels.length; i++){

    if(labels[i].className == 'overlabel' ){

      // Skip labels that do not have a named association with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for' );
      if(!id || !(field = document.getElementById(id)) ){
        continue;
      } 

      // Change the applied class to hover the label over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if(field.value !== '' ){
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function (){
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function (){
        if(this.value === '' ){
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to label elements (for Safari).
      labels[i].onclick = function (){
        var id, field;
        id = this.getAttribute('for' );
        if(id && (field = document.getElementById(id)) ){
          field.focus();
        }
      };

    }
  }
}


/* -- mooTools Accordions -- */

function initAccordion(){
	var a, s, t;
	
	s = $$('.stretcher' );
	t = $$('.toggler' );

	a = new Accordion(t, s, {
		show: -1
		,alwaysHide: true
		,opacity: false
		,onActive: function(toggler, element){
			toggler.addClass('open' );
			}
		,onBackground: function(toggler, element){
			toggler.removeClass('open' );
			}
	});
	
	// cancel default link behavior
	t.addEvent('click', function(e){ e = new Event(e).stop(); });	
}
function initSearchAccordion(){
	var a, s, t, input;
	
	s = $$('#nav-util form' );
	t = $$('#util-search a' );
	input = $('s' );

	a = new Accordion(t, s, {
		show: -1
		,alwaysHide: true
		,opacity: false
		,onActive: function(toggler, element){
			toggler.addClass('open' );
			//input.focus(); 
			}
		,onBackground: function(toggler, element){
			toggler.removeClass('open' );
			}
	});
	
	// cancel default link behavior
	t.addEvent('click', function(e){ e = new Event(e).stop(); });	
}


/* -- map links -- */

function initMapLinks(){
	var entries, anchor, anchorText;
	
	entries = $$('#map-lst .btn' );
	
	entries.each(function (el){
		
		// create <a>
		anchor = document.createElement('a' );
		anchorText = document.createTextNode('View on map' );
		anchor.className = 'map';
		anchor.setAttribute( 'href', '#media' );	
		
		anchor.onclick = function(){
			val = el.id.replace( "mk-", "" );
			
			mapClick('marker'+val); 
			if(el.id.replace("mk-","") < 2){ 
				
				return false; 
			};
		};
		
		anchor.appendChild(anchorText);
		// add <a> to DOM
		el.appendChild(anchor);
	});	
	
}

/* -- load scripts -- */

window.addEvent('domready', function(){
	if(document.getElementById){
		initOverLabels();
		initAccordion();
		initSearchAccordion();
		initMapLinks();
		initBodyClass();
	}
});