var Form=require("./form.js"),globalIndex=0,RawUploadButton=function(i,e){var t=$.extend({inited:function(){},callback:function(i,e){},height:40,sizeLimit:2097152,server:"/url/to/upload",text:"上传",textUploading:"正在上传...",backgroundColor:"#CCC",textColor:"#333",extensions:"jpg,png,gif,jpeg"},e),o=globalIndex++,n=$(i),a=this,r=['<iframe id="iframeRawUploadButton',o,'" scrolling="no" frameborder="0" style="width:100%;height:',t.height,'px;"></iframe>'];n.html(r.join(""));for(var d=[],l=t.extensions.split(","),s=l.length;s>=0;s--){var c=l[s];c&&(-1!="jpg,jpeg,png,gif,bmp".indexOf(c)?-1==d.indexOf("image/*")&&d.push("image/*"):d.push("application/octet-stream"))}var h=null;setTimeout(function(){var i=['<form enctype="multipart/form-data" action="',t.server,'" method="post" target="hiddenIframe">','<div class="file" style="width:100%;height:',t.height,'px;position:relative;cursor:pointer;">','<div class="text" style="border-radius:2px;background:',t.backgroundColor,";color:",t.textColor,";width:100%;line-height:",t.height,"px;text-align:center;font-family:'Segoe UI','Lucida Grande',Helvetica,Arial,'Microsoft YaHei',FreeSans,Arimo,'Droid Sans','wenquanyi micro hei','Hiragino Sans GB','Hiragino Sans GB W3',FontAwesome,sans-serif;\">",t.text,"</div>",'<div class="preview" style="display:none;border-radius:2px;background:#FFF;background-size:cover;background-repeat:no-repeat;color:',t.textColor,";width:100%;line-height:",t.height,"px;height:",t.height,'px;"></div>','<input type="file" name="file" accept="'+d.join(",")+'" style="width:100%;height:',t.height,"px;overflow:hidden;font-size: ",t.height,'px;position:absolute;right:0;top:0;cursor:pointer;opacity:0;filter:alpha(opacity=0);" />',"</div>",'<div class="loading" style="border-radius:2px;background:',t.backgroundColor,";color:",t.textColor,";width:100%;line-height:",t.height,"px;text-align:center;font-size:14px;font-family:'Segoe UI','Lucida Grande',Helvetica,Arial,'Microsoft YaHei',FreeSans,Arimo,'Droid Sans','wenquanyi micro hei','Hiragino Sans GB','Hiragino Sans GB W3',FontAwesome,sans-serif;\">",t.textUploading,"</div>","</form>",'<iframe name="hiddenIframe" scrolling="no" frameborder="0" style="width:1px;height:1px;"></iframe>'],e=$("#iframeRawUploadButton"+o).get(0);e&&e.contentWindow&&($(e.contentWindow.document.head).html("<meta charset='utf-8'/>"),h=$(e.contentWindow.document.body),h.css({padding:0,margin:0}),h.html(i.join("")),$(h).find("input[name=file]").on("change",function(){if(this.files[0].size>t.sizeLimit)return void Form.defaultCallback({code:-1,msg:"文件太大"});$(h).find(".file").hide(),$(h).find(".loading").show(),$(h).find("form").submit()}),$(h).find("iframe").on("load",function(){$(h).find(".loading").hide(),$(h).find(".file").show();var i,e=this.contentWindow,o=e.document.body,r=$(o).find("pre").html()||$(o).html();try{i=JSON.parse(r)}catch(e){i={code:-1,msg:"parse json error : "+JSON.stringify(r)}}Form.defaultCallback(i,{success:function(i){var e={name:i.data.data.name,size:i.data.data.size,path:i.data.path,localPath:$(h).find("input[type=file]").val()};t.callback.call(a,e,n)}})}),t.inited.call(a))},0),this.setPreview=function(i){i&&(0===i.indexOf("http://")||0===i.indexOf("https://")||(i=0===i.indexOf("//")?window.location.protocol+i:0===i.indexOf("/")?window.location.protocol+"//"+window.location.host+i:window.location.protocol+"//"+window.location.host+"/"+i)),$(h).find(".file .preview").css({backgroundImage:"url("+i+")"}),$(h).find(".file .preview").show(),$(h).find(".file .text").hide()},this.clearPreview=function(){$(h).find(".file .preview").hide(),$(h).find(".file .text").show()}};module.exports=RawUploadButton;