/*
* jQuery Frame Dialog 1.1.2
*
* Copyright (c) 2009 SolutionStream.com & Michael J. Ryan (http://www.solutionstream.com/)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
* 
* Requires:
*	jquery ui dialog
*/
(function(f){f.FrameDialog=f.FrameDialog||{};f.FrameDialog._results=f.FrameDialog._results||{};var d=(window.localization&&window.localization.OK)||"OK";var c=(window.localization&&window.localization.CANCEL)||"Cancel";var e={};var b={w:f(window).width(),h:f(window).height()};e[d]=function(){f(this).dialog("close");};e[c]=function(){var g=f(this);f.FrameDialog.clearResult(g.attr("id"));g.dialog("close");};f.FrameDialog._defaultOptions={modal:true,closeOnEscape:false,position:"center",buttons:e};var a=0;f.FrameDialog.create=function(r){try{var o=Math.random().toString(16).replace(".","")+(new Date()).valueOf().toString(16);var g=f.extend(f.FrameDialog._defaultOptions,r||{});var h=(g&&g.url)||null;if(h===null){throw new Error("MODAL ERROR: Option 'url' not specified!");}h=h.replace(/(^|[^:])\/+/g,"$1/");try{delete g.url;}catch(k){}var l=f("<iframe frameborder='0' scrolling='auto' background='transparent' />").attr("id",o+"-VIEW").attr("name",o+"-VIEW").attr("src",h).css("margin","0").css("border","0").css("padding","0").css("top","0").css("left","0").css("right","0").css("bottom","0").css("width","100px").css("height","100px");var m=f("<div>&nbsp;</div>").css("position","absolute").css("margin","0").css("border","0").css("padding","0").css("top","0").css("left","0").css("right","0").css("bottom","0").css("width","100%").css("height","100%").css("display","none");var p=f("<div />").attr("id",o).css("margin","0").css("border","0").css("padding","0").css("top","0").css("left","0").css("right","0").css("bottom","0").css("overflow","hidden").hide().append(l).append(m).appendTo(document.body).bind("dialogbeforeclose",function(u,v){var w=f(this);var t=w.attr("id");if(u&&u.originalTarget&&u.originalTarget.nodeName&&u.originalTarget.nodeName=="SPAN"){f.FrameDialog.clearResult(t);}if(u&&u.originalEvent&&u.originalEvent.currentTarget&&u.originalEvent.currentTarget.tagName&&u.originalEvent.currentTarget.tagName=="A"){f.FrameDialog.clearResult(t);}var s=f.FrameDialog._results[t]||null;return s;}).bind("dialogclose",function(u,v){var w=f(this);var t=w.attr("id");var s=f.FrameDialog._results[t]||null;w.attr("result",s);window.setTimeout(function(){w.dialog("destroy");w.remove();m.remove();p.remove();try{delete f.FrameDialog._results[t];}catch(x){}},100);return s;}).dialog(g);var j=f(document).width();var i=p.parent(".ui-resizable");i.css("width",(g.minWidth||g.width||200)+"px").css("height",(g.minHeight||g.height||120)+"px").bind("dragstart",function(){m.show();}).bind("dragstop",function(){m.hide();}).bind("resizestart",function(){m.show();}).bind("resize",function(){l.css("height",p.height()+"px").css("width",p.width()+"px");}).bind("resizestop",function(){m.hide();l.css("height",p.height()+"px").css("width",p.width()+"px");});window.setTimeout(function(){i.trigger("resizestop");},100);var n=i.width();if(g.position=="center"||(g.position&&(g.position[0]=="center"||g.position[1]=="center"))){var q=parseInt(parseFloat(j-n)/2);if(q<0){q=0;}i.css("left",q+"px");}if(g.position=="right"||(g.position&&(g.position[0]=="right"||g.position[1]=="right"))){var q=j-n-5;if(q<0){q=0;}i.css("left",q+"px");}a=0;return p;}catch(k){try{p.dialog("destroy");}catch(k){}try{p.remove();}catch(k){}try{m.remove();}catch(k){}try{l.remove();}catch(k){}if(k instanceof TypeError&&a<10){if(console&&typeof console.dir=="function"){console.dir({"Error Creating FrameDialog":{error:k,retry:a}});}a++;return f.FrameDialog.create(r);}a=0;throw k;}};f.FrameDialog._getUid=function(){if(window.parent&&window.parent.frames&&window.parent.document&&window.parent.document.getElementsByTagName){var h=window.parent.document.getElementsByTagName("IFRAME");for(var g=0;g<h.length;g++){var j=h[g].id||h[g].name||g;if(window.parent.frames[j]==window){return j.replace("-VIEW","");}}}return null;};f.FrameDialog.current=function(){if(window.parent&&window.parent.jQuery){return window.parent.jQuery("#"+f.FrameDialog._getUid());}return null;};f.FrameDialog.clearResult=function(g){if(g){try{delete f.FrameDialog._results[g];}catch(h){}}else{var g=f.FrameDialog._getUid();if(g!=null&&window.parent&&window.parent.jQuery&&window.parent.jQuery.FrameDialog&&window.parent.jQuery.FrameDialog._results){try{delete window.parent.jQuery.FrameDialog._results[g];}catch(h){}}}};f.FrameDialog.setResult=function(h,g){if(g){jQuery.FrameDialog._results[g]=h;}else{var g=f.FrameDialog._getUid();if(g!=null&&window.parent&&window.parent.jQuery&&window.parent.jQuery.FrameDialog&&window.parent.jQuery.FrameDialog._results){window.parent.jQuery.FrameDialog._results[g]=h;}}};f.FrameDialog.closeDialog=function(g){if(g){jQuery("#"+g).dialog("close");}else{var g=f.FrameDialog._getUid();if(g!=null&&window.parent&&window.parent.jQuery){window.parent.jQuery("#"+g).dialog("close");}}return false;};f.FrameDialog.cancelDialog=function(g){f.FrameDialog.clearResult(g);f.FrameDialog.closeDialog(g);return false;};f.fn.setResult=function(g){return this.each(function(){f.FrameDialog.setResult(g,f(this).attr("id"));});};f.fn.clearResult=function(){return this.each(function(){f.FrameDialog.clearResult(f(this).attr("id"));});};f.fn.closeDialog=function(){return this.dialog("close");};f.fn.cancelDialog=function(){return this.clearResult().closeDialog();};f.fn.window=function(){if(this.attr("tagName")=="IFRAME"||this.attr("tagName")=="FRAME"){return window.frames[this.attr("name")||this.attr("id")];}var g=this.find("iframe, frame")[0];return(g&&window.frames[g.name||g.id])||null;};})(jQuery);
