(function($){
	
	var firstLiClass = 'sg-first',
		lastLiClass = 'sg-last';
	
	$.widget("sg.sg_sitenav_top", {
		options: {
			index_selected: 0,
			parents_disabled: false
		},
		
		_create: function(){
			var $widget = this;
			
			
			
			var $tag_rootul = this.element.find('ul.level-0');
			$tag_rootul.children('li.level-0').each(function(){
				var $width = Math.ceil($(this).width());
				$(this).width( $width+'px' );
			});
			
			var $tag_ul = this.element.find('ul');
			$tag_ul.each(function(){
				//$(this).find('li:first').addClass( firstLiClass );
				//$(this).find('li:last').addClass( lastLiClass );
				var $width_ul = 0;
				$(this).children('li').each(function(){
					$width_ul += $(this).outerWidth();
				});
				$(this).width( $width_ul+'px' );
			});
			
			var $tag_childul = this.element.find('ul.level-1');
			$tag_childul.each(function(){
				var $tag_li = $(this).children('li');
				
				//set children li padding
				var $width_diff = $tag_rootul.outerWidth() - $(this).outerWidth();
				if($width_diff>0){
					var $width_padding = $width_diff/($tag_li.length);
				}else{
					var $width_padding = 10;
				}
				
				var $width_ul = 0;
				$tag_li.each(function(){
					$(this).children('a').css('padding-left', $width_padding/2+'px');
					$(this).children('a').css('padding-right', $width_padding/2+'px');
					$width_ul += $(this).outerWidth();
				});
				//$(this).width( Math.floor($width_ul)+'px' );
				$(this).width( $width_ul+10+'px' );
			});
			
			
			
			this.setSelectedIndex();
			this.showChildrenByIndex( this.options.index_selected );
			
			var $tag_li = this.element.find('li.level-0');
			$tag_li.each(function(){
				//ONLY LI WITH CHILDREN
				
				var $this_tag_li = $(this);
				if( $widget.hasChildren($this_tag_li) ){
					//hover functions
					$this_tag_li.hover(
						function(){
							$widget.showChildrenByIndex( $this_tag_li.index() );
						},
						function(){
							$widget.showChildrenByIndex( $widget.options.index_selected );
						}
					);
					//disable click functions
					if($widget.options.parents_disabled == true){
						var $i = $(this).index();
						
						/*
						var $tag_link = $(this).find('a.level-0');
						var $tag_span = $('<span class="'+$tag_link.attr('class')+'">'+$tag_link.html()+'</span>');
						$tag_link.remove();
						$tag_span.prependTo($(this));
						*/
						
						/*
						$(this).find('a.level-0').hover(function(){
							//$widget.setSelectedIndex( $i );
							$widget.showChildrenByIndex( $i );
							return false;
						});
						*/
						
						$(this).find('a.level-0').click(function(){
							return false;
						});
					}
				}
			});
		},
		
		setSelectedIndex: function( $i_selected ){
			if($i_selected != undefined){
				this.options.index_selected = $i_selected;
			}else{
				var $tag_selected = this.element.find('li.level-0.selected');
				//if(this.hasChildren($tag_selected)){
					var $i_selected = $tag_selected.index();
					if($i_selected >= 0){
						this.options.index_selected = $i_selected;
					}
				//}
			}
		},
		
		hasChildren: function($tag_li){
			if($tag_li.find('ul').html()){
				return true;
			}else{
				return false;
			}
		},
		
		showChildrenByIndex: function( $i_selected ){
			var $tag_li = this.element.find('li.level-0');
			$tag_li.each(function(){
				$i = $(this).index();
				
				var $tag_ul = $(this).find('ul');
				var $tag_arrow = $(this).find('.sg-arrow');
				
				var $arr_pos = $(this).position();
				/*
				var $pos_right = $(this).parents('ul').width() - $arr_pos.left;
				$pos_right = $pos_right - ($(this).width() / 2);
				$pos_right = $pos_right - ($tag_arrow.width() / 2);
				
				$tag_arrow.css('right', $pos_right+'px');
				*/
				var $pos_left = $arr_pos.left + ($(this).width() / 2) - ($tag_arrow.width() / 2);
				$tag_arrow.css('left', $pos_left+'px');
				
				/*
				var $width_li = $(this).width();
				var $tag_ul = $(this).find('ul');
				var $tag_arrow = $(this).find('.sg-arrow');
				*/
				//$tag_ul.width();
				
				//var $arr_pos = $(this).position();
				//$tag_ul.css('left', ($arr_pos.left-15)+'px');
				//$tag_arrow.css('margin-left', ($width_li/2-15)+'px');
				
				
				if($i != $i_selected){
					$tag_ul.hide();
				}else{
					$tag_ul.show();
				}
			});
		}
	});
}( jQuery ) );
