Event.observe(window, 'load',
function() {
	
  // do the feature nav
  if ($('nav_feature')) {
    var nav_items = $('nav_feature').getElementsByTagName('li');
    $A(nav_items).each(function(el) {
      // get the element id
      var ider = el.getAttribute('id');
			
      // make sure all links return false on click
      var el_link = el.getElementsByTagName('a');
			el_link[0].onclick = function() { return false; }
			
      var ider = el.getAttribute('id');
      Event.observe(el, 'click', feature_swap, false);
    });
  }
	
  // do the feature nav
  if ($('nav_feature_preview')) {
    var nav_items2 = $('nav_feature_preview').getElementsByTagName('li');
    $A(nav_items2).each(function(el) {
      // get the element id
      var ider = el.getAttribute('id');
			
      // make sure all links return false on click
      var el_link = el.getElementsByTagName('a');
			el_link[0].onclick = function() { return false; }
			
      var ider = el.getAttribute('id');
      Event.observe(el, 'click', feature_preview_swap, false);
    });
  }
  
  setupCompanyMenu();
  setupSupportMenu();
	
});

function feature_swap(evt) {
	
	// current element
	var which = getEventSource(evt).getAttribute('tag');
	
	// set all of the items back to unselected background
	if ($('nav_feature')) {
		var nav_items = $('nav_feature').getElementsByTagName('li');
		$A(nav_items).each( function(el) {
  		$(el).style.background = "url('img/public/other/power_button.png') no-repeat 10px 5px";
			// hover
			$(el).onmouseover = function() { el.style.background = "url('img/public/other/power_button_over.png') no-repeat"; }
		  // hover out
			$(el).onmouseout = function() { el.style.background = "url('img/public/other/power_button.png') no-repeat 10px 5px"; }
		});
	}
	// set the selected item to the selected background
	$(getEventSource(evt)).up('li')['style']['background'] = "url('img/public/other/power_button_on.png') no-repeat";
  $(getEventSource(evt)).up('li').onmouseover = function() { }
  $(getEventSource(evt)).up('li').onmouseout = function() { }
	
	// fade all open items, except selected div
	var nav_items = $('nav_feature').getElementsByTagName('li');
	$A(nav_items).each( function(el) {
		if (which != $(el).down('a').getAttribute('tag')) {
			if (!Element.hasClassName(el, 'hidden')) {
				new Effect.Fade('feature_' + $(el).down('a').getAttribute('tag'));
				Element.addClassName(el, 'hidden');
			}
		} else {
			Element.removeClassName(el, 'hidden');
		}
	});
	
	// find the current, load the content, then show it..
	// everything is one second which is enough time to load and flow nicely
	var d = new Date();
	new Ajax.Request("features/display/" + which,
		{
			method:'get',
			parameters:'',
			onComplete: processAjax.bindAsEventListener(this, $(getEventSource(evt)).getAttribute('tag'))
		}
	);
	
}

function feature_preview_swap(evt) {
	
	// current element
	var which = getEventSource(evt).getAttribute('tag');
	
	// set all of the items back to unselected background
	if ($('nav_feature_preview')) {
		var nav_items = $('nav_feature_preview').getElementsByTagName('li');
		$A(nav_items).each( function(el) {
		  // has to be set to nothing or :hover wont work
			$(el).style.background = '';
			//$(el).style.background = "url('img/public/other/power_button.png') no-repeat 10px 5px";
		});
	}
	// set the selected item to the selected background
	$(getEventSource(evt)).up('li')['style']['background'] = "url('" + web_root + "img/public/other/power_button_on.png') no-repeat";
	
	// fade all open items, except selected div
	var nav_items = $('nav_feature_preview').getElementsByTagName('li');
	$A(nav_items).each( function(el) {
		if (which != $(el).down('a').getAttribute('tag')) {
			if (!Element.hasClassName(el, 'hidden')) {
				new Effect.Fade('feature_preview_' + $(el).down('a').getAttribute('tag'));
				Element.addClassName(el, 'hidden');
			}
		} else {
			Element.removeClassName(el, 'hidden');
		}
	});
	
	// find the current, load the content, then show it..
	// everything is one second which is enough time to load and flow nicely
	var d = new Date();
	new Ajax.Request(web_root + "features/display/" + which,
		{
			method:'get',
			parameters:'',
			onComplete: processAjax_preview.bindAsEventListener(this, $(getEventSource(evt)).getAttribute('tag'))
		}
	);
	
}

function processAjax(request, which) {
	if ($('feature_content')) {
		// pause added
		// IE 6 couldn't render twice
	
		setTimeout(function() {
			// change the content of the div
			html_start = "<div id='feature_" + which + "' style='display: none; padding: 12px;'>";
			html_end = "</div>";
			$('feature_content').innerHTML = html_start + request.responseText + html_end;
			
			new Effect.Appear("feature_" + which);
		
			}, 600);
	}
}

function processAjax_preview(request, which) {
	if ($('feature_preview_content')) {
		// pause added
		// IE 6 couldn't render twice
		setTimeout(function() {
			// change the content of the div
			html_start = "<div id='feature_preview_" + which + "' style='display: none; padding: 12px;'>";
			html_end = "</div>";
			$('feature_preview_content').innerHTML = html_start + request.responseText + html_end;
			
			new Effect.Appear("feature_preview_" + which);
		
			}, 600);
	}
}


/* ----------------------------------------------------------------
	HELPERS
---------------------------------------------------------------- */

function getEventSource(e) {
	if (!e) e = window.event;
	if (e.originalTarget)
		return e.originalTarget;
	else if (e.srcElement)
	return e.srcElement;
}