var $=require("jquery"),Form=require("./form.js"),ImagesLoaded=require("./../../masonry/imagesloaded.js"),inited=!1,Convenient={initAll:function(Dialog){$(document).on("click","[data-confirm]",function(){if(!$(this).is("[data-ajax-request]")){var t=$(this).attr("data-href"),a=$(this).is("[data-new-window]");return Dialog.confirm($(this).attr("data-confirm"),function(){a?window.open(t,"_blank"):window.location.href=t}),!1}}),$(document).on("click","[data-ajax-request]",function(){var url=$(this).attr("data-ajax-request"),confirm=$(this).attr("data-confirm"),loading=$(this).is("[data-ajax-request-loading]"),method=$(this).attr("data-method"),jsonStr=$(this).attr("data-request"),jsonp=$(this).is("[data-ajax-jsonp]"),callback=$(this).data("callback");callback||(callback=Form.defaultCallback);var data={};if(jsonStr)try{data=JSON.parse(jsonStr)}catch(e){try{eval("data = "+jsonStr+";")}catch(t){}}else data={};var sendRequest=function(){loading&&Dialog.loadingOn(),$.ajax({async:!0,url:url,type:method||"get",dataType:jsonp?"jsonp":"json",data:data,success:function(t){loading&&Dialog.loadingOff(),callback(t,{},Dialog)},error:function(t){loading&&Dialog.loadingOff(),alert("请求出错")}})};return confirm?Dialog.confirm(confirm,function(){sendRequest()}):sendRequest(),!1}),$(document).on("click","[data-dialog-request]",function(){var t=$(this),a=t.attr("data-dialog-request"),i={closeCallback:function(){t.trigger("dialog.close",[t])}};t.attr("data-dialog-width")&&(i.width=t.attr("data-dialog-width")),t.attr("data-dialog-height")&&(i.height=t.attr("data-dialog-height")),Dialog.dialog(a,i)}),$(document).on("click","[data-image-preview]",function(){var option={};$(this).attr("data-option")&&eval("option = "+$(this).attr("data-option"));var opt=$.extend({title:null,width:"auto",height:"auto"},option),imgUrl=$(this).attr("href");imgUrl||(imgUrl=$(this).attr("data-image-preview")),imgUrl||(imgUrl=$(this).attr("src"));var windowWidth=$(window).width(),windowHeight=$(window).height();Dialog&&Dialog.loadingOn();var img=new Image;return img.onerror=function(){Dialog?(Dialog.loadingOff(),Dialog.tipError("加载图片出错")):alert("加载图片出错")},img.onload=function(){Dialog&&Dialog.loadingOff();var t=windowWidth-40,a=windowHeight-40,i=img.width,o=img.height;i>t&&(o=parseInt(o*t/i),i=t),o>a&&(i=parseInt(i*a/o),o=a);var n=['<div style="width:',i,"px;height:",o,'px;">','   <img src="',imgUrl,'" style="width:',i,"px;height:",o,'px;" />',"</div>"].join("");Dialog.dialogContent(n,opt)},img.src=imgUrl,!1})},initSmartImage:function(){$(document).ready(function(){setTimeout(function(){$("[data-smart-image]").each(function(t,a){$(a).attr("src",$(a).attr("data-smart-image"))})},0)})},initTip:function(t){t=t||null,$(document).on("click","[data-tip-error]",function(){return t?t.tipError($(this).attr("data-tip-error")):alert($(this).attr("data-tip-error")),!1}),$(document).on("click","[data-tip-success]",function(){return t?t.tipError($(this).attr("data-tip-success")):alert($(this).attr("data-tip-success")),!1}),$(document).on("mouseover","[data-tip-popover]",function(a){var i=$(this).attr("data-tip-popover"),o=this;t?t.tipPopoverShow(o,i):$(o).attr("title",i)}).on("mouseout","[data-tip-popover]",function(a){var i=this;t&&t.tipPopoverHide(i)})}};Convenient.init=function(t){inited||(inited=!0,Convenient.initAll(t),Convenient.initSmartImage(),Convenient.initTip(t))},module.exports=Convenient;