/**
 * Easy Gallery
 *
 * @author Tobias Schibler <tobias@tujo.no>
 * @package JavaScript
 * @copyright 2011 tujo ANS
 */



function EasyGallery(){}
EasyGallery.prototype.init = function(rootEl){
	this.rootEl = rootEl;
//	this.currentCount = 1;
//	this.countTotal = 0;
//	this.countLoaded = 0;
//	this.refresh();
};
//EasyGallery.prototype.refresh = function(){
//	this.countTotal = $('ul li', this.rootEl).length;
//};
//EasyGallery.prototype.setCurrentByLeft = function(left){
//	this.currentCount = Math.round(left/$.screen().w)+1;
//	this.callOnNavigate();
//};
//EasyGallery.prototype.prev = function(fn){
//	if(this.currentCount > 1)
//		this.currentCount--;
//	this.goTo(this.currentCount, fn);
//};
//EasyGallery.prototype.next = function(fn){
//	if(this.currentCount < this.countTotal)
//		this.currentCount++;
//	this.goTo(this.currentCount, fn);
//};
//EasyGallery.prototype.goTo = function(num, fn){
//	this.callOnNavigate();
//	var elem = $('ul li:eq('+(num-1)+')', this.rootEl);
//	fn.apply(this, [elem, num]);
//	return elem;
//};
//EasyGallery.prototype.onNavigate = function(fn){
//	this.navigateFn = fn;
//};
//EasyGallery.prototype.callOnNavigate = function(){
//	this.navigateFn.apply(this, [{
//		isFirst: this.currentCount === 1,
//		isLast: this.currentCount === this.countTotal
//	}]);
//};
EasyGallery.prototype.onLoad = function(fn){
	if(!typeof fn === 'function')
		return warn('No onLoad Callback!');
	this.onLoadFn = fn;
};
EasyGallery.prototype._createLi = function(data){
	var target = this;
	var liEl = $('<li />');
	var imgEl = $('<img />');
	liEl.css({
		'float': 'left'
	});
	imgEl.prop({
		'src': data.src
	});
	imgEl.css({

	});
	imgEl.load(function(){
		target.countLoaded++;
//		target.refresh();
		target.onLoadFn.apply(target, [this, target.countLoaded, target.countTotal]);
	});
	$(liEl).append(imgEl);

	return liEl;
};
EasyGallery.prototype.setDefCSS = function(){
	$(this.rootEl).css({
		'overflow': 'hidden'
	});
	$('ul', this.rootEl).css({
		'list-style': 'none',
		'overflow': 'hidden'
	});
};
EasyGallery.prototype.setImages = function(list, fn){
	var target = this;
	if(!typeof fn === 'function')
		return warn('No onLoad Callback!');
	this.setDefCSS();
	var d, elem;
	for(var i=0,l=list.length;i<l;++i){
		d = fn.apply(target, [list[i], l]);
		if(!d)
			continue;
		elem = this._createLi(d);
		$('ul', this.rootEl).append(elem);
	}
};


