/*
 * jQuery Impromptu
 * By: Trent Richardson [http://trentrichardson.com]
 * Version 1.2
 * Last Modified: 11/21/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:'mlb', buttons:{ 'Close':true }, loaded:function(){}, submit:function(){return true;}, callback:function(){}, 
	  /* width: 400, */ container:'body', opacity:0.6, overlayspeed:100, promptspeed:'fast', show:'fadeIn'
	},
	
	SetImpromptuDefaults: function(o){ 
		jQuery.ImpromptuDefaults = jQuery.extend({},jQuery.ImpromptuDefaults,o);
	},
	
	promptClose: function(fn) {
		var jqi  = $('#mlb');
		var jqif = $('#mlbfade');
		jqi.hide();
		jqif.fadeOut(300, function() {
		  jqi.remove();
		  jqif.remove();
		});

	  (fn)();

    return false;
	},
	
	prompt: function(m,o){
		o = jQuery.extend({},jQuery.ImpromptuDefaults,o);
		
		var ie6 = (jQuery.browser.msie && jQuery.browser.version < 7);	
		var b = 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 = '<div src="" class="'+ o.prefix +'fade" id="'+ o.prefix +'fade"></div>';
		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">';
		jQuery.each(o.buttons,function(k,v){ msgbox += '<button name="'+ o.prefix +'button'+ k +'" id="'+ o.prefix +'button'+ k +'" value="'+ v +'" class="'+v+'">'+ k +'</button>'}) ;
		msgbox += '</div></div></div>';
		
		var jqi = b.prepend(msgbox).children('#'+ o.prefix);
		var jqif = b.prepend(fade).children('#'+ o.prefix +'fade');
		
		//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() }); };
    
    var jtop = (window.innerHeight || document.body.clientHeight) + 'px';
    
    if (o.width) {
      jqi.width(o.width);
    }
    
    if (o.height) {
      $("." + o.prefix + "message", jqi).height(o.height);
    }

    function getScrollY() {
      var scrollY = 0;
      if( document.documentElement && document.documentElement.scrollTop ) {
        scrollY = document.documentElement.scrollTop;
      } else if( document.body && document.body.scrollTop ) {
        scrollY = document.body.scrollTop;
      } else if( window.pageYOffset ) {
        scrollY = window.pageYOffset;
      } else if( window.scrollY ) {
        scrollY = window.scrollY;
      } 
      return scrollY;
    };

    var height = b[0].scrollHeight; //b.height();
    
		jqif.css({ position: "absolute", height: height, width: "100%", top: 0, left: 0, right: 0, bottom: 0, zIndex: 5998, display: "none", opacity: o.opacity });

    if (o.height) {
      height = o.height + 150;
    } else {
      height = jqi.height()
    }

		jqi.css({ position: "absolute", 
		  top: "50%", left: "50%", 
		  display: "none", zIndex: 5999, 
		  marginLeft: -(jqi.width()  / 2) + "px",
		  marginTop: getScrollY() - (height / 2) + "px"
		  /*marginTop: window.scrollY - (jqi.height() / 2) + "px" // Not working in IE7 */
		});
		  //marginLeft: ((((jqi.css("paddingLeft").split("px")[0]*1) + jqi.width())/2)*-1) 

		var msg = jqi.children('.'+ o.prefix +'container').children('.'+ o.prefix +'message');
		jQuery('#'+ o.prefix +'buttons').children('button').add(msg.find('button.impromptu')).click(function(){ 
			var data = msg.find('form').formToArray();
			var clicked = o.buttons[jQuery(this).text()] || jQuery(this).attr('value');	
			if(o.submit(clicked,msg,data)){		
				jqi.remove(); 
        // if(ie6)jQuery(window).unbind('scroll',ie6scroll);//ie6, remove the scroll event
				jqif.fadeOut(o.overlayspeed,function(){
					jqif.remove();
					o.callback(clicked,msg,data);
				});
			}
		});
				
		// this is completely weird, but for some reason IE6 wouldn't
		// work in the standard way
		$.each(msg.find('a.impromptu-close'),function(index, node) {
		  node.onclick = function() {
        jqi.remove();
        // if(ie6)jQuery(window).unbind('scroll',ie6scroll);//ie6, remove the scroll event
          jqif.fadeOut(o.overlayspeed, function(){ jqif.remove(); });
        return false;		    
		  }
		});
		
    // msg.find('a.impromptu-close').click(function() {
    //   alert("hello");
    //       // jqi.remove();
    //       // if(ie6)jQuery(window).unbind('scroll',ie6scroll);//ie6, remove the scroll event
    //       //       jqif.fadeOut(o.overlayspeed, function(){ jqif.remove(); });
    //   return false;
    // })
    
    
    
    // msg.find('button.impromptu').click(function(){ 
    //  var data = msg.find('form').formToArray();
    //  var clicked = jQuery(this).attr('value'); // o.buttons[jQuery(this).text()];  
    //  if(o.submit(clicked,msg,data)){   
    //    jqi.remove(); 
    //    if(ie6)jQuery(window).unbind('scroll',ie6scroll);//ie6, remove the scroll event
    //    jqif.fadeOut(o.overlayspeed,function(){
    //      jqif.remove();
    //      o.callback(clicked,msg,data);
    //    });
    //  }
    // });  	
				
    // if(ie6) jQuery(window).scroll(ie6scroll);//ie6, add a scroll event to fix position:fixed
    jqif.fadeIn(o.overlayspeed);
		jqi[o.show](o.promptspeed,o.loaded);
		return jqi;
	}	
});
