var system ={
		win : false,
		mac : false,
		xll : false
	};
//检测平台
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);

(function(){
	var _self = this;
	//平台、设备和操作系统     
	
	//添加子项
	var addItem = function($ele,params,btns){
		if(typeof $ele != 'object'){
			throw new Error('addItem�����Ĳ��������Ǹ�jqueryԪ��');
		}
		var $item = $('<li class="dd-item" data-id="'+params.id+'" data-parent="'+params.parent+'"></li>').appendTo($ele);
		var $item_ele = $('<div class="dd-handle">'+params.name+'</div>').appendTo($item);
		if(btns != null){
			if(eval(btns).length > 0){
				//���ذ�ť
				var $btns = $('<div class="pull-right action-buttons"></div>').appendTo($item_ele);
				$.each(btns,function(i,val){
					var $btn = '';
					if(val.type == 'add'){
						$btn = $('<a class="green" href="#"><i class="ace-icon fa fa-plus bigger-130"></i></a>').appendTo($btns);
					}else if(val.type == 'edit'){
						$btn = $('<a class="blue" href="#"><i class="ace-icon fa fa-pencil bigger-130"></i></a>').appendTo($btns);
					}else if(val.type == 'remove'){
						$btn = $('<a class="red" href="#"><i class="ace-icon fa fa-trash-o bigger-130"></i></a>').appendTo($btns);
					}else{
						//��������
						throw new Error('addItem�����ĵ������������� ��ʽ[{ type:"add",click:function(){} },...] type����ֵ add��edit��remove');
					}
					if(typeof val.click == 'function'){
						$btn.on('click',val.click);
						$btn.on('mousedown', function(e){
		    				e.stopPropagation();
						});
					}
				});
			}
		}
		return $item;
	}
	//添加集
	var addList = function($ele){
		if(typeof $ele != 'object'){
			throw new Error('addList�����Ĳ��������Ǹ�jqueryԪ��');
		}
		var $list = $('<ol class="dd-list"></ol>').appendTo($ele);
		return $list;
	}
	
	var createNestable = function($ele,jsonArray,btns){
		if($ele.length <= 0){
			throw new Error('û���ҵ�Ԫ��');
		}
		if(typeof jsonArray != 'object'){
			throw new Error('����jsonArray ������һ��json����');
		}
		var $list = addList($ele);
		//jsonArray [{id:1,name:'������Ϣ',childrens:[{},{},{}]},{id:2,name:'������Ϣ'},{id:3,name:'Ȩ����Ϣ'}]
		$.each(jsonArray,function(index,value){
			//��list�в���item
			var $item = addItem($list,{id:value.id,name:value.name},btns);
			if(value.childrens != null){
				createNestable($item,value.childrens,btns);
			}
		});
	}
	
	
	
	_self.ZZXUtils = {};
	_self.ZZXUtils.onMask = function(){
		$('.my-mask').remove();
		$('.page-content-area').append('<div class="my-mask"><i class="ace-icon loading-icon fa fa-spinner fa-spin fa-5x white my-center"></i></div>');
	}
	_self.ZZXUtils.unMask = function(){
		$('.my-mask').remove();
	}
	_self.ZZXUtils.showModal = function(element,fn){
		var $modal = $(element);
		if($modal.length <= 0){
			return false;
		}
		$modal.before('<div id="modalMask" class="my-mask"></div>');
		$modal.show();
		
		var $button = $modal.find('button[data-dismiss=modal]');
		$button.unbind();
		$button.bind('click',function(){
			var btn = $(this).attr('btn-type');
			$modal.parent().find('#modalMask').remove();
			$modal.hide();
			typeof fn == 'function' ? fn(btn) : '';
			return false;
		});
	}
	_self.ZZXUtils.initNestable = function(ele,jsonArray,btns){
		createNestable($(ele),jsonArray,btns);
	}
	_self.ZZXUtils.bindscroll = function($modal){
		if($modal == null || $modal.length <= 0){
			return false;
		}
		$modal.unbind('shown.bs.modal');
		$modal.on('shown.bs.modal',function(){
			//绑定滚轮事件
			$(document).unbind('mousewheel DOMMouseScroll');
			$(document).bind('mousewheel DOMMouseScroll',function(e){
				var wheeldelta = e.originalEvent.wheelDelta;
				$(document).scrollTop($(document).scrollTop() - wheeldelta);
				console.log(wheeldelta);
			});
			
			$modal.unbind('hidden.bs.modal');
			$modal.on('hidden.bs.modal',function(){
				//modal关闭时 解除绑定事件
				$(document).unbind('mousewheel DOMMouseScroll');
			});
		});
	}
})();