/**
 * 布局管理器<br/>
 * 用于控制页面中各个容器布局及容器内对象位置关系
 *
 * @author Leebay,Joyous
 * @since 2011-01-12
 * @version 1.0.0.3
 */
/* LayoutManager Function
---------------------------------*/
var _initHeight = null ;
var _initAutoConHeight = null ;


function reSizeCenter() {
	var __main = $("#Main");
	var __centerLayout = $(".CenterLayout");
    var __flowLayout = $(".FlowLayout");


	for ( var i = 0; i < __centerLayout.size(); i++) {
		var left = __centerLayout.eq(i).children(".Left");
		var right = __centerLayout.eq(i).children(".Right");

    	var __leftBorderWidth = 0; //LeftPanel左右两边border的宽度和

    	if(left.length > 0){
	    	if(left.css("border-right-width").length>2){
	    		var __w = left.css("border-right-width").substr(0,left.css("border-right-width").length-2);
	    		__leftBorderWidth = __leftBorderWidth+(__w-0);
	    	}

	    	if(left.css("border-left-width").length>2){
	    		var __w = left.css("border-left-width").substr(0,left.css("border-left-width").length-2);
	    		__leftBorderWidth = __leftBorderWidth+(__w-0);
	    	}
    	}

		var leftWidth = left.width()+__leftBorderWidth;
		var rightWidth = right.width();

		__centerLayout.eq(i).children(".Center").width(
            __main.width() - leftWidth - rightWidth
        );

	}

    if (__flowLayout.size() > 0) {
        for ( var i = 0; i < __flowLayout.size(); i++) {
            var flowWidth = __flowLayout.eq(i).parent().width();
            var flowLayoutChildren = __flowLayout.eq(i).children();
            var flowChildRealCount = flowLayoutChildren.size();
            var flowChildWidth = flowLayoutChildren.width();
            var flowChildMaxCount = parseInt(flowWidth/flowChildWidth);
            var flowChildCount = flowChildMaxCount > flowChildRealCount ? flowChildRealCount : flowChildMaxCount;
            var marginWidth = parseInt((flowWidth - flowChildWidth * flowChildCount)/(flowChildCount + 1));
            var remainWidth = flowWidth - marginWidth * (flowChildCount + 1) - flowChildWidth * flowChildCount;

            flowLayoutChildren.css("margin-left", marginWidth);

            if (flowChildRealCount > flowChildMaxCount) {
                for (var j=0; j< Math.ceil(flowChildRealCount/flowChildMaxCount); j++) {
                    flowLayoutChildren.eq(flowChildMaxCount*j).css("margin-left", remainWidth/2 + marginWidth);
                }
            }
        }
    }
}

function reSetAutoHeight(){
	var _autoKey = ".AutoHeight";

	if(_initHeight == null){
		_initHeight=$("#Main").height();
	}

	if(_initAutoConHeight == null){
		_initAutoConHeight=$(_autoKey).height();
	}

	if(this._autoHeight==null){
		return;
	}
	var __main = $("#Main");

	var _cons = _autoHeight.containers;
	var _height = _autoHeight.height;

	var _windowHeight = $(window).height()-(_initHeight-_initAutoConHeight)-10;

	//如果输入的为非数字对象,则按自动100%高度处理
	if(isNaN(_height)){
		_height = _windowHeight;
	}

	if (_height <_autoHeight.minHeight){
		_height=_autoHeight.minHeight;
	}

	for(var i=0; i<_cons.size(); i++){
		//for(var j=0; j<_cons[i].length;j++){
		//	alert(_cons[i][j]);
		//}
		$(_autoKey).eq(0).children(_cons[i][0]).height(_height);
	}
}

function init(){
	reSizeCenter();
    reSetAutoHeight();
}

$(document).ready(function() {
	init()
});

$(window).resize(function() {
	init()
});
