(function($) {
	$.fn.Navigato = function(options)
	{		
		options = $.extend({}, $.fn.Navigato.defaults, options);
		
		this.each(
			function()
			{
				var $this = $(this);
				var container = $(this).parent();
				var buttonLabel = $(this).children('a').html();
				var labelWidth = $(this).width();
				
				$(this).children('a').html('');
				$(this).children('a').append('<div class="NavigatoButton"><div class="NavigatoLeft"></div><div class="NavigatoLabel"><span>'+buttonLabel+'</span></div><div class="NavigatoRight"></div></div>');
				
				var buttonWidth = labelWidth+options.leftImg_w+options.rightImg_w;

				$(this).children('a').css({textDecoration:'none'});
				
				$(this).children('a').children('.NavigatoButton').css({
					marginLeft:options.margin_left+'px',
					marginRight:options.margin_right+'px',
					marginTop:options.margin_top+'px',
					marginBottom:options.margin_bottom+'px'
				})
				
				$(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').css({
					width:options.leftImg_w+'px',
					backgroundImage:'url('+options.leftImg+')',
					backgroundRepeat:'no-repeat',
					float:'left'
				});	
				
				if(options.bgOrientation == 'v'){
					var repeat = 'repeat-x';	
				}else{
					var repeat = 'no-repeat';	
				}
				$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').css({
					backgroundImage:'url('+options.centerImg+')',
					backgroundRepeat:repeat,
					float:'left',
					color:options.tColor_normal
				});
				
				if(options.fixedWidth > 0){
					var newWidth = options.fixedWidth - options.leftImg_w - options.rightImg_w;
					$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').css({
						width:newWidth+'px'																				 
					});
				}
				
				$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').children('span').css({
					display:'block',
					paddingLeft:options.tPadding_left+'px',
					paddingRight:options.tPadding_right+'px',
					paddingTop:options.tPadding_top+'px',
					paddingBottom:options.tPadding_bottom+'px'
				});
				
				$(this).children('a').children('.NavigatoButton').children('.NavigatoRight').css({
					width:options.rightImg_w+'px',
					backgroundImage:'url('+options.rightImg+')',
					backgroundRepeat:'no-repeat',
					float:'left'
				});
				
				if(options.fixedHeight > 0){
					var newHeight = options.fixedHeight;
				}else{
					var newHeight = $(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').height();	
				}
				var backgroundPOS_y = newHeight-options.leftImg_h;
				var backgroundPOS_x = 0;
				
				$(this).children('a').children('.NavigatoButton').children('.NavigatoRight').css({
					height:newHeight+'px',
					backgroundPosition:backgroundPOS_x+'px '+backgroundPOS_y+'px'
				});
				
				$(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').css({
					height:newHeight+'px',
					backgroundPosition:backgroundPOS_x+'px '+backgroundPOS_y+'px'																				 
				});
				
				$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').css({
					height:newHeight+'px',
					backgroundPosition:backgroundPOS_x+'px '+backgroundPOS_y+'px'																				 
				});
				
				if($(this).attr('class') == "on"){
					if(options.bgOrientation == 'v'){
						var normalPOS = $(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').height()-(options.leftImg_h);
						var overPOS_y = normalPOS-options.leftImg_h;
						var overPOS_x = 0;
					}else{
						overPOS_x = -($(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').width());
						overPOS_y = $(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').height()-(options.leftImg_h);	
					}
					
					$(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').css({
						backgroundPosition:overPOS_x+'px '+overPOS_y+'px'
					});
					
					if(options.bgOrientation == 'h'){
						overPOS_x = -($(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').width());
					}
					
					$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').css({
						backgroundPosition:overPOS_x+'px '+overPOS_y+'px',
						color:options.tColor_on
					});
					
					if(options.bgOrientation == 'h'){
						overPOS_x = -($(this).children('a').children('.NavigatoButton').children('.NavigatoRight').width());
					}
					
					$(this).children('a').children('.NavigatoButton').children('.NavigatoRight').css({
						backgroundPosition:overPOS_x+'px '+overPOS_y+'px'
					});
				}
				
				$(this).mouseover(function(){
					if(options.bgOrientation == 'v'){
						var normalPOS = $(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').height()-(options.leftImg_h);
						var overPOS_y = normalPOS-options.leftImg_h;
						var overPOS_x = 0;
					}else{
						overPOS_x = -($(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').width());
						overPOS_y = $(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').height()-(options.leftImg_h);	
					}
					
					$(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').css({
						backgroundPosition:overPOS_x+'px '+overPOS_y+'px'
					});
					
					if(options.bgOrientation == 'h'){
						overPOS_x = -($(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').width());
					}
					
					$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').css({
						backgroundPosition:overPOS_x+'px '+overPOS_y+'px',
						color:options.tColor_on
					});
					
					if(options.bgOrientation == 'h'){
						overPOS_x = -($(this).children('a').children('.NavigatoButton').children('.NavigatoRight').width());
					}
					
					$(this).children('a').children('.NavigatoButton').children('.NavigatoRight').css({
						backgroundPosition:overPOS_x+'px '+overPOS_y+'px'
					});
				});
				
				if($(this).attr('class') == "on"){
					
				}else{
					$(this).mouseout(function(){
						var normal_y = $(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').height()-(options.leftImg_h);
						$(this).children('a').children('.NavigatoButton').children('.NavigatoLeft').css({
							backgroundPosition:'0px '+normal_y+'px'
						});
						
						$(this).children('a').children('.NavigatoButton').children('.NavigatoLabel').css({
							backgroundPosition:'0px '+normal_y+'px',
							color:options.tColor_normal
						});
						
						$(this).children('a').children('.NavigatoButton').children('.NavigatoRight').css({
							backgroundPosition:'0px '+normal_y+'px'
						});
					});
				}
			}
		);
	};
	
	$.fn.Navigato.rotate = function(){
			
		
	}
	
	$.fn.Navigato.defaults = {
		leftImg : 'js/Navigato/leftImg2.png',
		leftImg_w : 15,
		leftImg_h : 28,
		rightImg : 'js/Navigato/rightImg2.png',
		rightImg_w : 15,
		rightImg_h : 28,
		centerImg : 'js/Navigato/centerImg2.png',
		centerImg_h : 28,
		margin_left : 0,
		margin_right : 0,
		margin_top : 5,
		margin_bottom : 0,
		tPadding_left : 0,
		tPadding_right : 0,
		tPadding_top : 8,
		tPadding_bottom : 0,
		tColor_normal : '#004e89',
		tColor_on     : '#cd1d1d',
		fixedWidth : 0,
		fixedHeight : 28,
		bgOrientation : 'v'
	};
})(jQuery);
