/**
 * Image Preload
 *
 * Now with more jive and backtalk!
 *
 * @author Avery Brooks <brooks@radicalmedia.com>
 * @version 1.1
 * @copyright 2009
 */
var PreLoad = function(_options) {

	// Counts
	this.i = 0; // total images
	this.j = 0;
	this.d = 0;
	this.f = 0;

	// Callbacks
	this._before = (_options.before && typeof(_options.before)==="function") ? _options.before : function(){};
	this._item = (_options.item && typeof(_options.item)==="function") ? _options.item : function(){};
	this._after = (_options.complete && typeof(_options.complete)==="function") ? _options.complete : function(){};

	// Target Selector
	this.selector = _options.selector;

}
PreLoad.prototype.init = function() {
	
	// before load callback
	this._before();

	var _PL = this;

	$(this.selector).each(function(){_PL.count();});
	$(this.selector).each(function(){

		var agent = navigator.userAgent.toLowerCase();
		if (agent.indexOf('iphone')!=-1 || agent.indexOf('ipad')!=-1) {
			_PL.load();
			return;
		}

		// There would be a test here for failed (!200 status) images, though it
		// needs some tweaking to support IE etc

		// Already Loaded
		if (this.complete) {
			_PL.load();
		// Unloaded
		} else {
			$(this).bind("load",function(){_PL.load();});
		}

	});
}
PreLoad.prototype.load = function() {

	// Increase load count, fire "each" handler
	this.i++;
	this._item();
	// if loaded is complete, fire "done" handler
	if (this.i === this.j) {
		this._after();
	}
}
PreLoad.prototype.count = function() {
	this.j++;
	// this._found(i);
}
PreLoad.prototype.fail = function() {
	this.f++;
	// fire failed handler here
}

// Placeholders for callbacks (these are dynamically applied so should not be prototyped) ?
// PreLoad.prototype._before = function() {}
// PreLoad.prototype._each = function(ele) {}
// PreLoad.prototype._done = function() {}

// PreLoad.prototype._found = function(ele) {}
// PreLoad.prototype._failed = function(ele) {}

