ACMS.Dispatch.Admin._media=function(elm){var objDropArea=elm;var mediaData=new Array;var objDispArea=document.getElementById("js-media_disp_area");var compiled=_.template($("#js-media_tmpl").html());if(window.File&&window.FileReader){$("#js-media_browser").remove();objDropArea.addEventListener("drop",function(event){$(this).removeClass("drop_enter");event.preventDefault(event);readFiles(event.dataTransfer.files)},false);objDropArea.addEventListener("dragover",function(event){event.preventDefault()},
false);objDropArea.addEventListener("dragenter",function(event){$(this).addClass("drop_enter")},false);objDropArea.addEventListener("dragleave",function(event){$(this).removeClass("drop_enter")},false);document.getElementById("js-media_files").addEventListener("change",function(event){readFiles(event.target.files)},false)}else $("#js-media_multiple").remove();$("#js-media_upload").click(function(event){if(mediaData.length>0){_.each(mediaData,function(file,el){var obj=file.media_obj;var size=$("[name=image_size]").val();
var fd=new FormData;var tag=$("[name=media_tag_text]").val();if(!tag)tag=$("[name=media_tag_select]").val();if(!tag)tag="";fd.append("file",file);fd.append("size",size);fd.append("tags",tag);fd.append("multi","on");fd.append("ACMS_POST_Media_Upload",true);progressbar(obj,"start");$.ajax({type:"POST",data:fd,processData:false,contentType:false,success:function(html){if(html=="SUCCESS")progressbar(obj,"success");else progressbar(obj,"error")},error:function(xhr,statusText){progressbar(obj,"error")},
complete:function(xhr,statusText){delete mediaData[el]}})});$("[name=media_tag_select]").val("");$("[name=media_tag_text]").val("")}});function readFiles(files){for(var i=0;i<files.length;i++){var f=files[i];if(!f.type.match("image.*"));var objFileReader=new FileReader;objFileReader.onerror=function(evt){return};if(f.type.match("image.*")){objFileReader.onload=function(theFile){return function(e){theFile.filetype="image";theFile.preview=this.result;rebuild(theFile)}}(f);objFileReader.readAsDataURL(f)}else if(f.type.match("video.*"))return;
else if(f.type.match("application.*")){objFileReader.onload=function(theFile){return function(e){theFile.filetype="file";rebuild(theFile)}}(f);objFileReader.readAsBinaryString(f)}}}function rebuild(data){var mediaHtml=compiled({data:data});var mediaHash=(data.name+data.size+data.lastModifiedDate).replace(/\s+/g,"");var mediaHtml=$(mediaHtml).attr("data-media_id",mediaHash).get(0);$("#js-media_disp_area tbody").append(mediaHtml);data.media_data=mediaHash;data.media_obj=$(mediaHtml);mediaData.push(data);
$(".js-media_cancel_btn").unbind("click");$(".js-media_cancel_btn").click(function(event){var self=$(this);var parent=self.parents("tr");var media_id=parent.data("media_id");_.each(mediaData,function(index,el){if(index.media_data==media_id)mediaData.splice(el,1)});parent.remove()})}function progressbar($obj,$on){var $bar=$obj.find(".acms-progress");var $mess=$obj.find(".acms-progress-mess");if($on=="start"){$bar.show();$mess.text("\u9001\u4fe1\u4e2d")}else if($on=="success")setTimeout(function(){$obj.remove()},
500);else if($on=="error")setTimeout(function(){$bar.removeClass("acms-active").find(".acms-progress-bar").removeClass("acms-progress-bar-info").addClass("acms-progress-bar-danger");$mess.text("\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002\u30d5\u30a1\u30a4\u30eb\u30b5\u30a4\u30ba\u3001\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3\u3092\u78ba\u8a8d\u3057\u3066\u4e0b\u3055\u3044\u3002")},500)}};
