/*
 * SimpleModal 1.4.1 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * Copyright (c) 2010 Eric Martin (http://twitter.com/ericmmartin)
 * Dual licensed under the MIT and GPL licenses
 * Revision: $Id: jquery.simplemodal.js 261 2010-11-05 21:16:20Z emartin24 $
 */

/**
 * SimpleModal is a lightweight jQuery plugin that provides a simple
 * interface to create a modal dialog.
 *
 * The goal of SimpleModal is to provide developers with a cross-browser
 * overlay and container that will be populated with data provided to
 * SimpleModal.
 *
 * There are two ways to call SimpleModal:
 * 1) As a chained function on a jQuery object, like $('#myDiv').modal();.
 * This call would place the DOM object, #myDiv, inside a modal dialog.
 * Chaining requires a jQuery object. An optional options object can be
 * passed as a parameter.
 *
 * @example $('<div>my data</div>').modal({options});
 * @example $('#myDiv').modal({options});
 * @example jQueryObject.modal({options});
 *
 * 2) As a stand-alone function, like $.modal(data). The data parameter
 * is required and an optional options object can be passed as a second
 * parameter. This method provides more flexibility in the types of data
 * that are allowed. The data could be a DOM object, a jQuery object, HTML
 * or a string.
 *
 * @example $.modal('<div>my data</div>', {options});
 * @example $.modal('my data', {options});
 * @example $.modal($('#myDiv'), {options});
 * @example $.modal(jQueryObject, {options});
 * @example $.modal(document.getElementById('myDiv'), {options});
 *
 * A SimpleModal call can contain multiple elements, but only one modal
 * dialog can be created at a time. Which means that all of the matched
 * elements will be displayed within the modal container.
 *
 * SimpleModal internally sets the CSS needed to display the modal dialog
 * properly in all browsers, yet provides the developer with the flexibility
 * to easily control the look and feel. The styling for SimpleModal can be
 * done through external stylesheets, or through SimpleModal, using the
 * overlayCss, containerCss, and dataCss options.
 *
 * SimpleModal has been tested in the following browsers:
 * - IE 6, 7, 8, 9
 * - Firefox 2, 3, 4
 * - Opera 9, 10
 * - Safari 3, 4, 5
 * - Chrome 1, 2, 3, 4, 5, 6
 *
 * @name SimpleModal
 * @type jQuery
 * @requires jQuery v1.2.4
 * @cat Plugins/Windows and Overlays
 * @author Eric Martin (http://ericmmartin.com)
 * @version 1.4.1
 */
