/**
 * 
 * @cchen
 * @date    2016-07-25 
 * @version 1.0
 * @plugin Toast
 */
;(function(root,factory){
	root.toast = factory();

})(window,function(){
	var toast = function(options){
		this.opt = {};
		this.opacity=0;
		this.raf='';
		this.st='';
		this.status = 0;
		this.loadingEle = null;
		window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
		window.cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
		this.extend(options);
	};
	toast.prototype = {
		extend: function(options){
			var defaults = {
				bgColor: '#666',  //16进制  rbga均可
				textColor: '#fff',
				pos: 'bottom',  //bottom,center,num(from bottom)
				unit: 'px',   //rem,%
				timer: 2000   //toast停留时间
			};
			var key;
			for(key in options){
				var defaultVal = defaults[key];
				var optionVal = options[key];
				if(optionVal == defaultVal){
					continue;
				}
				else if(optionVal !== undefined){
					defaults[key] = optionVal;
				}
				
			}
			this.opt = defaults;

		},
		
		fadeIn:function(newToast,type){
			var _this = this;
			_this.opacity+=0.01;
			newToast.style.opacity = _this.opacity;
			if(_this.opacity<1 && _this.status==0 ){
				_this.raf = requestAnimationFrame(function(){
					_this.fadeIn(newToast,type);
				});
			}
			else{
				cancelAnimationFrame(_this.raf);
				if(type == 1){
					_this.st = setTimeout(function(){
						_this.raf = _this.fadeOut(newToast);
					},_this.opt.timer);
				}
			}
		},
		fadeOut:function(newToast){
			var _this = this;
			_this.opacity-=0.01;
			_this.status = 1;
			newToast.style.opacity = _this.opacity.toFixed(2);
			if(_this.opacity>0){
				requestAnimationFrame(function(){
					_this.raf = _this.fadeOut(newToast);
				});
			}
			else{
				cancelAnimationFrame(_this.raf);
				newToast.style.zIndex = -99;
			}

		},
		createEntity: function(msg,timer,type){
			 var _this = this;
			 var idNum = Math.random().toString().replace('.', '');
			 var toastId = 'toast'+idNum;
			 var body = document.getElementsByTagName('body')[0];
			 var newToast = document.createElement("div");
			 var posY='0';
			 _this.status = 0;
			 if(typeof(timer) == 'number'){
			 	this.opt.timer = timer;
			 }
			 if(type ==1){
			 	newToast.innerHTML = msg;
			 } 
			 else if(type == 2){
				newToast.innerHTML = msg;
				var loaderIcon = document.createElement("img");
				loaderIcon.src=baseUrl + "/static/weixin/cd/images/toast-load.gif";
				loaderIcon.setAttribute('style','vertical-align:top;margin-right:5px;display:inline-block;width:15px;');
				newToast.insertBefore(loaderIcon, newToast.firstChild);
			 }
			 newToast.id = toastId;  //设置id
			 newToast.className = 'cToast';
			 if(_this.opt.pos == 'center'){
				 posY = 'top:50%';
			 } 
			 else if(_this.opt.pos == 'bottom'){
			 	posY = 'bottom:15'+_this.opt.unit;
			 }
			 else if(typeof(_this.opt.pos) == 'number'){
			 	posY= 'bottom:'+_this.opt.pos+_this.opt.unit;
			 }

		 	 newToast.setAttribute('style','position:fixed;'+posY+';padding:15px 15px;font-size:14px;background-color:'+_this.opt.bgColor+';color:'+_this.opt.textColor+';opacity:0;border-radius:3px;max-width:80%;z-index:999;');
			 body.appendChild(newToast);
			 var toastW = newToast.innerWidth ? newToast.innerWidth : newToast.clientWidth;
			 newToast.style.left = '50%'; 
			 newToast.style.marginLeft = -toastW/2+'px';
			 return newToast;
			
		},
		alert: function(msg,timer){
			 var _this = this;
			 if(document.getElementsByClassName('cToast').length>0){
			 	var someToasts = document.getElementsByClassName('cToast');
			 	someToasts[0].parentNode.removeChild(someToasts[0]);
			 	cancelAnimationFrame(_this.raf);
			 	clearTimeout(_this.st);
			 	_this.opacity = 0;
			 }
			 var newToast = _this.createEntity(msg,timer,1);
			 requestAnimationFrame(function(){
			 	_this.raf = _this.fadeIn(newToast,1);
			 });
		},
		loadStart: function(msg){
			var _this = this;
			if(document.getElementsByClassName('cToast').length>0){
				var someToasts = document.getElementsByClassName('cToast');
			 	someToasts[0].parentNode.removeChild(someToasts[0]);
			 	cancelAnimationFrame(_this.raf);
			 	clearTimeout(_this.st);
			 	_this.opacity = 0;
			 }
			var newToast = _this.createEntity(msg,'',2);
			requestAnimationFrame(function(){
			 	_this.raf = _this.fadeIn(newToast,2);
			});
			_this.loadingEle = newToast;
		},
		loadFinish: function(msg){
			var _this = this;
			_this.loadingEle.style.left = '0'; 
			_this.loadingEle.innerHTML = msg;
			var toastW = _this.loadingEle.innerWidth ? _this.loadingEle.innerWidth : _this.loadingEle.clientWidth;
			_this.loadingEle.style.left = '50%'; 
			_this.loadingEle.style.marginLeft = -toastW/2+'px';
			setTimeout(function(){
				_this.raf = _this.fadeOut(_this.loadingEle);
			},1000);
		}

	};
	return toast;
});