/* Released: 2010-03-11 16:50:57 */


/**
 * 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 = {
		
		defaults : {
			containerPadding:	0,
			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',
			htmlStart:			'',
			htmlEnd:			'',
			contentClass:		null,
			type:				'html',
			ajaxPostType:		'get',
			ajaxData:			null
		},
		
		settings : null,
		
				
		_show: function(source, options, callback) {
			var me = this;
			this.settings = $.extend({}, this.defaults, options);
			
			$.infoWindow._hide();
			$.infoWindow._overlay('show');
			$.infoWindow._maintainPosition(true);	
			
			
			
			$('body').append('<div id="infowindow_container"><div id="infowindow_content"></div></div>');
			
			if( me.settings.contentClass ) $("#infowindow_content").addClass(me.settings.contentClass);

			$('#infowindow_container').css({
				position: 'absolute',
				zIndex: 1000,
				padding: me.settings.containerPadding
			});
			
			
			switch(me.settings.type) {
				case 'html':
					$('#infowindow_content').html(me.settings.htmlStart + source + me.settings.htmlEnd);
					$.infoWindow._closeBtns();
					$.infoWindow._reposition();
					$.infoWindow._callback(callback);
					
					break;
				case 'ajax':
					$('#infowindow_content').html(me.settings.htmlStart + '<div class="' + me.settings.loadingClass + '"></div>' + me.settings.htmlEnd);
					$.infoWindow._reposition();
					
					$.ajax({
						type: me.settings.ajaxPostType,
						url: source,
						cache: false,
						data: me.settings.ajaxData,

						success: function(html){
							$('#infowindow_content')
								.find('.'+me.settings.loadingClass)
								.replaceWith(html);
								$.infoWindow._closeBtns();
								$.infoWindow._reposition();
								$.infoWindow._callback(callback);
						}
					});
				break;
			}

		},
		
		_callback: function(callback) {
			if(callback) {
				callback.call($('#infowindow_content'));
			}
		},
		
		_closeBtns: function() {
			$('#infowindow_container .'+this.settings.closeClass).bind('click', function(e){
				e.preventDefault();
				$.infoWindow._hide();
			});
		},
		
		_hide: function() {
			$('#infowindow_container').remove();
			$.infoWindow._overlay('hide');
			$.infoWindow._maintainPosition(false);
		},
		
		
		_overlay: function(status) {
			var me = this;
			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: me.settings.overlayOpacity
					}).bind('click', function(){
						if( me.settings.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( this.settings.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);
