(function ($) {
	
	$.layout = function (options) {
		return $.layout.impl.init(options);
	};
	
	$.fn.layout = function (options) {
		return $.layout.impl.init(this, options);
	};
	
	/*
	 * layout default options
	 */
	$.layout.defaults = {
	    maxContentW:    1226,
	    maxContentH:    684,
	    minContentW:    1005
	};
	
	$.layout.impl = {
		
		/*
		 * layout options
		 */
		opts: null,
		
		/*
		 * layout helper
		 */
		helper: {},
		
		/*
		 * Initialize the layout
		 */
		init: function (options) {
            
            var self    = this;
            
            self.opts = $.extend({}, $.layout.defaults, options);
            
            // create the helper objects
            self.helper.container       = $('div.content-container');
            self.helper.footer          = $('div.footer-container');
            
            // set the options
            self.opts.ratio             = self.opts.maxContentH/self.opts.maxContentW;
            self.opts.minContentH       = Math.floor(self.opts.ratio*self.opts.minContentW);
            //self.opts.minContentH       = 560;
            
            // set the layout dimensions
            self.setDimensions();
            
            // bind to window resize
            $(window).resize(function(){
                
                self.setDimensions();
                
            });
            
			return self;
		},
		
		/*
		 * Set the layout dimensions
		 */
		setDimensions: function () {
		    
		    var self        = this;
		    var winW        = $(window).width();
		    var winH        = $(window).height();
		    var minW        = self.opts.minContentW;
		    var minH        = self.opts.minContentH;
		    var maxW        = self.opts.maxContentW;
		    var maxH        = self.opts.maxContentH;
		    var $content    = self.helper.container;
		    var $footer     = self.helper.footer;
		    var r           = self.opts.ratio;
		    var spacer      = 24;
		    
		    // set min content height by default
//		    $content.css({
//		        height:                 minH + $footer.height() + 27 + 'px'
//		    });
		    
            if (winH <= (minH + $footer.height() + 27))
            { // min
                
                $footer.css({
                    backgroundColor:    '#fff',
                    width:              minW - 62 + 'px'
                });
                
                $content.css({
                    height:             minH + 'px',
                    width:              (winW < $footer.width()) ? $footer.width() + 'px' : '100%'
                });
            }
            else if (winH > (minH + $footer.height() + 27) && winH <= (maxH + $footer.height() + 27))
            { // betw. max and min
                
                $footer.css({
                    backgroundColor:    '#fff',
                    width:              Math.floor(($content.height() - 40)/r) + 'px'
                });
                
                $content.css({
                    height:             winH - $footer.height() - 27 + 'px',
                    width:              (winW < $footer.width()) ? $footer.width() + 'px' : '100%'
                });
            }
            else
            { // max
                
                $footer.css({
                    backgroundColor:    '#fff',
                    width:              maxW + 'px'
                });
                
                $content.css({
                    height:             winH - $footer.height() - 27 + 'px',
                    width:              (winW < $footer.width()) ? $footer.width() + 'px' : '100%'
                });
            }
//            
//            // set width
//            if (winH < (minH + $footer.height() + spacer))
//            {                
//                $footer.css({
//                    width:      minW - 13 + 'px'
//                });
//                
//                if (winW < minW)
//                {
//                    $footer.css({
//                        margin:     '0 ' + spacer - 3 + 'px'
//                    });
//                }
//                else
//                {
//                    $footer.css({
//                        margin:     '0 auto'
//                    });
//                }
//            }
//            else if (winH - $footer.height() - 27 - 24 >= maxH)
//            {
//                $footer.css({
//                    width:      maxW + 'px',
//                    margin:     '0 auto'
//                });
//            }
//            else
//            {
//                $footer.css({
//                    width:      Math.floor(($content.height() - 39)/r) + 'px',
//                    margin:     '0 auto'
//                });
//            }
//		    
//		    if (winW < $footer.width())
//            {
//                $content.css({
//                    width:     $footer.width() + 'px'
//                });
//            }
		    
		    
		    
		    return;
		}
		
	};
})(jQuery);