/*
	jQuery extention library
	marjune8@gmail.com
*/

jQuery.fn.setMaxWidth=function(additional)
{
	if(isNaN(additional)) additional=0;

	var maxWidth=0;
	this.each(function(){
		var itemWidth=$(this).width();
		if(itemWidth>maxWidth) maxWidth=itemWidth;
	});
	this.width(maxWidth+additional);
	
	return this;
}

jQuery.fn.setMaxHeight=function(additional)
{
	if(isNaN(additional)) additional=0;

	var maxHeight=0;
	this.each(function(){
		var itemHeight=$(this).height();
		if(itemHeight>maxHeight) maxHeight=itemHeight;
	});
	this.height(maxHeight+additional);
	
	return this;
}

jQuery.fn.horizontalCenter=function()
{
	this.each(function(){
		var $parent=$(this).parent();
		var size=($parent.width()-$(this).width())/2;
		$parent.css('position','relative');
		$(this)
			.css('position','relative')
			.css('left',size+'px')
		;
	})
	
	return this;
}

jQuery.fn.verticalCenter=function()
{
	this.each(function(){
		var $parent=$(this).parent();
		var size=($parent.height()-$(this).height())/2;
		$parent.css('position','relative');
		$(this)
			.css('position','relative')
			.css('top',size+'px')
		;
	})
	
	return this;
}

jQuery.fn.bottomBar=function()
{
	//if bottom bar is invisible (scroll downn to see), keep original position
	//if page is to short that the bottom bar appear in middle of browser's window, make it bottom

	$obj=this;

	$(window).load(function(){
		function adjustBottomBar()
		{
			if($(window).scrollTop()+$(window).height()<= parseInt( this.attr('originalBottomY') ))
			{
				this.css('position','static');
			}
			else
			{
				this.css('position','absolute').css('bottom','0');
			}
		}

		$obj.attr('originalBottomY',$obj.offset().top+$obj.height());

		$(window).resize(function(){
			adjustBottomBar.call($obj);
		});

		$(document).ready(function(){
			adjustBottomBar.call($obj);
		});
	});
}
