/*
* @ Insurance.js
* @ Author: Guo Chen
**/

(function(){
	function Insurance(settings){
		this.opt=$.extend({
			con:null,//dom
			data:[{title:"5A级旅行社",value:1115}],
			color:["#B1DB4C","#565454"],
			label:{
				color:"#8A898A",
				fontSize:"14px"
			},
			numericalLabel:{
				fontSize:"12px"
			},
			animateTime:2250
		},settings);
		this.init.apply(this,arguments);
	}
	Insurance.prototype={
		init:function(){
			this.render();
			this.barIncrease();
			this.numIncrease(this.opt.con.find('[data-percent]'),'percent');
			this.numIncrease(this.opt.con.find('[data-population]'),'population');
		},
		render:function(){
			var pa=this.opt;
			var uh=this.getUnitHeight();
			var html="";
			var li="";
			for(var i in pa.data){
				li="<li style='margin-bottom:"+uh+"px;'>"
						+"<label style='font-size:"+pa.label.fontSize+";color:"+pa.label.color+";'>"+pa.data[i].title+"</label>"
						+"<div style='margin:0px 15px 0px 10px;vertical-align:middle;width:235px;background-color:"+pa.color[1]+";height:"+2*uh+"px;line-height:"+2*uh+"px;position:relative;display:inline-block;'>"
							+"<div data-bar='"+i+"' style='display:inline-block;border-top-right-radius:5px;border-bottom-right-radius:5px;width:0px;height:"+2*uh+"px;background-color:"+pa.color[0]+";'></div>"
							+"<span data-percent='"+i+"' style='float:right;margin-right:10px;'>0%</span>"
						+"</div>"
						+"<span data-population='"+i+"'>0人</span>"
					+"</li>";
				html+=li;
			}
			pa.con.html("<ul style='text-align:left;font-size:"+pa.numericalLabel.fontSize+"px;color:"+pa.color[0]+";'>"+html+"</ul>");				
		},
		barIncrease:function(){
			var self=this;
			this.opt.con.find("[data-bar]").each(function(){
				var w=self.getWidth($(this).data('bar'));
				$(this).animate({width:w+"px"},self.opt.animateTime);
			});
		},
		numIncrease:function(collection,key){
			var self=this;
			var num;
			var unit;
			collection.each(function(){
				num=((key=="percent") && (self.getPercent($(this).data(key)))) || ((key=="population") && (self.opt.data[$(this).data(key)].value));
				unit=((key=="percent") && ("%")) || ((key=="population") && ("人"));
				$(this).animateNumber({
					number:num,
				    numberStep: function(now, tween) {
				       	var floored_number = ((key=="percent")&&(Math.floor(now*100)/100))|| Math.floor(now);
				            target = $(tween.elem);
				    	target.text(floored_number+""+unit);
				    }
				},self.opt.animateTime);
			});
		},
		getConHeight:function(){
			return this.opt.con.height();
		},
		getUnitHeight:function(){
			var uh=this.getConHeight()/(3*this.opt.data.length-1);
			return (uh>0)?(uh):(0);
		},
		getPercent:function(t){
			var total=0;
			for(var i=0 in this.opt.data){
				total+=this.opt.data[i].value;
			}
			return Math.round(this.opt.data[t].value/total*10000)/100;
		},
		getWidth:function(t){
			var valArray=new Array();
			for(var i=0 in this.opt.data){
				valArray.push(this.opt.data[i].value);
			}
			var maxVal=Math.max.apply(null,valArray);
			var width=180/maxVal*this.opt.data[t].value;
			return width;
		}
	}
	Insurance.prototype.constructor=Insurance;
	window.jClass_Insurance=Insurance;
})();