﻿/**
 * Activity * 
 * @author Ray Law
 * @version 1.0
 * 
 * copyright 2007, Ray Law
 * 
 * This class handles the AJAX call and utility function for TVministry - series admin page
 */


function Activity(){
	//alert("Hello! I'm Activity !")
	this.$function_gateway = 'activity_function.php';
	
	//UI Object
	this.$_LIST_DIV;
	this.$_LOADING_DIV;
	this.$_RESULT_DIV;
	this.$_COUNT_DIV;
	this.$_IMG_DIV;
	this.$_PAGE;
	this.$_SEARCH_FORM;
	this.$_INSERT_FORM;
	this.$_EDIT_FORM;
	this.$_PARENT_REF;
	
	this.$last_page_index = 0;
	this.$record_per_page = 10;
	this.$loading_icon = '<img src="images/icon/circleball-loader-red.gif" width="16" height="16">';
	this.$delete_icon = '<img src="images/icon/delete2.gif" width="14" height="12" border="0">';
	
	//Error message array
	this.$msg = new Array();
	//FAQ message array
	this.$faq_msg = new Array();
	
	/* Define your error message below */
	this.$msg[0] = "新增成功!";
	this.$msg[1] = "新增失敗!!!";
	this.$msg[2] = "刪除成功!";
	this.$msg[3] = "刪除失敗!!!";
	this.$msg[4] = "修改成功!";
	this.$msg[5] = "修改失敗!!!";
	this.$msg[6] = "請先選擇一個資源連結!!!";
	this.$msg[7] = "請先選擇一個記錄!!!";
	this.$msg[8] = "上載成功!";
	this.$msg[9] = "上載失敗!!!";
	this.$msg[10] = "上載失敗!!! 只可上載圖片!";
	/* ---------------------------------*/
	
	/* Define your faq message below */
	this.$faq_msg[0] = "FAQ 0!";
	this.$faq_msg[1] = "FAQ 1!";
	this.$faq_msg[2] = "FAQ 2!";
	/* ---------------------------------*/
	this.$date = new Date();
	//this.$current_year = 2004;
	//this.$current_month = 1;
	//this.$pointer = 0;
	
	//alert(this.$current_year);
	//this.$year = new Array();
	//this.$month = new Array();
	//for($i=0; $i < 4 ;$i++){
		//$i=0;
	//while(this.$current_year <= this.$date.getFullYear()){
		
		//if(this.$current_month > 12){
//			this.$current_year++;
//			this.$current_month =1;
//		}
//		//alert(this.$current_month);
//		this.$year[$i] =  this.$current_year ;
//		this.$month[$i] = this.$current_month;
//		this.$current_month++;
//		$i++;
//	}
	this.$current_month = this.$date.getMonth() + 1;
	this.$current_year = this.$date.getFullYear();
	

	
};


Activity.prototype.getActivity = function($year,$month,$day){
	$path = $year+$month+$day;
	document.location = 'activity_content.php?id='+$path+"&year="+$year+"&month="+$month*1;
	
}

Activity.prototype.nextMonth = function(){
	//this.$pointer++;
	this.$current_month++;
	if(this.$current_month > 12){
		this.$current_year++;
		this.$current_month =1;
	}
	
	
//	if(this.$pointer > 3){
//		this.$pointer = 3;
//		alert('資料未端');
//		return;	
//	}
	
	this.getEventCalendar(this.$current_year,this.$current_month);
	
}

Activity.prototype.previousMonth = function(){
	
	this.$current_month--;
	if(this.$current_month <= 0){
		this.$current_year--;
		this.$current_month =12;
	}
	
	this.getEventCalendar(this.$current_year,this.$current_month);
	
}

//Adding Methods
/**
 * A function used to get a Activity rescord detail by Date using AJAX
 * 
 * @param {int} $id
 * 		The target record ID. 
 */
