/* -- body class -- */

// adds class to body (for CSS styles that depend on JavaScript)
function initBodyClass() {
	document.getElementsByTagName("body")[0].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,
		input,
		s = $$('.stretcher'),
		t = $$('.toggler');

	a = new Accordion(t, s, {
		display: -1,
		alwaysHide: true,
		opacity: false,
		onActive: function( toggler, element ) {
			toggler.addClass('open');
			element.addClass('open');
			
			input = element.getElements('input');
			
			try {
				// focus first input if available
				if( input ) {
					input[0].focus();	
				}
			} catch(e) {
			
			}
			
		},
		onBackground: function( toggler, element ) {
			toggler.removeClass('open');
			element.removeClass('open');
		}
	});
	
	// cancel default link behavior
	t.addEvent('click', function(e) {
		e = new Event(e).stop();
	});
}


/* -- mooTools Tips -- */
function initTips() {
	var tips = new Tips($$('.tips a[title]'), {
		text: ''
	});
}

/* -- map links -- */

function initMapLinks() {
	var 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){
		initBodyClass();
		initOverLabels();
		initAccordion();
		initMapLinks();
		initTips();
	}
});
