/*
 * Plugin name : Navigation Builder
 * Author : Dae-Seon Moon
 */

(function($){
  $.fn.navBuilder = function(){
	var obj = {
	  s : this.selector,
	  c : this.context
	};

	return this.each(function(){
	  try {
		var setPosition = function(){
		  var navWidth = $(this).width();
		  var subNav = $(this).children('div.subNav');
		  var topPos = $(this).position().top + 50;
		  var leftPos = $(this).position().left;
		  var newLeftPos = leftPos - 4;
		  if(subNav.length == 1) {
			subNav.css({'visibility':'hidden','display':'block'});
			var subWidth = navWidth + 32;
			var height = subNav.children('.sub-nav-body:first').height();			
			subNav.css({
			  'width' : subWidth + 'px'
			});
			subNav.children('.sub-nav-left').css({
			  'height' : height + 'px'
			});
			subNav.children('.sub-nav-right').css({
			  'height' : height + 'px'
			});
			subNav.children('.sub-nav-body').css({
			  'width' : navWidth + 24 + 'px'
			});
			subNav.children('.sub-nav-bottom').children('.sub-nav-bottom-center').css({
			  'width': subWidth - 18 + 'px'
			});

			subNav.css({
			  /*'top' : topPos + 'px',*/
			  'z-index':10,
			  'left' : newLeftPos + 'px'
			});
			subNav.hide();
			subNav.css({'visibility':'visible'});
		  }
		};

		$(this).children('li').each(setPosition);
	  } catch(error){
	  }
	  finally{
		/* to prevent memory leak */
		setPosition = null;
		navWidth = null;
		subNav = null;
		topPos = null;
		leftPos = null;
		newLeftPos = null;
		subWidth = null;
		height = null;
	  }
	});
  };
})(jQuery);