/*
 * IMG FLY BOX  v1.3.1
 * http://www.wadadanet.jp/javascript-tips/img_fly_box/
 * http://www.wadadanet.jp/
 *
 * Copyright (c) 2012 Minoru Wada
 * Dual licensed under the MIT and GPL licenses.
 *
 * Date: 2012-01-31 18:00:00
 */
(function(){$(document).ready(function(){function d(a,b,c,d){var e={x:0,y:0,width:b.naturalWidth,height:b.naturalHeight};var f=a.offset().top+a.height()/2;var g=$(window).height()-100;if(e.height>g){var h=e.height;e.height=g;var i=e.height/h;e.width=e.width*i}if(c=="R"){e.x=a.offset().left+a.width()+d}else if(c=="L"){e.x=a.offset().left-d-e.width}else{e.x=a.offset().left+a.width()/2-e.width/2}e.y=f-e.height/2;if(e.x<0){e.x=0}return e}function c(a,b,c){var d=a.offset().left+a.width()/2-b/2;var e=a.offset().top+a.height()/2-c/2;return{x:d,y:e,width:b,height:c}}var a={x:0,y:0,width:20,height:20};var b=new Array;$("a.imgflybox").click(function(e){function O(){function d(){M=false;p.x=A.offset().left;p.y=A.offset().top;p.width=A.width();p.height=A.height();if(!y){$(document).unbind("mousemove",c).unbind("mouseup",d)}else{$(document).unbind("touchmove",c).unbind("touchend",d)}}function c(a){M=true;n=p.width-(a.pageX-l);o=p.height-(a.pageY-m);if(n/o>E){n=o*E}else{o=n/E}A.css("left",a.pageX).css("top",a.pageY).css("width",n+x).css("height",o+x);C.css("left",n-(20-x)).css("top",o-(20-x));B.css("width",n).css("height",o);D.css("width",n);return false}function b(){M=false;p.width=A.width();p.height=A.height();if(!y){$(document).unbind("mousemove",a).unbind("mouseup",b)}else{$(window).unbind("touchmove",a).unbind("touchend",b);alert("END")}}function a(a){M=true;n=a.pageX-l+p.width;o=a.pageY-m+p.height;if(n/o>E){n=o*E}else{o=n/E}A.css("width",n+x).css("height",o+x);C.css("left",n-(20-x)).css("top",o-(20-x));B.css("width",n).css("height",o);D.css("width",n);return false}if(!y){C.mousedown(function(c){M=false;l=c.pageX;m=c.pageY;n=0;o=0;$(document).mousemove(a).mouseup(b);return false})}else{C.bind("touchstart",function(c){M=false;l=c.pageX;m=c.pageY;n=0;o=0;$(window).bind("touchmove",a).mouseup("touchend",b);return false})}if(!y){resizeLTTarget.mousedown(function(a){M=false;l=a.pageX;m=a.pageY;n=0;o=0;$(document).mousemove(c).mouseup(d);return false})}else{resizeLTTarget.mousedown(function(a){M=false;l=a.pageX;m=a.pageY;n=0;o=0;$(document).bind("touchmove",c).bind("touchend",d);return false})}}function N(){function E(){p.y=A.offset().top;p.x=A.offset().left;B.unbind("mousemove",z).unbind("mouseup",C);return true}function C(){p.y=A.offset().top;p.x=A.offset().left;B.unbind("mousemove",z).unbind("mouseup",C)}function z(a){L=true;n=a.pageX-l+p.x;o=a.pageY-m+p.y;A.css("top",o).css("left",n);return false}mouseDownFlg=false;if(!y){B.mousedown(function(a){L=false;l=a.pageX;m=a.pageY;n=0;o=0;B.mousemove(z).mouseup(C);return false}).mouseover(function(){if(!M)D.show(100)}).mouseleave(function(){if(!M){D.hide(100);B.unbind("mousemove",z).unbind("mouseup",C)}}).click(function(){if(!L){A.remove()}})}else{var a=false;var b=false;var c,d,e,f,g,h,i,j,k,q,r,s;var t=0;function u(a,b,c,d){var e;e=Math.sqrt(Math.pow(c-a,2)+Math.pow(d-b,2));return e}B.bind("touchstart",function(f){f.preventDefault();p.x=A.offset().left;p.y=A.offset().top;e=f.originalEvent.touches;l=e[0].pageX;m=e[0].pageY;n=0;o=0;a=true;t++;if(e.length>1){l=e[0].pageX;m=e[0].pageY;c=e[1].pageX;d=e[1].pageY;h=u(l,m,c,d);b=true}$(document).bind("touchmove",v).bind("touchend",w)});function v(c){a=false;c.preventDefault();e=c.originalEvent.touches;if(b){n=e[0].pageX;o=e[0].pageY;f=e[1].pageX;g=e[1].pageY;i=u(n,o,f,g);j=i/h;k=p.width*j;q=p.height*j;r=(p.width-(k+x))/2+p.x;s=(p.height-(q+x))/2+p.y;A.css("width",k+x).css("height",q+x).css("left",r).css("top",s);B.css("width",k).css("height",q);D.css("width",k)}else{n=e[0].pageX-l+p.x;o=e[0].pageY-m+p.y;A.css("top",o).css("left",n)}D.show(100);return false}function w(c){t--;p.y=A.offset().top;p.x=A.offset().left;p.width=A.width();p.height=A.height();if(t<=1){b=false}if(t==0){D.hide(100);if(a){A.remove()}$(document).unbind("touchmove",v).unbind("touchend",w)}}}}if(!jQuery.support.opacity){return true}var f=new Date;var g="FLY_"+f.getTime().toString();var h=$(this).attr("rel");if(h==null)h="L:50";var i=h.split(":");var j=i[0]!=null?i[0]:"L";var k=i[1]!=null?i[1]:50;if(document.getElementById(g)==null){var l,m,n,o;var p;var q=$("img",this);var r=c(q,a.width,a.height);var s="'"+q.attr("src")+"'";var t=q.attr("width");var u=q.attr("height");var v='style="left:'+r.x+"px; top:"+r.y+'px;position:absolute;border:1px solid #CCC;"';var w="<div "+v+" id="+g+">";w+='<div id="'+g+'_loading" style="width:40px;height:15px;background-color:#CCC;font-size:10px;text-align:left;padding:0 0 0 12px;color:#FFF;">■</div>';w+="</div>";$("body").append(w);var x=8;var y=false;var z=navigator.userAgent;if(z.indexOf("iPhone")>-1||z.indexOf("iPad")>-1||z.indexOf("iPod")>-1){y=true;x=8}var A=$("#"+g);var B;var C;var D;var E;var F=$("#"+g+"_loading");var G=1;var H="";var I=setInterval(function(){H="";for(var a=1;a<=G;a++){H+="■"}F.html(H);G++;G=G>3?1:G},500);A.css({opacity:0}).animate({opacity:1});var J=$(this).attr("href");var K='<img src="'+J+'" width="50" />';A.append(K);$("img",A).load(function(){A.css({top:q.offset().top-12,left:q.offset().left-12});$(this).css({opacity:0});F.remove();b[g]=true;p=d(q,$(this)[0],j,k);cRect=p;A.animate({opacity:0},{duration:250,complete:function(){A.removeClass("IFO_loading").addClass("IFO_View").css({opacity:1}).animate({width:p.width+x,height:p.height+x,left:p.x,top:p.y},{duration:500,complete:function(){var a="";if(!y){a+='<div id="Fly_ResizerRB" style="width:20px;height:20px;background-color:#F00;cursor:nw-resize;position:absolute;left:'+(p.width-(20-x))+"px;top:"+(p.height-(20-x))+'px;"></div>';a+='<div id="Fly_ResizerLT" style="width:20px;height:20px;background-color:#F00;cursor:nw-resize;position:absolute;left:0px;top:0px;"></div>'}a+='<img src="'+J+'" width="'+p.width+'" height="'+p.height+'" style="padding:0;margin:0;position:absolute;left:'+x/2+"px;top:"+x/2+'px;" />';a+='<div id="Fly_directions" style="width:'+p.width+"px;height:20px;background-color:#000;position:absolute;bottom:"+x/2+"px;left:"+x/2+'px;text-align:left;">';a+='<span style="color:#CCC;height:1;font-size:12px;padding:0 6px;">CLOSE:CLICK　　MOVE:DRAG</span>';a+="</div>";A.html(a);$("img",this).css({opacity:0}).animate({opacity:1});setTimeout(function(){$("#Fly_directions",A).hide(100)},2e3);E=p.width/p.height;B=$("img",A);resizeLTTarget=$("#Fly_ResizerLT",A);C=$("#Fly_ResizerRB",A);D=$("#Fly_directions",A);N();O()}})}})})}else{$("#"+g).remove()}var L=false;var M=false;return false})})})()
