
jQuery.extend({	
	ImpromptuDefaults: { prefix:'jqi', buttons:{ Ok:true }, loaded:function(){}, submit:function(){return true;}, callback:function(){}, container:'body', opacity:0.6, overlayspeed:'slow', promptspeed:'fast', show:'show'},
	SetImpromptuDefaults: function(o){ 
		jQuery.ImpromptuDefaults = jQuery.extend({},jQuery.ImpromptuDefaults,o);
	},
	prompt: function(m,o){
		o = jQuery.extend({},jQuery.ImpromptuDefaults,o);
		
		var ie6 = (jQuery.browser.msie && jQuery.browser.version < 7);	
		var b = (ie6)? jQuery(document.body):jQuery(o.container);	
		var fade = '<div class="'+ o.prefix +'fade" id="'+ o.prefix +'fade"></div>';
		if((jQuery.browser.msie && jQuery('object, applet').length > 0) || ie6)
			fade = '<iframe src="" class="'+ o.prefix +'fade" id="'+ o.prefix +'fade"></iframe>';
		var msgbox = '<div class="'+ o.prefix +'" id="'+ o.prefix +'"><div class="'+ o.prefix +'container"><div class="'+ o.prefix +'message">'+ m +'</div><div class="'+ o.prefix +'buttons" id="'+ o.prefix +'buttons">';
		msgbox += '<a id="buttonYes" name="buttonYes" ajaxcall="async" class="jqiButton" href="javascript:;">tak<a/><a class="jqiButton" ajaxcall="async" id="buttonNo" name="buttonNo" href="javascript:;">nie<a/>';
		msgbox += '</div></div></div>';
		
		var jqi = b.prepend(msgbox).children('#'+ o.prefix);
		var jqif = b.prepend(fade).children('#'+ o.prefix +'fade');
		jqi=$('#'+ o.prefix);
		
		//ie6 calculation functions
		var getfoffset = function(){ return (document.documentElement.scrollTop || document.body.scrollTop) + 'px'; };
		var getjoffset = function(){ return (document.documentElement.scrollTop || document.body.scrollTop) + Math.round(15 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + 'px'; };
		var ie6scroll = function(){ jqif.css({ top: getfoffset() }); jqi.css({ top: getjoffset() }); };
		
		//jqif.css({ position: "absolute", height: (ie6)? "100%":b.height(), width: "100%", top: (ie6)? getfoffset():0, left: 0, right: 0, bottom: 0, zIndex: 998, display: "none" });
		jqi.css({ position: (ie6)? "absolute" : "fixed", top: (ie6)? getjoffset():"30%", margin: "auto", display: "none", zIndex: 999 });
			
		jQuery('#'+ o.prefix +'buttons').children('a').click(function()
		{ 
			if(o.submit(jqi,$(this)))
			{		
				jqi.remove(); 
				if(ie6)jQuery(window).unbind('scroll',ie6scroll);//ie6, remove the scroll event
				jqif.hide();
				jqif.remove();
			};
		});
				
		if(ie6) jQuery(window).scroll(ie6scroll);//ie6, add a scroll event to fix position:fixed
		jqif.show();
		jqi.show();
		return jqi;
	}	
});


function initModal()
{
$(".modal").each(function(){bindModal(this);});
}

function oldAlert(e)
{
    if(!e)
	{
	    e=event;
	};
    var source = e.srcElement;    
	if(!source)
	{
	    source = e.target;
    };
	var text = $(source).attr("modaltext");
	if(!text)
	    text = $(source).parent().attr("modaltext");
    if(!window.confirm(text))
    {
        return cancelEvent(e);
    };
}

function bindModal(el)
{
	if (el.onclick) 
	{
		el.onclickOld = el.onclick;
	};	
	if($.browser.safari || ($.browser.msie && $.browser.version<7))
    {
		el.onclick = oldAlert;    
	    return true;
	};
	el.onclick = function(e)
	{
	    if(!e)
	    {
	        e=event;
	    };
		cancelEvent(e);
		var question = $(el).attr("modaltext");
		var jqi = $.prompt(question, {show: "show",submit:function(modal,button)
		{
			if(button.get(0).id=="buttonYes")
			{
				var caller = modal.get(0).callerElement;
				var source = e.srcElement;
		        if(!source)
		        {
			        source = e.target;
		        };
		        source.prevTarget = caller;
				if(caller.onclickOld)
				{
					caller.onclickOld();
					return true;
				};
				if(caller.tagName=="INPUT" && caller.form)
				{
					var hid=$("<input type='hidden'/>").attr("name",caller.name).attr("value",caller.value).appendTo(caller.form);
					if(window.AJAXCbo)
					{
					    window.AJAXCbo.OnFormSubmit(e);
						
						/// ctl00$C$I$msgV$delBtn.x=-1&ctl00$C$I$msgV$delBtn.y=-1
					}
					else
					{
					    caller.form.submit();
					}
					$(hid).remove();
				}
				else if(caller.tagName=="A" && caller.href)
				{
					//$(caller).click();
					window.location = caller.href;
				};
			};
			return true;
		}
		});
		jqi.get(0).callerElement = el;
	};
}

function cancelEvent(e)
{
    if(!e)
    {
        e=event;
    };
	if(e.preventDefault)
		e.preventDefault();
	else
		e.returnValue=false;
	return false;	
}