/**
 * jQuery multipleWidth 
 *
 * under the MIT (http://www.opensource.org/licenses/mit-license.php) License.
 *
 * Copyright 2011-2012 onynnous.net All rights reserved.
 **/

(function($){
	$.fn.multipleWidth = function(option) {
		
	var def = {
		wrap : window,
		animation:false,
		delay: 0,
		time: 0,
		easeing: "liner",
		gridWidth : 100,
		paddingLeft:0,
		paddingRight:0,
		marginLeft:0,
		marginRight:0,
		gridSpace:0,
		adjustment:0,
		minSet:false,
		minWidth:1000,
		maxSet:false,
		maxWidth:2000,
		onFinish : function(){ return false; }
	};
	def.setTarget = this;
	
	return this.each(function() {
		
		var conf = $.extend(def, option);
		
		var getWindowSize = function(){
			var w = $(conf.wrap).width();
			setWidth(w);
		}
		
		var setWidth = function(windowSize){
			var n = 0;
			n = Math.floor((windowSize - conf.paddingLeft - conf.paddingRight - conf.marginLeft - conf.marginRight + conf.gridSpace) / (conf.gridWidth + conf.gridSpace));
			var w = n*(conf.gridWidth + conf.gridSpace) - conf.gridSpace + conf.adjustment;
			if(conf.minSet){
				if(w < conf.minWidth){
					w= conf.minWidth;
				}
			}
			if(conf.maxSet){
				if(w > conf.maxWidth){
					w = conf.maxWidth;
				}
			}
			if(conf.animation){
				var aniTimer = setTimeout(function(){
					$(conf.setTarget).stop(true).animate({
						width:w
					},conf.time, conf.easeing,function(){
						$(conf.setTarget).css("overflow","visible");
						conf.onFinish();
					});
				},conf.delay);
			}
			else {
				$(conf.setTarget).width(w);
				conf.onFinish();
			}
		}
		
		$(window).resize(getWindowSize);
		
		var init = function() {
			getWindowSize();
		};
		
		init();
	});
};
})(jQuery);
