UHG.namespace('UHG.ui.CustomDropDown');

(function($) {
		  
		  
	UHG.ui.CustomDropDown = function(element,options) {
		
		
		var elem = $(element);
		var obj = this;
		
		var _origHeight;
		var _nativeHeight;
		var _nativeWidth;
		var _parentWidth;
		
		var _position;
		
		
		//DEFAULT CONFIG OBJECT
		var defaults = {
			
			SETTINGS: {
				btnHeight: 86	
			},
			CLASSES: {
				FIRSTITEM: 'first',
				LASTITEM: 'last',
				MENUOPEN: 'subnav-open'
			},
			IDS: {},
			SELECTORS: {
				PARENT: 'li',
				REVEAL: 'ul',
				ITEM: 'ul li',
				FIRSTITEM: 'ul li:first-child',
				LASTITEM: 'ul li:last-child',
				FIRSTBTN: 'ul li.first a',
				FIRSTBTNWIDE: 'ul.first-wide li.first a',
				LASTBTN: 'ul li.last a'
			}			
		}
		
		
		//MERGE DEFAULT CONFIG OBJECT WITH OPTIONS
		var config = $.extend(true, defaults, options || {});		
		
		//INITIALIZE
		var init = function() {
			setup();
			attachListeners();
		}
		
		var setup = function() {
			_origHeight = $(config.SELECTORS.REVEAL,elem).height();
			$(config.SELECTORS.FIRSTITEM,elem).addClass('first');
			$(config.SELECTORS.LASTITEM,elem).addClass('last');
			
			setPosition();
			
			setHeightAndWidth();
		}
		
		var setHeightAndWidth = function() {
			$(config.SELECTORS.REVEAL,elem).css( { height:"auto" } );
			_nativeHeight = $(config.SELECTORS.REVEAL,elem).height();
			_nativeWidth = $(config.SELECTORS.REVEAL,elem).width();
			$(config.SELECTORS.REVEAL,elem).css( { "width":_nativeWidth } );
			_parentWidth = $(config.SELECTORS.REVEAL,elem).closest(config.SELECTORS.PARENT).width();
			if(_parentWidth > _nativeWidth) {
				$(config.SELECTORS.REVEAL,elem).css( { "width":_parentWidth } );
			} else {
				$(config.SELECTORS.REVEAL,elem).addClass('first-wide');
			}
			$(config.SELECTORS.REVEAL,elem).css( { "height":_origHeight } );	
		}
		
		var setPosition = function() {
			_position = elem.offset();
			$(config.SELECTORS.REVEAL,elem).offset( { "top":_position.top+66, "left":_position.left+2 } );
		}
				
		var attachListeners = function() {
			elem.mouseenter(openSelect);
			elem.mouseleave(closeSelect);
			
			$(window).resize(setPosition);
		}
		
		var openSelect = function() {
						
			$(config.SELECTORS.REVEAL,elem).stop();
			
			attachPie();
			
			if( $(config.SELECTORS.ITEM,elem).length > 1 ) {
				$(elem).addClass(config.CLASSES.MENUOPEN);
				$(config.SELECTORS.REVEAL,elem).animate( { "height":_nativeHeight }, 200 );
			}
			
			$(window).trigger('menuOpen');
			
		}
		
		var closeSelect = function() {
			$(config.SELECTORS.REVEAL,this).stop();
			$(config.SELECTORS.REVEAL,this).animate( { "height":_origHeight }, 200, function() { $(this).closest('li').removeClass(config.CLASSES.MENUOPEN) });
			
			detachPie();
			
			$(window).trigger('menuClose');
		}
		
		var attachPie = function() {
			if($.browser.msie) {
				$(config.SELECTORS.REVEAL,elem).each(function() { PIE.attach(this); });
				$(config.SELECTORS.FIRSTBTNWIDE,elem).each(function() { PIE.attach(this); });
				$(config.SELECTORS.LASTBTN,elem).each(function() { PIE.attach(this); });
			}
		}
		
		var detachPie = function() {
			if($.browser.msie) {
				$(config.SELECTORS.REVEAL,elem).each(function() { PIE.detach(this); });
				$(config.SELECTORS.FIRSTBTNWIDE,elem).each(function() { PIE.detach(this); });
				$(config.SELECTORS.LASTBTN,elem).each(function() { PIE.detach(this); }); 
			}
		}
		
		
		//INITIALIZE
		return init();
		
		
	};
	
	
	$.fn.uhgCustomDropDown = function(options) {
		return this.each(function() {
			var element = $(this);
			if (element.data('uhgCustomDropDown')) return;
			var uhgCustomDropDown = new UHG.ui.CustomDropDown(this, options);
			element.data('uhgCustomDropDown', uhgCustomDropDown);
		});
	};


})(jQuery);