Activity.prototype.getEventCalendar = function($year,$month){	
	$('calendar').innerHTML =this.$loading_icon;
	this.$current_year = $year;
	this.$current_month = $month;
	var $param = "year="+$year+"&month="+$month;
	//alert($param);
	this.ajaxCall(this.$function_gateway+'?_JOB=_GET_CALENDAR',$param,this.getEventCalendarFinish.bind(this));
	
	//this.showLoadingIcon($('result'));
}

/**
 * A AJAX callback function of getActivityByDate()
 * 
 * @param {object} transport
 * 		The AJAX retrun - output string from function gateway 
 */
Activity.prototype.getEventCalendarFinish = function($transport){
	var $response = $transport.responseText || "no response text";
	//var $arr = unserialize(base64decode($response));
	
	/* Define to action you want */
    //alert($response);
	$('_SHOW_MONTH').innerHTML = this.$current_year+"年"+this.$current_month+"月";
	$('calendar').innerHTML = $response;
	
}

/**
 * A function used to get a Activity rescord detail by Date using AJAX
 * 
 * @param {int} $id
 * 		The target record ID. 
 */
Activity.prototype.getEventByDate = function($date){	
	$('_DATE_LIST').innerHTML =this.$loading_icon;
	if($date == null){
		this.msg(7);
		return;
	}
	
	var $param = "date="+$date+"&_ORDER_BY=start_date";
	//alert($param);
	this.ajaxCall(this.$function_gateway+'?_JOB=_GET_EVENT_LIST',$param,this.getEventByDateFinish.bind(this));
	
	//this.showLoadingIcon($('result'));
}

/**
 * A AJAX callback function of getActivityByDate()
 * 
 * @param {object} transport
 * 		The AJAX retrun - output string from function gateway 
 */
Activity.prototype.getEventByDateFinish = function($transport){
	var $response = $transport.responseText || "no response text";
	//var $arr = unserialize(base64decode($response));
	
	/* Define to action you want */
//	alert($response);
	
	$('_DATE_LIST').innerHTML = $response;
	
}



/**
 * A function used to get a Activity rescord detail by ID using AJAX
 * 
 * @param {int} $id
 * 		The target record ID. 
 */
Activity.prototype.getEventByID = function($id){	
	if($id == null){
		this.msg(7);
		return;
	}
	
	var $param = "ID="+$id;
	this.ajaxCall(this.$function_gateway+'?_JOB=_GET_SERIES',$param,this.getEventByIDFinish.bind(this));
	
	this.showLoadingIcon($('result'));
}

/**
 * A AJAX callback function of getActivityByID()
 * 
 * @param {object} transport
 * 		The AJAX retrun - output string from function gateway 
 */
Activity.prototype.getEventByIDFinish = function($transport){
	var $response = $transport.responseText || "no response text";
	var $arr = unserialize(base64decode($response));
	
	/* Define to action you want */
	
	this.hideLoadingIcon($('result'));
	//alert($arr[0]['ID']);
	$record = $arr[0];
	$('ID').value = $record['ID'];
	$('ts_name').value = $record['ts_name_big5'];
	$('ts_brief').value = $record['ts_brief_big5'];
	$('ts_description').value = $record['ts_description_big5'];
	$('ts_web_url').value = $record['ts_web_url'];
	$('ts_web_url').value = $record['ts_web_url'];
	$('ts_web_url').value = $record['ts_web_url'];
	
	if($record['ts_status'] == 'public'){
		$('ts_status').options.selectedIndex = 0;
	}else{
		$('ts_status').options.selectedIndex = 1;
	}
	//if($record['ts_file'].length==""){
	
	var $file = $record['ts_file'];
	if($file == null){
		this.$_IMG_DIV.innerHTML = "";
	}else{
		this.showIMG($record['ts_file_ref'],$record['ts_file']['file_src_pathname'],this.$_IMG_DIV);
	}	
	
	this.setPostDate($record['ts_show_date']);
	this.setParentForEdit($record['resources_ref'],$record['r_name_big5']);
	//}
	
}

