document.observe("dom:loaded", function() {
  // initially hide all containers for tab content
  if ($('Gallery')) {
  	Gallery.init('Gallery',false);
  }
});

var Gallery = new Object;

var PATH = '/';

Gallery.div = false;
Gallery.pre = false;
Gallery.current = 0;
Gallery.queue = new Array;
Gallery.filenames = new Array;
Gallery.timeout = false;
Gallery.cache = new Array;
Gallery.isLoaded = new Array;
Gallery.zIndex = 100;
Gallery.navi = false;
Gallery.status = false;
Gallery.interval = false;
Gallery.slides = new Array;
Gallery.counter = 0;
Gallery.t = false;
Gallery.naviVisible = true;
Gallery.cacheQueue = new Array;
Gallery.cacheQueueCursor = 0;
Gallery.cacheRange = 3;
Gallery.skip = false;
Gallery.w = 800;
Gallery.h = 533;
Gallery.thumbnails = false;

Gallery.setSize = function(w,h) {
	
	var v = document.viewport.getDimensions();

	if ((v.height > 700)) {
		Gallery.w = 1051;
		Gallery.h = 700;
	}
	
	if ((v.height > 700)&&(v.width < 1300)) {
		Gallery.w = 931;
		Gallery.h = 620;
	}
	
	if((v.height < 700) && (v.height > 600)) {
		Gallery.w = 931;
		Gallery.h = 620;
	}
	
	if((v.height < 600)) {
		Gallery.w = 800;
		Gallery.h = 533;
	}
	
	$('page').style.width = (Gallery.w+250)+'px';
	
}

Gallery.init = function(div,timeout) {
			
	Gallery.div = $h(div);
	Gallery.total = Gallery.queue.length;
	
	Gallery.div.style.width = Gallery.w+'px';
	Gallery.div.style.height = Gallery.h+'px';
	
	$('GalleryOverlay').style.width = Gallery.w+'px';
	$('GalleryOverlay').style.height = Gallery.h+'px';
	
	$('counter').innerHTML = '<span id="counterCurrent"></span> / <span id="counterTotal"></span>';
	
	Gallery.setupNavi();
	Gallery.hideStatus();
	Gallery.hideNavi();
	
	var init = Gallery.parseUrl();
	
	Gallery.buildCacheQueue(init);
	Gallery.load(init,false,true);
	
	if(timeout) {
		Gallery.interval = setInterval(Gallery.next,timeout*1000);
	}
	
	document.getElementById('counterTotal').innerHTML = Gallery.total;
	document.getElementById('counterCurrent').innerHTML = 1;
	
	$('desc_thumbs').appendChild(Gallery.thumbnails);
	Gallery.thumbnails.hide();
	$('desc_thumbs').onmouseover = function() { Gallery.thumbnails.show(); }
	$('desc_thumbs').onmouseout = function() { Gallery.thumbnails.hide(); }
	
}

Gallery.setStatus = function(html) {
	Gallery.status.innerHTML = html;
}

Gallery.parseUrl = function() {
	var url = document.location.href.split('#');
	if(!url[1]) {
		return 0;
	} else {
		for(var i = 0; i < Gallery.filenames.length; i++) {
			if(Gallery.filenames[i].id == url[1]) {
				return i;
			}
		}
	}
	return 0;
}

Gallery.url = function(i) {
	
	var file = Gallery.filenames[i].id;
	file = file.replace('/uploads/media/','');
	document.location.href = '#'+file;
	
}

Gallery.add = function(srcimg,id) {
	
	if(!Gallery.thumbnails) {
		Gallery.thumbnails = document.createElement('div');
		Gallery.thumbnails.className = 'GalleryThumbnails';
		var bg = document.createElement('div');
		bg.className = 'bg';
		Gallery.thumbnails.appendChild(bg);
		
		var bg = document.createElement('div');
		bg.className = 'imgcontainer';
		Gallery.thumbnails.appendChild(bg);
		Gallery.thumbnails.style.width = (Gallery.w-40)+'px';
	}
	
	Gallery.filenames.push({
		img: srcimg,
		id: id
	});
	
	var img = PATH+'thumb.php?img='+srcimg+'&w='+Gallery.w+'&h='+Gallery.h;
	var thumb = PATH+'thumb.php?img='+srcimg+'&w='+78+'&h='+78;
	
	var tDiv = document.createElement('div');
	tDiv.className = 'GalleryThumb';
	tDiv.i = Gallery.queue.length;
	tDiv.onclick = function() { Gallery.show(this.i); }
	tDiv.onmouseover = function() { $(this).addClassName('hv'); }
	tDiv.onmouseout = function() { $(this).removeClassName('hv'); }
	var imgobj = document.createElement('img');
	imgobj.src = thumb;
	tDiv.appendChild(imgobj);
	
	Gallery.thumbnails.down('.imgcontainer').appendChild(tDiv);
	Gallery.queue.push(img);
}

