/**
 * infoWindow plugin for jQuery
 * @copyright 2009 info.nl
 * @author Arno Wolkers
 * @version 1.0
 * 
 * ----------------------------------------
 * HOW TO USE
 * ----------------------------------------
 * 
 *
 * ----------------------------------------
 * 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:		.6,			// transparency level of overlay
		overlayClickClose:	true,		// close the infoWindow when the overlay is clicked,
		closeClass:			'close',
		loadingClass:		'loading',
		htmlStart:			'',
		htmlEnd:			'',
		
				
		_show: function(source, options, callback) {
			var defaults = {
				contentClass:	null,
				type:			'html',
				ajaxPostType:	'get',
				ajaxData:		null
			}
			var settings = $.extend({}, defaults, options);
			
			$.infoWindow._hide();
			$.infoWindow._overlay('show');
			$.infoWindow._maintainPosition(true);	
			
			
			
			$('body').append('<div id="infowindow_container"><div id="infowindow_content"></div></div>');
			
			if( settings.contentClass ) $("#infowindow_content").addClass(settings.contentClass);

			$('#infowindow_container').css({
				position: 'absolute',
				zIndex: 1000,
				padding: $.infoWindow.containerPadding
			});
			
			
			switch(settings.type) {
				case 'html':
					$('#infowindow_content').html($.infoWindow.htmlStart + source + $.infoWindow.htmlEnd);
					$.infoWindow._closeBtns();
					$.infoWindow._reposition();
					$.infoWindow._callback(callback);
					
					break;
				case 'ajax':
					$('#infowindow_content').html($.infoWindow.htmlStart + '<div class="' + $.infoWindow.loadingClass + '"></div>' + $.infoWindow.htmlEnd);
					$.infoWindow._reposition();
					
					$.ajax({
						type: settings.ajaxPostType,
						url: source,
						cache: false,
						data: settings.ajaxData,

						success: function(html){
							$('#infowindow_content')
								.find('.'+$.infoWindow.loadingClass)
								.replaceWith(html);
								$.infoWindow._closeBtns();
								$.infoWindow._reposition();
								$.infoWindow._callback(callback);
						}
					});
				break;
			}

		},
		
		_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) {
			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: $.infoWindow.overlayOpacity
					}).bind('click', function(){
						if( $.infoWindow.overlayClickClose ) {
							$.infoWindow._hide();
						}
					});
				break;
				case 'hide':
					$('#infowindow_overlay').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);
