MoGallery = function(gid) {
   this.init = function(gid) {
     // do initialization here
     this.gid = gid;
	  var refthis = this;
	  $("div.gallery-"+refthis.gid).each(function(){
			$(this).find("a:has(img)").each(function(){
				refthis.gArr.push({"filename":$(this).attr("href"),"loaded":0});
			});
			$(this).html('<div class="main"><div class="pictureholder hide"><a href="hejsan.html" class="nextlink"><img width="468" height="467" src="http://www.bloggligan.se/material/trans.gif" alt="" /></a></div></div><div class="bottom"><div class="bottombar"><div class="control"><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="40"><a class="backlink hide" href="hejsan.html"><img src="http://www.bloggligan.se/material/arrow_prev.jpg" alt="Bakåt" /></a></td><td class="counter" align="center"></td><td width="40"><a class="nextlink hide" href="hejsan.html"><img src="http://www.bloggligan.se/material/arrow_next.jpg" alt="Framåt" /></a></td></tr></table></div></div></div>');
				refthis.piclen = refthis.gArr.length;
				refthis.changesrc(refthis.curid);
				refthis.active = 1;
				refthis.preload(refthis.curid, 1);
				refthis.preload(refthis.curid+1);
				refthis.changecount();
				
				$('div.gallery-'+refthis.gid+' .nextlink').show();
		
				$('div.gallery-'+refthis.gid+' .backlink').click(function(){
					return refthis.changepic(true);	
				});
	
				$('div.gallery-'+refthis.gid+' .nextlink').click(function(){
					return refthis.changepic();	
				});
				

		});  
	}
	
	this.changesrc = function(gid) {
		$('div.gallery-'+this.gid+' .pictureholder').css('backgroundImage','url('+this.gArr[gid].filename+')');
	}
	
	this.changecount = function() {
		$('div.gallery-'+this.gid+' .counter').html('<span>'+(this.picid+1)+'/</span><span class="grey">'+this.piclen+'</span>');
	}
	
	this.preload = function(id, activate) {
		var refthis = this;
		if(this.gArr[id].loaded == 0){
			var img = new Image();
  
			$(img).load(function() {
				if(activate == 1){
					$('.pictureholder').fadeIn(400,function(){
						refthis.active = 0;
						refthis.gArr[id].loaded = 1;
						if(refthis.curid != refthis.picid){
							refthis.changepic(refthis.picid);
						}
					});
				}
			})
			$(img).attr('src', refthis.gArr[id].filename);
		}else{
			if(activate == 1){
				$('div.gallery-'+refthis.gid+' .pictureholder').fadeIn(400,function(){
					refthis.active = 0;
					if(refthis.curid != refthis.picid){
						refthis.changepic(refthis.picid);
					}
				});
			}
		}
	}
	
	this.changepic = function(prev){
		var refthis = this;
		
		if(undefined===prev){
			refthis.picid++;
			if(refthis.picid < refthis.piclen-1){
				refthis.preload(refthis.picid+1);
			}
		}else if(prev == true){
			refthis.picid--;
		}else{
		}
		
		$('div.gallery-'+refthis.gid+' .backlink').show();
		$('div.gallery-'+refthis.gid+' .nextlink').show();
		
		if(refthis.picid == 0){
			$('div.gallery-'+refthis.gid+' .backlink').hide();
		}else if(refthis.picid == refthis.piclen-1){
			$('div.gallery-'+refthis.gid+' .nextlink').hide();
		}
		
		refthis.changecount();
		
		if(refthis.active == 1){
			return false;
		}else{
			refthis.active = 1;
			refthis.curid = refthis.picid;
			$('div.gallery-'+refthis.gid+' .pictureholder').fadeOut(400,function(){
				refthis.changesrc(refthis.curid);
				refthis.preload(refthis.curid,1);
			});
		}
		return false;
	}	
	
	this.gid = 0;
	this.picid =  0;
	this.curid = 0;
	this.active = 0;
	this.piclen = 0;
	this.fadetime = 400;
	this.gArr = new Array();
	
	this.init(gid);
}