CYC.Constant["Comment.up"] = "支持";
CYC.Constant["Comment.down"] = "反对";
CYC.Constant["Product.pageView"] = "产品浏览量";
CYC.Constant["Product.order"] = "订单量";


CYC.stat=function (elementCss, targetTypes, options){
	this.init(elementCss, targetTypes, options);
}

jQuery.extend(CYC.stat.prototype,{
	/**初始化 */
	init:function (elementCss, targetTypes, options){
		this.options = {
			createButton: true,
			autoUpdate: false,
			statCookieName: "statRestrict"
		}
		jQuery.extend(this.options, options);
		var instance = this;
		this.elements = jQuery(elementCss);		
		this.targetTypes = targetTypes;
		this.targetIds = [];		
		this.createButtonHtml();		
		if(this.options.autoUpdate)	{
			ElementStatDwrService.addSumStat(this.targetIds[0], this.targetTypes[0]);
		}
		this.initStat();
	},	
	
	createButtonHtml: function(){
		var instance = this;
		this.elements.each(function(){
			var element = jQuery(this);			
			
			jQuery.each(instance.targetTypes, function(i, targetType){	
				if(instance.options.createButton){
					var button = jQuery("<a href='javascript:void(0);' style='text-decoration: underline;color:#3F79CA;margin-left:10px;margin-right:3px;'></a>");
					button.text(CYC.Constant[targetType]);
					button.attr("ref", targetType);
					button.bind("click", instance.clickButtonEvent.cycbind(instance));
					button.appendTo(element);
				}else{
					var button = jQuery("<span/>");
					button.text(CYC.Constant[targetType]+":");
					button.attr("ref", targetType);
					button.appendTo(element);
				}
				var label = jQuery("<label>(0)</label>");
				label.appendTo(element);				
			});						
			instance.targetIds.push(element.attr("ref"));			
		});
	},	
	
    clickButtonEvent : function(event) {
		var target = jQuery(event.target);
		var label = target.next();
		var targetType = target.attr("ref");
		var targetId = target.parent().attr("ref");
		cookieForm = {};
		cookieForm.id = targetId;
		cookieForm.type = targetType.substring(0, targetType.lastIndexOf("."));
		if(this.isCookieExist(cookieForm)){
			alert("感谢您的评价，您今天已经评价过了！");			
		}else{
			this.updateSumStat(label,targetId,targetType,cookieForm);	
		}
	}, 	
	updateSumStat:function(label,targetId,targetType,cookieForm){		
		var instance = this;
		ElementStatDwrService.addSumStat(targetId, targetType, {
			callback : function(data) {
				label.text("("+data+")");
				instance.setCookie(cookieForm);
			}
		});
		
	},
    initStat: function(){
    	if(this.targetIds.length > 0){
	    	var instance = this;    	
	    	ElementStatDwrService.getSumStat(this.targetIds, this.targetTypes, {callback:function(data){
	    		jQuery.each(data, function(i, object){	
	    			var targetId = object[0];
	    			var isFind = false;
	    			instance.elements.each(function(){
	    				var element = jQuery(this);
	    				var ref = element.attr("ref");
	    				if(targetId == ref){
	    					element.find("label").each(function(i){
	    						jQuery(this).text("("+object[i+1]+")");
	    					});
	    				}
	    			});
	    		});
			}});
    	}
    },
    
    isCookieExist: function(cookieForm){
    	var cookieObjects = this.getCookie();
    	for(var i=0; i< cookieObjects.length; i++){
    		var temp = cookieObjects[i];
    		if(temp.id == cookieForm.id && temp.type==cookieForm.type){
    			return true;
    		}
    	}
    },
    
    setCookie:function (cookieForm){
    	var cookieObject = this.getCookie();
    	if(!this.isCookieExist(cookieForm)){
    		cookieObject.push(cookieForm);
        	jQuery.cookie(this.options.statCookieName, jQuery.toJSON(cookieObject), {path: '/'});
    	}    	
    },
	    
    getCookie:function (){
    	 var value = jQuery.cookie(this.options.statCookieName);
    	 if(value != null){
    		 return jQuery.parseJSON(value);
    	 }
    	 return [];
    }	
});





