(function(){
	function Age(settings){
		this.opt=$.extend({
			con:null,
			icon:"images/people.png",
			data:[{category:10,value:93,color:'#2CEEEE'}],
			aix:{
				color:'#FFFFFF',
				fontSize:'14px'
			},
			animateTime:2250,
			iconW:45,
			iconH:114
		},settings);
		this.init.apply(this,arguments);
	}
	Age.prototype={
		init:function(){
			this.render();
		},
		render:function(){
			this.opt.con.html(this.getChart()+this.getAix()+this.getLabel());
			this.numIncrease(this.opt.con.find('[data-percent]'));
			this.imgSize();
		},
		getAix:function(){
			var html="";
			var span="";
			for(var i=0 in this.opt.data){
				if(i!=0){
					span="<span style='display:inline-block;height:14px;width:61px;border-bottom:1px solid #CCCCCC;border-right:1px solid #CCCCCC;'></span>";
				}
				else{
					span=span="<span style='display:inline-block;height:14px;width:62px;border-bottom:1px solid #CCCCCC;border-left:1px solid #CCCCCC;border-right:1px solid #CCCCCC;'></span>";
				}
				html+=span;
			}
			html="<div style='text-align:center;'>"+html+"</div>";
			return html;
		},
		getChart:function(){
			var html="";
			var li="";
			for(var i=0 in this.opt.data){
				li="<li style='width:45px;display:inline-block;margin:0px 9px;'>"
						+"<div style='margin-bottom:15px' data-percent='"+i+"'></div>"
						+"<img data-person='"+i+"' src='"+this.opt.icon+"' style='vertical-align:bottom;width:0px;height:0px;'/>"
					+"</li>";
				html+=li;
			}
			html="<ul style='text-align:center;font-size:14px;color:"+this.opt.data[0].color+";'>"+html+"</ul>";
			return html;
		},
		getLabel:function(){
			var html="";
			var li="";
			for(var i=0;i<=this.opt.data.length;i++){
				if(i==0){
					li="<li style='width:64px;display:inline-block;'>0岁</li>";
				}
				else{
					li="<li style='width:64px;display:inline-block;'>"+this.opt.data[i-1].category+"岁</li>";
				}
				
				html+=li;
			}
			html="<ul style='margin-top:5px;text-align:center;font-size:"+this.opt.aix.fontSize+";color:"+this.opt.aix.color+";'>"+html+"</ul>";
			return html;
		},
		imgSize:function(){
			var sizeArray=new Array();
			for(var i=0 in this.opt.data){
				sizeArray.push(this.opt.data[i].value);
			}
			var max=Math.max.apply(null,sizeArray);
			var uw=this.opt.iconW*0.5/max;
			var uh=this.opt.iconH*0.5/max;
			var self=this;
			this.opt.con.find("[data-person]").each(function(){
				$(this).animate({
					width:self.opt.data[$(this).data('person')].value*uw+0.5*self.opt.iconW,
					height:self.opt.data[$(this).data('person')].value*uh+0.5*self.opt.iconH
				},self.opt.animateTime);
			});
		},
		numIncrease:function(collection){
			var self=this;
			collection.each(function(){
				$(this).animateNumber({
					number:self.getPercent($(this).data('percent')),
				    numberStep: function(now, tween) {
				       	var floored_number = Math.floor(now*100)/100;
				            target = $(tween.elem);
				    	target.text(floored_number+"%");
				    }
				},self.opt.animateTime);
			});
		},
		getPercent:function(t){
			var sum=0;
			for(var i=0 in this.opt.data){
				sum+=this.opt.data[i].value;
			}
			return Math.round(this.opt.data[t].value/sum*10000)/100;
		}
	}
	Age.prototype.constructor=Age;
	window.jClass_Age=Age;
})();