Gallery.buildCacheQueue = function(img) {
	
	Gallery.cacheQueue = new Array;
	
	var from = img;
	var range = Gallery.cacheRange;
	var last = from + range;
	var first = from;
	
	if(last > Gallery.total) {
		last = 0;
	}
	
	for(var i = first; i < last; i++) {
		if (!Gallery.isLoaded[i]) {
			Gallery.cacheQueue.push(i);
		}
	}
	
	if(Gallery.cacheQueue.length == 0) {
		Gallery.cacheQueue = false;
	}
	Gallery.cacheQueueCursor = 0;
	
}

Gallery.load = function(i,stop,show) {
	
	//Gallery.showStatus();
	//Gallery.setStatus('Ładowanie '+(i+1)+' / '+Gallery.total);
	
	Gallery.cache[i] = document.createElement('img');
	Gallery.cache[i].onload = function() {
		
		Gallery.cacheQueueCursor++;
		var nextImage = Gallery.cacheQueue[Gallery.cacheQueueCursor];
		
		Gallery.isLoaded[i] = true;
		
		if(nextImage&&(!stop)) {
			
			setTimeout(function(){
				Gallery.load(nextImage);
			}, 1);
			
		} else {
			//Gallery.setStatus('OK');
			//Gallery.hideStatus();
		}
		
		if(show) {
			Gallery.hidePre();
			Gallery.showFromCache(i);
		}
		
	}
	
	Gallery.cache[i].src = Gallery.queue[i];
}

Gallery.show = function(i) {

	Gallery.buildCacheQueue(i);
	document.getElementById('counterCurrent').innerHTML = (i+1);
	
	if (Gallery.isLoaded[i]) {
		Gallery.showFromCache(i);
		if (Gallery.cacheQueue) {
			Gallery.load(Gallery.cacheQueue[0], false, false);
		}
	} else {
		//Gallery.showStatus();
		//Gallery.setStatus('Trwa ladowanie...');
		Gallery.showPre();
		Gallery.load(i,false,true);
	}
	
}

Gallery.showFromCache = function(i) {
	
	Gallery.current = i;
		
	var d = document.createElement('div');
	d.className = 'GallerySlide';
	d.style.zIndex = Gallery.zIndex;
	d.style.width = Gallery.w+'px';
	
	d.onclick = function() {
		Gallery.next();
	}
	
	Gallery.slides[Gallery.counter] = d;
	if(Gallery.counter > 1) {
		var r = Gallery.slides[Gallery.counter-2];
		r.parentNode.removeChild(r);
	}
	Gallery.counter++;
	
	Gallery.zIndex++;
	Gallery.pre.style.zIndex = Gallery.zIndex;
	Gallery.zIndex++;
	Gallery.navi.style.zIndex = Gallery.zIndex;
	Gallery.zIndex++;
	
	var img = document.createElement('img');
	img.src = Gallery.cache[i].src;
	
	d.appendChild(img);
	Gallery.div.appendChild(d);
	
	if (Gallery.isLoaded[i]) {
		Gallery.hidePre(true);
		Gallery.appear(d);
	} else {
		d.style.display = 'none';
		img.onload = function() {
			d.style.display = 'block';
			Gallery.appear(d);
		}
	}
	
	Gallery.url(i);
}

Gallery.next = function() {
	var n = Gallery.current+1;
	if(n > (Gallery.total-1)) { var n = 0; }
	Gallery.show(n);
}

Gallery.prev = function() {
	var n = Gallery.current-1;
	if(n < 0) { var n = Gallery.total-1; }
	Gallery.show(n);
}

Gallery.appear = function(r) {
	var appear = {
			type: 'opacity',
			from: 0,
			to: 100,
			step: 4,
			delay: 10
		}
	$fx(r).fxAdd(appear).fxRun();
}

Gallery.hideStatus = function() {
	var appear = {
			type: 'opacity',
			from: 100,
			to: 0,
			step: -3,
			delay: 10
		}
	$fx(Gallery.status).fxAdd(appear).fxRun();
}