/**
 * A function used to get a Activity rescord detail by ID using AJAX
 * 
 * @param {int} $id
 * 		The target record ID. 
 */
Activity.prototype.getActivityList = function($reset_form){	
	
	if($reset_form != null){
		this.$last_page_index = 0;
		if(this.$_PAGE.options.length > 0){
			this.$_PAGE.options[0].selected = true;
		}
	}
	//Show loading icon
	this.showLoadingIcon(this.$_COUNT_DIV);
	//alert('Hey!');
	$param = this.$_SEARCH_FORM.serialize();
	this.ajaxCall(this.$function_gateway+'?_JOB=_GET_SERIES_LIST',$param,this.getActivityListFinish.bind(this));
}

/**
 * A function used to get Activity rescord list using AJAX
 * 
 * @param {object} transport
 * 		The AJAX retrun - output string from function gateway 
 */
Activity.prototype.getActivityListFinish = function(transport){
	
	var response = transport.responseText || "no response text";
	

	//alert(this.$function_gateway);
	//window.series.$function_gateway;


	//Show returned html code
	this.$_RESULT_DIV.innerHTML = response;
	
	
	//Get total number of record
	var datePart = response.match(/<!--TNR:([\d]*)-->/);  
	var TNR = datePart[1]; 
	
	//Show total number of record message
	this.$_COUNT_DIV.innerHTML = "共有 "+TNR+" 記錄";
  
   //Update the page control
  for($i = 0 ; $i < TNR*1 / this.$record_per_page ; $i++){
  	this.$_PAGE.options[$i] = new Option('第'+($i+1)+'頁',$i*10);
	if($i == this.$last_page_index){
		this.$_PAGE.options[$i].selected = true;
	}
  }
}

/**
 * Show Loading icon to target <DIV> or <SPAN>
 * 
 * @param {object} $div
 * 		<DIV> or <SPAN> object 
 */
Activity.prototype.showLoadingIcon = function($div){
	
	$div.innerHTML = this.$loading_icon ;
	
}

/**
 * Hide Loading icon to target <DIV> or <SPAN>
 * 
 * @param {object} $div
 * 		<DIV> or <SPAN> object 
 */
Activity.prototype.hideLoadingIcon = function($div){
	
	$div.innerHTML = "" ;
	
}

Activity.prototype.msg = function($no){
	alert(this.$msg[$no]);	
}

Activity.prototype.faq = function($no){
	alert(this.$faq_msg[$no]);	
}

Activity.prototype.nextPage = function(){
	
	if(this.$_PAGE.options.length > 1 && this.$_PAGE.selectedIndex < this.$_PAGE.options.length - 1){
		this.$_PAGE.selectedIndex++;
		this.$last_page_index = this.$_PAGE.selectedIndex;
		this.getActivityList();
	}else{
		alert('頁尾!');
	}

}

Activity.prototype.lastPage = function(){
	if(this.$_PAGE.options.length > 1){ 
	this.$_PAGE.selectedIndex = this.$_PAGE.options.length - 1;
	this.$last_page_index = this.$_PAGE.selectedIndex;
		this.getActivityList();
	}
}

Activity.prototype.firstPage = function(){
	if(this.$_PAGE.options.length > 1 ){
	this.$_PAGE.selectedIndex = 0;
	this.$last_page_index = this.$_PAGE.selectedIndex;
		this.getActivityList();
	}
}

Activity.prototype.backPage = function(){
	
	if(this.$_PAGE.options.length > 1 && this.$_PAGE.selectedIndex > 0){
		this.$_PAGE.selectedIndex--;
		this.$last_page_index = this.$_PAGE.selectedIndex;
		this.getActivityList();
	}else{
		alert('頁頂!');
	}

}

Activity.prototype.ajaxCall = function($url , $para , $callBack){

new Ajax.Request($url,   

{     
method:'post',
parameters: $para,
  
onSuccess: $callBack,     

onFailure: function(){ 
alert('Something went wrong...') 
}   

}); 

}




