/* ------------------------------ DETECTION ------------------------------ */

function detect() {
	// simplify things
	var agent 		= navigator.userAgent.toLowerCase();
	
	// detect platform
	this.isMac		= (agent.indexOf('mac') != -1);
	this.isWin		= (agent.indexOf('win') != -1);
	this.isWin2k	= (this.isWin && (
			agent.indexOf('nt 5') != -1));
	this.isWinSP2	= (this.isWin && (
			agent.indexOf('xp') != -1 || 
			agent.indexOf('sv1') != -1));
	this.isOther	= (
			agent.indexOf('unix') != -1 || 
			agent.indexOf('sunos') != -1 || 
			agent.indexOf('bsd') != -1 ||
			agent.indexOf('x11') != -1 || 
			agent.indexOf('linux') != -1);
	
	// detect browser
	this.isSafari	= (agent.indexOf('safari') != -1);
	this.isSafari2 	= (this.isSafari && (parseFloat(agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).substring(0,agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).indexOf(' '))) >=  300));
	this.isOpera	= (agent.indexOf('opera') != -1);
	this.isNN		= (agent.indexOf('netscape') != -1);
	this.isIE		= (agent.indexOf('msie') != -1);
	this.isFirefox	= (agent.indexOf('firefox') != -1);
}
var browser = new detect();

/* ------------------------------ EVENT HOOKS ------------------------------ */

function addEvents( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvents( obj, type, fn ) {
	if (obj.removeEventListener) {
		obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

function attachEventListener(target, eventType, functionRef, capture) {    
	if (typeof target.addEventListener != "undefined") {
		target.addEventListener(eventType, functionRef, capture);
	} else if (typeof target.attachEvent != "undefined") {
		target.attachEvent("on" + eventType, functionRef);
	} else {
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function") {
			var oldListener = target[eventType];

			target[eventType] = function() {
				oldListener();

				return functionRef();
			};
		} else {
			target[eventType] = functionRef;
		}
	}	
	return true;	
}

function detachEventListener(target, eventType, functionRef, capture) {
	if (typeof target.removeEventListener != "undefined") {
		target.removeEventListener(eventType, functionRef, capture);
	} else if (typeof target.detachEvent != "undefined") {
		target.detachEvent("on" + eventType, functionRef);
	} else {
		target["on" + eventType] = null;
	}
	return true;
}

function getEventTarget(event) {
  var targetElement = null;

  if (typeof event.target != "undefined") {
    targetElement = event.target;
  } else {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

/* ------------------------------ DOM ------------------------------ */

function getCoordinates(element){
	var obj = {
		'width': element.offsetWidth,
		'height': element.offsetHeight,
		'left': element.offsetLeft,
		'top': element.offsetLeft
	};
	obj.right = obj.left + obj.width;
	obj.bottom = obj.top + obj.height;
	return obj;
}

/* ------------------------------ NAVIGATION ------------------------------ */

function navigationInit() {
	var mainnav = document.getElementById('mainnav');
	
	// Grab all the <ul>'s inside the mainnav div.
	var ul_array = document.getElementById('mainnav').getElementsByTagName('ul');
	var li_array = ul_array[0].getElementsByTagName('li');
	
	// Step through all the <li>'s in the top level.
	var i = 0;
	var li = li_array[i];
	do {
		if (li.nodeType == 1) {
			i++;
			var div_array = li.getElementsByTagName('div');
			if (div_array.length > 0) {
				// Grab 'hideoverflow' and 'subnav_div' <div>'s.
				var hideoverflow_div = div_array[0];
				var subnav_div = div_array[1];
				
				// Hide <div>'s.
				var coords = getCoordinates(subnav_div);
				
				hideoverflow_div.style.height = "0px";
				hideoverflow_div.style['display'] = 'none';
				
				subnav_div.style.marginTop = (coords['height'] * -1.0) + "px";
				subnav_div.style['display'] = 'none';
				
				// Add toggle event to <a> tag.
				var a = li.getElementsByTagName('a')[0];
				attachEventListener(a, 'click', toggleMenuClick, false);
				
				// Trigger default item...
				if (i == defaultNavItem) {
					triggerToggleAnimation(div_array);
				}
			}
		}
		li = li.nextSibling;
	} while (li != null);
}

/* ------------------------------ CLICK EVENTS ------------------------------ */

function toggleMenuClick(event) {
	var target = getEventTarget(event);
	while (target.nodeName.toLowerCase() != "a") {
		target = target.parentNode;
	}
	
	var div_array = target.parentNode.getElementsByTagName('div');
	triggerToggleAnimation(div_array);
	
	// Disable default link action
	// REM - CAUSES PROBLEMS IN ALL BUT IE return false;
}

function triggerToggleAnimation(div_array) {
	if (div_array.length > 0) {
		var hideoverflow_div = div_array[0];
		var subnav_div = div_array[1];
		
		var coords = getCoordinates(subnav_div);
		
		if (hideoverflow_div.style['display'] == 'none') {
			// Menu is hidden.
			hideoverflow_div.style['display'] = 'block';
			subnav_div.style['display'] = 'block';
			var coords = getCoordinates(subnav_div);
			hideoverflow_div.style.height = "0px";
			subnav_div.style.marginTop = (coords['height'] * -1.0) + "px";
			doSlide(hideoverflow_div, 'height', 0, coords['height'], 30, 35);
			doSlide(subnav_div, 'marginTop', coords['height'] * -1.0, 0, 30, 35);
			hideoverflow_div.toggle = "visible";
		} else {
			// Menu is visible.
			var coords = getCoordinates(subnav_div);
			hideoverflow_div.style.height = coords['height'] + "px";
			subnav_div.style.marginTop = "0px";
			doSlide(hideoverflow_div, 'height', coords['height'], 0, 30, 35);
			doSlide(subnav_div, 'marginTop', 0, coords['height'] * -1.0, 30, 35);
			hideoverflow_div.toggle = "hidden";
		}
	}
}

/* ------------------------------ ANIMATION ------------------------------ */

/*
// SMOOTH
function easeInOut (t, b, c, d) {
	// t = time
	// d = duration
	// c = change
	// b = begin
	if ((t/=d/2) < 1) {
		return c/2 * t * t + b;
	} else {
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
}
*/

// BOUNCE
function easeOut (t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}
function easeIn (t, b, c, d) {
	return c - easeOut (d-t, 0, c, d) + b;
}
function easeInOut (t, b, c, d) {
	if (t < d/2) return easeIn (t*2, 0, c, d) * .5 + b;
	else return easeOut (t*2-d, 0, c, d) * .5 + c*.5 + b;
}


function doSlide(element,attribute,startValue,endValue,steps,intervals) {
	// doScrollDiv(categoriesnew,categoriesnew.scrollLeft,(targetLevel-3) * 194,10,35);
	
	if (element.valueChangeInterval) {
		window.clearInterval(element.valueChangeInterval);
	}
	
	var actStep = 0;
	element.valueChangeInterval = window.setInterval(
		function() {
			element.currentValue = easeOut(actStep,startValue,endValue-startValue,steps);
			element.style[attribute] = element.currentValue + "px";
			actStep++;
			if (actStep > steps) {
				window.clearInterval(element.valueChangeInterval);
				if (element.toggle == "hidden" ) {
					element.style['display'] = 'none';
				}
			}
			} 
			,intervals)
}

/* ------------------------------ INIT ------------------------------ */

attachEventListener(window, 'load', navigationInit, false);