(function(d){var c=d.browser.msie&&parseInt(d.browser.version)===6&&typeof window.XMLHttpRequest!=="object",b=d.browser.msie&&parseInt(d.browser.version)===7,e=null,a=[];d.modal=function(g,f){return d.modal.impl.init(g,f)};d.modal.close=function(){d.modal.impl.close()};d.modal.focus=function(f){d.modal.impl.focus(f)};d.modal.setContainerDimensions=function(){d.modal.impl.setContainerDimensions()};d.modal.setPosition=function(){d.modal.impl.setPosition()};d.modal.update=function(f,g){d.modal.impl.update(f,g)};d.fn.modal=function(f){return d.modal.impl.init(this,f)};d.modal.defaults={appendTo:"body",focus:true,opacity:50,overlayId:"simplemodal-overlay",overlayCss:{},containerId:"simplemodal-container",containerCss:{},dataId:"simplemodal-data",dataCss:{},minHeight:null,minWidth:null,maxHeight:null,maxWidth:null,autoResize:false,autoPosition:true,zIndex:1000,close:true,closeHTML:'<a class="modalCloseImg" title="Close"></a>',closeClass:"simplemodal-close",escClose:true,overlayClose:false,position:null,persist:false,modal:true,onOpen:null,onShow:null,onClose:null};d.modal.impl={d:{},init:function(h,f){var g=this;if(g.d.data){return false}e=d.browser.msie&&!d.boxModel;g.o=d.extend({},d.modal.defaults,f);g.zIndex=g.o.zIndex;g.occb=false;if(typeof h==="object"){h=h instanceof jQuery?h:d(h);g.d.placeholder=false;if(h.parent().parent().size()>0){h.before(d("<span></span>").attr("id","simplemodal-placeholder").css({display:"none"}));g.d.placeholder=true;g.display=h.css("display");if(!g.o.persist){g.d.orig=h.clone(true)}}}else{if(typeof h==="string"||typeof h==="number"){h=d("<div></div>").html(h)}else{alert("SimpleModal Error: Unsupported data type: "+typeof h);return g}}g.create(h);h=null;g.open();if(d.isFunction(g.o.onShow)){g.o.onShow.apply(g,[g.d])}return g},create:function(g){var f=this;a=f.getDimensions();if(f.o.modal&&c){f.d.iframe=d('<iframe src="javascript:false;"></iframe>').css(d.extend(f.o.iframeCss,{display:"none",opacity:0,position:"fixed",height:a[0],width:a[1],zIndex:f.o.zIndex,top:0,left:0})).appendTo(f.o.appendTo)}f.d.overlay=d("<div></div>").attr("id",f.o.overlayId).addClass("simplemodal-overlay").css(d.extend(f.o.overlayCss,{display:"none",opacity:f.o.opacity/100,height:f.o.modal?a[0]:0,width:f.o.modal?a[1]:0,position:"fixed",left:0,top:0,zIndex:f.o.zIndex+1})).appendTo(f.o.appendTo);f.d.container=d("<div></div>").attr("id",f.o.containerId).addClass("simplemodal-container").css(d.extend(f.o.containerCss,{display:"none",position:"fixed",zIndex:f.o.zIndex+2})).append(f.o.close&&f.o.closeHTML?d(f.o.closeHTML).addClass(f.o.closeClass):"").appendTo(f.o.appendTo);f.d.wrap=d("<div></div>").attr("tabIndex",-1).addClass("simplemodal-wrap").css({height:"100%",outline:0,width:"100%"}).appendTo(f.d.container);f.d.data=g.attr("id",g.attr("id")||f.o.dataId).addClass("simplemodal-data").css(d.extend(f.o.dataCss,{display:"none"})).appendTo("body");g=null;f.setContainerDimensions();f.d.data.appendTo(f.d.wrap);if(c||e){f.fixIE()}},bindEvents:function(){var f=this;d("."+f.o.closeClass).bind("click.simplemodal",function(g){g.preventDefault();f.close()});if(f.o.modal&&f.o.close&&f.o.overlayClose){f.d.overlay.bind("click.simplemodal",function(g){g.preventDefault();f.close()})}d(document).bind("keydown.simplemodal",function(g){if(f.o.modal&&g.keyCode===9){f.watchTab(g)}else{if((f.o.close&&f.o.escClose)&&g.keyCode===27){g.preventDefault();f.close()}}});d(window).bind("resize.simplemodal",function(){a=f.getDimensions();f.o.autoResize?f.setContainerDimensions():f.o.autoPosition&&f.setPosition();if(c||e){f.fixIE()}else{if(f.o.modal){f.d.iframe&&f.d.iframe.css({height:a[0],width:a[1]});f.d.overlay.css({height:a[0],width:a[1]})}}})},unbindEvents:function(){d("."+this.o.closeClass).unbind("click.simplemodal");d(document).unbind("keydown.simplemodal");d(window).unbind("resize.simplemodal");this.d.overlay.unbind("click.simplemodal")},fixIE:function(){var f=this,g=f.o.position;d.each([f.d.iframe||null,!f.o.modal?null:f.d.overlay,f.d.container],function(t,l){if(l){var q="document.body.clientHeight",v="document.body.clientWidth",x="document.body.scrollHeight",u="document.body.scrollLeft",o="document.body.scrollTop",k="document.body.scrollWidth",j="document.documentElement.clientHeight",r="document.documentElement.clientWidth",p="document.documentElement.scrollLeft",y="document.documentElement.scrollTop",z=l[0].style;z.position="absolute";if(t<2){z.removeExpression("height");z.removeExpression("width");z.setExpression("height",""+x+" > "+q+" ? "+x+" : "+q+' + "px"');z.setExpression("width",""+k+" > "+v+" ? "+k+" : "+v+' + "px"')}else{var n,h;if(g&&g.constructor===Array){var w=g[0]?typeof g[0]==="number"?g[0].toString():g[0].replace(/px/,""):l.css("top").replace(/px/,"");n=w.indexOf("%")===-1?w+" + (t = "+y+" ? "+y+" : "+o+') + "px"':parseInt(w.replace(/%/,""))+" * (("+j+" || "+q+") / 100) + (t = "+y+" ? "+y+" : "+o+') + "px"';if(g[1]){var m=typeof g[1]==="number"?g[1].toString():g[1].replace(/px/,"");h=m.indexOf("%")===-1?m+" + (t = "+p+" ? "+p+" : "+u+') + "px"':parseInt(m.replace(/%/,""))+" * (("+r+" || "+v+") / 100) + (t = "+p+" ? "+p+" : "+u+') + "px"'}}else{n="("+j+" || "+q+") / 2 - (this.offsetHeight / 2) + (t = "+y+" ? "+y+" : "+o+') + "px"';h="("+r+" || "+v+") / 2 - (this.offsetWidth / 2) + (t = "+p+" ? "+p+" : "+u+') + "px"'}z.removeExpression("top");z.removeExpression("left");z.setExpression("top",n);z.setExpression("left",h)}}})},focus:function(i){var g=this,h=i&&d.inArray(i,["first","last"])!==-1?i:"first";var f=d(":input:enabled:visible:"+h,g.d.wrap);setTimeout(function(){f.length>0?f.focus():g.d.wrap.focus()},10)},getDimensions:function(){var g=d(window);var f=d.browser.opera&&d.browser.version>"9.5"&&d.fn.jquery<"1.3"||d.browser.opera&&d.browser.version<"9.5"&&d.fn.jquery>"1.2.6"?g[0].innerHeight:g.height();return[f,g.width()]},getVal:function(f,g){return f?(typeof f==="number"?f:f==="auto"?0:f.indexOf("%")>0?((parseInt(f.replace(/%/,""))/100)*(g==="h"?a[0]:a[1])):parseInt(f.replace(/px/,""))):null},update:function(f,h){var g=this;if(!g.d.data){return false}g.d.origHeight=g.getVal(f,"h");g.d.origWidth=g.getVal(h,"w");g.d.data.hide();f&&g.d.container.css("height",f);h&&g.d.container.css("width",h);g.setContainerDimensions();g.d.data.show();g.o.focus&&g.focus();g.unbindEvents();g.bindEvents()},setContainerDimensions:function(){var q=this,i=c||b;var f=q.d.origHeight?q.d.origHeight:d.browser.opera?q.d.container.height():q.getVal(i?q.d.container[0].currentStyle.height:q.d.container.css("height"),"h"),h=q.d.origWidth?q.d.origWidth:d.browser.opera?q.d.container.width():q.getVal(i?q.d.container[0].currentStyle.width:q.d.container.css("width"),"w"),m=q.d.data.outerHeight(true),g=q.d.data.outerWidth(true);q.d.origHeight=q.d.origHeight||f;q.d.origWidth=q.d.origWidth||h;var j=q.o.maxHeight?q.getVal(q.o.maxHeight,"h"):null,n=q.o.maxWidth?q.getVal(q.o.maxWidth,"w"):null,l=j&&j<a[0]?j:a[0],p=n&&n<a[1]?n:a[1];var k=q.o.minHeight?q.getVal(q.o.minHeight,"h"):"auto";if(!f){if(!m){f=k}else{if(m>l){f=l}else{if(q.o.minHeight&&k!=="auto"&&m<k){f=k}else{f=m}}}}else{f=q.o.autoResize&&f>l?l:f<k?k:f}var o=q.o.minWidth?q.getVal(q.o.minWidth,"w"):"auto";if(!h){if(!g){h=o}else{if(g>p){h=p}else{if(q.o.minWidth&&o!=="auto"&&g<o){h=o}else{h=g}}}}else{h=q.o.autoResize&&h>p?p:h<o?o:h}q.d.container.css({height:f,width:h});q.d.wrap.css({overflow:(m>f||g>h)?"auto":"visible"});q.o.autoPosition&&q.setPosition()},setPosition:function(){var g=this,i,h,j=(a[0]/2)-(g.d.container.outerHeight(true)/2),f=(a[1]/2)-(g.d.container.outerWidth(true)/2);if(g.o.position&&Object.prototype.toString.call(g.o.position)==="[object Array]"){i=g.o.position[0]||j;h=g.o.position[1]||f}else{i=j;h=f}g.d.container.css({left:h,top:i})},watchTab:function(g){var f=this;if(d(g.target).parents(".simplemodal-container").length>0){f.inputs=d(":input:enabled:visible:first, :input:enabled:visible:last",f.d.data[0]);if((!g.shiftKey&&g.target===f.inputs[f.inputs.length-1])||(g.shiftKey&&g.target===f.inputs[0])||f.inputs.length===0){g.preventDefault();var h=g.shiftKey?"last":"first";f.focus(h)}}else{g.preventDefault();f.focus()}},open:function(){var f=this;f.d.iframe&&f.d.iframe.show();if(d.isFunction(f.o.onOpen)){f.o.onOpen.apply(f,[f.d])}else{f.d.overlay.show();f.d.container.show();f.d.data.show()}f.o.focus&&f.focus();f.bindEvents()},close:function(){var f=this;if(!f.d.data){return false}f.unbindEvents();if(d.isFunction(f.o.onClose)&&!f.occb){f.occb=true;f.o.onClose.apply(f,[f.d])}else{if(f.d.placeholder){var g=d("#simplemodal-placeholder");if(f.o.persist){g.replaceWith(f.d.data.removeClass("simplemodal-data").css("display",f.display))}else{f.d.data.hide().remove();g.replaceWith(f.d.orig)}}else{f.d.data.hide().remove()}f.d.container.hide().remove();f.d.overlay.hide();f.d.iframe&&f.d.iframe.hide().remove();setTimeout(function(){f.d.overlay.remove();f.d={}},10)}}}})(jQuery);
