


function ShowDialogWindow(settings) {
		
    settings = jQuery.extend({
      
      callback:                     function(){},
      callin:                       function(){},
		  width:                        800,
	    height:                       500,
	    query:                        '',
	    draggable:                    false,
      sizeable:                     false,
      askOnCancel:                  false, 
      src:                          '',
      shadowOffset:                 8,
      containerMargin:              8,
      windowID:                     'dlg_'+Math.round(Math.random()*1000),
      overlayOpacity:               0.4, 
      dialogClass:                  'dialogWindow',
      overlayClass:                 'dialogOverlay',
      iframeClass:                  'dialogIframe', 
      windowClass:                  'dialogContainer',
      loaderClass:                  'dialogLoader',
      headerClass:                  'dialogHeader',
      closeButtonClass:             'dialogButtonClose', 
      contentClass:                 'dialogContent',
      shadowClass:                  'dialogShadow',
      toolbarClass:                 'dialogToolbar',
      loaderClass:                  'dialogLoader',
      changesWillNotBeSavedLabel:   'Změny nebudou uloženy, chcete opravdu okno zavřít?',
      closeDialogLabel:             'Zavřít okno',
      submitLabel:                  'OK',
      cancelLabel:                  'Storno',
      title:                        'Dialog',
      buttonCancel:                 true,
      buttonSubmit:                 true,
      windowWidth:                  0,
      windowHeight:                 0	
      
		},settings);
		
		_initialize();
		
		
    var jQueryMatchedObj = this; 
		
    function _initialize() {
			_start(this,jQueryMatchedObj); 
			return false;
		}
	
    function _start(objClicked,jQueryMatchedObj) {
			
      //settings.src=objClicked.getAttribute('href');
      //settings.title=objClicked.getAttribute('title') || settings.title;
      _set_interface();
		}
		
    function _set_interface() {
			
			$("body").append('<div class="'+settings.dialogClass+'" id="'+settings.windowID+'_dialog"></div>');
			html='\n<div id="'+settings.windowID+'_overlay" class="'+settings.overlayClass+'">\n</div>\n<iframe id="'+settings.windowID+'_iframe" class="'+settings.iframeClass+'" frameborder="0"></iframe>\n<div class="'+settings.loaderClass+'" id="'+settings.windowID+'_loader"></div><div id="'+settings.windowID+'_shadow" class="'+settings.shadowClass+'"></div><div id="'+settings.windowID+'_window" class="'+settings.windowClass+'">\n<div id="'+settings.windowID+'_header" class="'+settings.headerClass+'">\n<h1>'+settings.title+'</h1>\n<a id="'+settings.windowID+'_closeButton" href="javascript: void(0);" class="'+settings.closeButtonClass+'" title="'+settings.closeDialogLabel+'">X</a>\n</div>\n' + (settings.src ? '<iframe class="'+settings.contentClass+'" id="'+settings.windowID+'_container" frameborder="0" src="javascript: false;"></iframe>' : '<div class="'+settings.contentClass+'" id="'+settings.windowID+'_container"></div>') + '\n';
      html+='<div id="'+settings.windowID+'_toolbar" class="'+settings.toolbarClass+'">';
      if(settings.buttonCancel) {html+='<a id="'+settings.windowID+'_cancelButton" href="javascript:void(0);" title="">'+settings.cancelLabel+'</a>';}
      if(settings.buttonSubmit) {html+='<a id="'+settings.windowID+'_submitButton" href="javascript:void(0);" title="">'+settings.submitLabel+'</a>';}
      html+='</div>\n</div>';
      $('#' + settings.windowID + '_dialog').hide().append(html);
      
      var mL = ($(window).width()/2) - (settings.width/2);
      var mT = ($(window).height()/2) - (settings.height/2);
      
      $('#'+settings.windowID+'_dialog').css({width: $(window).width()+'px', height: $(window).height()+'px'});
      $('#'+settings.windowID+'_overlay').css({opacity: settings.overlayOpacity}).maxZIndex({inc: 1});
      $('#'+settings.windowID+'_iframe').css({width: settings.width+'px', height: settings.height+'px'}).maxZIndex({inc: 1}).hide();
      $('#'+settings.windowID+'_loader').css({width: settings.width+'px', height: settings.height+'px'}).maxZIndex({inc: 1});
      //$('#'+settings.windowID+'_shadow').css({opacity: settings.overlayOpacity, width: settings.width+'px', height: settings.height+'px'}).maxZIndex({inc: 1}).hide();
      $('#'+settings.windowID+'_window').css({width: settings.width+'px', height: settings.height+'px'}).maxZIndex({inc: 1});
      $('#'+settings.windowID+'_window').hide();
      
      settings.windowObject = $('#'+settings.windowID+'_container');
      
      $('#'+settings.windowID+'_submitButton').unbind("click").click(function() 
            {
              _submit();
            });
      $('#'+settings.windowID+'_overlay, #'+settings.windowID+'_closeButton, #'+settings.windowID+'_cancelButton').click(function()
        {
          if(settings.askOnCancel)
            {
              try
                {
                  jConfirm(settings.changesWillNotBeSavedLabel, setting.closeDialogLabel, function(e)
                    {
                      if(e)
                        {_remove();}
                    });
                }
              catch(e)
                {
                  if(confirm(settings.changesWillNotBeSavedLabel))
                    {_remove();}
                }
            }
          else
            {
              _remove();
            }
        }
       ); 
      
      
      
      //settings.windowWidth = $("body").width();
      //settings.windowHeight = $("body").height();
      
      /*$('body').css({
					 overflow: 'hidden'
				    });
      */
      _set_position();
      
			
      $(window).resize(function() 
        {
          _set_position();
        });
      
      
      
      
      _show();
      
    }
		
		function _set_position()
		  {
        var arrPageSizes = ___getPageSize();
        var arrPageScroll = ___getPageScroll();
        
        //alert($("body").width());
        
        
				 // $('body').css({
					 //width:		arrPageSizes[0]
					 //height:		arrPageSizes[1]
					//  });
          $('#'+settings.windowID+'_overlay').css({
					 width:		arrPageSizes[0],
					 height:		arrPageSizes[1]
				    });
				
          
				  
          $('#'+settings.windowID+'_window, #'+settings.windowID+'_iframe').css({
					 top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				   left:	arrPageSizes[0]/2 - settings.width/2
          });
				  
				  
				  
				  $('#'+settings.windowID+'_loader').css({
					 top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					 left:	arrPageSizes[0]/2 - settings.width/2//arrPageScroll[0]
				  });
				  
				  $('#'+settings.windowID+'_shadow').css({
					 top:	arrPageScroll[1] + (arrPageSizes[3] / 10) + settings.shadowOffset,
					 left:	(arrPageSizes[0]/2 - settings.width/2) + settings.shadowOffset
				  });
				
      }
		
		function _submit()
		  {
        settings.callback(settings.windowObject);
        _remove();
      }
		
		function _show()  
      {
        $('#'+settings.windowID+'_dialog').show();
        if(settings.src)
          {
            $('#'+settings.windowID+'_container').attr("src", settings.src);
            $('#'+settings.windowID+'_container').load(function(){
              settings.callin(settings.windowObject);
              $('#'+settings.windowID+'_window').show();//.shadow({ color: "#444", offset: 5, opacity: 0.2 });
              $('#'+settings.windowID+'_container').css({
                width: $('#'+settings.windowID+'_window').width()-(settings.containerMargin*2)+'px', 
                height: $('#'+settings.windowID+'_window').height() - $('#'+settings.windowID+'_toolbar').height()-$('#'+settings.windowID+'_header').height()-(settings.containerMargin*2)+'px'
                }).maxZIndex({inc: 1});
      
              $('#'+settings.windowID+'_iframe').show();
              $('#'+settings.windowID+'_shadow').show();
        });
        }
      else
        {
          $('#'+settings.windowID+'_window').show();//.shadow({ color: "#444", offset: 5, opacity: 0.2 });
          $('#'+settings.windowID+'_container').css({
            width: $('#'+settings.windowID+'_window').width()-(settings.containerMargin*2)+'px', 
            height: $('#'+settings.windowID+'_window').height() - $('#'+settings.windowID+'_toolbar').height()-$('#'+settings.windowID+'_header').height()-(settings.containerMargin*2)+'px'
            }).maxZIndex({inc: 1});
      
          $('#'+settings.windowID+'_iframe').show();
          $('#'+settings.windowID+'_shadow').show();
          settings.callin(settings.windowObject);
        }
        
      }
		
		function _remove() {
			
      /*var arrPageSizes = ___getPageSize();
				  $('body').css({
					 width:		arrPageSizes[0],
					 height:		'100%',
					 overflow: 'visible'
					  });
			*/		  
      //$('body').css({
			//		 width:		settings.windowWidth,
					 //height:		settings.windowHeight
					 //overflow: 'visible'
			//	    });
      $('#'+settings.windowID+'_dialog').remove();
			
      
      //$('#'+settings.windowID+'_window').show();
      //$('#'+settings.windowID+'_shadow').show();
      //$('#'+settings.windowID+'_shadow').fadeOut(function(){$('#'+settings.windowID+'_window').fadeOut();});
      //$('#'+settings.windowID+'_overlay').fadeOut(function() { $('#'+settings.windowID+'_dialog').remove(); });
    }
		
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		
		//return this.unbind('click').click(_initialize);
	};


function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft,curtop];
  }
}


//$(function() {
//        $('.dialog').dialogWindow();
//    });

