var BF_Navigation = BF.extend({

	initialize : function()
	{
		this.init_sub_nav();
	},

	init_sub_nav : function( el_nav )
	{
		if ( false === $defined(el_nav) ) this.el_nav = $$("div.nav_category")[0];

		if ( false === $defined(this.el_nav) ) return false;

		this.reorder_sub_nav();

		this.el_nav.getElements('ul')[0].getChildren().each( function(el_li) {
			if ( 0 >= el_li.getElements('ul.active').length &&  0 < el_li.getElements('ul').length ) {
				el_li.addEvents({
					'mouseenter': function() {
						this.getParent().getElements('ul').each(function(el_ul){
							if ( ! el_ul.hasClass('active') ) el_ul.removeClass('ie-not-hover');
							else el_ul.addClass('ie-not-hover');
						});
						if ( $('nav_line') ) $('nav_line').set('class', this.get('name'));
					},
					'mouseleave': function() {
						this.getParent().getElements('ul').each(function(el_ul){
							if ( ! el_ul.hasClass('active') ) el_ul.addClass('ie-not-hover');
							else el_ul.removeClass('ie-not-hover');
						});
						if ( $('nav_line') ) $('nav_line').set('class', $('nav_line').get('name'));
					}
				}); // addEvents
			/* } else {
				el_li.addEvents({
					'mouseenter' : function() { if ( $('nav_line') ) $('nav_line').set('class', this.get('name')); },
					'mouseleave' : function() { if ( $('nav_line') ) $('nav_line').set('class', ''); }
				}); // addEvents */
			}; // endif
		}.bind(this)); // each
	},

	reorder_sub_nav : function()
	{
		if ( false === $defined(this.el_nav) ) return false;

		this.el_nav.getElements('li').each( function(el_nav_li) {
			el_nav_li.getElements('ul').each( function(el_ul) {
				var el_nav_ul_size = el_ul.getCoordinates();
				var el_nav_li = el_ul.getElements('li');

				var el_nav_direction = el_ul.hasClass('left') ? -1 : 1;

				el_nav_li.each( function(el_li, i) {
					if ( el_nav_direction > 0 ) {
						el_li.setStyle( 'margin-left', ( ( i * 21 ) + 26 ) + ( el_li.hasClass('left') ? 13 : 0 ) );
					}
					else {
						if ( Browser.Engine.trident )
							el_li.setStyle( 'margin-right', ( ( i * 21 ) + 20 ) );
						else
							el_li.setStyle( 'margin-left', ( -i * 21 ) - 121 );
					}; // endif
				}); // each

				if ( el_nav_direction < 0 )
					el_ul.setStyle( 'background-position', ( Browser.Engine.trident ? ( (el_nav_li.length - 1) * 20 + 180 ) + "px bottom" : "left bottom" ) );
				else if ( true === $defined(el_nav_li[0]) && el_nav_li[0].hasClass('left') )
					el_ul.setStyles({
						'width': 290,
						'top': 49,
						'background': "none"
					});
				else
					el_ul.setStyle( 'background-position', ( (el_nav_li.length - 1) * 20 - 255 ) + "px " + ( (el_nav_li.length - 1) * 20 - 260 ) + "px" );

			}); // each
		}); // each
	}

});

window.addEvent('domready', function() { BF_nav = new BF_Navigation() } );

/*-----------------------------------------+
 | Hover - Add :hover functionality for IE |
 +-----------------------------------------*/
var Hover = {
	// Create two-dimensional array of identifiers for hover effect
	// [id/class] [child nodes] [unique]
	collections : new Array(
		new Array("navigation", "li", true)
	),

	// Find all elemnts specified in array (IE only)
	init : function(collections) {
		if (document.all && document.getElementById && document.getElementsByTagName) {
			for (var i = 0; i < collections.length; i++) {
				var list = collections[i];
				var name = list[0];
				var delimiter = list[1];
				var unique = list[2];
				var children = new Array();

				if (unique) {
					// Unique element, find by ID
					var parent = document.getElementById(name);

					if (parent) {
						children = parent.getElementsByTagName(delimiter);
						Hover.addBehaviors(children);
					}
				} else {
					// Not unique, find by class
					var parents = document.getElementsByTagName("*");

					for (var j = 0; j < parents.length; j++) {
						if (parents[j].className.indexOf(name) > -1) {
							children = parents[j].getElementsByTagName(delimiter);
							Hover.addBehaviors(children);
						}
					}
				}
			}
		}
	},

	// Add class of "over" to elements when mouse hovers over them, remove when mouse stops hovering
	addBehaviors : function(collection) {
		for (var j = 0; j < collection.length; j++) {
			var node = collection[j];
			if (node.className.indexOf("current") == -1) {
				node.onmouseover = function() {
					this.className += " ie-hover";
					this.style.zIndex = 9999;  // Fixes IE z-index bug
				}
				node.onmouseout = function() {
					this.className = this.className.replace(" ie-hover", "");
				}
			}
		}
	}
};
