/* The copy right is belong to idealhome.com.tw
	 Author= Arthur Chang	 (張榮庭)
	 this document created day 2008/07/30
 */
/*
 * prerequisite : GenericHTMLObjOrganizationFunc.js, pageCounter.js
 */


function ObjectGrowingStatistics(){
	this.initial();
	this.underDoGrowingCounting = false;
	this.counter = new PageCounter();
}

ObjectGrowingStatistics.prototype.initial = function(){
	this.defineHTMLObjectId();
	this.defineHTMLObject();	
	this.setActivedBtn(this.visitor_growing_counter_btn);	
	this.initializeEventHandlers();	
}

ObjectGrowingStatistics.prototype.defineHTMLObjectId= function(){
	this.id_visitor_growing_counter_btn = "visitor_growing_counter";
	this.id_post_growing_counter_btn = "post_growing_counter";
	this.id_landlord_growing_counter_btn = "landlord_growing_counter";
	this.id_tenant_growing_counter_btn = "tenant_growing_counter";
	this.id_post_deal_completed_growing_counter_btn = "post_deal_completed_growing_counter";
	this.id_stastics_counter_image= "counter_pic";
	this.id_statistics_unit= "statistics_unit";	
	this.id_numberOfObjectCountDiv= "numberOfObjectCountDiv";
}

ObjectGrowingStatistics.prototype.defineHTMLObject= function(){
	this.visitor_growing_counter_btn = document.getElementById(this.id_visitor_growing_counter_btn);
	this.post_growing_counter_btn = document.getElementById(this.id_post_growing_counter_btn);
	this.landlord_growing_counter_btn = document.getElementById(this.id_landlord_growing_counter_btn);
	this.tenant_growing_counter_btn = document.getElementById(this.id_tenant_growing_counter_btn);	
	this.post_deal_completed_growing_counter_btn = document.getElementById(this.id_post_deal_completed_growing_counter_btn);
	this.statistics_unit = document.getElementById(this.id_statistics_unit);
	this.numberOfObjectCountDiv = document.getElementById(this.id_numberOfObjectCountDiv);
}

ObjectGrowingStatistics.prototype.setActivedBtn = function(target){
	// normal color
	if(this.activedbtn){
		this.activedbtn.style.backgroundColor ="#F0F0F0";
		this.activedbtn.style.color ="#006699";
	}
	// actived color
	this.activedbtn = target;
	this.activedbtn.style.backgroundColor ="#D9D9DB";
	this.activedbtn.style.color ="#000";
}

ObjectGrowingStatistics.prototype.initializeEventHandlers = function(){

	this.visitor_growing_counter_btn.objectGrowingStatistics = this;
	this.visitor_growing_counter_btn.onclick = function(){
		this.objectGrowingStatistics.doGrowingCounting("visitor");
		this.objectGrowingStatistics.setActivedBtn(this);
		this.objectGrowingStatistics.statistics_unit.innerHTML = "瀏覽人數";
		};

	this.post_growing_counter_btn.objectGrowingStatistics = this;
	this.post_growing_counter_btn.onclick = function(){
		this.objectGrowingStatistics.doGrowingCounting("post");
		this.objectGrowingStatistics.setActivedBtn(this);
		this.objectGrowingStatistics.statistics_unit.innerHTML = "出租物件數";
		};
	this.post_growing_counter_btn.onmouseover = function(){
			if(!this.objectGrowingStatistics.underDoGrowingCounting){
				this.objectGrowingStatistics.doCountingObjectSize("sumOfPost");
				this.objectGrowingStatistics.setActivedBtn(this);
			}
		};

	this.landlord_growing_counter_btn.objectGrowingStatistics = this;
	this.landlord_growing_counter_btn.onclick = function(){
		this.objectGrowingStatistics.doGrowingCounting("landlord");
		this.objectGrowingStatistics.setActivedBtn(this);
		this.objectGrowingStatistics.statistics_unit.innerHTML = "房東人數";
		};
	this.landlord_growing_counter_btn.onmouseover = function(){
		if(!this.objectGrowingStatistics.underDoGrowingCounting){
			this.objectGrowingStatistics.doCountingObjectSize("sumOfLandlord");
			this.objectGrowingStatistics.setActivedBtn(this);
		}
		};

	this.tenant_growing_counter_btn.objectGrowingStatistics = this;
	this.tenant_growing_counter_btn.onclick = function(){
		this.objectGrowingStatistics.doGrowingCounting("tenant");
		this.objectGrowingStatistics.setActivedBtn(this);
		this.objectGrowingStatistics.statistics_unit.innerHTML = "房客人數";
		};
	this.tenant_growing_counter_btn.onmouseover = function(){
		if(!this.objectGrowingStatistics.underDoGrowingCounting){
			this.objectGrowingStatistics.doCountingObjectSize("sumOfTenant");
			this.objectGrowingStatistics.setActivedBtn(this);
		}
		};

	this.post_deal_completed_growing_counter_btn.objectGrowingStatistics = this;
	this.post_deal_completed_growing_counter_btn.onclick = function(){
		this.objectGrowingStatistics.doGrowingCounting("dealCompletedPost");
		this.objectGrowingStatistics.setActivedBtn(this);
		this.objectGrowingStatistics.statistics_unit.innerHTML = "出租物件成交數";
		};
	this.post_deal_completed_growing_counter_btn.onmouseover = function(){
		if(!this.objectGrowingStatistics.underDoGrowingCounting){
			this.objectGrowingStatistics.doCountingObjectSize("sumOfDealCompletedPost");
			this.objectGrowingStatistics.setActivedBtn(this);
		}
		};
}

