// CONFIG
var ajaxMessageSlideSpeed = 100; // milliseconds
var ajaxMessageShowFor = 1000; // milliseconds
var ajaxMessageHeight = 20; // pixels


/* -- body class -- */

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


/* -- Fancy tooltips -- */

function initTips(){
	new Tips($$('.fav'), {
		maxTitleChars: 100
		, offsets: {'x': 8, 'y': 8}
	});
}


/* -- forums -- */

// converts the first <TD> to a link
function convertTDtoLink(theID){
	var columns, link;
	
	columns = $$('.forum table .tdlink');
	
	columns.each(function (el) {
		link = el.getElementsByTagName("a");

		if(link.length > 0){
			el.onclick = new Function("document.location.href='" + link[0].href + "'");
			el.className+=el.className?' linked':'linked';
			el.onmouseover=function() { this.className+=" hover"; }
			el.onmouseout=function() { this.className=this.className.replace(new RegExp(" hover\\b"), ""); }			
		}		
	});			
}


/* -- 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();
        }
      };

    }
  }
}


/* -- Ajax -- */

function showAjaxMessage(el) {
	var ajaxPanel = el.getParent();
	var hiddenMessage = ajaxPanel.getElements('span[class=hidden-ajax-message]')[0];
	var text = hiddenMessage.getText();
	hiddenMessage.remove(); // makes sure there is only one message at a time
	el.setText(text);
	var slideMessageIn = new Fx.Style(el, 'height', { duration: ajaxMessageSlideSpeed });
	slideMessageIn.addEvent('onComplete', function() {
		setTimeout(function() {
			var slideMessageIn = new Fx.Style(el, 'height', { duration: ajaxMessageSlideSpeed });
			slideMessageIn.addEvent('onComplete', function() {
				el.setStyle('display', 'none');
				el.setText('');
			});
			slideMessageIn.start(ajaxMessageHeight, 0);
		}, ajaxMessageShowFor);
	});
	slideMessageIn.start(0, ajaxMessageHeight);
}

function initAjaxPanels() {
	$('main').getElements('div[class=ajax-panel]').each(function(ajaxPanel) {
		ajaxPanel.getElements('a').each(function(alink){
			alink.removeEvents('click');
			alink.addEvent('click', function(e) {
				new Event(e).stop();
				var panelParent = ajaxPanel.getParent();
				var loaderDiv = panelParent.getElements('div[class=ajax-loader]')[0];
				loaderDiv.setStyle('display', 'block');
				loaderDiv.setStyle('height', panelParent.getSize().size.y);
				var messageDiv = panelParent.getElements('div[class=ajax-message]')[0];
				messageDiv.setStyle('display', 'block');
				new Ajax(alink.getProperty('href') + '1', {
					method: 'get',
					update: ajaxPanel,
					evalScripts: true,
					onComplete: function(link) {
						loaderDiv.setStyle('display', 'none');
						showAjaxMessage(messageDiv);
					}
				}).request();
			});
		});
	});
}


/* -- Tabs -- */
function initTabs() {
	var links = $$('.tabs ul a');

	links.each(function (el) {
		el.onclick = function(e) { new Event(e).stop(); }
	});

	var a = new Accordion(links, '.tab', {
		opacity: false
		,onActive: function(toggler, element){
			toggler.addClass('on');
			}
		,onBackground: function(toggler, element){
			toggler.removeClass('on');
			}	
	});
}


/* -- WebTrends file tracking -- */
function initWebTrendsTrack() {
	var links = $$('a.trackit');
	
	links.each(function (el) {
		el.onclick = function() { dcsMultiTrack('DCS.dcsuri', el.getAttribute("href",2), 'WT.ti', el.title); }
	});
}

/* -- Facebook Share Popup -- */
function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

/* -- call scripts -- */
window.addEvent('domready', function() {
	if (document.getElementById) {
		initBodyClass();
		initOverLabels();
		initAjaxPanels();
		initTabs();
		convertTDtoLink();
		/*initTips(); !!!! DISABLED FOR NOW pending further hax !!!! */
		initWebTrendsTrack();
	}
});
