if (typeof(gl_fw) == 'undefined') var gl_fw = 'auto';
if (typeof(gl_ew) == 'undefined') var gl_ew = 'auto';
if (typeof(gl_vt) == 'undefined') var gl_vt = 'auto';
if (typeof(gl_vh) == 'undefined') var gl_vh = 'auto';
if (typeof(gl_pm) == 'undefined') var gl_pm = 0;
if (typeof(gl_es) == 'undefined') var gl_es = true;
if (typeof(gl_ef) == 'undefined') var gl_ef = true;
if (typeof(gl_tc) == 'undefined') var gl_tc = 'alpha';
if (typeof(gl_si) == 'undefined') var gl_si = 'GL_ShowImage';
if (typeof(gl_hi) == 'undefined') var gl_hi = 'hideFader';



var pb_t = Math.ceil(gl_c/gl_l);
var gl_ms = [];
var gl_s = [];
var gl_id = 1;
var gl_im = false;



$(function() {
	$('#fader').click(function() { eval(gl_hi+'()'); });
	$('#gl_viwi_close').click(function() { eval(gl_hi+'()'); return false; });
});
$.getJSON('json/gallery', { r: gl_r }, function(jx) { gl_s = jx.img; } );



document.onkeydown = function(event)
{
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : null;
	if (keyCode == 0x1B) eval(gl_hi+'()');
	if (event.ctrlKey)
	{
		switch (keyCode)
		{
			case 0x25:
			if (gl_im) GL_LoadImage(-1);
			else eval(gl_si+'(gl_id)');
			break;
			case 0x27:
			if (gl_im) GL_LoadImage(1);
			else eval(gl_si+'(gl_id)');
			break;
			case 0x26:
			eval(gl_hi+'()');
			break;
			case 0x28:
			var fa = gl_d+gl_b+'/'+gl_s[gl_id][0];
			location = '/attach'+fa.substr(4);
			break;
		}
	}
}



function GL_SwitchVHC(zcc, s)
{
	$('#'+zcc).addClass(zcc+(s ? '' : ' '+gl_tc));
}



function GL_ShowImage(id, p)
{
	var fw = (gl_fw == 'auto') ? $('html').width() : gl_fw;
	var ew = (gl_ew == 'auto') ? fw : gl_ew;
	var vt = (gl_vt == 'auto') ? $('html').scrollTop() : gl_vt;
	var vh = (gl_vh == 'auto') ? $('html').height() : gl_vh;
	if (typeof(p) != 'undefined') GL_MultiLoad(p);
	if (typeof(gl_s[id]) != 'undefined')
	{
		var f = gl_s[id][0];
		var t = gl_s[id][1];
		var w = parseInt(gl_s[id][2]);
		var h = parseInt(gl_s[id][3]);
		var z = gl_d+gl_b+'/'+f;
	}
	else return false;
	var x = Math.round((fw-w)/2)-gl_pm;
	var y = Math.round((vh-h)/2)-gl_pm;
	gl_id = id;
	var img = '<img src="'+z+'" width="'+w+'" height="'+h+'" alt="'+t+'" title="'+t+'" />';
	var ipl = '<img src="'+gl_d+gl_b+'/'+gl_s[id==gl_c ? 1 : id+1][0]+'" style="display:none;">';
	var pnw = Math.round(w/3);
	if (gl_im)
	{
		$('#gl_viwi_img_b').hide();
		$('#gl_viwi_img_b').html(img+ipl);
		$('#gl_viwi_img_b').fadeIn();
		$('#gl_viwi_img_a').fadeOut(400, function() {
			$('#gl_viwi_img_a').html(img);
			$('#gl_viwi_img_a').show();
		} );
		$('#gl_viwi').animate({ width: w, height: h, left: x, top: y });
		$('#gl_viwi_prev').animate({ width: pnw, height: h });
		$('#gl_viwi_next').animate({ width: pnw, height: h });
		$('#gl_viwi_close').animate({ width: (w-pnw*2), height: h });
	}
	else
	{
		$('#gl_viwi_img_a').html(img);
		$('#gl_viwi_img_b').html(img);
		$('#gl_viwi').css({ width: w, height: h, left: x, top: y });
		if (gl_c == 1)
		{
			$('#gl_viwi_prev').css({ width: 0, height: 0 });
			$('#gl_viwi_next').css({ width: 0, height: 0 });
			$('#gl_viwi_close').css({ width: w, height: h });
			$('#gl_viwi_close').css('background-position', '-200px -200px');
		}
		else
		{
			$('#gl_viwi_prev').css({ width: pnw, height: h });
			$('#gl_viwi_next').css({ width: pnw, height: h });
			$('#gl_viwi_close').css({ width: (w-pnw*2), height: h });
			$('#gl_viwi_close').css('background-position', 'center');
		}
	}
	$('#gl_viwi_prev').href = z;
	$('#gl_viwi_next').href = z;
	$('#gl_viwi_close').href = z;
	if (gl_ef) showFader();
	if (gl_es) $('#gl_viwi').addClass('gl_viwi_shadow');
	if (-[1,]) $('#gl_viwi').fadeIn();
	else $('#gl_viwi').show(1);
	gl_im = true;
	return false;
}



