(function($){Drupal.modalFrame=Drupal.modalFrame||{dirtyFormsWarning:Drupal.t('Your changes will be lost if you close this popup now.'),options:{},iframe:{$container:null,$element:null},isOpen:false,isChildLoaded:false,parentReady:false,uniqueName:'modalframe-'+((new Date()).getTime())};Drupal.modalFrame.eventHandlerName=function(name){var self=this;return name+'.'+self.uniqueName;};Drupal.modalFrame.open=function(options){var self=this;if(self.isOpen||$('#modalframe-container').size()){return false;}
self.isChildLoaded=false;if(!self.parentReady){$(window).bind(self.eventHandlerName('childLoad'),function(event,iFrameWindow,isClosing){self.bindChild(iFrameWindow,isClosing);});$(window).bind(self.eventHandlerName('childUnload'),function(event,iFrameWindow){self.unbindChild(iFrameWindow);});$(window).bind(self.eventHandlerName('childClose'),function(event,args,statusMessages){self.close(args,statusMessages);});self.parentReady=true;}
if(Drupal.jsAC&&!Drupal.jsAC.prototype.modalFrameSelect){Drupal.jsAC.prototype.modalFrameSelect=Drupal.jsAC.prototype.select;Drupal.jsAC.prototype.select=function(node){this.modalFrameSelect(node);this.hidePopup();};}
self.options={url:options.url,width:options.width,height:options.height,autoFit:(options.autoFit==undefined||options.autoFit),draggable:(options.draggable==undefined||options.draggable),onOpen:options.onOpen,onLoad:options.onLoad,onSubmit:options.onSubmit,customDialogOptions:options.customDialogOptions};self.create();self.iframe.$container.dialog('option',{position:['-999em','-999em']}).dialog('open');return true;};Drupal.modalFrame.create=function(){var self=this;self.iframe.$element=$('<iframe id="modalframe-element" name="modalframe-element"'+($.browser.msie?' scrolling="yes" frameborder="0" allowTransparency="true"':'')+'/>');self.iframe.$container=$('<div id="modalframe-container"/>').append(self.iframe.$element);$('body').append(self.iframe.$container);var dialogOpen=function(){$('.modalframe').unbind('keypress.ui-dialog');$('.modalframe .ui-dialog-titlebar-close:not(.modalframe-processed)').addClass('modalframe-processed').attr('href','javascript:void(0)').attr('title',Drupal.t('Close')).unbind('click').bind('click',function(){self.close(false);return false;});if(!self.options.draggable){$('.modalframe .ui-dialog-titlebar').css('cursor','default');}
self.fixPosition($('.modalframe'),true);var dialogSize=self.sanitizeSize({width:self.options.width,height:self.options.height});var frameSize=$.extend({},dialogSize);frameSize.height-=$('.modalframe .ui-dialog-titlebar').outerHeight(true);var dialogPosition=self.computeCenterPosition($('.modalframe'),dialogSize);$('.modalframe').width(dialogSize.width).height(dialogSize.height);self.iframe.$container.width(frameSize.width).height(frameSize.height);self.iframe.$element.width(frameSize.width).height(frameSize.height);self.iframe.$container.dialog('option',{width:dialogSize.width,height:dialogSize.height});self.iframe.$element.hide();$('.modalframe').hide().css({top:dialogPosition.top,left:dialogPosition.left});$('.modalframe').fadeIn('slow',function(){self.load(self.options.url);});if(self.options.autoFit){var $window=$(window);self.currentWindowSize={width:$window.width(),height:$window.height()};$window.bind(self.eventHandlerName('resize'),function(){if(!self.isChildLoaded){return;}
if(!self.isOpen||!self.isObject(self.iframe.documentSize)){return;}
var newWindowSize={width:$window.width(),height:$window.height()};if(Math.abs(self.currentWindowSize.width-newWindowSize.width)>5||Math.abs(self.currentWindowSize.height-newWindowSize.height)>5){self.currentWindowSize=newWindowSize;self.resize();}});}
if($.isFunction(self.options.onOpen)){self.options.onOpen(self);}
self.isOpen=true;};var dialogBeforeClose=function(){if(self.beforeCloseEnabled){return true;}
if(!self.beforeCloseIsBusy){self.beforeCloseIsBusy=true;setTimeout(function(){self.close(false);},1);}
return false;};var dialogClose=function(){if(self.options.autoFit){$(window).unbind(self.eventHandlerName('resize'));delete self.currentWindowSize;}
$(document).unbind(self.eventHandlerName('keydown'));$('.modalframe .ui-dialog-titlebar-close').unbind(self.eventHandlerName('keydown'));self.fixPosition($('.modalframe'),false);try{self.iframe.$element.remove();self.iframe.$container.dialog('destroy').remove();}catch(e){};delete self.iframe.documentSize;delete self.iframe.Drupal;delete self.iframe.$element;delete self.iframe.$container;if(self.beforeCloseEnabled){delete self.beforeCloseEnabled;}
if(self.beforeCloseIsBusy){delete self.beforeCloseIsBusy;}
self.isOpen=false;};var dialogOptions={modal:true,autoOpen:false,closeOnEscape:true,draggable:self.options.draggable,resizable:false,title:Drupal.t('Loading...'),dialogClass:'modalframe',open:dialogOpen,beforeclose:dialogBeforeClose,close:dialogClose};if(self.options.draggable){dialogOptions.dragStart=function(){self.iframe.$container.hide();};dialogOptions.dragStop=function(){self.iframe.$container.show('fast');};}
$.extend(dialogOptions,self.options.customDialogOptions);self.iframe.$container.dialog(dialogOptions);};Drupal.modalFrame.load=function(url){var self=this;var iframe=self.iframe.$element.get(0);var doc=(iframe.contentWindow||iframe.contentDocument);if(doc.document){doc=doc.document;}
self.iframe.$element.bind('load',function(){if(!self.iframe.$element.is(':visible')){setTimeout(function(){try{self.iframe.$element.fadeIn('fast');}catch(e){}},1000);}});doc.location.replace(url);};Drupal.modalFrame.canClose=function(){var self=this;if(!self.isOpen){return false;}
if(self.isObject(self.iframe.Drupal)){try{if(self.isObject(self.iframe.Drupal.dirtyForms)){if(self.iframe.Drupal.dirtyForms.isDirty()&&!confirm(self.dirtyFormsWarning)){return false;}
self.iframe.Drupal.dirtyForms.warning=null;}
if(self.isObject(self.iframe.Drupal.onBeforeUnload)){self.iframe.Drupal.onBeforeUnload.disable();}}catch(e){}}
return true;};Drupal.modalFrame.close=function(args,statusMessages){var self=this;if(!self.canClose()){delete self.beforeCloseIsBusy;return false;}
function closeDialog(){if(!self.isObject(self.iframe.$container)){return;}
self.beforeCloseEnabled=true;self.iframe.$container.dialog('close');if($.isFunction(self.options.onSubmit)){self.options.onSubmit(args,statusMessages);}}
if(!self.isObject(self.iframe.$element)||!self.iframe.$element.size()||!self.iframe.$element.is(':visible')){closeDialog();}
else{self.iframe.$element.fadeOut('fast',function(){$('.modalframe').animate({height:'hide',opacity:'hide'},closeDialog);});}
return true;};Drupal.modalFrame.bindChild=function(iFrameWindow,isClosing){var self=this;var $iFrameWindow=iFrameWindow.jQuery;var $iFrameDocument=$iFrameWindow(iFrameWindow.document);self.iframe.Drupal=iFrameWindow.Drupal;if(isClosing){return;}
if(self.isObject(self.iframe.Drupal.dirtyForms)){self.iframe.Drupal.dirtyForms.warning=self.dirtyFormsWarning;}
$('.modalframe .ui-dialog-title').html($iFrameDocument.attr('title'));$iFrameDocument.attr('tabIndex',-1).css('outline',0);self.iframe.$element.fadeIn('slow',function(){self.iframe.documentSize={width:$iFrameDocument.width(),height:$iFrameWindow('body').height()+25};if(self.options.autoFit){self.currentWindowSize={width:$(window).width(),height:$(window).height()};self.resize();$(window).unbind(self.eventHandlerName('childResize')).bind(self.eventHandlerName('childResize'),function(){self.iframe.documentSize={width:$iFrameDocument.width(),height:$iFrameWindow('body').height()+25};self.resize();});}
var $closeButton=$('.modalframe .ui-dialog-titlebar-close');var $iFrameTabbables=$iFrameWindow(':tabbable:not(form)');var $firstTabbable=$iFrameTabbables.filter(':first');var $lastTabbable=$iFrameTabbables.filter(':last');if(!$firstTabbable.focus().size()){$iFrameDocument.focus();}
$(document).unbind(self.eventHandlerName('keydown'));$closeButton.unbind(self.eventHandlerName('keydown'));$closeButton.bind(self.eventHandlerName('keydown'),function(event){if(event.keyCode&&event.keyCode==$.ui.keyCode.TAB){var $target=(event.shiftKey?$lastTabbable:$firstTabbable);if(!$target.size()){$target=$iFrameDocument;}
setTimeout(function(){$target.focus();},10);return false;}});$iFrameDocument.bind(self.eventHandlerName('keydown'),function(event){if(event.keyCode){if(event.keyCode==$.ui.keyCode.TAB){if(event.shiftKey&&event.target==$firstTabbable.get(0)){setTimeout(function(){$closeButton.focus();},10);return false;}
else if(!event.shiftKey&&event.target==$lastTabbable.get(0)){setTimeout(function(){$closeButton.focus();},10);return false;}}
else if(event.keyCode==$.ui.keyCode.ESCAPE){setTimeout(function(){self.close(false);},10);return false;}}});$(document).bind(self.eventHandlerName('keydown'),function(event){if(event.keyCode&&event.keyCode==$.ui.keyCode.TAB){setTimeout(function(){if(!$iFrameWindow(':tabbable:not(form):first').focus().size()){$closeButton.focus();}},10);return false;}});self.isChildLoaded=true;self.iframe.$container.addClass('modalframe-loaded');if($.isFunction(self.options.onLoad)){self.options.onLoad(self,$iFrameWindow,$iFrameDocument);}});};Drupal.modalFrame.unbindChild=function(iFrameWindow){var self=this;self.isChildLoaded=false;iFrameWindow.jQuery(iFrameWindow.document).unbind(self.eventHandlerName('keydown'));$(window).unbind(self.eventHandlerName('childResize'));$('.modalframe .ui-dialog-title').html(Drupal.t('Please, wait...'));self.iframe.$container.removeClass('modalframe-loaded');self.iframe.$element.fadeOut('fast');};Drupal.modalFrame.isObject=function(something){return(something!==null&&typeof something==='object');};Drupal.modalFrame.sanitizeSize=function(size){var width,height;var $window=$(window);var minWidth=300,maxWidth=$window.width()-30;if(typeof size.width!='number'){width=maxWidth;}
else if(size.width<minWidth||size.width>maxWidth){width=Math.min(maxWidth,Math.max(minWidth,size.width));}
else{width=size.width;}
var minHeight=100,maxHeight=$window.height()-30;if(typeof size.height!='number'){height=maxHeight;}
else if(size.height<minHeight||size.height>maxHeight){height=Math.min(maxHeight,Math.max(minHeight,size.height));}
else{height=size.height;}
return{width:width,height:height};};Drupal.modalFrame.fixPosition=function($element,isOpen){var self=this,$window=$(window);if($.browser.msie&&parseInt($.browser.version)<=6){if(isOpen){var yPos=$window.scrollTop();var xPos=$window.scrollLeft();$window.bind(self.eventHandlerName('scroll'),function(){window.scrollTo(xPos,yPos);});}
else{$window.unbind(self.eventHandlerName('scroll'));}}
else{if(isOpen){var offset=$element.offset();$element.css({left:(offset.left-$window.scrollLeft()),top:(offset.top-$window.scrollTop()),position:'fixed'});}}};Drupal.modalFrame.computeCenterPosition=function($element,elementSize){var $window=$(window);var position={left:Math.max(0,parseInt(($window.width()-elementSize.width)/2)),top:Math.max(0,parseInt(($window.height()-elementSize.height)/2))};if($element.css('position')!='fixed'){var $document=$(document);position.left+=$document.scrollLeft();position.top+=$document.scrollTop();}
return position;};Drupal.modalFrame.resize=function(){var self=this,documentSize=self.iframe.documentSize;var maxSize=self.sanitizeSize({}),titleBarHeight=$('.modalframe .ui-dialog-titlebar').outerHeight(true);var frameSize=self.sanitizeSize(documentSize),dialogSize=$.extend({},frameSize);if((dialogSize.height+titleBarHeight)<=maxSize.height){dialogSize.height+=titleBarHeight;}
else{dialogSize.height=maxSize.height;frameSize.height=dialogSize.height-titleBarHeight;}
var dialogPosition=self.computeCenterPosition($('.modalframe'),dialogSize);var animationOptions=$.extend(dialogSize,dialogPosition);$('.modalframe').animate(animationOptions,'fast',function(){if(self.isObject(self.iframe.$element)&&self.isObject(self.iframe.$container)){$('.modalframe').width(dialogSize.width).height(dialogSize.height);self.iframe.$container.width(frameSize.width).height(frameSize.height);self.iframe.$element.width(frameSize.width).height(frameSize.height);self.iframe.$container.dialog('option',{width:dialogSize.width,height:dialogSize.height});}});};Drupal.theme.prototype.modalFrameThrobber=function(){return'<div class="modalframe-throbber">&nbsp;</div>';};})(jQuery);
