var CurrentStatus = {
	DataObj: {},
	FinalData: {},
	InfoObj: {},
	ImgArray: [],
	IconLR: [],
	IconAmt: 0,
	VoteAmt: 0,
	BlockAction: true,
	SimpleLifeInit: function() {
		var hash = $H();
		hash['action'] = 'SimpleLifePK';
		CurrentStatus.DataListQuery(hash, './func/Query.php');
	},
	VoteAction: function(Mode) {
		if (this.BlockAction) return;
		if (VoteStatus != 3) {
			(VoteStatus == 1) ? alert('Coming soon！ 敬請期待！') : alert('活動時間已結束，謝謝您的參與！');
			return;
		}//end if
		var GetIcon =  (Mode == 'VoteL') ? this.IconLR[0] : this.IconLR[1];
		GetIcon.Vote++;
		$('PKPoint').innerHTML = ++this.VoteAmt;
		this.GoPK();
	},
	GoPK: function() {
		this.BlockAction = true;
		
		while ($('PKImgL').childNodes.length > 0) $('PKImgL').removeChild($('PKImgL').firstChild);
		while ($('PKImgR').childNodes.length > 0) $('PKImgR').removeChild($('PKImgR').firstChild);
		$('PKImgL').className = 'LoadingStatus';
		$('PKImgR').className = 'LoadingStatus';
		$('PKImgL').style.left = null;
		$('PKImgL').style.top = null;
		$('PKImgR').style.left = null;
		$('PKImgR').style.top = null;
		
		var DamiData = this.ImgArray.clone();
		for (var i=0; i<2; i++) {
			//var GetIcon = this.ImgArray[Math.floor(Math.random()*this.ImgArray.length)];
			var GetIcon = DamiData.splice(Math.floor(Math.random()*DamiData.length), 1);
			this.IconLR[i] = this.InfoObj[GetIcon];
			(this.IconLR[i].isImgReady) ? this.PKImgReady((i == 0) ? 'PKImgL' : 'PKImgR') : this.IconLoader(i, this.IconLR[i].ImgPath);
		}//end for
	},
	PKImgReady: function(Type) {
		var GetIcon =  (Type == 'PKImgL') ? this.IconLR[0] : this.IconLR[1];
		if (!GetIcon.PosX) 	GetIcon.PosX = 155 - Math.floor(GetIcon.ImgW/2);
		if (!GetIcon.PosY) 	GetIcon.PosY = 233 - Math.floor(GetIcon.ImgH/2);
		
		var ImgSet = document.createElement('img');
		ImgSet.src = GetIcon.ImgPath;
		ImgSet.style.width = GetIcon.ImgW + 'px';
		ImgSet.style.height = GetIcon.ImgH + 'px';
		$(Type).appendChild(ImgSet);
		$(Type).className = 'ShowStatus';
		$(Type).style.left = GetIcon.PosX + 'px';
		$(Type).style.top = GetIcon.PosY + 'px';
		$(Type+'List').innerHTML = GetIcon.ListID;
		$(Type+'Title').innerHTML = '<a href="./SimpleLifeShow.html?listid='+GetIcon.ListID+'&order=regidate">'+GetIcon.Title+'</a>';
		
		if (this.IconLR[0].isImgReady && this.IconLR[1].isImgReady) this.BlockAction = false;
	},
	IconLoader: function(Type, ImgPath) {
		var Loader = new Image();
		Loader.onload = function() {
			var TypeSet = this.Type;
			var WidthSet = this.width;
			var HeightSet = this.height;
			setTimeout(function() { CurrentStatus.LoadComplete({Type:TypeSet, ImgW:WidthSet, ImgH:HeightSet, Status:'Ok'}); }, 10);
		};
		Loader.onerror = function() {
			var TypeSet = this.Type;
			setTimeout(function() { CurrentStatus.LoadComplete({Type:TypeSet, Status:'Error'}); }, 10);
		};
		Loader.Type = (Type == 0) ? 'PKImgL' : 'PKImgR';
		Loader.src = ImgPath;
	},
	LoadComplete: function(DataObj) {
		var GetIcon =  (DataObj.Type == 'PKImgL') ? this.IconLR[0] : this.IconLR[1];
		if (DataObj.Status == 'Error') {
			GetIcon.ImgPath = './img/nophotoB.gif';
			this.IconLoader(((DataObj.Type == 'PKImgL') ? 0 : 1) , GetIcon.ImgPath);
		} else {
			GetIcon.isImgReady = true;
			GetIcon.ImgW = DataObj.ImgW;
			GetIcon.ImgH = DataObj.ImgH;
			this.PKImgReady(DataObj.Type);
		}//end if
	},
	CheckOut: function() {
		var CheckAmt = 0;
		var hash = $H();
		 hash['action'] = 'VoteCheckIn';
		var SendFlag = false;
		for (var i in this.InfoObj) if (this.InfoObj[i].Vote != 0) { hash[this.InfoObj[i].ListID+'Set'] = this.InfoObj[i].Vote; SendFlag = true; };
		if (!SendFlag) return;
		//alert(hash.toQueryString());
		var req = new Ajax.Request('./func/Query.php',
									{method: 'post',
									 parameters: hash.toQueryString()
									});
	},
	GetKeyCode: function(e) {
		var KeyCode;
		if (window.event && window.event.srcElement)
			KeyCode = window.event.keyCode;
		else if (e && e.target)
			KeyCode = e.keyCode;
		return KeyCode;
	},
	KeydownEvent: function(e){
		var KeyCode = CurrentStatus.GetKeyCode(e);
		switch (KeyCode) {
			case 37:
				CurrentStatus.VoteAction('VoteL');
				break;
			case 39:
				CurrentStatus.VoteAction('VoteR');
				break;
		}//end switch
	},
	DataListQuery: function(Vars, Api) {
		var req = new Ajax.Request(Api,
									{method: 'post',
									 parameters: Vars.toQueryString(),
									 onComplete: CurrentStatus.QueryDone
									});
	},
	QueryDone: function(r) {
		var ResultObj = r.responseText.evalJSON();
		switch (ResultObj.info) {
			case 'SimpleLifePKErrorNoData':
				alert('NoData');
				break;
			case 'SimpleLifePKSuccess':
				CurrentStatus.VoteAmt = ResultObj.Data.pop();
				CurrentStatus.DataObj = ResultObj.Data;
				$('PKPoint').innerHTML = CurrentStatus.VoteAmt;
				CurrentStatus.InfoObj = {};
				CurrentStatus.ImgArray = [];
				for (var i=0; i<CurrentStatus.DataObj.length; i++) {
					var ComposeID = CurrentStatus.DataObj[i].Nickname+'^'+CurrentStatus.DataObj[i].Pid;
					var DataSet = {};
					DataSet.ListID = CurrentStatus.DataObj[i].ListID;
					DataSet.ImgPath = CurrentStatus.DataObj[i].ImgPath;
					DataSet.Title = CurrentStatus.DataObj[i].Title;
					DataSet.Vote = 0;
					DataSet.isImgReady = false;
					CurrentStatus.InfoObj[ComposeID] = DataSet;
					CurrentStatus.ImgArray.push(ComposeID);
				}//end for
				//for (var i in CurrentStatus.InfoObj) alert(i+' '+CurrentStatus.InfoObj[i].ImgPath);
				$('ContainerL').style.position = 'absolute';
				$('ContainerL').style.clip = 'rect(0px, ' + 310 + 'px, ' + 467 + 'px, 0px)';
				$('ContainerR').style.position = 'absolute';
				$('ContainerR').style.clip = 'rect(0px, ' + 310 + 'px, ' + 467 + 'px, 0px)';
				
				$('VoteL').onclick = function() { CurrentStatus.VoteAction(this.id); };
				$('VoteR').onclick = function() { CurrentStatus.VoteAction(this.id); };
				$('CheckOut').onclick = function() { CurrentStatus.CheckOut(); };
				
				var GetHTML = document.getElementsByTagName('html')[0];
				GetHTML.style.overflowX = 'hidden';
				GetHTML.onkeydown = function(e) { CurrentStatus.KeydownEvent(e) };
				GetHTML.focus();
				Event.observe(window, "unload", function() { CurrentStatus.CheckOut(); }, false);
				
				CurrentStatus.GoPK();
				break;
			default:
				alert(ResultObj.info);
		}//end switch
	}
};

addLoadListener(CurrentStatus.SimpleLifeInit);