function onFaderClick()
{
	if (typeof(viwi_data) != 'undefined') setTimeout("$('#gl_viwi').html(viwi_data)", 1000);
	gl_im = false;
}



function GL_LoadImage(o)
{
	$('#gl_viwi_prev').blur();
	$('#gl_viwi_next').blur();
	var i = gl_id+o;
	if (i == 0) i = gl_c;
	if (i == (gl_c+1)) i = 1;
	eval(gl_si+'(i)');
	return false;
}



function GL_MultiLoad(p)
{
	gl_b = p;
	gl_s = gl_ms[p];
	gl_c = gl_s.length-1;
}



function PB_ShowPage(p)
{
	pb_c = p;
	var s = (p-1)*gl_l+1;
	var e = p*gl_l;
	if (e > gl_c) e = gl_c;
	var b = '\n';
	for (var i=s; i<=e; i++)
	{
		var l = '.'+gl_b+'/'+gl_s[i][0];
		if (gl_e) l = l.substr(0, l.lastIndexOf('.'));
		b += '\t<a href="'+l+'" onclick="return '+gl_si+'('+i+');"><img src="'+gl_d+gl_b+'/'+gl_t+'/'+gl_s[i][0]+'" width="'+gl_w+'" height="'+gl_h+'" alt="'+gl_s[i][1]+'" title="'
			+gl_s[i][1]+'" /></a>\n';
	}
	$('#gl_thumbs_b').hide();
	$('#gl_thumbs_b').html(b);
	$('#gl_thumbs_b').fadeIn();
	$('#gl_thumbs_a').fadeOut(400, function() {
		$('#gl_thumbs_a').html(b);
		$('#gl_thumbs_a').show();
	} );
	$('#pb_next').html((p == pb_t) ? '<span>'+lc_next+'</span>' : '<a href="'+location.pathname+'/page-'+(p+1)+'" onclick="pb_c++; return PB_ShowPage(pb_c);">'+lc_next+'</a>');
	$('#pb_prev').html((p == 1) ? '<span>'+lc_prev+'</span>' : '<a href="'+location.pathname+'/page-'+(p-1)+'" onclick="pb_c--; return PB_ShowPage(pb_c);">'+lc_prev+'</a>');
	var c = '\n';
	for (var i=1; i<=pb_t; i++) c += (p == i) ? '<span class="b">'+i+'</span>\n' : '<a href="'+location.pathname+'/page-'+i+'" onclick="return PB_ShowPage('+i+');">'+i+'</a>\n';
	$('#pb_pages').html(c);
	return false;
}

