﻿var HashTable = new Object();
var aryModules = null;
var aryResult = [];

function ChooseMoudles()
{
	if(aryModules == null)
	{
		GetModules();
	}
	ShowChooseModule();
}
function GetModules()
{
	aryModules = Ajax_HomePage.GetAllModules().value;
	for(var i in aryModules)
	{
		var aryTemp = eval(aryModules[i]);
		var objModuleData = new ModuleData();
		objModuleData.ROW_ID = aryTemp[0];
		objModuleData.IsConst = aryTemp[1] || false;
		objModuleData.RightFirst = aryTemp[2] || false;
		HashTable[aryTemp[0]] = objModuleData;
	}
}
function ShowChooseModule()
{
	if($("hdivChooseArea").innerHTML != "")
	{
		$("hdivChooseArea").style.display = "block";
	}
	else
	{
		for(var i in HashTable)
		{
			var objModuleData = HashTable[i];
			var hdivOne = CreateEle("div", "", $("hdivChooseArea"));
			var hcbCheckBox = CreateEle("input");
			hcbCheckBox.type = "checkbox";
			hdivOne.appendChild(hcbCheckBox);
			hcbCheckBox.id = "hcbCheckBox_" + objModuleData.ROW_ID;
			if(GetChildrenByClassName($("hdivModules"), "Module " + objModuleData.ROW_ID).length > 0)
			{
				hcbCheckBox.checked = true;
			}
			var hlblText = CreateEle("label");
			hlblText.htmlFor = hcbCheckBox.id;
			hdivOne.appendChild(hlblText);
			hlblText.innerHTML = objModuleData.ROW_ID;
		}
		var hdivOK = CreateEle("div", "", $("hdivChooseArea"));
		hdivOK.innerHTML = "完成选择";
		addEventHandler(hdivOK, "click", ChangeModule);
		var hdivAll = CreateEle("div", "", $("hdivChooseArea"));
		hdivAll.innerHTML = "全选";
		addEventHandler(hdivAll, "click", SelectAll);
	}
}
function SelectAll(obj)
{
	var aryCheckBoxes = $("hdivChooseArea").getElementsByTagName("input");
	for(var i in aryCheckBoxes)
	{
		if(aryCheckBoxes[i].type == "checkbox")
		{
			if(this.innerHTML == "全选")
			{
				aryCheckBoxes[i].checked = true;
			}
			else
			{
				aryCheckBoxes[i].checked = false;
			}
		}
	}
}
function ChangeModule()
{
	$("hdivChooseArea").style.display = "none";
	var aryCheckBoxes = $("hdivChooseArea").getElementsByTagName("input");
	aryResult = new Array();
	for(var i in aryCheckBoxes)
	{
		if(aryCheckBoxes[i].checked)
		{
			var aryTemp = HashTable[aryCheckBoxes[i].id.split("_")[1]];
			aryResult.push(aryTemp);
		}
	}
	ShowModules(false);//测试的时候设为true
}
function ShowModules(bWriteCookie)
{
	//aryResult.sort(RandomSort); //打乱模块顺序
	var objAutoRank = new AutoRank(aryResult, "hdivModules");
	if(!!bWriteCookie)
	{
		setCookie(GetString(aryResult), "HomePage", "", 365);
	}
}
function RandomSort()
{
	return Math.random() >= 0.5;
}
function ReadCookie()
{
	aryResult = [];
	var strCK = getCookie("HomePage", "");
	var aryTemp = eval("[" + strCK + "]");
	GetModules();
	if(aryTemp.length == 0)
	{
		var aryDefaultModules = Ajax_HomePage.GetDefaultModules().value;
		for(var j = 0; j < aryDefaultModules.length; j++)
		{
			var aryTemp = eval(aryDefaultModules[j]);
			aryResult.push(HashTable[aryTemp[0]]);
		}
	}
	else
	{
		for(var i = 0; i < aryTemp.length; i++)
		{
			aryResult.push(HashTable[aryTemp[i]]);
		}
	}
	ShowModules();
}
function GetString(aryOfAry)
{
	var result = ""
	for(var i = 0; i < aryOfAry.length; ++i)
	{
		if(i != 0) result += ",";
		result += aryOfAry[i].ROW_ID;
	}
	return result;
}

