(function( $ ){

	var rows = [];
	var gridpos = 0;
	var methods = {
		init : function( options ) {
			
			var $this = $(this),
			data = $this.data('gridnavi'),
			gridnavi = $('', {});
			
			rows = $this.find('ul');

			var i = 0;
			var l = rows.length;
			for( i; i<l; i++ )
			{
				var row = $(rows[i]);
				row.gridnavirow({rowIndex:i});
			}
			
			if( !data )
			{
				$(this).data('gridnavi', {});
			}
		},
		
		prev : function()
		{
			$this = $(this);
			var selected = $this.find('.selected');
			
			if( selected.prev().length > 0  )
			{
				selected.prev().find('a').trigger('click');
			}
			else
			{
				var nextrow = selected.parent().parent().prev();
				if( nextrow.length > 0 )
				{
					nextrow.find('ul:first li:last a').trigger('click');
				}
				else
				{
					$this.find('ul:last li:last a').trigger('click');
				}
			}
			
			selected.parents('ul').trigger('mouseleave');
		},
		
		next : function()
		{
			$this = $(this);
			var selected = $this.find('.selected');
			
			if( selected.next().length > 0  )
			{
				selected.next().find('a').trigger('click');
			}
			else
			{
				var nextrow = selected.parent().parent().next();
				if( nextrow.length > 0 )
				{
					nextrow.find('ul:first li:first a').click();
				}
				else
				{
					$this.find('ul:first li:first a').click();
				}
			}
			
			selected.parents('ul').trigger('mouseleave');
		},
		
		prevRow : function()
		{
			$this = $(this);
			var selected = $this.find('.selected');

			var targetIndex = selected.parent().parent().index() - 1;
			if( targetIndex < 0 ) targetIndex = 3;
			var targetRow = $('.row:eq('+targetIndex+')').find('.rowcontent');
			var dx = -targetRow.offset().left / ( targetRow.width() - $(window).width() );
			if( !dx ) dx = 0;
			var nextIndex = ( Math.round(dx * (targetRow.find('.block').length-1)) );
			 
			var nextrow = $('#grid ul:eq('+targetIndex+')');
			if( nextrow.length > 0 )
			{
				nextrow.find('li:eq('+nextIndex+') a').trigger('click');
			}
			
			selected.parents('ul').trigger('mouseleave');
		},
		
		nextRow : function()
		{
			$this = $(this);
			var selected = $this.find('.selected');

			var targetIndex = selected.parent().parent().index() + 1;
			if( targetIndex > 3 ) targetIndex = 0;
			var targetRow = $('.row:eq('+targetIndex+')').find('.rowcontent');
			var dx = -targetRow.offset().left / ( targetRow.width() - $(window).width() );
			if( !dx ) dx = 0;
			var nextIndex = ( Math.round(dx * (targetRow.find('.block').length-1)) );
			 
			var nextrow = $('#grid ul:eq('+targetIndex+')');
			if( nextrow.length > 0 )
			{
				nextrow.find('li:eq('+nextIndex+') a').trigger('click');
			}
			
			selected.parents('ul').trigger('mouseleave');
		},
		
		select : function( rowIndex, itemIndex )
		{
			$this = $(this);
			var targetItem = $this.find('ul:eq('+rowIndex+') li:eq('+itemIndex+')');
			if( $this.find('.selected').parent().parent().index() === rowIndex && $this.find('.selected').index() === itemIndex )
			{
				return;
			}
			$this.find('.selected').removeClass('selected');
			targetItem.addClass('selected');
			var evt = jQuery.Event('itemSelected');
			evt.row = rowIndex;
			evt.item = itemIndex;
			evt.targetId = targetItem.find('a').attr('href');
			evt.targetId = '#' + targetItem.find('a').attr('rel');
			evt.page = targetItem.find('a').attr('rel');
			$this.trigger(evt);
		},
		
		selectByPage : function( rel )
		{
			$this = $(this);
			var targetItem = $this.find('a[rel="'+rel+'"]').parent();
			targetItem.find('a').click();
		},
		
		getgridpos : function()
		{
			return gridpos;
		},
		
		setgridpos : function( val )
		{
			gridpos = val;
		}
	};

	$.fn.gridnavi = function( method ) {
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.gridnavi' );
		}
	};

	$.fn.gridnavirow = function(options)
	{
		var rowIndex;
		var rowPosition = 0;

		var $this = $(this);
		
		var grid = $this.parents('ul');
		rowIndex = options.rowIndex;
		
		$this.width( ( $this.children().length < 3 ? 3 : $this.children().length ) * 40 - 5 );
		$this.mouseenter(function()
		{
			if( $this.parent().index() == 3 ) return;
			var rowpos = grid.gridnavi('getgridpos');
			//$(this).children('li').each(function()
			$('#grid ul').not(':last').children('li').each(function()
			{
				//if( $(this).position().left >= -rowPosition && $(this).position().left <= -rowPosition + 70 )
				if( $(this).position().left >= -rowpos && $(this).position().left <= -rowpos + 70 )
				{
					$(this).stop().animate({'opacity':1});
				}
				else
				{
					$(this).stop().animate({'opacity':.5});
				}
			});
		});
		
		$this.mouseleave(function()
		{
			if( $this.parent().index() == 3 ) return;
			var rowpos = grid.gridnavi('getgridpos');
			//$(this).children('li').each(function()
			$('#grid ul').not(':last').children('li').each(function()
			{
				//if( $(this).position().left >= -rowPosition && $(this).position().left <= -rowPosition + 70 )
				if( $(this).position().left >= -rowpos && $(this).position().left <= -rowpos + 70 )
				{
					$(this).stop().animate({'opacity':1});
				}
				else
				{
					$(this).stop().animate({'opacity':.001});
				}
			});
		});
		
		$this.find('li a').each(function(){
	  		$(this).click(function(e){
	  			var parentUl = $this;
	  			grid.gridnavi('select',parentUl.parent().index(),$(this).parent().index());
	  			
	  			var firstVisibleIndex = -Math.floor( parentUl.position().left / 40 );
	  			var lastVisibleIndex = firstVisibleIndex + 2;
	  			var lastPossibleIndex = parentUl.children().length - 1;
	  			
	  			var clickedIndex = $(this).parent().index();

	  			if( clickedIndex >= lastVisibleIndex )
	  			{
	  				dx = ( Math.floor( parentUl.position().left / 35 ) * 35 ) - ( ( clickedIndex - 1 - firstVisibleIndex ) * 35 );
	  			}
	  			else if( clickedIndex <= firstVisibleIndex )
	  			{
	  				dx = ( Math.floor( parentUl.position().left / 35 ) * 35 ) + ( ( lastVisibleIndex - clickedIndex - 1 ) * 35 );
	  			}
	  			else
	  			{
	  				dx = ( Math.floor( parentUl.position().left / 35 ) * 35 )
	  			}
	  			
	  			dx = Math.min( 0, Math.max( dx, ( (lastPossibleIndex-2) * -35 ) ) );
	  			rowPosition = dx;
	  			grid.gridnavi('setgridpos', rowPosition);
	  			//parentUl.trigger('mouseenter');
	  			//parentUl.stop().animate( { 'left' : dx, avoidTransforms : true }, 500, 'easeInOutCubic' );
	  			$('ul#grid').find('ul:eq(0)').trigger('mouseenter').stop().animate( { 'left' : dx, avoidTransforms : true }, 500, 'easeInOutCubic' );
	  			$('ul#grid').find('ul:eq(1)').trigger('mouseenter').stop().animate( { 'left' : dx, avoidTransforms : true }, 500, 'easeInOutCubic' );
	  			$('ul#grid').find('ul:eq(2)').trigger('mouseenter').stop().animate( { 'left' : dx, avoidTransforms : true }, 500, 'easeInOutCubic' );
	  		});
	  	});
		
		$this.trigger( 'mouseleave' );
	};

})( jQuery );
