/**
 * infoWindow plugin for jQuery
 * @copyright 2009 info.nl
 * @author Arno Wolkers
 * @version 1.0
 * 
 * ----------------------------------------
 * HOW TO USE
 * ----------------------------------------
 * $('div').infoToolTip();
 * 
 *
 * ----------------------------------------
 * OPTIONS
 * ----------------------------------------
 * See at the bottom of this page for public settings
 * Use the following to set the default value for all the sliders at once:
 * $.fn.infoWindow.defaults.className = 'foo';
 *  
 * 
 */
(function($){
	$.infoWindow = {
		containerPadding:	10,
		repositionOnResize:	true,		// re-centers the dialog on window resize
		overlayOpacity:		.5,			// transparency level of overlay
		overlayClickClose:	true,		// close the infoWindow when the overlay is clicked,
		closeClass:			'close',
		loadingClass:		'loading',
		overlayFadeSpeed: 400,
		htmlStart:			'',
		htmlEnd:			'',
		
		
		_show: function(source, options, callback) {
			var defaults = {
				contentClass:	null,
				type: 'html',
				ajaxPostType:	'get',
				ajaxData: null,
				overlayClickClose: null
			}
			var s = $.extend({}, defaults, options);
			
			if( s.overlayClickClose === null )
				s.overlayClickClose = $.infoWindow.overlayClickClose;
				
			$.infoWindow._hide();
			$.infoWindow._overlay('show', s.overlayFadeSpeed, s.overlayClickClose);
			$.infoWindow._maintainPosition(true);
			
			$('body').append('<div id="infowindow_container"><div id="infowindow_content"></div></div>');
			if( s.contentClass ) $("#infowindow_content").addClass(s.contentClass);

			$('#infowindow_container').css({
				position: 'absolute',
				zIndex: 1000,
				padding: $.infoWindow.containerPadding
			});
			
			
			switch(s.type) {
				case 'ajax':
					$('#infowindow_content').html($.infoWindow.htmlStart + '<div class="' + $.infoWindow.loadingClass + '"></div>' + $.infoWindow.htmlEnd);
					$.infoWindow._reposition();
					
					$.ajax({
						type: s.ajaxPostType,
						url: source,
						cache: false,
						data: s.ajaxData,

						success: function(html){
							$('#infowindow_content')
								.find('.'+$.infoWindow.loadingClass)
								.replaceWith(html);
								$.infoWindow._closeBtns();
								$.infoWindow._reposition();
								$.infoWindow._callback(callback);
						}
					});
					break;
				
				default:
					$('#infowindow_content').html($.infoWindow.htmlStart + source + $.infoWindow.htmlEnd);
					$.infoWindow._closeBtns();
					$.infoWindow._reposition();
					$.infoWindow._callback(callback);	
					break;
			}
		}, // END _show()
		
		_callback: function(callback) {
			if(callback) {
				callback.call($('#infowindow_content'));
			}
		},
		
		_closeBtns: function() {
			$('#infowindow_container .'+$.infoWindow.closeClass).bind('click', function(e){
				e.preventDefault();
				$.infoWindow._hide();
			});
		},
		
		_hide: function() {
			$('#infowindow_container').remove();
			$.infoWindow._overlay('hide');
			$.infoWindow._maintainPosition(false);
		},
		
		
		_overlay: function(status, fadeSpeed, clickClose) {
			switch( status ) {
				case 'show':
					$.infoWindow._overlay('hide');
					$('body').append('<div id="infowindow_overlay"></div>');
					$('#infowindow_overlay').css({
						position: 'absolute',
						zIndex: 999,
						top: '0',
						left: '0',
						width: '100%',
						height: $(document).height(),
						opacity: 0
					})
					.fadeTo(fadeSpeed || $.infoWindow.overlayFadeSpeed, $.infoWindow.overlayOpacity)
					.bind('click.infoWindow', function(){
						if( clickClose ){
							$.infoWindow._hide();
						}
					});
				break;
				case 'hide':
					$('#infowindow_overlay').fadeOut(fadeSpeed || $.infoWindow.overlayFadeSpeed, function(){
						$(this).remove();
					});
				break;
			}
		},
		
		
		_reposition: function() {	
			var new_top = $(window).scrollTop() + (($(window).height() / 2) - ($('#infowindow_container').outerHeight() / 2));
			var new_left = (($(window).width() / 2) - ($('#infowindow_container').outerWidth() / 2));
			if( new_top < 0 ) new_top = 0;
			if( new_left < 0 ) new_left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) new_top = new_top + $(window).scrollTop();
			
			$('#infowindow_container').css({
				top: new_top + 'px',
				left: new_left + 'px'
			});
			$('#infowindow_overlay').height(0).height( $(document).height() );
		},
		
		
		_maintainPosition: function(status) {
			if( $.infoWindow.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', $.infoWindow._reposition);
					break;
					case false:
						$(window).unbind('resize', $.infoWindow._reposition);
					break;
				}
			}
		}
		
		
		
	};
	// Shortuct functions
	infoWindow = function(source, opts, callback) {
		$.infoWindow._show(source, opts, callback);
	}
})(jQuery);