var onez=onez||{};
onez.snapshot={};
$(function(){
  (function(snapshot){
    snapshot.imgReader=function(item){
      var blob = item.getAsFile(),reader = new FileReader();
      reader.onload = function( e ){
        $.post('?_p=kindeditor&_m=base64',{data:e.target.result.split(';base64,')[1]},function(data){
          if(typeof data.error!='undefined'){
            onez.alert(data.error);
          }else if(typeof data.url!='undefined'){
            KindEditor.insertHtml(snapshot.data.id, '<img src="'+data.url+'" class="image" />');
          }
        },'json');
      };
      reader.readAsDataURL( blob );
    };
    snapshot.paste=function(e,data){
      snapshot.data=data;
      var clipboardData=null;
      if(typeof e.clipboardData!='undefined'){
        clipboardData=e.clipboardData;
      }else if(typeof e.originalEvent!='undefined' && typeof e.originalEvent.clipboardData!='undefined'){
        clipboardData=e.originalEvent.clipboardData;
      }
      if( clipboardData ){
        var i = 0,items, item, types;
        items = clipboardData.items;
        if( !items ){
          return;
        }
        item = items[0];
        types = clipboardData.types || [];
        for( ; i < types.length; i++ ){
            if( types[i] === 'Files' ){
                item = items[i];
                break;
            }
        }
        if( item && item.kind === 'file' && item.type.match(/^image\//i) ){
            snapshot.imgReader(item);
        }
      }
    };
  })(onez.snapshot);
});
$(function(){
  $(document).bind('onez-kindeditor-create',function(e,data){
    data.obj.bind('paste', function (e) {
      onez.snapshot.paste(e.event,data);
    });
  });
});