/*
 *
 * @name AJAX Popup
 * @type jQuery
 * @requires jQuery v1.3.2
 * @author Daniele Tardia (http://www.lupusinfabula.it)
 * @version 0.3
 *
 * Customization of SimpleModal 1.3.3 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * Copyright (c) 2009 Eric Martin (http://twitter.com/EricMMartin)
 * Dual licensed under the MIT and GPL licenses
 * Revision: $Id: jquery.simplemodal.js 228 2009-10-30 13:34:27Z emartin24 $
 *
 */
;(function ($) {
	var ie6 = $.browser.msie && parseInt($.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object",
		ieQuirks = null,
		w = [];

	/*
	 * Stand-alone function to create a mdbpopup dialog.
	 *
	 * @param {string, object} data A string, jQuery object or DOM object
	 * @param {object} [options] An optional object containing options overrides
	 */
	$.mdbpopup = function (data, options) {
		return $.mdbpopup.impl.init(data, options);
	};

	/*
	 * Stand-alone close function to close the mdbpopup dialog
	 */
	$.mdbpopup.close = function () {
		$.mdbpopup.impl.close();
	};
	/*
	 * Stand-alone close function to close the mdbpopup dialog
	 */
	$.mdbpopup.setOverlayClose = function (value) {
		$.mdbpopup.impl.setOverlayClose(value);
	};
	/*
	 *
	 */
	$.mdbpopup.setTimeClose = function (value,duration) {
		$.mdbpopup.impl.setTimeClose(value,duration);
	};
	/*
	 *
	 */
	$.mdbpopup.changePage = function (value, options) {
		$.mdbpopup.impl.changePage(value, options);
	};
	/*
	 * Chained function to create a mdbpopup dialog.
	 *
	 * @param {object} [options] An optional object containing options overrides
	 */
	$.fn.mdbpopup = function (options) {
		return $.mdbpopup.impl.init(this, options);
	};

	/*
	 * mdbpopup default options
	 *
	 * appendTo:		(String:'body') The jQuery selector to append the elements to. For ASP.NET, use 'form'.
	 * focus:			(Boolean:true) Forces focus to remain on the mdbpopup dialog
	 * opacity:			(Number:50) The opacity value for the overlay div, from 0 - 100
	 * overlayId:		(String:'mdbpopup-overlay') The DOM element id for the overlay div
	 * overlayCss:		(Object:{}) The CSS styling for the overlay div
	 * containerId:		(String:'mdbpopup-container') The DOM element id for the container div
	 * containerCss:	(Object:{}) The CSS styling for the container div
	 * dataId:			(String:'mdbpopup-data') The DOM element id for the data div
	 * dataCss:			(Object:{}) The CSS styling for the data div
	 * minHeight:		(Number:200) The minimum height for the container
	 * minWidth:		(Number:200) The minimum width for the container
	 * maxHeight:		(Number:null) The maximum height for the container. If not specified, the window height is used.
	 * maxWidth:		(Number:null) The maximum width for the container. If not specified, the window width is used.
	 * autoResize:		(Boolean:false) Resize container on window resize? Use with caution - this may have undesirable side-effects.
	 * autoPosition:	(Boolean:true) Reposition container on window resize?
	 * zIndex:			(Number: 1000) Starting z-index value
	 * close:			(Boolean:true) If true, closeHTML, escClose and overClose will be used if set.
	 							If false, none of them will be used.
	 * closeHTML:		(String:'<a class="mdbpopupCloseImg" title="Close"></a>') The HTML for the
							default close link. mdbpopup will automatically add the closeClass to this element.
	 * closeClass:		(String:'mdbpopup-close') The CSS class used to bind to the close event
	 * escClose:		(Boolean:true) Allow Esc keypress to close the dialog?
	 * overlayClose:	(Boolean:false) Allow click on overlay to close the dialog?
	 * position:		(Array:null) Position of container [top, left]. Can be number of pixels or percentage
	 * persist:			(Boolean:false) Persist the data across mdbpopup calls? Only used for existing
								DOM elements. If true, the data will be maintained across mdbpopup calls, if false,
								the data will be reverted to its original state.
	 * onOpen:			(Function:null) The callback function used in place of mdbpopup's open
	 * onShow:			(Function:null) The callback function used after the mdbpopup dialog has opened
	 * onClose:			(Function:null) The callback function used in place of mdbpopup's close
	 */
	$.mdbpopup.defaults = {
		title:'&nbsp;',
		headerId:'mdbpopup-header',
		loaderPath:'/images/jquery/mdbpopup/loader.gif',
		asincString: true,
		timed: false,
		closeTime: 5,
		closeTimer: -1,
		borderTableId: 'mdbpopup-borderTable',
		borderTableCss: {},
		externalURL:'',
		appendTo: 'body',
		focus: true,
		opacity: 70,
		overlayId: 'mdbpopup-overlay',
		overlayCss: {},
		containerId: 'mdbpopup-container',
		containerCss: {},
		dataId: 'mdbpopup-data',
		dataCss: {},
		minWidth: 400,
		minHeight: 150,
		maxHeight: null,
		maxWidth: null,
		autoResize: false,
		autoPosition: true,
		zIndex: 1000,
		close: true,
		closeHTML: '<a href="javascript:void(0)" class="mdbpopupCloseImg" title="Close" style="float:right; margin-right: 8px;"><img src="/images/jquery/mdbpopup/chiudi.gif" border="0" width="45" height="11"></a>',
		closeClass: 'mdbpopup-close',
		escClose: true,
		overlayClose: true,
		position: null,
		persist: false,
		//onOpen: null,
		onShow: null,
		//onClose: null
		onOpen: function(dialog){},
		onClose: function (dialog) {
			dialog.container.fadeOut(200);
			dialog.borderTable.fadeOut(200);
			dialog.overlay.fadeOut(200, function () {
				$.mdbpopup.close();
			});
		}
	};

	/*
	 * Main mdbpopup object
	 */
	$.mdbpopup.impl = {
		/*
		 * mdbpopup dialog options
		 */
		o: null,
		/*
		 * Contains the mdbpopup dialog elements and is the object passed
		 * back to the callback (onOpen, onShow, onClose) functions
		 */
		d: {},
		/*
		 * Initialize the mdbpopup dialog
		 */
		init: function (data, options) {
			var s = this;

			// don't allow multiple calls
			if (s.d.data) {
				return false;
			}

			// $.boxModel is undefined if checked earlier
			ieQuirks = $.browser.msie && !$.boxModel;

			// merge defaults and user options
			s.o = $.extend({}, $.mdbpopup.defaults, options);

			// keep track of z-index
			s.zIndex = s.o.zIndex;

			// set the onClose callback flag
			s.occb = false;

			// determine how to handle the data based on its type
			if (typeof data == 'object') {
				// convert DOM object to a jQuery object
				data = data instanceof jQuery ? data : $(data);

				// if the object came from the DOM, keep track of its parent
				if (data.parent().parent().size() > 0) {
					s.d.parentNode = data.parent();

					// persist changes? if not, make a clone of the element
					if (!s.o.persist) {
						s.d.orig = data.clone(true);
					}
				}
			}
			else if (typeof data == 'string' || typeof data == 'number') {
				// just insert the data as innerHTML
				var tmp = data;
				data = $('<div></div>').html(tmp).data('url',tmp);
			}
			else {
				// unsupported data type!
				alert('mdbpopup Error: Unsupported data type: ' + typeof data);
				return s;
			}

			// create the mdbpopup overlay, container and, if necessary, iframe
			s.create(data);
			data = null;

			// display the mdbpopup dialog
			s.open();

			// useful for adding events/manipulating data in the mdbpopup dialog
			if ($.isFunction(s.o.onShow)) {
				s.o.onShow.apply(s, [s.d]);
			}

			// don't break the chain =)
			return s;
		},
		/*
		 * Create and add the mdbpopup overlay and container to the page
		 */
		create: function (data) {
			var s = this;

			// get the window properties
			w = s.getDimensions();

			// add an iframe to prevent select options from bleeding through
			if (ie6) {
				s.d.iframe = $('<iframe src="javascript:false;"></iframe>')
					.css($.extend(s.o.iframeCss, {
						display: 'none',
						opacity: 0,
						position: 'fixed',
						height: w[0],
						width: w[1],
						zIndex: s.o.zIndex,
						top: 0,
						left: 0
					}))
					.appendTo(s.o.appendTo);
			}

			// create the overlay
			s.d.overlay = $('<div></div>')
				.attr('id', s.o.overlayId)
				.addClass('mdbpopup-overlay')
				.css($.extend(s.o.overlayCss, {
					display: 'none',
					opacity: s.o.opacity / 100,
					height: w[0],
					width: w[1],
					position: 'fixed',
					left: 0,
					top: 0,
					zIndex: s.o.zIndex + 1
				}))
				.appendTo(s.o.appendTo);

			s.d.header =  $('<div></div>')
				.attr('id', s.o.headerId)
				.addClass('mdbpopup-header')
				.append("<b style='margin-left: 10px;'>"+((s.o.title != null && s.o.title != '')?s.o.title:'')+"</b>")
				.append(s.o.close && s.o.closeHTML
					? $(s.o.closeHTML).addClass(s.o.closeClass)
					: '')
				.append($('<div style="clear: both;"></div>'));

			var tableCode  = '<div><table cellpadding="0" cellspacing="0">';
			tableCode += '<tr><td class="mdbpopup-cornerLT"><img src="/images/jquery/mdbpopup/pixel.gif"></td><td></td><td class="mdbpopup-cornerRT"></td></tr>';
			tableCode += '<tr><td></td><td class="mdbpopup-center"></td><td></td></tr>';
			tableCode += '<tr><td class="mdbpopup-cornerLB"></td><td></td><td class="mdbpopup-cornerRB"></td></tr></table></div>';
			s.d.borderTable = $(tableCode)
			.attr('id', s.o.borderTableId)
			.addClass('mdbpopup-borderTable')
			.css($.extend(s.o.borderTableCss, {
				display: 'none',
				position: 'fixed',
				zIndex: s.o.zIndex + 2
			})).appendTo(s.o.appendTo);

			// create the container
			s.d.container = $('<div></div>')
				.attr('id', s.o.containerId)
				.addClass('mdbpopup-container')
				.css($.extend(s.o.containerCss, {
					display: 'none',
					position: 'fixed',
					zIndex: s.o.zIndex + 3
				}));
			s.d.container.append(s.d.header);
			s.d.container.appendTo(s.o.appendTo);
			//s.d.container.find("#mdbpopupContainerCenter").append(s.d.header)

			s.d.wrap = $('<div></div>')
				.attr('tabIndex', -1)
				.addClass('mdbpopup-wrap')
				.css({height: '100%', outline: 0, width: '100%',
					cssFloat:'left',
					backgroundRepeat:'no-repeat',
					backgroundPosition:'center'})
				.appendTo(s.d.container);
				//.appendTo(s.d.container.find("#mdbpopupContainerCenter"));

			// add styling and attributes to the data
			// append to body to get correct dimensions, then move to wrap
			s.d.data = data
				.attr('id', data.attr('id') || s.o.dataId)
				.addClass('mdbpopup-data')
				.css($.extend(s.o.dataCss, {
						display: 'none'
				}))
				.appendTo('body');
			if (s.o.asincString) {
				s.o.externalURL = s.d.data.data('url');
				//s.d.data.html();
				s.d.data.html('');
				s.d.wrap.css('background-image','url("'+s.o.loaderPath+'")');
			}
			data = null;

			s.setContainerDimensions();
			s.d.data.appendTo(s.d.wrap);

			// fix issues with IE
			if (ie6 || ieQuirks) {
				s.fixIE();
			}
		},
		/*
		 * Bind events
		 */
		bindEvents: function () {
			var s = this;

			// bind the close event to any element with the closeClass class
			$('.' + s.o.closeClass).bind('click.mdbpopup', function (e) {
				e.preventDefault();
				s.close();
			});

			// bind the overlay click to the close function, if enabled
			if (s.o.close && s.o.overlayClose) {
				//s.d.overlay.bind('click.mdbpopup', function (e) {
				//	e.preventDefault();
				//	s.close();
				//});
				s.setOverlayClose(true);
			}

			// bind keydown events
			$(document).bind('keydown.mdbpopup', function (e) {
				if (s.o.focus && e.keyCode == 9) { // TAB
					s.watchTab(e);
				}
				else if ((s.o.close && s.o.escClose) && e.keyCode == 27) { // ESC
					e.preventDefault();
					s.close();
				}
			});

			// update window size

			$(window).bind('resize.mdbpopup', function () {
				// redetermine the window width/height
				w = s.getDimensions();

				// reposition the dialog
				s.setContainerDimensions(true);

				if (ie6 || ieQuirks) {
					s.fixIE();
				}
				else {
					// update the iframe & overlay
					s.d.iframe && s.d.iframe.css({height: w[0], width: w[1]});
					s.d.overlay.css({height: w[0], width: w[1]});
				}
			});
		},
		/*
		 * Unbind events
		 */
		unbindEvents: function () {
			$('.' + this.o.closeClass).unbind('click.mdbpopup');
			$(document).unbind('keydown.mdbpopup');
			//$(window).unbind('resize.mdbpopup');
			this.d.overlay.unbind('click.mdbpopup');
		},
		/*
		 * Fix issues in IE6 and IE7 in quirks mode
		 */
		fixIE: function () {
			var s=this, p = s.o.position;

			// simulate fixed position - adapted from BlockUI
			$.each([s.d.iframe || null, s.d.overlay, s.d.borderTable, s.d.container], function (i, el) {
				if (el) {
					var bch = 'document.body.clientHeight', bcw = 'document.body.clientWidth',
						bsh = 'document.body.scrollHeight', bsl = 'document.body.scrollLeft',
						bst = 'document.body.scrollTop', bsw = 'document.body.scrollWidth',
						ch = 'document.documentElement.clientHeight', cw = 'document.documentElement.clientWidth',
						sl = 'document.documentElement.scrollLeft', st = 'document.documentElement.scrollTop',
						s = el[0].style;

					s.position = 'absolute';
					if (i < 2) {
						s.removeExpression('height');
						s.removeExpression('width');
						//s.setExpression('height','' + bsh + ' > ' + bch + ' ? ' + bsh + ' : ' + bch + ' + "px"');
						s.setExpression('width','' + bsw + ' > ' + bcw + ' ? ' + bsw + ' : ' + bcw + ' + "px"');
					}
					else {
						var te, le;
						if (p && p.constructor == Array) {
							var top = p[0]
								? typeof p[0] == 'number' ? p[0].toString() : p[0].replace(/px/, '')
								: el.css('top').replace(/px/, '');
							te = top.indexOf('%') == -1
								? top + ' + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"'
								: parseInt(top.replace(/%/, '')) + ' * ((' + ch + ' || ' + bch + ') / 100) + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"';

							if (p[1]) {
								var left = typeof p[1] == 'number' ? p[1].toString() : p[1].replace(/px/, '');
								le = left.indexOf('%') == -1
									? left + ' + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"'
									: parseInt(left.replace(/%/, '')) + ' * ((' + cw + ' || ' + bcw + ') / 100) + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"';
							}
						}
						else {
							te = '(' + ch + ' || ' + bch + ') / 2 - (this.offsetHeight / 2) + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"';
							le = '(' + cw + ' || ' + bcw + ') / 2 - (this.offsetWidth / 2) + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"';
						}
						s.removeExpression('top');
						s.removeExpression('left');
						s.setExpression('top', te);
						s.setExpression('left', le);
					}
				}
			});
		},
		focus: function (pos) {
			var s = this, p = pos || 'first';

			// focus on dialog or the first visible/enabled input element
			var input = $(':input:enabled:visible:' + p, s.d.wrap);
			input.length > 0 ? input.focus() : s.d.wrap.focus();
		},
		getDimensions: function () {
			var el = $(window);

			// fix a jQuery/Opera bug with determining the window height
			var h = $.browser.opera && $.browser.version > '9.5' && $.fn.jquery <= '1.2.6' ? document.documentElement['clientHeight'] :
				$.browser.opera && $.browser.version < '9.5' && $.fn.jquery > '1.2.6' ? window.innerHeight :
				el.height();
			return [h, el.width()];
		},
		getVal: function (v) {
			return v == 'auto' ? 0
				: v.indexOf('%') > 0 ? v
					: parseInt(v.replace(/px/, ''));
		},
		trace: function (msg) {
			//if($("#debug"))$("#debug").html($("#debug").html()+msg+"<br/>");
		},
		setContainerDimensions: function (resize) {
			var s = this;

			if (!resize || (resize && s.o.autoResize)) {

				var cornersW = s.getVal(s.d.borderTable.find(".mdbpopup-cornerLT").css('width'))+s.getVal(s.d.borderTable.find(".mdbpopup-cornerRT").css('width'));
				var cornersH = s.getVal(s.d.borderTable.find(".mdbpopup-cornerLT").css('height'))+s.getVal(s.d.borderTable.find(".mdbpopup-cornerLB").css('height'));

				// get the dimensions for the container and data
				var ch = s.getVal(s.d.container.css('height')), cw = s.getVal(s.d.container.css('width')),
					dh = s.d.data.outerHeight(true)+ s.getVal(s.d.data.css('margin-top'))+ s.getVal(s.d.data.css('margin-bottom')), dw = s.d.data.outerWidth(true);

				var mh = s.o.maxHeight && s.o.maxHeight < (w[0]-cornersW) ? s.o.maxHeight : (w[0]-cornersW),
					mw = s.o.maxWidth && s.o.maxWidth < (w[1]-cornersH) ? s.o.maxWidth : (w[1]-cornersH);


				// DT Modifica per jQuery 1.7.
    			ch = 0; cw=0;

				// height
				if (!ch) {
					if (!dh) {ch = s.o.minHeight;}
					else {
						if (dh > mh) {ch = mh;}
						else if (dh < s.o.minHeight) {ch = s.o.minHeight;}
						else {ch = dh;}
					}
				}
				else {
					ch = ch > mh ? mh : ch;
				}

				// width
				if (!cw) {
					if (!dw) {cw = s.o.minWidth;}
					else {
						if (dw > mw) {cw = mw;}
						else if (dw < s.o.minWidth) {cw = s.o.minWidth;}
						else {cw = dw;}
					}
				}
				else {
					cw = cw > mw ? mw : cw;
				}
				s.d.container.css({height: ch, width: cw});
				s.d.borderTable.css({height: ch + cornersH, width: cw + cornersW});
				s.d.borderTable.find(".mdbpopup-center").css({height: ch, width: cw});
				s.d.header.css({width: cw - s.getVal(s.d.header.css("padding-left")) - s.getVal(s.d.header.css("padding-right"))});

				s.d.wrap.css({height: ch - s.getVal(s.d.header.css('height')) - (cornersH/2) - (($.browser.msie)?15:0)});
				if (dh > ch || dw > cw) {
					s.d.wrap.css({overflow:'hidden'});
					//radames di default c'era auto
				}
			}

			if (s.o.autoPosition) {
				s.setPosition();
			}
		},
		setPosition: function () {
			var s = this, top, left,
				hc = (w[0]/2) - ((s.d.container)?(s.d.container.outerHeight(true)/2):0),
				vc = (w[1]/2) - ((s.d.container)?(s.d.container.outerWidth(true)/2):0);

			if (s.o.position && Object.prototype.toString.call(s.o.position) === "[object Array]") {
				top = s.o.position[0] || hc;
				left = s.o.position[1] || vc;
			} else {
				top = hc;
				left = vc;
			}
			if(s.d.container)s.d.container.css({left: left, top: top});
			if(s.d.borderTable){
				var cornersW = s.getVal(s.d.borderTable.find(".mdbpopup-cornerLT").css('width'));
				var cornersH = s.getVal(s.d.borderTable.find(".mdbpopup-cornerLT").css('height'));
				//alert(s.d.borderTable.find(".mdbpopup-cornerLT img").css('height'))
				s.d.borderTable.css({left: left - cornersW, top: top - cornersH});
			}
		},
		watchTab: function (e) {
			var s = this;

			if ($(e.target).parents('.mdbpopup-container').length > 0) {
				// save the list of inputs
				s.inputs = $(':input:enabled:visible:first, :input:enabled:visible:last', s.d.data[0]);

				// if it's the first or last tabbable element, refocus
				if ((!e.shiftKey && e.target == s.inputs[s.inputs.length -1]) ||
						(e.shiftKey && e.target == s.inputs[0]) ||
						s.inputs.length == 0) {
					e.preventDefault();
					var pos = e.shiftKey ? 'last' : 'first';
					setTimeout(function () {s.focus(pos);}, 10);
				}
			}
			else {
				// might be necessary when custom onShow callback is used
				e.preventDefault();
				setTimeout(function () {s.focus();}, 10);
			}
		},
		/*
		 * Open the modal dialog elements
		 * - Note: If you use the onOpen callback, you must "show" the
		 *	        overlay and container elements manually
		 *         (the iframe will be handled by mdbpopup)
		 */
		open: function () {
			var s = this;
			// display the iframe
			s.d.iframe && s.d.iframe.show();

			if ($.isFunction(s.o.onOpen)) {
				// execute the onOpen callback
				//s.o.onOpen.apply(s, [s.d]);
				s.d.overlay.fadeIn(200, function(){
					//s.d.data.hide();
				});
				s.d.borderTable.fadeIn(200);
				s.d.container.fadeIn(200, function(){
					if(!s.o.asincString)s.d.data.fadeIn(200, function(){
						clearTimeout(s.o.closeTimer);
						if(s.o.timed)s.o.closeTimer = setTimeout(function () {s.close();}, s.o.closeTime*1000);
						//if(s.o.asincString) alert("ss:"+s.o.externalURL);// ;
					});
					else s.loadExternal(s.o.externalURL);
				});
			}
			else {
				// display the remaining elements
				s.d.overlay.show();
				s.d.borderTable.show();
				s.d.container.show();
				if(!s.o.asincString){
					clearTimeout(s.o.closeTimer);
					if(s.o.timed)s.o.closeTimer = setTimeout(function () {s.close();}, s.o.closeTime*1000);
					s.d.data.show();
				}
			}

			s.focus();

			// bind default events
			s.bindEvents();
		},
		/*
		 * Close the mdbpopup dialog
		 * - Note: If you use an onClose callback, you must remove the
		 *         overlay, container and iframe elements manually
		 *
		 * @param {boolean} external Indicates whether the call to this
		 *     function was internal or external. If it was external, the
		 *     onClose callback will be ignored
		 */
		close: function () {
			var s = this;

			//clear automatic close timer
			clearTimeout(s.o.closeTimer);

			// prevent close when dialog does not exist
			if (!s.d.data) {
				return false;
			}

			// remove the default events
			s.unbindEvents();

			if ($.isFunction(s.o.onClose) && !s.occb) {
				// set the onClose callback flag
				s.occb = true;

				// execute the onClose callback
				s.o.onClose.apply(s, [s.d]);
			}
			else {
				// if the data came from the DOM, put it back
				if (s.d.parentNode) {
					// save changes to the data?
					if (s.o.persist) {
						// insert the (possibly) modified data back into the DOM
						s.d.data.hide().appendTo(s.d.parentNode);
					}
					else {
						// remove the current and insert the original,
						// unmodified data back into the DOM
						s.d.data.hide().remove();
						s.d.orig.appendTo(s.d.parentNode);
					}
				}
				else {
					// otherwise, remove it
					s.d.data.hide().remove();
				}

				// remove the remaining elements
				s.d.borderTable.hide().remove();
				s.d.container.hide().remove();
				s.d.overlay.hide().remove();
				s.d.iframe && s.d.iframe.hide().remove();

				// reset the dialog object
				s.d = {};
			}
		},
		loadExternal: function (data) {
			var s = this;
			s.d.data.html("");
			s.d.wrap.css('background-image','url("'+s.o.loaderPath+'")');
			s.d.data.load(data,null,function (responseText, textStatus, XMLHttpRequest) {
				s.d.container.css('height',0);
				s.d.container.css('width',0);
				s.d.wrap.css('background-image','none');
				//
				s.d.data.show();
				s.d.header.find("b").html(s.o.title);
				s.setContainerDimensions();
				s.setFormsActions();

				clearTimeout(s.o.closeTimer);
				if(s.o.timed)s.o.closeTimer = setTimeout(function () {s.close();}, s.o.closeTime*1000);
			});
		},setFormsActions: function (){
			var s = this;
			s.d.container.find("form").each(function(d,e){
				var beforeFun = null;
				if($(this).attr("onsubmit")!=null && $(this).attr("onsubmit")!=""){
					var stAr = $(this).attr("onsubmit").split(" ");
					for(var i in stAr){
						if (stAr[i] != "return" && stAr[i].indexOf("(") != -1) {
							if($.isFunction(window[stAr[i].substring(0,stAr[i].indexOf("("))])){
								var fun = window[stAr[i].substring(0, stAr[i].indexOf("("))];
								beforeFun = function(beforeFunData, jQueryForm, OptionsObject){
									var result = fun(beforeFunData,jQueryForm,OptionsObject);
									if(result)s.setOverlayClose(false);
									return result;
								}
							}
							$(this).attr("onsubmit","");
						}
					}
				}
				$(this).ajaxForm({
			        // target identifies the element(s) to update with the server response
			        //target: '#htmlExampleTarget',

			        // success identifies the function to invoke when the server response
			        // has been received; here we apply a fade-in effect to the new content
			        success: function (responseText, textStatus, XMLHttpRequest) {
						s.d.data.html(responseText)
						s.d.container.css('height',0);
						s.d.container.css('width',0);
						s.d.wrap.css('background-image','none');
						//
						s.d.data.show();
						s.setContainerDimensions();
						s.setFormsActions();

						clearTimeout(s.o.closeTimer);
						if(s.o.timed)s.o.closeTimer = setTimeout(function () {s.close();}, s.o.closeTime*1000);
					}
					,beforeSubmit:beforeFun
			    });
			});
		},setOverlayClose: function(value){
			var s = this;
			s.o.overlayClose = value;
			if(value){
				s.d.overlay.bind('click.mdbpopup', function (e) {
					e.preventDefault();
					s.close();
				});
			}else{
				s.d.overlay.unbind('click.mdbpopup');
			}
		},setTimeClose: function(value, duration){
			var s = this;
			clearTimeout(s.o.closeTimer);
			if (value) {
				s.o.timed  = value;
				s.o.closeTime = (duration!=null && !isNaN(duration))?duration:s.o.closeTime;
				s.o.closeTimer = setTimeout(function(){
					s.close();
				}, s.o.closeTime * 1000);
			}else clearTimeout(s.o.closeTimer);
		},changePage: function(data, options){
			var s = this;
			clearTimeout(s.o.closeTimer);
			s.d.data.html("");
			s.d.wrap.css('background-image','url("'+s.o.loaderPath+'")');
			s.d.container.css('height',0);
			s.d.container.css('width',0);
			//
			s.d.data.hide();
			s.setContainerDimensions();
			// merge defaults and user options
			s.o = $.extend({}, $.mdbpopup.defaults, options);
			s.loadExternal(data);
		}
	};
})(jQuery);

