(function($){
    $.fn.extend({
		JmodalDialogInit:function() {
			if($('#JmodalDialogDiv').length==0) {
        		$('<div id="JmodalDialogDimBackgroundDiv"></div>'+
        		'<div id="JmodalDialogDiv">'+
            		'<table border="0" cellpadding="0" cellspacing="0">'+
            			'<tr>'+
                    		'<td class="JmodalDialogTopLeft"></td>'+
                    		'<td class="JmodalDialogTopCenter"></td>'+
                    		'<td class="JmodalDialogTopRight"></td>'+
                    	'</tr>'+
            			'<tr>'+
            				'<td class="JmodalDialogMiddleLeft"></td>'+
            				'<td bgcolor="#999999" valign="top"><a href="#" id="JmodalDialogClose">Close X</a></td>'+
            				'<td class="JmodalDialogMiddleRight"></td>'+
            			'</tr>'+
                    	'<tr>'+
                    		'<td class="JmodalDialogMiddleLeft"></td>'+
                    		'<td id="JmodalDialogContentDiv" bgcolor="white" valign="top"></td>'+
                    		'<td class="JmodalDialogMiddleRight"></td>'+
                    	'</tr>'+
                    	'<tr>'+
                    		'<td class="JmodalDialogBottomLeft"></td>'+
                    		'<td class="JmodalDialogBottomCenter"></td>'+
                    		'<td class="JmodalDialogBottomRight"></td>'+
                    	'</tr>'+
                    '</table>'+
        		'</div>').appendTo('body');
    			$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height());
            	$('#JmodalDialogClose').click(function(){
            		var transitionType=$('#JmodalDialogContentDiv').data('transitionType');
            		switch(transitionType.toLowerCase()) {
            			case 'fadein' :
            				$('#JmodalDialogDiv').fadeOut('slow', function() {
            					$('#JmodalDialogDimBackgroundDiv').css({'display':'none'});
            				}); break;
            			case 'show' :
            				$('#JmodalDialogDiv').hide(0, function() {
            					$('#JmodalDialogDimBackgroundDiv').css({'display':'none'});
            				}); break;
            			default :
            				$('#JmodalDialogDiv').slideUp('slow', function() {
            					$('#JmodalDialogDimBackgroundDiv').css({'display':'none'});
            				});
            		} return false;
            	});
            }
        },
        JmodalDialog:function(options){        
        	//This method renders HTML inside iframe or HTML via Param
        	return this.click(function(e){
        		e.preventDefault();
        		var nWinHeight=$(window).height()-100;        		
        		var dOptions= {
        			width:755,
        			height:nWinHeight,
        			padding:0,
        			HTML:'',
        			top:'top',
        			transitionType:'slidedown',
        			scrolling:'auto'
        		};
        		if(options) $.extend(dOptions, options);
        		//use default height if new height is bigger, else set to new height
        		//if(dOptions.height > nWinHeight) dOptions.height=nWinHeight;
        		$(this).JmodalDialogInit();        		
               	$('#JmodalDialogContentDiv').css('width', dOptions.width + 'px');
                $('#JmodalDialogContentDiv').css('height', dOptions.height + 'px');
                $('#JmodalDialogContentDiv').css('padding', dOptions.padding + 'px');
                //Store data for later use
                $('#JmodalDialogContentDiv').data('JmodalDialogContentDivHeight', dOptions.height);
                $('#JmodalDialogContentDiv').data('top', dOptions.top);
                $('#JmodalDialogContentDiv').data('height', dOptions.height);
                $('#JmodalDialogContentDiv').data('transitionType', dOptions.transitionType);
                $('#JmodalDialogContentDiv').html(dOptions.HTML ? dOptions.HTML : '<iframe frameborder="0" scrolling="' + dOptions.scrolling + '" src="' + $(this).attr('href') + '" style="width:100%; height:100%; border:0;display:block;marginwidth:0;marginheight:0;"/>');                
                $('#JmodalDialogDiv').JmodalDialogCenter();
        		$('#JmodalDialogDimBackgroundDiv').css({'display':'block'});
        		if($.browser.msie && $.browser.version <="6.0")
        			$('#JmodalDialogDiv').bgiframe();
        		switch(dOptions.transitionType.toLowerCase()) {
        			case 'fadein' :
        				$('#JmodalDialogDiv').fadeIn('slow', function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        				break;
        			case 'show' :
        				$('#JmodalDialogDiv').show(0, function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        				break;
        			default :
        				$('#JmodalDialogDiv').slideDown('slow', function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        		}
        	});
        },
        JmodalDialog1:function(options){        
        	//This method renders HTML via param or through JQuery .data('HTML','YourHTML')
        	return this.click(function(e){
        		e.preventDefault();
        		var nWinHeight=$(window).height()-100;        		
        		var dOptions= {
        			width:755,
        			height:nWinHeight,
        			padding:0,
        			HTML:'',
        			top:'top',
        			transitionType:'slidedown'
        		};
        		if(options) $.extend(dOptions, options);
        		//use default height if new height is bigger, else set to new height
        		//if(dOptions.height > nWinHeight) dOptions.height=nWinHeight;
        		$(this).JmodalDialogInit();        		
               	$('#JmodalDialogContentDiv').css('width', dOptions.width + 'px');
                $('#JmodalDialogContentDiv').css('height', dOptions.height + 'px');
                $('#JmodalDialogContentDiv').css('padding', dOptions.padding + 'px');
                //Store data for later use
                $('#JmodalDialogContentDiv').data('JmodalDialogContentDivHeight', dOptions.height);
                $('#JmodalDialogContentDiv').data('top', dOptions.top);
                $('#JmodalDialogContentDiv').data('height', dOptions.height);
                $('#JmodalDialogContentDiv').data('transitionType', dOptions.transitionType);
                $('#JmodalDialogContentDiv').html(dOptions.HTML ? dOptions.HTML : $(this).data('HTML'));                
                $('#JmodalDialogDiv').JmodalDialogCenter();
        		$('#JmodalDialogDimBackgroundDiv').css({'display':'block'});
        		if($.browser.msie && $.browser.version <="6.0")
        			$('#JmodalDialogDiv').bgiframe();
        		switch(dOptions.transitionType.toLowerCase()) {
        			case 'fadein' :
        				$('#JmodalDialogDiv').fadeIn('slow', function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        				break;
        			case 'show' :
        				$('#JmodalDialogDiv').show(0, function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        				break;
        			default :
        				$('#JmodalDialogDiv').slideDown('slow', function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        		}
        	});
        },
        JmodalDialogAutoHeight:function(options){        
        	//This method renders HTML inside iframe or HTML via Param
        	return this.click(function(e){
        		e.preventDefault();      		
        		var dOptions= {
        			width:755,
        			padding:0,
        			HTML:'',
        			top:'top',
        			transitionType:'slidedown',
        			scrolling:'no'
        		};
        		if(options) $.extend(dOptions, options);
        		$(this).JmodalDialogInit();        		
               	$('#JmodalDialogContentDiv').css('width', dOptions.width + 'px');
                $('#JmodalDialogContentDiv').css('padding', dOptions.padding + 'px');
                //Store data for later use
                $('#JmodalDialogContentDiv').data('top', dOptions.top);
                $('#JmodalDialogContentDiv').data('transitionType', dOptions.transitionType);
                $('#JmodalDialogContentDiv').html(dOptions.HTML ? dOptions.HTML : '<iframe id="Jiframe" width="100%" frameborder="0" scrolling="' + dOptions.scrolling + '" src="' + $(this).attr('href') + '" style="border:0;display:block;marginwidth:0;marginheight:0;"/>');                
                //auto height
                $.fn.JresizeFrame();
         		$("#Jiframe").load($.fn.JresizeFrame);
         		
                $('#JmodalDialogDiv').JmodalDialogCenter();
        		$('#JmodalDialogDimBackgroundDiv').css({'display':'block'});
        		if($.browser.msie && $.browser.version <="6.0")
        			$('#JmodalDialogDiv').bgiframe();
        		switch(dOptions.transitionType.toLowerCase()) {
        			case 'fadein' :
        				$('#JmodalDialogDiv').fadeIn('slow', function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        				break;
        			case 'show' :
        				$('#JmodalDialogDiv').show(0, function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        				break;
        			default :
        				$('#JmodalDialogDiv').slideDown('slow', function() {
        					$('#JmodalDialogDimBackgroundDiv').css('height', $(document).height() + 'px');
        				});
        		}
        	});
        },
        JresizeFrame:function(){
        	$("#Jiframe", top.document).css({ height: 0 });
            var contentHeight = $("#Jiframe", top.document).contents().find('body').attr('scrollHeight');
            $("#Jiframe", top.document).css({ height: contentHeight});
        },
        JmodalDialogCenter:function () {
        	/*backup
            return this.each(function() {
            	var _top=$('#JmodalDialogContentDiv').data('top');
            	if(_top=='top') {_top=-20; }
            	else {_top=($(window).height() - $(this).outerHeight()) / 2; }
                var left = ($(window).width() - $(this).outerWidth()) / 2;
                $(this).css({position:'absolute', margin:'0px', top:(_top + 'px'), left: (left > 0 ? left : 0)+'px'});
            });
            */
        	if($('#JmodalDialogContentDiv').data('top')=='top') {
        		var left = ($(window).width() - $(this).outerWidth()) / 2;
            	$(this).css({position:'absolute', margin:'0px', top:(-20 + 'px'), left: (left > 0 ? left : 0)+'px'});
        	}
        	else {
        	    if($.browser.msie && $.browser.version <="6.0")
        		    $(this).css({position:'absolute'});
        		else 
        		    $(this).css({position:'fixed'});
        		$(this).css('left',($(window).width()-$(this).outerWidth())/ 2 + 'px');
				$(this).css('top',($(window).height()-$(this).outerHeight())/ 2 + 'px');
        	}   return this;
        }
	});
})(jQuery); 
$(window).bind('resize', function() {
	if($('#JmodalDialogDimBackgroundDiv').length > 0) $('#JmodalDialogDimBackgroundDiv').css('height', $(window).height() + 'px');
	if($('#JmodalDialogDiv').length > 0) $('#JmodalDialogDiv').JmodalDialogCenter();
	if($('#JmodalDialogContentDiv').length==1) {
		//original height
		//var nJmodalDialogContentDivHeight=$('#JmodalDialogContentDiv').data('JmodalDialogContentDivHeight');
		//window new height
		//var nWinNewHeight=$(window).height()-100;
		//var nNewHeight=nWinNewHeight;
		//set modal dialog back to its orginal height if it's less than window new height
		//if(nJmodalDialogContentDivHeight <= nWinNewHeight)
		//	nNewHeight=nJmodalDialogContentDivHeight;
		//$('#JmodalDialogContentDiv').css('height', nNewHeight + 'px');
		$('#JmodalDialogDimBackgroundDiv').css('height', ($(document).height()) + 'px');
	}
});
