<?php
!defined('IN_ONEZ') && exit('Access Denied');
?>
<script type="text/javascript">
onez.snapshot={};
$(function(){
  (function(snapshot){
    snapshot.imgReader=function(item,tagname,obj){
      var blob = item.getAsFile(),reader = new FileReader();
      reader.onload = function( e ){
        $.post('?_p=chat.group.plugin.image&_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'){
            $('.chat').trigger('chat-file',data.url);
          }
        },'json');
      };
      reader.readAsDataURL( blob );
    };
    snapshot.insert=function(obj,html){
      var obj=obj.get(0);
      var range, node;
      if(!obj.hasfocus) {
        obj.focus();
      }
      if(window.getSelection && window.getSelection().getRangeAt){
        range = window.getSelection().getRangeAt(0);
        range.collapse(false);
        node = range.createContextualFragment(html);
        var c = node.lastChild;
        range.insertNode(node);
        if(c){
          range.setEndAfter(c);
          range.setStartAfter(c)
        }
        var j = window.getSelection();
        j.removeAllRanges();
        j.addRange(range);
      } else if (document.selection && document.selection.createRange) {
          document.selection.createRange().pasteHTML(html);
      }
    };
    snapshot.paste=function(e){
      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,$(this).get(0).tagName.toLowerCase(),$(this));
        }
      }
    };
    $('.inputbox').bind('paste',snapshot.paste);
  })(onez.snapshot);
});
onez.createXHR=function (){ 
    if(typeof XMLHttpRequest != "undefined"){ 
        return new XMLHttpRequest(); 
    }else if(typeof ActiveXObject != "undefined"){ 
        //适合IE7之前的版本 
        if(typeof arguments.callee.activeXString != "string"){ 
            var versions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML.XMLHttp"]; 
            for(var i=0,len=versions.length; i<len; i++){ 
                try{ 
                    var xhr = new ActiveXObject(versions[i]); 
                    arguments.callee.activeXString = versions[i]; 
                    return xhr; 
                }catch (ex){ 
                } 
            } 
        } 
        
        return new ActiveXObject(arguments.callee.activeXString); 
    }else{ 
        throw new Error("No XHR object available."); 
    }; 
};
group.AddImage=function(){
  if (typeof(Worker)!="undefined"){
    $('#file_image').remove();
    var file=$('<input type="file" id="file_image" accept="image/" style="visibility: hidden;" />').appendTo('body');
    try{
      var onChange=function(){
        if(this.files.length>0){
          var xhr = onez.createXHR();
          xhr.open("post",'?_p=chat.group.plugin.image&_m=upload', true);
          xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
          xhr.onreadystatechange = function(){
            if (xhr.readyState == 4){
              var flag = xhr.responseText;
              if(flag.substr(0,2)=='ok'){
                $('.chat').trigger('chat-file',flag.substr(2));
              }else{
                onez.alert(flag);
              }
            }
          };
          var fd = new FormData(); 
          fd.append("file", this.files[0]); 
          xhr.send(fd); 
        }
        file.get(0).removeEventListener("change",onChange);
        file.get(0).addEventListener("change",onChange);
      };
      file.get(0).addEventListener("change",onChange);
    }catch(e){
    }
    $('.icon-image').unbind().bind('click',function(){
      $('#file_image').trigger('click');
    });
    return;
  }
  var that=this;
  var Width=that.box.find('a.icon-image').width();
  var Height=that.box.find('a.icon-image').height();
  that.fileid++;
  var ID='image'+that.fileid;
  var Vars='&apiurl='+encodeURIComponent('<?=onez()->homepage()?>/?_p=<?=$this->token?>&_m=upload');
  var Flash='<?=$this->url?>/images/image.swf?rndid='+ID+'';
  var FlashHtml='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="position: absolute;left: 0;top: 0;" ';
  FlashHtml+='codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,29,0" ';
  FlashHtml+='width="' + Width + '" height="' + Height + '" id="' + ID + '">';
  FlashHtml+='<param name="movie" value="' + Flash + '">';
  FlashHtml+='<param name="quality" value="high">';
  FlashHtml+='<param name="wmode" value="transparent">';
  FlashHtml+='<param name="allowScriptAccess" value="always">';
  FlashHtml+='<param name="FlashVars" value="'+Vars+'">';
  FlashHtml+='<embed src="' + Flash + '" style="position: absolute;left: 0;top: 0;" name="' + ID + '" quality="high" allowScriptAccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
  FlashHtml+='type="application/x-shockwave-flash" width="' + Width + '" FlashVars="'+Vars+'" wmode="transparent" height="' + Height + '"></embed>';
  FlashHtml+='</object>';
  that.box.find('.flash-image').remove();
  $('<div class="flash-image" style="position: absolute;left: 0;top: 0;"></div>').html(FlashHtml).css({width:Width+'px',height:Height+'px'}).appendTo(that.box.find('a.icon-image'));
};
$('<a href="javascript:;" class="icon-image">图片</a>').appendTo('.chat-toolbar');
group.AddImage();
</script>