///////////////////////////////////////////////////////////////
// StripLoad is an extension for Loader.js
// Call the StripLoad.stripLoad(); method passing it a selector
// StripLoad will then remove all images within the selector
// preload them using Loader.js
// Once completing loading it will append the images back
// into the html where they came from
// Alternatively you can call the methods individually eg
// StripLoad.stripLoad.strip("#myDiv").load();
// then bind an event listener and listen for the LOADED event
// you can then call the append method StripLoad.append();
///////////////////////////////////////////////////////////////

var StripLoad = {
	
	STRIPPED : "stripped",
	LOADED : "loaded",
	APPENDED : "appended",
	
	jStripLoad : null,	
	jLoader : null,
	
	images : null,
	toLoad : [],
	stripped : [],
	
	stripLoad : function(selector)
	{
		StripLoad.jStripLoad = $(StripLoad);
		StripLoad.jLoader = $(Loader);
		StripLoad.jLoader.bind(Loader.COMPLETE, StripLoad.append);
		if(selector && typeof(selector) != "undefined") StripLoad.strip(selector);
		return StripLoad;
	},
	
	strip : function(selector)
	{	
		StripLoad.images = $(selector).find("img");
		StripLoad.images.each(function() {
			var details = { parent:$(this).parent(), url:$(this).attr("src"), classes:$(this).attr("class"), after:$(this).prev() };
			StripLoad.stripped.push(details);
			if(Loader.findIndexInArray(details.url, StripLoad.toLoad) == -1) StripLoad.toLoad.push(details.url);
			$(this).remove();
		});
		StripLoad.load(StripLoad.toLoad);
		return StripLoad;
	},
	
	load : function()
	{
		Loader.load(StripLoad.toLoad).start();
		return StripLoad;
	},
	
	append : function()
	{
		for(var i in StripLoad.stripped) {
			var image =  $( Loader.getContentAsCopy(StripLoad.stripped[i].url) );
			image.attr("class", StripLoad.stripped[i].classes);
			image.hide();
			StripLoad.stripped[i].after.length > 0 ? StripLoad.stripped[i].after.after(image) : StripLoad.stripped[i].parent.prepend(image);
			image.fadeIn(1000);
		}
		StripLoad.jStripLoad.trigger(StripLoad.APPENDED);
		return StripLoad;
	}
	
};
