/* -- body class -- */

// adds class to body (for CSS styles that depend on JavaScript)
function initBodyClass() {
	if (!document.getElementsByTagName) return false;
	var thebody = document.getElementsByTagName("body")[0];	
	
	thebody.className += " js";
}


/* -- compact form labels -- */

function hideLabel (field_id, hide) {
  if (!document.getElementsByTagName) return false;
  
  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 () {
  if (!document.getElementById) return;      

  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();
        }
      };

    }
  }
}


/* -- fancy tooltips -- */

function initTips() {
	var Tips1 = new Tips($$('.tips'), {
			maxTitleChars: 22
			,offsets: {'x': 8, 'y': 8}
		});	
}


/* -- mootools accordion (togglers) -- */

var a;


/* - init togglers - */
function initToggles() {
	var s = $$('.stretcher');

	// show stretchers (hidden by default)	
	for (i=0; i<s.length; i++) {
		s[i].style.visibility = 'visible';
	}
}
	
/* - close toggler - */
function closeNavToggle() {
	a.display(-1);	
}


/* - global nav togglers - */
function initNavToggles() {
	var s, t, o, i, tabs, link;
	
	s = $$('#nav .stretcher');
	t = $$('#nav .toggler');
	o = $('overlay');
	
	// setup overlay
	o.addEvent('click', closeNavToggle);
	
	// setup toggles
	a = new Accordion(t, s, {
		show: -1
		,alwaysHide: true
		,opacity: false		
		,duration: 'short'
		,onActive: function(tog, el){			 
			tog.addClass('toggler-open');
			
			// activate overlay (for current toggler)
			i = this.elements.indexOf(el);
			o.setStyles({visibility: 'visible', height: Window.getScrollHeight()+'px'});
			o.className = '';
			o.addClass(i);
			}
		,onBackground: function(tog, el){
			tog.removeClass('toggler-open');
			
			// remove overlay (for current toggler)
			i = this.elements.indexOf(el);
			if (o.className == i) { o.removeClass(i); o.setStyles({visibility: 'hidden'}); }
			}				
	});
	
	// inject close links
	s.each(function(el) {
		link = new Element('a');
		link.setProperty('href','#close');
		link.setProperty('title','Close');
		link.setProperty('class','close');
		link.addEvent('click', closeNavToggle);	
		link.addEvent('click', function(e) {
			e = new Event(e).stop();
		});		
	
		el.appendChild(link);
	});		

	// setup tabs
	if ($('nav-dash')) {
		tabs = new Yetii({
			id: 'nav-dash',
			activeclass: 'on'
		});
	}
	
	// cancel default link behavior
	t.addEvent('click', function(e) { e = new Event(e).stop(); });
}


/* -- network nav -- */

// init network nav togglers
function initNetworkToggles() {
	var a2, s, t, i;
	
	s = $$('#network .stretcher');
	t = $$('#network .toggler');
	
	// setup toggles
	a2 = new Accordion(t, s, {
		show: -1
		,alwaysHide: true
		,opacity: false		
		,duration: 'short'
		,onActive: function(tog, el){			 
			tog.addClass('toggler-open');
			}
		,onBackground: function(tog, el){
			tog.removeClass('toggler-open');
			}		
	});
	
	// cancel default link behavior
	t.addEvent('click', function(e) { e = new Event(e).stop(); });
}


/* -- 'latest from' nav -- */

// toggle 'latest from' content
function toggleLatestNav(theID) {
	var content = $$("#latest dd");
	
	// hide all content
	content.each(function(el) {
		el.style.left = "-9999px";
	});		
	
	// determine which content to show
	if (theID == "latest-everybody") {
		$('latest-everybody-content').style.left = "218px";
	} else {
		$('latest-friends-content').style.left = "218px";
	}
}

// set 'latest from' active state
function latestNavActive(links, activeLink) {
	links.each(function(el) {
		el.removeClass('on'); // remove 'on' class from all links
	});
	
	activeLink.addClass('on'); // add 'on' class to active link
}

// init 'latest from' nav
function initLatestNav() {
	var links = $$("#latest dt");
	
	links.each(function (el) {
		el.addEvent('click', function(e) {
			toggleLatestNav(this.id);
			latestNavActive(links, el);
		});
	});	
}


/* -- load scripts -- */

cbb.addEvent(window, 'load', cbb.init);
tablePaginater.addEvent(window, "load",   tablePaginater.init);
tablePaginater.addEvent(window, "unload", tablePaginater.onUnLoad);

window.addEvent('domready', function() {
	initBodyClass();
	initToggles();
	initNavToggles();
	initNetworkToggles();
	initOverLabels();
	initTips();
	initLatestNav();
});


/*  Open window script used in the How Do I section  */
function MM_openBrWindow(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}