/**
* Generic Image Preloader v0.1 by Angel Kostadinov
* Copyright 2010 Toxic Media Ltd Sofia, Bulgaria
*/
function Preloader()
{
	/* Cache queue */
	this.cache = [];
	
	/* Number of preloaded images */
	this.count = 0;
	
	/* Flag to abort preload process */
	this.abort = false;
	
	/* Number of milliseconds to delay callback function invokation */
	this.delay = 10;
	
	/* Number of milliseconds between subsequent progress validation calls */
	this.timeout = 20;
};


Preloader.prototype.getDomain = function()
{
	var domain = 'http://' + document.location.toString().match(/:\/\/(.[^\/]+\/(cms.bg){0,1})/)[1];
	
	return !0 ? 'http://www.toxicmedia.bg/' : domain;
}

Preloader.prototype.addFlashPreloader = function( id )
{
	var so = new SWFObject(this.getDomain() + "templates/themes/galaxy/flash/loader.swf", "galaxyloader", "40", "40", "8", "#336699");
	so.addParam("wmode", "transparent");
	
	if (null !== document.getElementById(id))
	{
		so.write(id.toString());
	}
	
	return this;
};

Preloader.prototype.prePreload = function()
{
	/* Empty cache */
	this.cache = [];
	
	/* Reset preloaded items */
	this.count = 0;
	
	/* False abort flag */
	this.abort = false;
	
	return this;
};

Preloader.prototype.postPreload = function()
{
	$('#preloader').hide();
	
	return this;
};

Preloader.prototype.onComplete = function( image )
{
	this.count++;
};

Preloader.prototype.onAbort = function( image )
{
	this.abort = true;
};

Preloader.prototype.progress = function(callback)
{
	for (var image in this.cache)
	{
		if (image.complete)
		{
			this.onComplete(image);
		}
	}
	
	if (this.count != this.cache.length && !this.abort)
	{
		setTimeout(this.delegate(this, this['progress'],[callback]),this.getProgressTimeout());
	}
	else 
	{
		setTimeout(this.delegate(this, callback,[]),this.delay);
	}
};

Preloader.prototype.getProgressTimeout = function() 
{
	return this.timeout <= 20 ? 20 : this.timeout; /* Low subseqent method calls can result in high CPU usage */
};

Preloader.prototype.getStylesheets = function()
{
	return $('link[rel="stylesheet"]',document.head).map(function()
	{
		return $(this).attr('href');
	}).get();
};

Preloader.prototype.preload = function( callback )
{
	/* Load sheet */
	var sheet = $.ajax({
		url: this.getStylesheets().slice(0,1),
		async:false
	}).responseText;
	
	var resources = sheet.match(/[^\(]+\.(gif|jpg|jpeg|png)/g).slice(1);
	
	for (var i in resources)
	{
		var image = new Image();
		
		image.onload  = this.delegate(this, this['onComplete'],[image]);
		image.onerror = this.delegate(this, this['onAbort'],[image]);
		image.onabort = this.delegate(this, this['onAbort'],[image]);
		
		/* Set image source */
		image.src = this.getDomain() + 'templates/themes/galaxy/' + resources[i].substr(3);
		
		this.cache.push(image);
	}
	
	this.progress(callback);
};

Preloader.prototype.preloadResponse = function(response, callback)
{
	var resources = response.match(/[^\"]+\.(gif|jpg|jpeg|png)/g);
	
	if (resources && resources.length)
	{
	
		resources = resources.slice(1);
		
		for (var i in resources)
		{
			var image = new Image();
				
			image.onload  = this.delegate(this, this['onComplete'],[image]);
			image.onerror = this.delegate(this, this['onAbort'],[image]);
			image.onabort = this.delegate(this, this['onAbort'],[image]);
			
			/* Set image source */
			image.src = this.getDomain() + resources[i].trim();
			
			this.cache.push(image);
		}
	}
	
	this.progress(callback);
};

Preloader.prototype.delegate = function(target, method, args)
{
	return (typeof method == "function") ? function() 
	{ 
		return method.apply(target, args); 
	} : function()
	{
		return false;
	};
};

var preloader = new Preloader();