Gallery.showStatus = function() {
	var appear = {
			type: 'opacity',
			from: 0,
			to: 100,
			step: 3,
			delay: 10
		}
	$fx(Gallery.status).fxAdd(appear).fxRun();
}

Gallery.showPre = function() {
	/*var appear = {
			type: 'opacity',
			from: 0,
			to: 100,
			step: 3,
			delay: 10
		}
	$fx(Gallery.pre).fxAdd(appear).fxRun();*/
	Gallery.pre.style.display = 'block';
}

Gallery.hidePre = function(inst) {
	/*var appear = {
			type: 'opacity',
			from: 100,
			to: 0,
			step: -3,
			delay: 10
		}
		
	if(inst) {
		var appear = {
			type: 'opacity',
			from:0,
			to:0,
			step:1,
			delay:1
		}
	}
	$fx(Gallery.pre).fxAdd(appear).fxRun();*/
	Gallery.pre.style.display = 'none';
}

Gallery.setupNavi = function() {
	var n = document.createElement('div');
	n.className = 'GalleryNext';
	var nb = document.createElement('div');
	nb.className = 'GalleryNextBox';
	nb.innerHTML = '';
	n.appendChild(nb);
	
	var p = document.createElement('div');
	p.className = 'GalleryPrev';
	var pb = document.createElement('div');
	pb.className = 'GalleryPrevBox';
	pb.innerHTML = '';
	p.appendChild(pb);
	
	var m = document.createElement('div');
	m.className = 'GalleryMiddle';
	m.style.width = (Gallery.w-(2*100))+'px';
	
	
	Gallery.div.onmouseover = function() {
		Gallery.showNavi();
	}
	
	Gallery.div.onmouseout = function() {
		Gallery.hideNavi();
	}
	
	n.onclick = function() { clearInterval(Gallery.interval); Gallery.next(); }
	p.onclick = function() { clearInterval(Gallery.interval); Gallery.prev(); }
	m.onclick = function() { clearInterval(Gallery.interval); Gallery.next(); }
	
	n.onmouseover = function() { this.addClassName('hv'); }
	p.onmouseover = function() { this.addClassName('hv'); }
	
	n.onmouseout = function() { this.removeClassName('hv'); }
	p.onmouseout = function() { this.removeClassName('hv'); }
	
	Gallery.navi = document.createElement('div');
	Gallery.navi.className = 'GalleryNavi';
	Gallery.navi.style.width = Gallery.w+'px';
	Gallery.navi.style.height = Gallery.h+'px';
	
	Gallery.status = document.createElement('div');
	Gallery.status.className = 'GalleryStatus';
	
	Gallery.navi.appendChild(n);
	Gallery.navi.appendChild(m);
	Gallery.navi.appendChild(p);
	Gallery.navi.appendChild(Gallery.status);
	
	Gallery.div.appendChild(Gallery.navi);
	
	p.style.top = ((Gallery.h-p.getHeight())/2)+'px';
	n.style.top = ((Gallery.h-n.getHeight())/2)+'px';
	
	var pre = document.createElement('div');
	pre.className = 'GalleryPreloader';
	
	var spinner = document.createElement('div');
	spinner.className = 'GallerySpinner';
	
	pre.appendChild(spinner);
	
	Gallery.pre = pre;	
	Gallery.div.appendChild(Gallery.pre);
	
	pre.style.left = ((Gallery.w-pre.getWidth())/2)+'px';
	pre.style.top = ((Gallery.h-pre.getHeight())/2)+'px';
}

Gallery.showNavi = function() {
	clearTimeout(Gallery.t);
	
	if (!Gallery.naviVisible) {
		Gallery.t = setTimeout(function(){
			var appear = {
				type: 'opacity',
				from: 0,
				to: 100,
				step: 4,
				delay: 10
			}
			$fx(Gallery.navi).fxAdd(appear).fxRun();
			Gallery.naviVisible = true;
		}, 100);
	}
}

Gallery.hideNavi = function() {
	clearTimeout(Gallery.t);
	
	if (Gallery.naviVisible) {
		Gallery.t = setTimeout(function(){
			var appear = {
				type: 'opacity',
				from: 100,
				to: 0,
				step: -4,
				delay: 10
			}
			$fx(Gallery.navi).fxAdd(appear).fxRun();
			Gallery.naviVisible = false;
		}, 100);
	}
}


