/* SVN FILE: $Id: jquery.mi.ajax.js 1837 2009-11-10 17:32:26Z AD7six $ */

/*!
 * mi_ajax.js Load (specified) links via ajax
 *
 * @package       base
 * @copyright     Copyright (c) 2009, Andy Dawson
 * @link          www.ad7six.com
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

$(function() {

/**
 *
 * Add the following:
 * 	miAjax.setup(this, {your override options here});
 *
 */
	miAjax = {

/**
 * miAjax.defaults
 *
 * Default options, used if you don't specify anything when calling setup
 *
 * selector:       what to look for
 * target:         the target div for updates - leave as null for automatic detection
 * layout:         if specified this will be appended as a get var to the url for each request e.g
 *             		.load('/this/page/please?layout=funky', ...
 * useAnchors:     whether to check for an anchor on first load, and also whether to update the url
 *             		when a link is clicked. false, absolute or relative
 * useBlockUI:     if available, will use the blockUI plugin as an indicator
 * showGrowl:      useful for debugging, shows what's being loaded and when
 * blockUIOptions: passed to blockUI directly
 */
		defaults: {
			selector: '.paging a:not([href^=#]), th a[href*=/page:]',
			target: null,
			layout: null,
			useAnchors: 'relative',
			useBlockUI: true,
			showGrowl: false,
			blockUIOptions: {
				message: '<h2>' + __('Loading...') + '</h2>',
				applyPlatformOpacityRules: false,
				timeout: 10000
			}
		},

/**
 * MiAjax.options
 *
 * Runtime options
 *
 * @see defaults
 */
		options: {},

/**
 * miAjax.setup function
 *
 * Process the options and find all matching links inside the scope (if specified, otherwise
 * page-wide). If a target is not specified in the options, the first .container found above the
 * link it what is used as the target for the ajax content.
 *
 * On first page load (if there's no scope assume first call), check if there's an anchor and if so
 * load it as if a link had been clicked
 *
 */
		setup: function(scope, options) {
			miAjax.options = $.extend(miAjax.defaults, options || {});
			$(miAjax.options.selector, scope).live('click', function() {
				var _this = $(this);
				var target = $(miAjax.options.target || _this.parents('.container'));
				miAjax.load(target, _this.attr('href'));
				return false;
			});
			if (miAjax.options.useAnchors) {
				var anchor = document.location.hash.replace('#', '');
				if (anchor.match('/\//')) {
					if (anchor.substr(0, 1) != '/') {
						anchor = document.location.pathname.replace(/\/+$/, '/') + anchor;
					}
					miAjax.load($('#content .container'), anchor);
				}
			}
		},

/**
 * miAjax.load function
 *
 * load the passed href (after minor tweaking) into the target (div). If no target is specified in
 * the call miAjax.options.target is used if defined, else the first .container above the link is
 * used as the target. This means you can have more than one set of ajax-loading content on the
 * same page and they won't interfere with each other.
 * If using blockUI - call before and unblock after loading has completed
 * Update the url anchor if specified
 */
		load: function (target, href) {
			var target = target || $(miAjax.options.target || _this.parents('.container'));
			var layoutSuffix = miAjax.options.layout? '?layout=' + miAjax.options.layout : '';
			var url = href + '.ajax' + layoutSuffix;
			if (miAjax.options.useBlockUI) {
				try{
					if (miAjax.options.showGrowl) {
						$.growlUI(sprintf(__('Loading %s'), href));
					}
					target.block(miAjax.options.blockUIOptions);
				} catch(e) {try{console.log('Warning: BlockUI failed or isn\'t available.')} catch(e) {}}
			}
			target.load(url,function(response) {
				try{
					target.unblock();
				} catch(e) {}
			});
			if (miAjax.options.useAnchors) {
				if (miAjax.options.useAnchors == 'relative') {
					href = href.replace(document.location.pathname.replace('/\/$', '') + '/', '');
				}
				window.location = '#' + href;
			}
		}
	};
});
