﻿/* ----------------------------------Customize Class-------------------------------------------------------------- */
var Customize = Class.create();

Customize.prototype = {
	initialize : function(){
		this.CustomizeSetting = CookieHelper.GetCustomizeSetting();
		this.DisableShowContent = CookieHelper.GetContentSetting();
//this.CustomizeSetting = Customize_DEFAULT_SETTING;
	},
	LoadPage : function(){ //获得定制页面内容
		var aTmpColumns = $('trs_customize').childNodes;  // 获取td
		var aColumns = [];
		for (var i = 0; i < aTmpColumns.length; i++){
			var eColumn = aTmpColumns[i];
			if(eColumn.nodeType != 3 && Element.hasClassName(eColumn,'customize_column')){ //3为文本节点
				aColumns.push(eColumn);
			}
		}
		aColumns.each(function(_eColumn , _nIndex){
			this._LoadColumn(_eColumn, _nIndex);
		}.bind(this));
	},  
	_LoadColumn : function(_eColumn,_nIndex){ //@priavted获得栏目得页面内容
		var aColumnValue = this.CustomizeSetting[_nIndex];
		aColumnValue.each(function(_oCellItemId,_nIndex){
			if(!this.DisableShowContent.include(_oCellItemId))
			   this._LoadCell(_eColumn,_oCellItemId);
		}.bind(this));
	},
	_LoadCell : function(_eColumn,_oCellItemId){ //@priavted获得远程页面内容，priavted
		var oCellItem = Customize_DATA[_oCellItemId];
		if(!oCellItem) return;
		var eDiv = document.createElement('DIV');
		eDiv.title = oCellItem.name; 
		_eColumn.appendChild(eDiv);
		new Ajax.Updater(eDiv, oCellItem.url, {
			method:"GET",
			parameters:""
		});	
	},
	SaveSetting : function(){    //页面布局设置
		for(var i=0; i<Customize_DEFAULT_SETTING.length; i++){
			var eSetting = $('trs_customize_setting_'+i);
			if(eSetting){
				this.CustomizeSetting[i] = this._GetColumnSetting(eSetting);
			}
			else{
				this.CustomizeSetting[i] = [];
			}
		}
		CookieHelper.SetCustomizeSetting(this.CustomizeSetting); 
	},
	_GetColumnSetting:function(_eSetting){
		var oColumnSetting = [];	
		for(var i=0; i<_eSetting.options.length; i++){		 
			oColumnSetting[i] = _eSetting.options[i].value;    
		}
		return oColumnSetting;
	},
	LoadSetting:function(){//获取本地得页面定制配置 
		var aTmpColumns = $('trs_customize_setting').childNodes;
		var aColumns = [];
		for (var i = 0; i < aTmpColumns.length; i++){
			var eColumn = aTmpColumns[i];
			if(eColumn.nodeType != 3 && Element.hasClassName(eColumn,'customize_setting')){
				aColumns.push(eColumn);
			}
		}
		aColumns.each(function(_eColumn , _nIndex){
			this._LoadColumnSetting(_eColumn, _nIndex);
		}.bind(this));
		this._BindSettingEvent();
	},
		
	_BindSettingEvent : function(){
		Event.observe($('trs_customize_setting'),'click',function(_event){
			var event = window.event||_event;
			var elSrcElement = Event.element(event);
			if(Element.hasClassName(elSrcElement,'customize_oper')){
				var aMoveStep = (elSrcElement.getAttribute('_oper',2)||'0,0').split(',');
				this._SettingMove.apply(this,aMoveStep);
				return false;
			}
		}.bind(this));
	},
	_SettingMove : function(_nColumnId, _hStep, _vStep){
		var nColumnId = parseInt(_nColumnId,10);
		_hStep = parseInt(_hStep,10);
		_vStep = parseInt(_vStep,10);
		var elSetting = $('trs_customize_setting_'+nColumnId);
		var count = 0;
		for(var i=0;i<elSetting.size;i++){
			if(elSetting.options[i]!=null){
				if(elSetting.options[i].selected){
					count++;
				}
			}
		}
		//alert(elSetting.selectedIndex);
		if(elSetting!=null){
			var nSelectedIndex = elSetting.selectedIndex;
			
			//var maxSelectIndex = nSelectedIndex + count -1;
			
			//add by Wang Yiwen 
			if(elSetting.options.length < 1 || nSelectedIndex == -1){
				return;
			}
			
			if(_hStep==0){
				if (nSelectedIndex>0 && _vStep==-1) {  //上移
					for(var m=0;m<count;m++){
						var sText1 = elSetting.options[nSelectedIndex].text;
						var sValue1 = elSetting.options[nSelectedIndex].value;
						elSetting.options[nSelectedIndex].text = elSetting.options[nSelectedIndex-1].text;
						elSetting.options[nSelectedIndex].value = elSetting.options[nSelectedIndex-1].value;
						elSetting.options[nSelectedIndex-1].text = sText1;
						elSetting.options[nSelectedIndex-1].value = sValue1;
						elSetting.selectedIndex++;
						nSelectedIndex = elSetting.selectedIndex;
					}
					//if(elSetting.selectedIndex <= maxSelectIndex){
					//	this._SettingMove.apply(this, arguments);
					//}

				}
				else if (nSelectedIndex<elSetting.options.length-1 && _vStep==1) {//下移
				var nflag = nSelectedIndex;
					for(var j=0;j<count;j++){
						
						for(var n=0;n<count;n++){
							sText2 = elSetting.options[nSelectedIndex+n].text;
							sValue2 = elSetting.options[nSelectedIndex+n].value;
							elSetting.options[nSelectedIndex+n].text = elSetting.options[nSelectedIndex+n+1].text;
							elSetting.options[nSelectedIndex+n].value = elSetting.options[nSelectedIndex+n+1].value;
							elSetting.options[nSelectedIndex+n+1].text = sText2;
							elSetting.options[nSelectedIndex+n+1].value = sValue2;
						}
						//nSelectedIndex++;
					}	
				}
			}
			else{
				var nTargetColumnId = nColumnId+_hStep;
				if(nTargetColumnId>=0&&nTargetColumnId<Customize_DEFAULT_SETTING.length){
					var elTargetSetting = $('trs_customize_setting_'+nTargetColumnId);
					if(elTargetSetting){
						var sText = elSetting.options[nSelectedIndex].text;
						var sValue = elSetting.options[nSelectedIndex].value;
						elTargetSetting.options[elTargetSetting.options.length] = 
							new Option(sText, sValue, false, false);
						elTargetSetting.selectedIndex = elTargetSetting.options.length-1;
						elTargetSetting.scrollIntoView(false);
						elSetting.options.remove(nSelectedIndex);
					}
				}
			}
			if(elSetting.selectedIndex!=-1&&_hStep!=0){
				this._SettingMove.apply(this, arguments);
			}
		}
	},
	_LoadColumnSetting:function(_eColumn,_nIndex){//@priavted获得栏目得选择内容
		var aColumnValue = this.CustomizeSetting[_nIndex];
		var elSetting = document.createElement('SELECT');
		elSetting.id = 'trs_customize_setting_'+_nIndex;
		elSetting.className = 'trs_customize_setting';
		elSetting.size = 7;
		elSetting.multiple = true;
		_eColumn.appendChild(elSetting);
		for(var i=0; i<aColumnValue.length; i++){
			var nCellItemId = aColumnValue[i];
			if(!nCellItemId)continue;
			if(!this.DisableShowContent.include(nCellItemId))
			   elSetting.options.add (new Option(Customize_DATA[nCellItemId].name, nCellItemId, false, false));
			
			}	
	},
	_RemoveFromSetting : function(_sValue){
		for(var i=0;i<this.CustomizeSetting.length;i++){
			this.CustomizeSetting[i] = this.CustomizeSetting[i].without(_sValue);
		}
	},
	_PushIntoSetting : function(_sValue, _nIndex){
		var aColumn = this.CustomizeSetting[_nIndex];
		if(aColumn){
			aColumn.push(_sValue);
		}
	},
	_ExistInSetting : function(_sValue){
		for(var i=0;i<this.CustomizeSetting.length;i++){
			var aCurrColumn = this.CustomizeSetting[i];
			for(var j=0;j<aCurrColumn.length;j++){
				if(aCurrColumn[j]==_sValue)return true;
			}
		}
		return false;
	},
	_GetMyIndex : function(_sValue){
		for(var i=0;i<Customize_DEFAULT_SETTING.length;i++){
			var aCurrColumn = Customize_DEFAULT_SETTING[i];
			for(var j=0;j<aCurrColumn.length;j++){
				if(aCurrColumn[j]==_sValue)return i;
			}
		}
		return -1;
	},
	AdjustSetting : function(_aValues){
		for(var i=0;i<_aValues.length;i++){
			var arrValue = _aValues[i];
			var sValue = arrValue[0];
			var bChecked = arrValue[1];
			if(bChecked&&!this._ExistInSetting(sValue)){
				var nIndex = this._GetMyIndex(sValue);
				this._PushIntoSetting(sValue, nIndex);
			}
			else if(!bChecked&&this._ExistInSetting(sValue)){
				this._RemoveFromSetting(sValue);
			}
		}
	},
	ContentSaveSetting : function(){    //内容定制设置     
		var aAllContents = $("content_customize").getElementsByClassName("trs_content_customize");
		this.DisableShowContent = [];
		var aValues = [];
		//AllContents alter to aAllContents by Wang Yiwen
		aAllContents.each(function (eContent){
			if(eContent.checked==false)
				this.DisableShowContent.push(eContent.id);
			aValues.push([eContent.id,eContent.checked]);
		}.bind(this));
		CookieHelper.SetValue("TRS_CUSTOMIZE_CONTENT_SETTING", this.DisableShowContent, 1*24*30); 
		this.AdjustSetting(aValues);
		CookieHelper.SetCustomizeSetting(this.CustomizeSetting); 
	 },
	 LoadContentSetting:function(){
		 if(this.DisableShowContent){
			 this.DisableShowContent.each(function(disableId){ 
				 $(disableId).checked = false;
			 });
		 }
	 }
}
/* ----------------------------------CookieHelper Static Class-------------------------------------------------------------- */
var CookieHelper = {
	GetCustomizeSetting : function(){
	    var oCustomizeSetting = [];
		if(CookieHelper.GetValue("TRS_CUSTOMIZE_SETTING")==null){
			oCustomizeSetting = Customize_DEFAULT_SETTING;
			CookieHelper.SetCustomizeSetting(Customize_DEFAULT_SETTING);
		}
		else{
			var chnlid="873,1791,874,875,2024,880,881,882,1990,1991,866,867,868,869,2861";
			var aTmpData = CookieHelper.GetValue("TRS_CUSTOMIZE_SETTING").split("!");
			for(var i=0; i<aTmpData.length; i++){
				oCustomizeSetting[i] = aTmpData[i].split(","); 
				for(var j=0; j<oCustomizeSetting[i].length; j++){
				if(chnlid.indexOf(oCustomizeSetting[i][j])==-1){
				oCustomizeSetting = Customize_DEFAULT_SETTING;
				CookieHelper.SetCustomizeSetting(Customize_DEFAULT_SETTING);
				};
				}

			}
		}
		return oCustomizeSetting;
	},
	SetCustomizeSetting : function(_oCustomizeData){
		CookieHelper.SetValue("TRS_CUSTOMIZE_SETTING",_oCustomizeData.join("!"),1*24*30);
	},
    GetContentSetting : function(){
		if(CookieHelper.GetValue("TRS_CUSTOMIZE_CONTENT_SETTING")!=null)
			return CookieHelper.GetValue("TRS_CUSTOMIZE_CONTENT_SETTING").split(",");
		return [];	
	},
	SetValue:function(name,value,hours,path,domain,secure){  //设置cookie值
		var str = "";
		var nextTime = new Date();
		nextTime.setHours(nextTime.getHours()+hours);
		str = name+"="+escape(value);
		if(hours)
			str += ";expires="+nextTime.toGMTString();
		if(path){
			str += ";path="+path;
		}
		else{
			var sLocation = window.location.href;
			var nPos = sLocation.indexOf("/", 10);
			if(nPos>0){
				var nNextPos = sLocation.indexOf("/", nPos+1);
				str += ";path="+sLocation.substring(nPos, nNextPos+1);
			}else{
				alert("错误地址"+sLocation);
			}
		}
		if(domain)
			str += ";domain="+domain;
		if(secure)
			str += ";secure";
		document.cookie = str;
	}, 
	GetValue:function(name){ //得到cookie值
		var rs = new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie);
		if(rs!=null)
			return unescape(rs[2]);
		return null;
	} 
}  

