/**
 * Popup (modal) plugin written on the jQuery framework.
 * 
 * @author jaredhoyt
 * @copyright Tekniq Data Corporation
 */
var magicbox = {
	// Generic callbacks to be overwritten
	callbacks: {
		onHide: function() {}
	},

	// Magicbox settings
	settings: {
		css: new Object,
		persistent: false,
		showEffects: true,
		title: 'Magicbox Popup'
	},
	
	// Initializing function called when document is ready
	init: function() {
		// Create magicbox html
		$('body').append("<div id='magicBox'><div id='magicHeader'><span id='magicTitle'></span><div id='magicClose'></div></div><div id='magicContent'></div></div><div id='magicLoader'></div><div id='magicBackground' style='filter:alpha(opacity=70);'></div>");
	},
	
	// Center magicbox in the browser window
	center: function(selector) {				
		scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
		scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

		$(selector).css({
			'left': $(window).width()/2 - $(selector).width()/2 + scrollX,
			'top': $(window).height()/2 - $(selector).height()/2 + scrollY
		});
	},
	
	// Configure magicbox settings or return a setting value
	config: function(property, value) {
		if (typeof property == 'undefined') {
			return magicbox.settings;
		}
		if (typeof property == 'string') {
			if (typeof value == 'undefined') {
				return magicbox.settings[property];
			}
			magicbox.settings[property] = value;
		} else if (typeof property == 'object') {
			$.extend(magicbox.settings, property);
		}
	},

	// Hide magicbox
	hide: function(callback) {
		onhide = function(){
			callback = (typeof callback == 'function') ? callback : magicbox.callbacks.onHide;
			
			magicbox.toggleSelects();
			callback();
		};
				
		if ($('#magicBox').is(':visible')) {
			$('#magicLoader').hide();
			if (magicbox.config('showEffects')) {
				$('#magicBackground').fadeOut('fast');
				$('#magicBox').fadeOut('fast', onhide);
			} else {
				$('#magicBox, #magicBackground').hide();
				onhide();
			}
		}		
	},
	
	// Load resource into magicbox with AJAX call
	load: function(resource, options, callback) {
		magicbox.toggleSelects('hide');
		
		// Stretch the background to current dimensions of document
		$('#magicBackground').css({
			'height': $(document).height(),
			'width': $(document).width()
		});
		
		// Show AJAX loader while html is being requested
		magicbox.center('#magicLoader');
		$('#magicLoader, #magicBackground').show();

		if (magicbox.config('persistent') && magicbox.prevResource == resource) {
			magicbox.show();
		} else {
			// Optional parameters
			if (typeof options == 'function') {
				callback = options;
				options = new Object;
			} else {
				options = options || new Object;
				callback = callback || function(){};
			}
			
			// Merge options paramater with default magicbox settings
			$.extend(settings = new Object, magicbox.settings, options);
						
			$('#magicContent').load(resource, settings.data, function(data) {
				$('#magicBox').removeAttr('style').css(settings.css);
				$('#magicTitle').html(settings.title);
				
				if ($('#magicBox img').length) {
					$('#magicBox img').load(function(){
						magicbox.show();
						magicbox.prevResource = resource;
						callback(data);
					});
				} else {
					magicbox.show();
					magicbox.prevResource = resource;
					callback(data);
				}				
			});
		}
		return false;
	},	
	
	// Cross-browser overflow:scroll fixes
	scrollFix: function() {
		// Hide disabled scrollbar in standard browsers
		if ($('#magicBox').height() > 600) {
			$('#magicContent').css('overflow-y', 'scroll');
		} else {
			$('#magicContent').css('overflow-y', 'hidden');
		}
		
		// Adjust width in IE to compensate for vertical scrollbar
		if (!$.support.objectAll) {
			if ($('#magicBox').height() > 600) {
				$('#magicContent').css('padding-right', '32px');
			} else {
				$('#magicContent').css('padding-right', '15px');
			}
		}
	},
	
	// Show magicbox
	show: function() {
		if ($('#magicBox').is(':hidden')) {
			magicbox.scrollFix();
			magicbox.center('#magicBox');
			$('#magicLoader').hide();
			
			if (magicbox.config('showEffects')) {
				$('#magicBox').fadeIn('normal');
			} else {
				$('#magicBox').show();
			}
		}
	},
	
	// * IE 6 hack (remove when ie6 is deprecated)
	// Hide/show select dropdowns since IE6 doesn't properly include them in the z-index
	toggleSelects: function(action) {
		$('select').toggleClass('hidden', action == 'hide');
	}
};

jQuery(function() {
	// Initialize magicbox
	magicbox.init();
				
	// Bind close events
	$('#magicClose, #magicBackground').click(magicbox.hide);
	$().keypress(function(event) { 
		// Bind escape key
		if (event.which == 27 && $('#magicBox').is(':visible')) {
			magicbox.hide();
		}
	});
});
