var LOG_IN = {};
LOG_IN.rootpath = 'cgi/';
LOG_IN.List = {
	month_pos: 0
,	start_weekday: 0 /*0~6*/
,	last_day: 0 /*28~31*/
,	list: []
};
LOG_IN.getLogInList = function(){
	var err_id = 'ListTableError';
	var token = T.GetToken();
	if(!token){
		T.error(err_id, 'You must be logged in to view your dragonica online stamps.  Please log in at the top of this page and try again.  If you do not have a THQ*ICE Passport, click Register Now to get your free account.');
		return;
	}
	$.ajax({
        type: "POST",
        url: LOG_IN.rootpath + "log_in_list.php",
        dataType: 'json',
        data: {
        	token: token
		,	month_pos: LOG_IN.List.month_pos
		,	timenow: new Date()
        },
        timeout: 30000,
        success: function(json){
            if (json.code === RETURN_CODE.SUCCESS) {
            	LOG_IN.List.month_pos = json.month_pos;
            	LOG_IN.List.start_weekday = json.start_weekday;
            	LOG_IN.List.last_day = json.last_day;
            	LOG_IN.List.list = json.list;
            	var tbl = '', len = json.list.length, i =0, pgs = '', idx = {}, j = 0, w=0, r=0;
            	
				for(; i < len; i++){
					idx['i' + json.list[i].Day] = 1;
				}
				tbl += '<tr class="log_in_title"><th class=""></th><th class=""></th><th class=""></th><th class=""></th><th class=""></th><th class=""></th><th class=""></th></tr>';
				
				for(i= 1 - LOG_IN.List.start_weekday; i<=LOG_IN.List.last_day; i++){
					j = w%7;
					if(j == 0){
						tbl += '<tr>';
						r++;
					}
					if(i<=0){
						tbl += '<td></td>';
					}else{
						if(typeof (idx['i' + i]) != 'undefined'){
							tbl += '<td class="' + ( (j == 0 || j == 6) ? 'log_in_stamp_pink' : 'log_in_stamp_white' ) + '">' + i + '</td>';
						}else{
							tbl += '<td>' + i + '</td>';
						}
					}
					if(j == 6){
						tbl += '</tr>';
					}
					w++;
				}
				while(w%7 != 0){
					tbl += '<td></td>';
					w++;
				}
				tbl += '</tr>';
				tbl = '<table width="534" border="0" cellspacing="1" cellpadding="1">' + tbl + '</table>';
				document.getElementById('LogInListTable').className = 'calendar_bg_' + r;
            	$('#LogInListTable').html(tbl);
				var tblC = '';
				tblC = '<table><tr><td><img src="../../images/special_log_in/month_prev.jpg" onclick="javascript:LOG_IN.prevMonth();" style="cursor:pointer;" /></td>' +
				'<td><img src="../../images/special_log_in/month_'+json.month+'.jpg" /></td>' +
				'<td><img src="../../images/special_log_in/year_'+json.year+'.jpg" /></td>' +
				'<td><img src="../../images/special_log_in/month_next.jpg" onclick="javascript:LOG_IN.nextMonth();" style="cursor:pointer;" /></td>' +
				'</tr></table>';
            	$('#LogInListCtrl').html(tblC);
				$('#LogInStampsCount').html('You have ' + len + ' stamps so far.');
            }else {
            	if(json.msg == -1){
            		T.error(err_id, 'Your session timed out. Please log in again.');
            	}else{
                	T.error(err_id, json.msg);
            	}
            }
        },
		error:function(){
			T.error(err_id, getLang('http_request_faild'));
		}
	});
};
LOG_IN.prevMonth = function(){
	LOG_IN.List.month_pos -= 1;
	LOG_IN.getLogInList();
};
LOG_IN.nextMonth = function(){
	LOG_IN.List.month_pos += 1;
	LOG_IN.getLogInList();
};
SSO.LoginActivateFunction = function(){
	window.location.reload();
};
SSO.LogoutActivateFunction = function(){
	window.location.reload();
};