galerie = function(user, pics){
	this._user = user || 0;
	this._wrapper = $('wrapper').get();
	this._gal = $('div').setAttribute('class', 'galerie').setStyle('MozTransition', 'left 1s ease-out, top 1s ease-in-out').setStyle('left', -parseInt(DOOMLIB.getStyle(this._wrapper,'width'))+'px').get();
	$('bgGal').append(this._gal).get();
	this._pics = pics || {};
	this._curPic = this._max  = 0;

}
galerie.prototype={
	init:function(){
		var thisObj = this;
		this.getPic(this._curPic, function(){thisObj.scrollTo(thisObj._curPic)}, '&count=true');
		this.setNavigation('navigation');
		for(i=0;i<this._pics.length;i++){
			this._pics[i].nr = nr;
		}
	},
	setNavigation:function(id){
		var thisObj = this;
		var navigation = $(id).get();
		
		//$(navigation.getElementsByClassName('navBottom')[0]).setStyle('display', 'none');
		//$(navigation.getElementsByClassName('navTop')[0]).setStyle('display', 'none');
		
		var elem = $(hasClass('navRight')[0]).addEvent('click', function(){
			if(typeof thisObj._pics[thisObj._curPic+1] =='undefined'){
				thisObj._pics[thisObj._curPic+1] = thisObj._pics[thisObj._curPic-thisObj._max];
				delete thisObj._pics[thisObj._curPic+1].init;
			}else{
				thisObj._max = Math.max(thisObj._curPic+1,thisObj._max);
			}
			if(typeof thisObj._pics[thisObj._curPic+1] !='undefined'){
				thisObj.getPic(thisObj._curPic+1, function(){thisObj.scrollTo(thisObj._curPic+1)});
			}
		}).get();
		
		if(typeof thisObj._pics[thisObj._curPic+1] =='undefined'){
			
			$(elem).setStyle('display', 'none');
		}
		$(hasClass('navLeft')[0]).addEvent('click', function(){
			if(typeof thisObj._pics[thisObj._curPic-1] !='undefined'){
				thisObj.getPic(thisObj._curPic-1, function(){thisObj.scrollTo(thisObj._curPic-1)});
			}
		});
	},
	getPic:function(nr, success, add){
		var thisObj = this;
		var add = add || '';
		if(typeof thisObj._pics[nr]!='undefined'){
			
			thisObj.addPic(nr);
			success();
		}else{
			/*rq.send(PR+'co/ajax/get.php', 'get', 'uId='+this._user.id+'&a=pic&nr='+nr+add, 'j', function(res){
				if(res.success=='true'){
					thisObj._pics[nr] = res.pic;
					thisObj.addPic(nr);
					if(typeof res.count!='undefined'){
						thisObj._count=res.count;
					}
					success();
				}else{
					alert('error');
				}
			});*/
		}
	},
	scrollTo:function(nr){
		this._curPic = nr;
		$(this._gal).setStyle('left', (-parseInt(DOOMLIB.getStyle(this._wrapper,'width'))*nr)+'px');
	},
	addPic:function(nr){
		var thisObj = this;
		
		if(typeof this._pics[nr].init=='undefined')
			this._pics[nr].init=true;
		else
			return false;
		
		var img = $('img').get();
		var wrapper =  $('li').setAttribute('class', 'galPicWrapper').get();
		
		var wWidth = parseInt(DOOMLIB.getStyle(thisObj._wrapper,'width'));
		var wHeight = parseInt(DOOMLIB.getStyle(thisObj._wrapper,'height'));
		$(wrapper).setStyle('width', wWidth+'px').setStyle('height', wHeight+'px');
		
		var image = new Image();
		$(image).addEvent('load', function(){
			img.src = AS+'/src/'+thisObj._pics[nr].url;
			thisObj._pics[nr].width = this.width || 1200;
			thisObj._pics[nr].height = this.height || 800;
			thisObj._pics[nr].elem = img;
			
			$(wrapper).append(img);
			
			thisObj.resizePic(nr);
			
			//$(wrapper).append(thisObj.info(thisObj._pics[nr]));
		});
		
		image.src =AS+'/src/'+this._pics[nr].url;
		
		$(this._gal).append(wrapper);
		
	},
	info:function(data){
		var title = data.title || 'title';
		var des = data.des || 'description';
		var info = $('ul').setAttribute('class', 'infoField').get();
		$(info).append($('li').append($('span').setAttribute('class', 'bb').append((data.nr+1)+' / '+this._count).get()).get());
		$(info).append($('li').append($('span').setAttribute('class', 'bb').append(DOOMLIB.timestampToTime(data.upd)).get()).get());
		$(info).append($('li').append($('span').setAttribute('class', 'bb').append(data.rate).get()).get());
		$(info).append($('li').append($('span').setAttribute('class', 'bb').append(title).get()).get());
		$(info).append($('li').append($('span').setAttribute('class', 'bb').append(des).get()).get());
		return info;
	},
	resize:function(){
		var counter = 0;
		for(var i in this._pics){
			this.resizePic(i);
			counter++;
		}
		$(this._gal).setStyle('left', (-parseInt(DOOMLIB.getStyle(this._wrapper,'width'))*this._curPic)+'px');
	},
	resizePic:function(nr){
		var pic = this._pics[nr].elem;
		if(typeof pic=='undefined')
			return false;
		var elem = pic.parentNode;
		
		var wWidth = parseInt(DOOMLIB.getStyle(this._wrapper,'width'));
		var wHeight = parseInt(DOOMLIB.getStyle(this._wrapper,'height'));
		
		
		
		$(elem).setStyle('left', (parseInt(DOOMLIB.getStyle(this._wrapper,'width'))*nr)+'px');
		var nrel= this.relation(this._pics[nr].width, this._pics[nr].height, 5000, 5000, wWidth , wHeight);
		
		$(pic).setStyle('width', nrel.w+'px').setStyle('height', nrel.h+'px').setStyle('left', (wWidth-nrel.w)/2+'px').setStyle('top', (wHeight-nrel.h)/2+'px');
		//return {'w':}
	},
	relation:function(istWidth, istHeight, maxW, maxH, minW, minH){
		if(istWidth>istHeight){
			var sollWidth = Math.max(minW, Math.min(istWidth, maxW));
			var sollHeight = istHeight*sollWidth/istWidth;
		}else if(istWidth<istHeight){
			var sollHeight = Math.max(minH, Math.min(istHeight, maxH));
			var sollWidth = istWidth*sollHeight/istHeight;
		}else{
			var sollHeight = Math.max(minH, Math.min(istHeight, maxH));
			var sollWidth =Math.max(minW, Math.min(istWidth, maxW));
		}
		return {'w':sollWidth,'h':sollHeight};
	}
}
