﻿var ModuleData = function()
{
	this.ROW_ID = null;
	this.IsConst = null;
	this.RightFirst = null;
	this.Width = null;
};
ModuleData.prototype.toString = function()
{
	return "[" + this.ROW_ID + "," + this.IsConst + "," + this.Width + "] ";
};

var AutoRank = Class.create();
AutoRank.prototype = 
{
	initialize: function(aryModules, parent)
	{
		this.Parent = $(parent);//父容器
		this.AryModules = aryModules;//模块数组
		this.AryResult = null;//结果数组
		this.ConstModules = null;//不能变的模块
		this.VarModules = null;//可以变的模块
		this.AryModuleObjs = null;
		
		this.Stat();//分类
		this.Rank();//排版
		this.Display();
		this.GetDatas();
		
		this.NumLoaded = 0;
	},
	
	//
	Stat: function()
	{
		this.ConstModules = [];
		this.VarModules = [];
		for(var i = 0; i < this.AryModules.length; i++)
		{
			try
			{
				if(this.AryModules[i].IsConst == true)
				{
					this.ConstModules.push(this.AryModules[i]);
					this.AryModules[i].Width = 32;
				}
				else
				{
					this.VarModules.push(this.AryModules[i]);
					this.AryModules[i].Width = 8;
				}
			}
			catch(ex)
			{
			}
		}
		this.SortRightFirst();
		switch(this.VarModules.length % 3)
		{
			case 1:
				this.VarModules[this.VarModules.length - 1].Width = 32;
				break;
			case 2:
				this.VarModules[this.VarModules.length - 2].Width = 24;
				break;
		}
	},
	
	Rank: function()
	{
		var iConstNum = this.ConstModules.length;
		var iVarNum = this.VarModules.length;
		var iVarRow = Math.ceil(iVarNum / 3);
		var iAllRow = iConstNum + iVarRow;
		var iConstAdded = 0;
		var iVarAdded = 0;
		
		this.AryResult = [];
		for(var i = 0; i < iAllRow * 2; i++)
		{
			if(i % 2 == 0)
			{
				for(var j = 0; j < 3 && this.VarModules.length > 0; j++)
				{
					var varModule = this.VarModules.shift();
					if(varModule.Width == 8 && (j == 0 || j == 1) && this.VarModules.length > 0)
					{
						varModule.Width = 12;
					}
					this.AryResult.push(varModule);
					iVarAdded++;
				}
			}
			else if(iConstAdded < iConstNum)
			{
				this.AryResult.push(this.ConstModules[iConstAdded])
				iConstAdded++;
			}
		}
	},
	
	Display: function()
	{
		this.AryModuleObjs = [];
		this.Parent.innerHTML = "";
		for(var i = 0; i < this.AryResult.length; i++)
		{
			var objModule = CreateEle("div", "", this.Parent);
			objModule.innerHTML = this.GetLoadingDiv();
			var iWidth = this.GetRealWidth(this.AryResult[i].Width);
			objModule.style.width = iWidth + "px";
			objModule.className = this.GetCssName(iWidth);
			this.AryModuleObjs.push(objModule);
		}
		CreateEle("div", "Clear", this.Parent);
	},
	
	GetLoadingDiv: function(bSuccess)
	{
		var strModuleHtml = '<div class="ModuleOuter">'
				+ '<div class="ModuleInner">'
				+ '  <div class="H2Background">'
				+ '		<span class="Title"></span>'
				+ '		<div class="Left"></div>'
				+ '		<div class="Right"></div>'
				+ '		<div class="MoreIcon"><a href="#"></a></div>'
				+ '	</div>'
				+ '	<div style="height:330px;" class="DisplayArea Loading">';
		if(!bSuccess)
		{
			strModuleHtml += '<div class="Loading"></div>';
		}
		else
		{
			strModuleHtml += '<div style="height:200px; text-align:center; color:red;">模块加载失败！</div>';
		}
		strModuleHtml += '	</div>'
				+'</div></div>';
		return strModuleHtml;
	},
	
	//读取数据
	GetDatas: function()
	{
		this.NumLoaded = 0;
		this.GetOneData();
	},
	
	GetOneData: function()
	{
		if(this.NumLoaded >= this.AryResult.length) return;
		var i = this.NumLoaded;
		Ajax_HomePage.GetContent(this.AryResult[i].ROW_ID, this.GetRealWidth(this.AryResult[i].Width) - 30, Bind(this, GetContent_CallBack));
		
		function GetContent_CallBack(response)
		{
			var strInnerHTML = response.value;
			if(strInnerHTML != null && strInnerHTML != "")
			{
				this.AryModuleObjs[i].innerHTML = strInnerHTML;
			}
			else
			{
				this.AryModuleObjs[i].innerHTML = this.GetLoadingDiv(true);
			}
			
			this.NumLoaded++;
			this.GetOneData();
		}
	},
	
	SortRightFirst: function()
	{
		//让最右优先的放在 length%3 == 2 的位置
		var rfModules = [];
		var nmModules = [];
		for(var i = 0; i < this.VarModules.length; i++)
		{
			if(this.VarModules[i].RightFirst == true)
			{
				rfModules.push(this.VarModules[i]);
			}
			else
			{
				nmModules.push(this.VarModules[i]);
			}
		}
		this.VarModules = [];
		var work = 1;
		while(work == 1)
		{
			work = 0;
			if(nmModules.length > 0)
			{
				this.VarModules.push(nmModules.shift());
				work = 1;
			}
			if(nmModules.length > 0)
			{
				this.VarModules.push(nmModules.shift());
				work = 1;
			}
			if(rfModules.length > 0)
			{
				this.VarModules.push(rfModules.shift());
				work = 1;
			}
		}
	},
	
	GetCssName: function(width)
	{
		var cssName = "S";
		switch(width)
		{
			case 360:
				cssName = "M";
				break;
			case 720:
				cssName = "L";
				break;
			case 970:
				cssName = "XL";
				break;
		}
		return "Module " + cssName;
	},
	
	GetRealWidth: function(width)
	{
		var iWidth = 250;
		switch(width)
		{
			case 12:
				iWidth = 360;
				break;
			case 8:
				iWidth = 250;
				break;
			case 24:
				iWidth = 720;
				break;
			case 32:
				iWidth = 970;
				break;
		}
		return iWidth;
	}
};
