(function($){

	/**
	 * Helper class for rendering layout changes via AJAX.
	 *
	 * @class BACheetahAJAXLayout

	 */
	BACheetahAJAXLayout = function( data, callback )
	{

		this._data      = $.extend( {}, this._defaults, typeof data == 'string' ? BACheetah._jsonParse( data ) : data );
		this._callback				= callback;
		this._post    				= BACheetahConfig.postId;
		this._head    				= $('head').eq(0);
		this._body    				= $('body').eq(0);

		// Setup the new CSS vars if we have new CSS.
		if ( this._data.css ) {
			if ( 'inline' === BACheetahConfig.enqueueMethod ) {
				this._oldCss  = $('#ba-cheetah-layout-' + this._post + '-inline-css');
				this._newCss  = $('<style id="ba-cheetah-layout-' + this._post + '-inline-css" type="text/css">'+ this._data.css +'</style>');
			} else {
				this._loader  = $('<img src="' + this._data.css + '" />');
				this._oldCss  = $('link[href*="/cache/' + this._post + '-layout"]');
				this._newCss  = $('<link rel="stylesheet" id="ba-cheetah-layout-' + this._post + '-css"  href="'+ this._data.css +'" />');
			}
		}

		// Setup partial or full JS refresh vars.
		if ( this._data.partial ) {
			if ( this._data.js ) {
				this._oldJs = $('#ba-cheetah-partial-refresh-js');
				this._newJs = $('<script type="text/javascript" id="ba-cheetah-partial-refresh-js">'+ this._data.js +'</script>');
			}
			if ( this._data.nodeId ) {
				if ( this._data.oldNodeId ) {
					this._oldScriptsStyles 	= $( '.ba-cheetah-node-scripts-styles[data-node="' + this._data.oldNodeId + '"]' );
					this._content 			= $( '.ba-cheetah-node-' + this._data.oldNodeId );
				} else {
					this._oldScriptsStyles 	= $( '.ba-cheetah-node-scripts-styles[data-node="' + this._data.nodeId + '"]' );
					this._content 			= $( '.ba-cheetah-node-' + this._data.nodeId ).eq(0);
				}
			}
		} else {
			if ( 'inline' === BACheetahConfig.enqueueMethod ) {
				this._oldJs = $('#ba-cheetah-layout-' + this._post + '-inline-js');
				this._newJs = $('<script id="ba-cheetah-layout-' + this._post + '-inline-js">'+ this._data.js +'</script>');
			} else {
				this._oldJs = $('script[src*="/cache/' + this._post + '"]');
				this._newJs = $('<script src="'+ this._data.js +'"></script>');
			}
			this._oldScriptsStyles 	= $( '.ba-cheetah-layout-scripts-styles' );
			this._content 			= $( BACheetah._contentClass );
		}

		this._init();
	};

	/**
	 * Prototype for new instances.
	 *

	 * @property {Object} prototype
	 */
	BACheetahAJAXLayout.prototype = {

		/**
		 * Defaults for the data sent from the server.
		 *

		 * @access private
		 * @property {Object} _defaults
		 */
		_defaults 			: {
			partial 		: false,
			nodeId 			: null,
			nodeType 		: null,
			nodeParent 		: null,
			nodePosition 	: null,
			oldNodeId 		: null,
			html 			: null,
			scriptsStyles 	: null,
			css 			: null,
			js 				: null
		},

		/**
		 * Data from the server for this render.
		 *

		 * @access private
		 * @property {Object} _data
		 */
		_data 				: null,

		/**
		 * A function to call when the render is complete.
		 *

		 * @access private
		 * @property {Function} _callback
		 */
		_callback			: function(){},

		/**
		 * The ID of this post.
		 *

		 * @access private
		 * @property {Number} _post
		 */
		_post    			: null,

		/**
		 * A jQuery reference to the head element.
		 *

		 * @access private
		 * @property {Object} _head
		 */
		_head    			: null,

		/**
		 * A jQuery reference to the body element.
		 *

		 * @access private
		 * @property {Object} _body
		 */
		_body    			: null,

		/**
		 * An jQuery reference to an image element that is used
		 * to preload the new CSS file using the onerror hack.
		 *

		 * @access private
		 * @property {Object} _loader
		 */
		_loader  			: null,

		/**
		 * An jQuery reference to the old CSS element.
		 *

		 * @access private
		 * @property {Object} _oldCss
		 */
		_oldCss				: null,

		/**
		 * An jQuery reference to the new CSS element.
		 *

		 * @access private
		 * @property {Object} _newCss
		 */
		_newCss				: null,

		/**
		 * An jQuery reference to the old JS element.
		 *

		 * @access private
		 * @property {Object} _oldJs
		 */
		_oldJs				: null,

		/**
		 * An jQuery reference to the new JS element.
		 *

		 * @access private
		 * @property {Object} _newJs
		 */
		_newJs   			: null,

		/**
		 * An jQuery reference to the old div that holds scripts
		 * and styles generated by widgets and shortcodes.
		 *

		 * @access private
		 * @property {Object} _oldScriptsStyles
		 */
		_oldScriptsStyles 	: null,

		/**
		 * An jQuery reference to the content element.
		 *

		 * @access private
		 * @property {Object} _content
		 */
		_content 			: null,

		/**
		 * Starts the render by loading the new CSS file.
		 *

		 * @access private
		 * @method _init
		 */
		_init: function()
		{
			// Set the body height so the page doesn't scroll.
			this._body.height( this._body.height() );

			// Load the new CSS.
			if ( this._data.css )  {
				if ( 'inline' === BACheetahConfig.enqueueMethod ) {
					this._addNewCSS();
					this._finish();
				} else {
					// Load CSS stylesheet using modern methods or fallback to the old way.
					if ( 'onload' in document.createElement( 'link' ) ) {
						this._newCss.on( 'load', $.proxy( this._finish, this ) );
						this._addNewCSS();
					} else {
						this._loader.on( 'error', $.proxy( this._loadNewCSSFallbackComplete, this ) );
						this._body.append( this._loader );
					}
				}
			} else {
				// We don't have new CSS, finish the render.
				this._finish();
			}
		},

		/**
		 * Removes the fallback loader, adds the new CSS once it has loaded,
		 * and sets a quick timeout to finish the render.
		 *

		 * @access private
		 * @method _loadNewCSSFallbackComplete
		 */
		_loadNewCSSFallbackComplete: function()
		{
			// Remove the loader.
			this._loader.remove();

			// Add the new layout css.
			this._addNewCSS();

			// Set a quick timeout to ensure the css has taken effect.
			setTimeout( $.proxy( this._finish, this ), 250 );
		},

		/**
		 * Adds the new CSS once it has been loaded.
		 *

		 * @access private
		 * @method _addNewCSS
		 */
		_addNewCSS: function()
		{
			if ( this._oldCss.length > 0 ) {
				this._oldCss.after( this._newCss );
			} else {
				this._head.append( this._newCss );
			}
		},

		/**
		 * Finishes the render after the CSS has been loaded.
		 *

		 * @access private
		 * @method _finish
		 */
		_finish: function()
		{
			// Remove the old content and assets.
			this._removeOldContentAndAssets();

			// Clean the new HTML.
			this._cleanNewHTML();

			// Clean up the new JS and CSS assets.
			this._cleanNewAssets();

			// Add the new HTML.
			this._addNewHTML();

			// Add widget/shortcode JS and CSS assets.
			this._addNewScriptsStyles();

			// Add the new layout JS.
			this._addNewJS();

			// Send the layout rendered event.
			$( BACheetah._contentClass ).trigger( 'ba-cheetah.layout-rendered' );

			// Hide the loader.
			BACheetah.hideAjaxLoader();
		},

		/**
		 * Removes old content and assets from the page.
		 *

		 * @access private
		 * @method _removeOldContentAndAssets
		 */
		_removeOldContentAndAssets: function()
		{
			if ( this._content ) {
				this._content.empty();
			}
			if ( this._oldCss ) {
				this._oldCss.remove();
			}
			if ( this._oldJs ) {
				this._oldJs.remove();
			}
			if ( this._oldScriptsStyles ) {
				this._oldScriptsStyles.remove();
			}
		},

		/**
		 * Removes scripts and styles from _data.html that have been added by
		 * widgets and shortcodes and adds them to _data.scriptsStyles.
		 *

		 * @access private
		 * @method _cleanNewHTML
		 */
		_cleanNewHTML: function()
		{
			// Only proceed if _data.scriptsStyles is set.
			if ( ! this._data.scriptsStyles ) {
				return;
			}

			// Setup vars.
			var html 	 		= $( '<div>' + this._data.html + '</div>' ),
				nodeClass 		= 'ba-cheetah-row',
				scriptsStyles 	= this._data.scriptsStyles,
				removed 		= '';

			// Get the class of the nodes that should be in data.html.
			if ( this._data.partial ) {
				if ( 'column-group' == this._data.nodeType ) {
					nodeClass = 'ba-cheetah-col-group';
				}
				else if ( 'column' == this._data.nodeType ) {
					nodeClass = 'ba-cheetah-col';
				}
				else if ( 'row-inside' == this._data.nodeType ) {
					nodeClass = 'ba-cheetah-row-type-row-inside';
				}
				else {
					nodeClass = 'ba-cheetah-' + this._data.nodeType;
				}
			}

			// Remove elements that shouldn't be in data.html.
			html.find( '> *, script' ).each( function() {
				if ( ! $( this ).hasClass( nodeClass ) && 'application/json' != $( this ).attr( 'type' ) ) {
					removed 	   = $( this ).remove();
					scriptsStyles += removed[0].outerHTML;
				}
			});

			// Wrap scriptsStyles if we have any content in it.
			if ( '' !== scriptsStyles ) {
				if ( this._data.partial ) {
					scriptsStyles = '<div class="ba-cheetah-node-scripts-styles" data-node="' + this._data.nodeId + '">' + scriptsStyles + '<div>';
				}
				else {
					scriptsStyles = '<div class="ba-cheetah-node-scripts-styles">' + scriptsStyles + '<div>';
				}
			}

			// Update the data object.
			this._data.html 			= html.html();
			this._data.scriptsStyles 	= scriptsStyles;
		},

		/**
		 * Adds the new HTML to the page.
		 *

		 * @access private
		 * @method _addNewHTML
		 */
		_addNewHTML: function()
		{
			var siblings;

			// Add HTML for a partial refresh.
			if ( this._data.partial ) {

				// If data.nodeParent is present, we have a new node.
				if ( this._data.nodeParent ) {

					// Get sibling rows.
					if ( this._data.nodeParent.hasClass( 'ba-cheetah-content' ) ) {
						siblings = this._data.nodeParent.find( ' > .ba-cheetah-row' );
					}
					// Get sibling column groups.
					else if ( this._data.nodeParent.hasClass( 'ba-cheetah-row-content' ) ) {
						siblings = this._data.nodeParent.find( ' > .ba-cheetah-col-group' );
					}
					// Get sibling columns.
					else if ( this._data.nodeParent.hasClass( 'ba-cheetah-col-group' ) ) {
						siblings = this._data.nodeParent.find( ' > .ba-cheetah-col' );
					}
					// Get sibling modules.
					else {
						siblings = this._data.nodeParent.find( ' > .ba-cheetah-col-group, > .ba-cheetah-module' );
					}

					// Filter out any clones created by duplicating.
					siblings = siblings.filter( ':not(.ba-cheetah-node-clone)' );

					// Add the new node.
					if ( 0 === siblings.length || this._data.nodePosition >= siblings.length ) {
						this._data.nodeParent.append( this._data.html );
					}
					else {
						siblings.eq( this._data.nodePosition ).before( this._data.html );
					}

					// Remove node loading placeholder in case we have one.
					if ( this._data.nodeId ) {
						BACheetah._removeNodeLoadingPlaceholder( $( '.ba-cheetah-node-' + this._data.nodeId ) );
					}
				}
				// We must be refreshing an existing node.
				else {
					this._content.after( this._data.html );
					this._content.remove();
				}
			}
			// Add HTML for a full refresh.
			else {
				this._content.append( this._data.html );
			}

			// Refresh preview HTML of nodes within other nodes (such as modules in a row) to ensure
			// any changes to the nested node are preserved after we've inserted the new HTML.
			if ( BACheetah.preview && this._data.nodeId && this._data.nodeId != BACheetah.preview.nodeId ) {
				var previewNode = $( BACheetah.preview.classes.node );
				var isChild = previewNode.closest( '.ba-cheetah-node-' + this._data.nodeId ).length;
				if ( isChild ) {
					previewNode.html( BACheetah.preview.elements.node.html() );
				}
			}

			// Fire the addNewHTML callback if we have one.
			if ( this._data.onAddNewHTML ) {
				this._data.onAddNewHTML();
			}
		},

		/**
		 * Removes unnecessary JS and CSS assets from the layout.
		 *

		 * @access private
		 * @method _cleanAssets
		 */
		_cleanNewAssets: function()
		{
			var nodeId 	= null,
				self 	= this;

			// Remove duplicate assets from _data.html.
			this._data.html = this._removeDuplicateAssets( this._data.html );

			// Remove duplicate assets from _data.scriptsStyles.
			if ( this._data.scriptsStyles && '' !== this._data.scriptsStyles ) {
				this._data.scriptsStyles = this._removeDuplicateAssets( this._data.scriptsStyles );
			}

			// Remove all partial JS and CSS if this is a full render.
			if ( ! this._data.partial ) {
				$( '#ba-cheetah-partial-refresh-js' ).remove();
				$( '.ba-cheetah-node-scripts-styles' ).remove();
			}
			// Else, remove assets that aren't needed.
			else {

				$( '.ba-cheetah-node-scripts-styles' ).each( function() {
					if ( self._data.html.indexOf( 'ba-cheetah-node-' + $( this ).data( 'node' ) ) > -1 ) {
						$( this ).remove();
					}
				} );
			}
		},

		/**
		 * Removes JS and CSS that is already on the page
		 * from the provided HTML content.
		 *

		 * @access private
		 * @method _removeDuplicateAssets
		 * @param {String} html The HTML content to remove assets from.
		 * @return {String} The cleaned HTML content.
		 */
		_removeDuplicateAssets: function( html )
		{
			var cleaned = $( '<div>' + html + '</div>' ),
				src     = '',
				script  = null,
				href    = '',
				link    = null,
				loc 	= window.location,
				origin  = loc.protocol + '//' + loc.hostname + ( loc.port ? ':' + loc.port : '' );

			// Remove duplicate scripts.
			cleaned.find( 'script' ).each( function() {

				src = $( this ).attr( 'src' );

				if ( 'undefined' != typeof src ) {

					src     = src.replace( origin, '' );
					script  = $( 'script[src*="' + src + '"]' );

					if ( script.length > 0 ) {
						$( this ).remove();
					}
				}
			});

			// Remove duplicate links.
			cleaned.find( 'link' ).each( function() {

				href = $( this ).attr( 'href' );

				if ( 'undefined' != typeof href ) {

					href  = href.replace( origin, '' );
					link  = $( 'link[href*="' + href + '"]' );

					if ( link.length > 0 ) {
						$( this ).remove();
					}
				}
			});

			return cleaned.html();
		},

		/**
		 * Adds the new scripts and styles to the page.
		 *

		 * @access private
		 * @method _addNewScriptsStyles
		 */
		_addNewScriptsStyles: function()
		{

			if ( this._data.scriptsStyles && '' !== this._data.scriptsStyles ) {
				this._body.append( this._data.scriptsStyles );
			}
		},

		/**
		 * Adds the new layout JS to the page.
		 *

		 * @access private
		 * @method _addNewJS
		 */
		_addNewJS: function()
		{
			setTimeout( $.proxy( function() {

				if ( this._newJs ) {
					this._head.append( this._newJs );
				}

				BACheetah.triggerHook( 'didRenderLayoutJSComplete' );

			}, this ), 50 );
		},

		/**
		 * Called when the render has been completed.
		 *

		 * @access private
		 * @method _complete
		 */
		_complete: function()
		{
			if ( BACheetah._dragging ) {
				BACheetah._highlightRowsAndColsForDrag();
				BACheetah._refreshSortables();
			} else {
				BACheetah._setupEmptyLayout();
				BACheetah._highlightEmptyCols();
				BACheetah._initDropTargets();
				BACheetah._initSortables();
				BACheetah._resizeLayout();
			}

			BACheetah._initMediaElements();
			BACheetahLayout.init();
			BACheetahResponsiveEditing.refreshPreview( this._callback );
			BACheetah.triggerHook( 'didRenderLayoutComplete' );

			this._body.height( 'auto' );
		}
	};

})(jQuery);
