(function($){
	if(!$) throw new error("需要jquery");

	var Swmesh = function(container , parms){
		var m = this;

		var option = {
			rowCount : 0,
			columnCount : 0,
			/*
				meshes : [{type : 'img', posValue:'', opoValue:'',}] / [{color: '#000'}] ;
			*/
			meshes : null
		};
		jQuery.extend(option, parms);

		var _meshes = option.meshes;
		var _rowCount = option.rowCount;
		var _columnCount = option.columnCount;

		m.container = $(container);
		m.maxWidth = m.container.width();
		m.maxHeight = m.container.height();	
		m.meshList = new Array();
		m.isExecute = false;	//是否执行过动画;

		if(_meshes && $.isArray(_meshes)) 
		{

			var oneWidth = m.maxWidth / _columnCount;
			var oneHeight = m.maxHeight / _rowCount;
			var colArray = new Array();
			var currCol = 1, currRow = 1;

			for (var index in _meshes) {
				var _ms = _meshes[index];
				colArray.push(
				{	
					width : oneWidth,
					height : oneHeight,
					type : _ms.type,
					posValue : _ms.posValue,
					opoValue : _ms.opoValue
				});

				if(currCol++ == _columnCount)
				{			
					m.meshList.push(colArray);
					if(currRow++ == _rowCount)					
						break;

					colArray = new Array();
					currCol = 1;			
				}				
			}			
		}
		
		var containerHTML = new String();	
		for (var r = 0 ; r < m.meshList.length ; r++) {
			var rowMesh = m.meshList[r];

			for (var c = 0 ; c < rowMesh.length ; c++) {
				var _cMesh = rowMesh[c];
				if(_cMesh.type == 'color')
				{
					containerHTML += '<div style="float:left; width:'+_cMesh.width+'px; height:'+_cMesh.height+'px;" background-color:'+_cMesh.posValue+'" data-posvalue="'+_cMesh.posValue+'" data-opovalue="'+_cMesh.opoValue+'" data-type="'+_cMesh.type+'"></div>';
				}
				else if(_cMesh.type == 'img')
				{
					containerHTML += '<div style="float:left; width:'+_cMesh.width+'px; height:'+_cMesh.height+'px;" data-posvalue="'+_cMesh.posValue+'" data-opovalue="'+_cMesh.opoValue+'" data-type="'+_cMesh.type+'"><img style="width:'+_cMesh.width+'px; height:'+_cMesh.height+'px"	 class="resize" src="'+_cMesh.posValue+'" /></div>';
				}
			}
		}
		m.container.html(containerHTML);
		/*
		meshList = new Array();
		meshList = [ 	//行	
					   	[ [{width:1,height:1,type:'color',value:'#aa'}],[{width:1,height:1,color:#000,src:''}] ],	//列
					  	[ [{width:1,height:1,color:#000,src:''}],[{width:1,height:1,color:#000,src:''}] ]	
					]
		*/
		
	}

	Swmesh.prototype.animate = function(speed ,model ,type ,delay , option)
	{
		var m = this;
		/*
			type 	默认 rotat-left
			rotat-bottom 下翻
			rotat-top 上翻
			rotat-left 左翻
			rotat-right 右翻
		*/
		var _type = type || 'rotat-left';
		var _delay = delay || 0;	//延迟
		/*
			动画模式
			together : 同时旋转
			continuity ： 一个一个的连续旋转
		*/
		var _model = model || 'together'; 
		var _itemList = m.container.children();
		var _option = {
			intervalAcc : 0 ,
			interval : 1000,
			minInterval : 0 ,
			speedAcc : 0,
			miniSpee : 0
		}
		jQuery.extend(_option, option);
		var _animatedCss;
		if(_option.speedAcc === 0)
		{
			_animatedCss = 
			{
				'-webkit-transition' : speed +'s ease-out',
				'-moz-transition' 	 : speed +'s ease-out',
				'-ms-transition' 	 : speed +'s ease-out',
				'-o-transition' 	 : speed +'s ease-out',
				'transition' 	  	 : speed +'s ease-out'
			}
		}

		m.animateTimers = new Array();
		m.animateClass = 'swmesh-' + _type;

		var _inDelay = _delay;
		for (var i = 0; i < _itemList.length; i++) {
			var _item = _itemList[i];
	
			if(_model === 'together')
			{
				_inDelay = _delay;
			}
			if(_model === 'continuity' && i > 0)
			{
				var _interval = (_option.interval - _option.intervalAcc * i) ;
				_inDelay += _interval < _option.minInterval ? _option.minInterval : _interval;
			}

			if(_option.speedAcc != 0)
			{
				var _an_speed = speed - _option.speedAcc * (i + 1);
				_an_speed = _an_speed < _option.miniSpee ? _option.miniSpee : _an_speed;
				
				_animatedCss = 
				{
					'-webkit-transition' : _an_speed +'s ease-out',
					'-moz-transition' 	 : _an_speed +'s ease-out',
					'-ms-transition' 	 : _an_speed +'s ease-out',
					'-o-transition' 	 : _an_speed +'s ease-out',
					'transition' 	  	 : _an_speed +'s ease-out'
				}
			}

			var _timeout = window.setTimeout(function (item) {
		        return function () {	          
		        		item.addClass(m.animateClass);	
		        		item.css(_animatedCss);
		        		var _an_type = item.data('type');
		        		var _an_value = item.data('opovalue');

		        		if(_an_type === 'color')
		        		{		        			
		        			item.css("background-color",_an_value); 
		        		}
		        		else if(_an_type === 'img')
		        		{
		        			item.children().attr('src',_an_value);
		        		}
			       };
      		} ($(_item)), _inDelay);

      		m.animateTimers.push(_timeout);
		}

		m.isExecute = true; //标记已经执行过动画
		//item.removeClass('animated ' + animation);
		//item.on('transitionend webkitTransitionEnd MSTransitionEnd oTransitionEnd', function() { 
			
		//});
  		
	}

	Swmesh.prototype.reset = function()
	{
		var m = this;
		if(m.isExecute)
		{
			for (var i in m.animateTimers) {
				clearTimeout(m.animateTimers[i]);
			}

			var _emptyAnimatedCss = 	
			{
				'-webkit-transition' : '',
				'-moz-transition' 	 : '',
				'-ms-transition' 	 : '',
				'-o-transition' 	 : '',
				'transition' 	  	 : ''
			}

			m.container.children().each(function(el){
				var elObj = $(this);
				var _re_type = elObj.data('type');

				elObj.removeClass(m.animateClass);
				elObj.css(_emptyAnimatedCss);
				if(_re_type === 'color')
				{					
					elObj.css("background-color", elObj.data('posvalue'));
				}else if(_re_type === 'img')
				{
					console.log(elObj.data('posvalue'))
					elObj.children().attr('src' , elObj.data('posvalue'));
				}
			});

			m.isExecute = false;
		}
	}

	window.Swmesh = Swmesh;
})(jQuery);