/* quickbox.js (rev .2.3)
	requires routines.js
	(c) Aula.info 2005-2006 (info.aula.info.hu) under CC Attribution-ShareAlike 2.5 License
 */
addLoadEvent(init);

// global init
function init() {
	window.contentDiv = _id('gdiv');
	window.contentBox = _id('gbox');
	window.idesc = _id('idesc');
	window.loadBox = _id('gload');
	window.thumbnails = new Array();
	window.thumbnails = _tag('span','contentbox','tn');
	collectTN();
	keyNavInit();
	toggle('w');
}
window.onresize = resizeBox; // specially needed for Opera
window.index = 0; // default picture index in window.pics array
window.locked = 0; // default: no gallery loaded

function collectTN () {
	for (var n = 0; n < window.thumbnails.length; n++) {
		addEventByTNLoop(n);
		// hide images after loading
		window.thumbnails[n].style.backgroundImage = 'url(sprites/'+window.pics[n]+')';
		window.thumbnails[n].firstChild.style.visibility = 'hidden';
	}
}

function addEventByTNLoop(m) {
	addEvent(window.thumbnails[m],'click', function() { showPic(m); });
	addEvent(window.thumbnails[m],'mouseover', function() { saturate(window.thumbnails[m]); });
	addEvent(window.thumbnails[m],'mouseout', function() { desaturate(window.thumbnails[m]); });
}

function showIf(obj) {
	var o = _id(obj);
	
	o.style.display = o.style.display != 'block' ? 'block' : 'none';
	if (o.id == 'gbox') { o.style.visibility = 'hidden'; } 
	// Hide contentbox while loading image (visibility:hidden needed workaround: Opera does not load image if display:none)
}

function showPic (idx) {
	window.index = idx;
	pic = window.pics[idx];
	desc = window.descs[idx];
	window.contentDiv.innerHTML = '<img id="current" title="Előző: ← | Következő: → | Bezárás: Esc" src="pictures/'+pic+'" alt="" />';
	window.idesc.innerHTML = desc;
	addImgLoadEvt();
	if (window.locked == 0) {
	showIf('gbg'); }
	window.locked = 1;
	showIf('gbox');
	showIf('gload');
}

function restore() {
	window.locked = 0;
	showIf('gbg');
	showIf('gbox');
	window.contentDiv.innerHTML = '<img id="current" src="pictures/1px.gif" alt="" />';
}

function resizeBox () {
// revision required -- now it works with IE 7 in IE 6 way 
window.loadBox.style.display='none';
window.contentBox.style.visibility='visible';

myImg = _id('current');
myDiv = window.contentBox;

// +2px = image borders width
ki = myImg.width+2+'px';

myDiv.style.width = ki;

// viewport dimensions for modern browsers and for old IE
wWidth = document.documentElement ? document.documentElement.clientWidth : self.offsetWidth;
wHeight = self.innerHeight ? self.innerHeight : document.documentElement.clientHeight;

if (wWidth >= parseInt(ki)) {
	myDiv.style.left = Math.round((wWidth-parseInt(ki))/2)+'px';
}
else {
	myDiv.style.left = '1px';
}

gHeight = myDiv.offsetHeight;
pHeight = window.pageYOffset;

if (wHeight >= gHeight) {
// top position for IE6 or 5 (partly simulates position:fixed) --  also solves IE7beta3 bugs 
	if (document.documentElement) { myDiv.style.top = Math.round((wHeight-Math.round(gHeight))/2) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) + 'px'; }
	else { myDiv.style.top = Math.round((wHeight-Math.round(gHeight))/2) + pHeight + 'px'; }
}
else  {
	if (document.documentElement) { 	myDiv.style.top = 10+ ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) + 'px'; }
	else { myDiv.style.top = 10 + pHeight + 'px'; }
}

}

function addImgLoadEvt() {
// add load event to content image
	var picobj = _id('current');
	addEvent(picobj,'load', resizeBox);
}


function infinitePics(dir) {
	var nextindex =  window.index+dir;
	if (nextindex > (window.pics.length-1)) { nextindex = 0; }
	else if (nextindex < 0) { nextindex = window.pics.length-1; }
	
	window.contentBox.style.display = 'none';
	// window.locked = 1;
	showPic(nextindex);
}


function keyNavInit() {
	document.onkeydown = function(event) {
	var keyCode = getKeyCode(event);
	
	if (window.locked==1) {
	switch(keyCode) {
		case 27:
			restore();
			break;
		case 121:
			restore();
			break;
		case 37:
			infinitePics(-1);
			break;
		case 38:
			infinitePics(-1);
			break;
		case 39:
			infinitePics(1);
			break;
		case 40:
			infinitePics(1);
			break;
		default:
			return;
	}
	}
	}
}

function saturate (obj) {
	obj.style.backgroundPosition = 'top right';
}

function desaturate (obj) {
	obj.style.backgroundPosition = 'top left';
}