var objectGrowingStatisticsObject;
var xmlHttpGCCounting;
ObjectGrowingStatistics.prototype.doGrowingCounting = function (forWhat){
	if (this.underDoGrowingCounting){
		alert("處理中");
		return;
	}

		this.underDoGrowingCounting = true;
		xmlHttpGCCounting=GetXmlHttpObject();
		if (xmlHttpGCCounting==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		objectGrowingStatisticsObject = this;
	//	xmlHttpGCCounting.owner = this;
		var url="?plugins=object-growing-statistics&action=" + forWhat;
		xmlHttpGCCounting.onreadystatechange=this.getGrowingCountingResult;
		xmlHttpGCCounting.open("GET",url,true,"","");
		xmlHttpGCCounting.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttpGCCounting.send(null);
		this.setStatisticsCounterImage("images/loading01.gif", true);

}

ObjectGrowingStatistics.prototype.getGrowingCountingResult = function (){
	if (xmlHttpGCCounting.readyState==4 || xmlHttpGCCounting.readyState=="complete")
	{
		var response;
		response = xmlHttpGCCounting.responseText;		
		if (response != ""){
			lines = response.split("\n");
			if(lines[0]=="success"){
				objectGrowingStatisticsObject.setStatisticsCounterImage(lines[1]);
				//xmlHttpGCCounting.owner.setStatisticsCounterImage(lines[1]);
			}else{
				alert(lines[1]);
			}
			
		}
		//xmlHttpGCCounting.owner.underDoGrowingCounting = false;
		objectGrowingStatisticsObject.underDoGrowingCounting = false;
	}
}

ObjectGrowingStatistics.prototype.setStatisticsCounterImage = function(src, needreload){
	if(!needreload){
		document.getElementById(this.id_stastics_counter_image).src = src + "?" + Math.random();
	}else{
		document.getElementById(this.id_stastics_counter_image).src = src;
	}
}

ObjectGrowingStatistics.prototype.invokeGrowingCounting = function(target){
	launchAnEvent(target, "click");
}


var xmlHttpCOS;
ObjectGrowingStatistics.prototype.doCountingObjectSize = function (forWhat){
	if (this.underDoCountingObjectSize){
		return;
	}
		this.underDoCountingObjectSize = true;
		xmlHttpCOS=GetXmlHttpObject();
		if (xmlHttpCOS==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		//xmlHttpCOS.owner = this;  // this code can only work on IE 7 and firefox
		objectGrowingStatisticsObject = this;
		var url="?plugins=object-growing-statistics&action=" + forWhat;
		xmlHttpCOS.onreadystatechange=this.getCountingObjectSizeResult;
		xmlHttpCOS.open("GET",url,true,"","");
		xmlHttpCOS.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttpCOS.send(null);
		this.setCountingObjectSizeContent(true);
}

ObjectGrowingStatistics.prototype.getCountingObjectSizeResult = function (){
	if (xmlHttpCOS.readyState==4 || xmlHttpCOS.readyState=="complete")
	{
		var response;
		response = xmlHttpCOS.responseText;
		if (response != ""){
			lines = response.split("\n");
			if(lines[0]=="success"){
				//xmlHttpCOS.owner.setCountingObjectSizeContent(false,lines[1]);  
				objectGrowingStatisticsObject.setCountingObjectSizeContent(false,lines[1]); 
			}else{
				alert(lines[1]);
			}
			
		}
		//xmlHttpCOS.owner.underDoCountingObjectSize = false;  
		objectGrowingStatisticsObject.underDoCountingObjectSize = false; 
	}
}

ObjectGrowingStatistics.prototype.setCountingObjectSizeContent = function(counting, number){
	if(!counting){
		this.counter.converNumToImgs(number, this.numberOfObjectCountDiv, "white");
	}else{
		this.numberOfObjectCountDiv.innerHTML = "";
	}
}



