/*
 * jQuery Impromptu
 * By: Trent Richardson [http://trentrichardson.com]
 * Version 1.1
 * Last Modified: 11/11/2007
 * 
 * Copyright 2007 Trent Richardson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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', msgHeight: 0, zindex: 999},
	SetImpromptuDefaults: function(o){ 
		jQuery.ImpromptuDefaults = jQuery.extend({},jQuery.ImpromptuDefaults,o);
	},
	prompt: function(m,o){
		o = jQuery.extend({},jQuery.ImpromptuDefaults,o);
		
		var hasFade = !o.notFade;
		
		var fadezindex = o.zindex - 1;
		
		var ie6 = (jQuery.browser.msie && jQuery.browser.version < 7);	
		var b = jQuery(o.container);
		var fade = null;
		if((jQuery.browser.msie && jQuery('object, applet').length > 0) || ie6)
			fade = '<iframe src="" class="'+ o.prefix +'fade" id="'+ o.prefix +'fade" style="display:none" frameborder="no"></iframe>';
	    else
	        fade = '<div class="'+ o.prefix +'fade" id="'+ o.prefix +'fade"></div>'; 
		var msgbox = '<div style="display:none" 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">';
		jQuery.each(o.buttons,function(k,v){ msgbox += '<button name="'+ o.prefix +'button'+ k +'" id="'+ o.prefix +'button'+ k +'" value="'+ v +'">'+ k +'</button>'}) ;
		msgbox += '</div></div></div>';
		
		var jqi = b.prepend(msgbox).children('#'+ o.prefix);
		var jqif = b.prepend(fade).children('#'+ o.prefix +'fade');				
		if(ie6) b.css({ overflow: "hidden" });//ie6

		jqif.css({ height: b.height(), width: "100%", position: "absolute", top: 0, left: 0, right: 0, bottom: 0, zIndex: fadezindex, display: "none", opacity: o.opacity });
        jqi.css({ position: (ie6 ? "absolute" : "absolute"), top: "50%", left: "50%", display: "none", zIndex: o.zindex, marginLeft: ((((jqi.css("paddingLeft").split("px")[0]*1) + jqi.width())/2)*-1), marginTop: (((jqi.height())/2)*-1) });
		if (ie6) {
			jqi.css({marginTop: $.getScrollOffset()[1]});
		}
        
        jqi.open = function() {
            jqi.css({ position: (ie6 ? "absolute" : "absolute"), top: "50%", left: "50%", display: "none", zIndex: o.zindex, marginLeft: ((((jqi.css("paddingLeft").split("px")[0]*1) + jqi.width())/2)*-1), marginTop: (((jqi.height())/2)*-1) });
            if (o.msgHeight) {
                var msg = $('.jqimessage', jqi);
                msg.height(o.msgHeight);
                msg.css('overflow-y', 'scroll');
            }
            if (hasFade) {
                jqif.fadeIn(o.overlayspeed);
            }
            jqi[o.show](o.promptspeed,function(){o.loaded(jqi)});
			window.scroll(0,0);
        }
		
		jqi.center = function(div){
		  jqi.css({ position: (ie6 ? "absolute" : "absolute"), top: "50%", left: "50%", display: "none", zIndex: o.zindex, marginLeft: ((((div.css("paddingLeft").split("px")[0]*1) + div.width())/2)*-1), marginTop: (((div.height())/2)*-1) });
		}
		
		jqi.close = function(clicked,msg) {
			//jqi.remove();
			jqi.hide();
			if (hasFade) { 
				jqif.fadeOut(o.overlayspeed,function(){
					//jqif.remove();
					if(ie6) b.css({ overflow: "auto" });//ie6
					o.callback(clicked,msg);
				});
			}
		}
		
		jQuery('#'+ o.prefix +'buttons').children('button').click(function(){ 
			var msg = jqi.children('.'+ o.prefix +'container').children('.'+ o.prefix +'message');
			var clicked = o.buttons[jQuery(this).text()];	
			if(o.submit(clicked,msg)){		
				jqi.close(clicked,msg);
			}
		});
		
		//jqif.fadeIn(o.overlayspeed);
		//jqi[o.show](o.promptspeed,function(){o.loaded(jqi)});
		
		return jqi;
	}	
});
$.getScrollOffset = function(){
  	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [x,y];
}
$.preloadImages = function(preload){
$(document.createElement('img')).bind('load', function(){
if(preload[0]) this.src = preload.shift();
}).trigger('load');
}