// JavaScript Document

$(document).ready(function(){
	var optionNums = $(".Votebg").length;
	for ( var i = 0; i < optionNums; i++ ) {
		var percentNum = $("#percentNum" + i).val();
		var length = percentNum * 200 - 4;
		loadProgressLine("Middlebg" + i,length);
		//$("#Middlebg" + i).width(length);
	}
});

/**
* 加載進度條
* @param Int length 長度
**/
function loadProgressLine(id,length)
{
	for ( i = 1; i <= length; i++ ) {
		$("#" + id).width(i);
	}
}

var OneVote = ({
	
	/**
	* 新增候選項
	**/
	addNewOption: function() {
		if ( $("#optionsNum").val() == '20' ) {
			msgBox.showText("投票選項已達20項，不能再新增了");
			return false;
		}
		var html = '<div style="width:300px;text-align:left;font-size:14px;border-bottom:1px solid #cccccc;padding:5px 5px;margin-bottom:10px;"><b>增加投票項</b></div>';
		html += '<div style="text-align:left;padding-left:20px;margin-bottom:10px;">請輸入新增的投票候選項：</div>';
		html += '<div style="margin-bottom:20px;"><input type="text" id="newOption" style="width:250px;border:1px solid #cccccc;height:18px;padding-top:3px;"></div>';
		html += '<div style="margin-bottom:20px;"><input type="button" value="確認" onclick="OneVote.submitAddNewOption();" class="CptButtom" style="margin-right:5px;"><input type="button" value="關閉" class="CptButtomGray" onclick="msgBox.removeMsgBox();"></div>';
		
		msgBox.showHtml(html);
	},
	submitAddNewOption: function() {
		var voteId = $("#voteId").val();
		var newOption = foramt26str($("#newOption").val());
		if ( newOption.Trim() == '' ) {
			msgBox.showText('投票項不能為空');
			return false;
		}
		$.ajax({
			type: "POST",
			dataType: 'html',
			url: "./voteAjax.php",
			data: "module=index&action=addNewOption&voteId=" + voteId + "&newOption=" + newOption,
			success: function(html){
				// 檢測是否登陸
				if ( !ChkLoginStatus.chkLoginIn(html) ) {
					return false;
				}
				if ( html.indexOf('-1') != -1 ) {
					msgBox.showText("操作錯誤");
					return false;
				} 
				
				msgBox.showText("新增成功");
				//刷新頁面
				window.setTimeout("refreshPage()",2000);
			}
		});
	},
	/**
	* 修改截止時間
	**/
	editEndDate: function() {
		var html = '<div style="width:300px;text-align:left;font-size:14px;border-bottom:1px solid #cccccc;padding:5px 5px;margin-bottom:10px;"><b>修改截止時間</b></div>';
		html += '<div style="text-align:left;padding-left:20px;margin-bottom:10px;">請設置新的截止時間：</div>';
		html += '<div style="margin-bottom:30px;">';
		
		var endDate = $("#endDate").text();
		var arr = endDate.split(' ');
		var dateArr = arr[0].split('-');
		var yearY = dateArr[0];
		var month = dateArr[1];
		var day = dateArr[2];
		var timeArr = arr[1].split(':');
		var hour = timeArr[0];
		
		var date = new Date();
		var year = date.getFullYear();
		html += '<select id="year" onchange="Vote.getDaysWithYM();">';
		for ( var i = year; i <= year + 4; i++ ) {
			html += '<option value=' + i + '>' + i + '</option>';
		}
		html += '</select>年';
		
		html += '<select id="month" onchange="Vote.getDaysWithYM();">';
		for ( var i = 1; i <= 12; i++ ) {
			num = i;
			if ( i < 10 ) num = '0' + i;
			html += '<option value=' + i + '>' + num + '</option>';
		}
		html += '</select>月';
		
		html += '<select id="day">';
		for ( var i = 1; i <= 31; i++ ) {
			num = i;
			if ( i < 10 ) num = '0' + i;
			html += '<option value=' + i + ' id="day' + i + '">' + num + '</option>';
		}
		html += '</select>日';
		
		html += '<select id="hour">';
		for ( var i = 0; i <= 23; i++ ) {
			num = i;
			if ( i < 10 ) num = '0' + i;
			html += '<option value=' + i + '>' + num + '</option>';
		}
		html += '</select>時';
		
		html += '</div>';
		html += '<div style="margin-bottom:20px;"><input type="button" value="確認" onclick="OneVote.submitEditEndDate();" class="CptButtom" style="margin-right:5px;"><input type="button" value="關閉" class="CptButtomGray" onclick="msgBox.removeMsgBox();"></div>';
		
		msgBox.showHtml(html);
		
		//設置年份
		$("#year").val(yearY);
		
		//設置月份
		$("#month").val(month);
		
		//設置日
		$("#day").val(day);
		
		//設置時
		$("#hour").val(hour);
	},
	submitEditEndDate: function() {
		var voteId = $("#voteId").val();
		var date = $("#year").val() + '-' + $("#month").val() + '-' + $("#day").val() + ' ' + $("#hour").val() + ':00:00';
		
		if ( !Vote.chkEndDateCurentDate($("#year").val(),$("#month").val(),$("#day").val(),$("#hour").val()) ) {
			msgBox.showText('截止時間不能早於當前時間');
			return false;
		}
		
		$.ajax({
			type: "POST",
			dataType: 'html',
			url: "./voteAjax.php",
			data: "module=index&action=editEndDate&voteId=" + voteId + "&date=" + date,
			success: function(html){
				// 檢測是否登陸
				if ( !ChkLoginStatus.chkLoginIn(html) ) {
					return false;
				}
				if ( html.indexOf('-1') != -1 ) {
					msgBox.showText("操作錯誤");
					return false;
				} 
				
				msgBox.showText("截止時間修改成功");
				//刷新頁面
				window.setTimeout("refreshPage()",2000);
			}
		});
	},
	/**
	* 發表/修改投票說明
	**/
	editVoteInfo: function() {
		var voteInfo = $("#voteInfo").text();
		var title = '';
		if ( voteInfo == '' ) {
			title = '寫寫投票總結';
		} else {
			title = '修改投票總結';
		}
		var html = '<div style="width:350px;text-align:left;font-size:14px;border-bottom:1px solid #cccccc;padding:5px 5px;margin-bottom:10px;"><b>' + title + '</b></div>';
		html += '<div style="text-align:left;margin:10px 10px;">請輸入新的投票總結：</div>';
		html += '<div style="margin-bottom:30px;"><textarea cols="" rows="" id="voteInfoArea" style="width:300px;height:200px;">' + voteInfo + '</textarea></div>';
		html += '<div style="margin-bottom:20px;"><input type="button" value="確認" onclick="OneVote.submitVoteInfo();" class="CptButtom" style="margin-right:5px;"><input type="button" value="關閉" class="CptButtomGray" onclick="msgBox.removeMsgBox();"></div>';
		
		msgBox.showHtml(html);
	},
	submitVoteInfo: function() {
		var voteId = $("#voteId").val();
		var voteInfo = foramt26str($("#voteInfoArea").val());
		if ( voteInfo.Trim() == '' ) {
			msgBox.showText('投票總結不能為空！');
			return false;
		}
		$.ajax({
			type: "POST",
			dataType: 'html',
			url: "./voteAjax.php",
			data: "module=index&action=editVoteInfo&voteId=" + voteId + "&voteInfo=" + voteInfo,
			success: function(html){
				// 檢測是否登陸
				if ( !ChkLoginStatus.chkLoginIn(html) ) {
					return false;
				}
				if ( html.indexOf('-1') != -1 ) {
					msgBox.showText("操作錯誤");
					return false;
				} 
				
				msgBox.showText("投票總結修改成功");
				//刷新頁面
				window.setTimeout("refreshPage()",2000);
			}
		});
	},
	/**
	* 投票
	**/
	vote: function() {
		var voteId = $("#voteId").val();
		var userId = $("#userId").val();
		var optionsId = OneVote.chkSelOptions();
		
		if ( optionsId == '0' ) {
			msgBox.showText("請選擇投票項");
			return false;
		} else if (optionsId == '-1' ) {
			msgBox.showText("本投票最多只能選擇" + $("#selectNums").text() + "個選項！");
			return false;
		}
		
		optionsId = optionsId.substr(0,optionsId.length-1);
		msgBox.loadingShow(); 
		
		$.ajax({
			type: "POST",
			dataType: 'html',
			url: "./voteAjax.php",
			data: "module=index&action=vote&voteId=" + voteId + "&userId=" + userId + "&optionsId=" + optionsId,
			success: function(html){
				// 檢測是否登陸
				if ( !ChkLoginStatus.chkLoginIn(html) ) {
					return false;
				}
				if ( html.indexOf('-1') != -1 ) {
					msgBox.showText("操作錯誤");
					return false;
				} 
				
				msgBox.showText("投票成功");
				//刷新頁面
				window.setTimeout("refreshPage()",2000);
			}
		});
	},
	/**
	* 刪除投票
	**/
	delVote: function() {
		msgBox.showTrueOrFalse2("刪除","你確認要刪除該投票嗎？","OneVote.submitDelVote()");
	},
	submitDelVote: function() {
		var voteId = $("#voteId").val();
		
		$.ajax({
			type: "POST",
			dataType: 'html',
			url: "./voteAjax.php",
			data: "module=index&action=delVote&voteId=" + voteId,
			success: function(html){
				// 檢測是否登陸
				if ( !ChkLoginStatus.chkLoginIn(html) ) {
					return false;
				}
				if ( html.indexOf('-1') != -1 ) {
					msgBox.showText("操作錯誤");
					return false;
				} 
				
				msgBox.showText("投票刪除成功");
				
				window.location.href = "http://vote." + getUrl() + "/vote.php?action=myVotes";
			}
		});
	},
	/**
	* 檢測選擇的投票項是否正確
	* @return String
	**/
	chkSelOptions: function() {
		var optionsNum = document.getElementsByName("selOption").length;
		var selectNums = $("#selectNums").text();
		
		var hadSelNums = 0;
		var optionsId = '';
		for ( var i = 0; i < optionsNum; i++ ) {
			if ( document.getElementsByName("selOption")[i].checked == true ) {
				hadSelNums++;
				optionsId += document.getElementsByName("selOption")[i].value + ',';
			}
		}
		
		if ( parseInt(hadSelNums) == 0 ) {
			return '0';
		}else if ( parseInt(hadSelNums) > parseInt(selectNums) ) {
			return '-1';
		}
		return optionsId;
	},
	/**
	* 即時檢查選擇的投票項數是否正確
	**/
	chkSelOptionNumsQ: function(obj) {
		//如果是取消選擇的話就不用檢測了 
		if ( obj.checked == false ) {
			return false;	
		}
		
		var result = OneVote.chkSelOptions();
		if ( result == '0' ) {
			msgBox.showText("請選擇投票項");
			return false;
		} else if ( OneVote.chkSelOptions() == '-1' ) {
			msgBox.showText("本投票最多只能選擇" + $("#selectNums").text() + "個選項！");
			obj.checked = false;
			return false;
		}
	}
});

var Comment = ({
	
});

//刷新頁面
function refreshPage()
{
	location.reload();
}