var GALLERY_COMPLETE_WIDTH = 0;
var GALLERY_ORIG_SIZES = {};
var GALLERY_OLD_REL_X = 160;
var GALLERY_REL_X = 160;

var GALLERY_IMAGES = 0;
var GALLERY_CUR_SECTION = 0;
var GALLERY_CUR_IMAGE = 0;
var GALLERY_FIRST_SECTION = 0;

var GALLERY_ZOOM_ACTIVE = 0;
var GALLERY_ZOOM_IMAGE_LOADED = 0;
var GALLERY_ZOOM_X = 0;
var GALLERY_ZOOM_Y = 0;
var GALLERY_ZOOM_X_WINDOW = 0;
var GALLERY_ZOOM_Y_WINDOW = 0;

var GALLERY_IMAGE_LOADED = 0;

var GALLERY_FADE_DELAY = 700;

function gallery_absolutepos(el)
{
	var x = 1, y = 1;
	do {
		x += el.offsetLeft;
		y += el.offsetTop;
	} while (el = el.offsetParent);

	return {x:x, y:y};
}

function gallery_mousemove(event)
{
	var abspos = gallery_absolutepos(document.getElementById('gallery_images'));

	GALLERY_REL_X = event.pageX-abspos.x;
}

function gallery_anim()
{
	var rel_x = Math.round((9*GALLERY_OLD_REL_X+GALLERY_REL_X)/10.0);

	GALLERY_OLD_REL_X = rel_x;

	var container = document.getElementById('gallery_images');
	var scroller = document.getElementById('gallery_images_scroller');

	var mouse_perc = (rel_x / container.offsetWidth);

	var l = -Math.round(((GALLERY_COMPLETE_WIDTH+600) - (container.offsetWidth))*mouse_perc) + 200;

	scroller.style.marginLeft = l + 'px';

	var divs = document.getElementsByTagName('div');
	for (var a=0; a<divs.length; a++) {
		if (divs[a].className == 'gallery_image') {
			var jqimg = $(divs[a]);

			var pos = divs[a].offsetLeft - container.offsetLeft;
			var opac = 1.0;

			if (pos < 100) {
				opac = (pos/100);
				if (opac < 0.0) opac = 0.0;
			}
			if (pos + divs[a].offsetWidth > container.offsetWidth - 100) {
				opac = (container.offsetWidth-(pos+divs[a].offsetWidth))/100;
				if (opac < 0.0) opac = 0.0;
			}

			jqimg.css('opacity', opac);
		}
	}

	window.setTimeout('gallery_anim();', 20);
}

function gallery_init()
{
	$('#gallery_images').mousemove(gallery_mousemove);

	gallery_anim();
}

function gallery_imgs_loaded()
{
	var imgs = document.getElementsByTagName('img');
	for (var a=0; a<imgs.length; a++) {
		if (imgs[a].className == 'gallery_img_img') {
			var jqimg = $('#'+imgs[a].id);

			if (0) {
				GALLERY_ORIG_SIZES[imgs[a].id] = [imgs[a].width, imgs[a].height];

				jqimg.width(Math.round(jqimg.width()*0.8));
				jqimg.height(Math.round(jqimg.height()*0.8));
			}

			jqimg.css('opacity', 0);
			jqimg.css('visibility', 'visible');
			jqimg.delay(a*120).animate({opacity:1});
		}
	}
}

function gallery_img_hover(inout, num)
{
	return;

	if (inout) {
		$('#gallery_img_'+num).animate({
			width: GALLERY_ORIG_SIZES['gallery_img_'+num][0],
			height: GALLERY_ORIG_SIZES['gallery_img_'+num][1],
		}, 300);
	}
	else {
		$('#gallery_img_'+num).animate({
			width: GALLERY_ORIG_SIZES['gallery_img_'+num][0]*0.8,
			height: GALLERY_ORIG_SIZES['gallery_img_'+num][1]*0.8,
		}, 300);
	}
}


function gallery_image_loaded()
{
	GALLERY_IMAGE_LOADED = 1;
	$(document).mousemove(gallery_zoom_update_pos);

	var fn = document.getElementById('carousel_img_'+GALLERY_CUR_SECTION).src.replace('/thumb/', '/.');

//	document.getElementById('gallery_zoom_img_wrapper').innerHTML = '\
//		<img src="' + document.getElementById('carousel_img_'+GALLERY_CUR_SECTION).src.replace('/thumb/', '/.') + '" alt="" id="gallery_zoom_img">\
//	';

	if (IDEVICE) {
		var fni = document.getElementById('carousel_img_'+GALLERY_CUR_SECTION).src.replace('/thumb/', '/thumb/idevice-');
		document.getElementById('fullscreen_button').innerHTML = '\
			<div id="fullscreen_button_idevice">\
				<a href="/idevice_viewer.php?img='+escape(fni).replace(/\+/g, '%2B').replace(/\//g, '%2F')+'" target="_blank">\
					'+FULLSCREEN_BUTTON_LABEL+'\
				</a>\
			</div>\
		';
	}
	else {
		var scale = CAROUSEL.sizes_zoom[GALLERY_CUR_IMAGE-1];

		document.getElementById('fullscreen_button').innerHTML = '\
			<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="115" height="30" id="Fullscreen-Viewer" align="middle">\
			<param name="allowScriptAccess" value="sameDomain" />\
			<param name="allowFullScreen" value="true" />\
			<param name="movie" value="/Fullscreen-Viewer.swf?label='+escape(FULLSCREEN_BUTTON_LABEL)+'&amp;img='+escape(fn).replace(/\+/g, '%2B').replace(/\//g, '%2F')+'&amp;scaleyo='+scale+'" /><param name="quality" value="best" /><param name="bgcolor" value="#333333" />	<embed src="/Fullscreen-Viewer.swf?label='+escape(FULLSCREEN_BUTTON_LABEL)+'&amp;img='+escape(fn).replace(/\+/g, '%2B').replace(/\//g, '%2F')+'&amp;scaleyo='+scale+'" quality="best" bgcolor="#333333" width="115" height="30" name="Fullscreen-Viewer" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
			</object>\
		';
	}

	$('#fullscreen_button').fadeIn();

}

function gallery_check_loaded()
{
	if (document.getElementById('gallery_big_image') && document.getElementById('gallery_big_image').complete) {
		window.clearInterval(GALLERY_LOAD_INT);

		$('h1').hide();

		document.getElementById('gallery_cur_img_number').innerHTML = GALLERY_CUR_IMAGE;

		var custom_y = 0;
		if (parseInt(CAROUSEL.yposes_detail[GALLERY_CUR_IMAGE-1])) custom_y = parseInt(CAROUSEL.yposes_detail[GALLERY_CUR_IMAGE-1]);

		document.getElementById('gallery_big_image_wrapper').style.bottom = (-30+custom_y)+'px';

		$('#gallery_big_image').delay((IE8 ? 0 : GALLERY_FADE_DELAY)).fadeIn((IE8 ? 0 : GALLERY_FADE_DELAY/2), gallery_image_loaded);
		$('#h2_'+GALLERY_CUR_SECTION).delay(900).fadeIn(GALLERY_FADE_DELAY);
		$('#text_'+GALLERY_CUR_SECTION).delay(1100).fadeIn(GALLERY_FADE_DELAY);

		$('#gallery_nav').delay(1300).fadeIn(GALLERY_FADE_DELAY);
	}
}

var GALLERY_LOAD_INT;
var GALLERY_ZOOM_INT;
var GALLERY_STOPPED = 0;
function gallery_click(e, sn)
{
	GALLERY_CUR_SECTION = sn;
	GALLERY_CUR_IMAGE = sn - (GALLERY_FIRST_SECTION-1);

	if (document.getElementById('further_info')) {
		$('#further_info').fadeOut(GALLERY_FADE_DELAY);
	}

	var w = window.screen.availWidth;
	var h = window.screen.availHeight;

	document.getElementById('gallery_big_image_wrapper').innerHTML = '\
		<img src="'+e.src.replace('/thumb/', '/')+'" alt="" style="display: none" id="gallery_big_image">\
	';

	$('#galerie_intro').delay(300).fadeOut(GALLERY_FADE_DELAY);

	$('#fullscreen_button').fadeOut();

	GALLERY_STOPPED = 1;

	$('.carousel_img').fadeOut((IE8 ? 0 : GALLERY_FADE_DELAY), function()
	{
		document.getElementById('carousel').className = '';
	});

	GALLERY_LOAD_INT = window.setInterval(gallery_check_loaded, 500);

	// Zoom now in popup
//	GALLERY_ZOOM_INT = window.setInterval(gallery_zoom, 30);
}

function gallery_click_flash(name, sn)
{
	sn+=2;

	if (document.getElementById('further_info')) {
		$('#further_info').fadeOut(GALLERY_FADE_DELAY);
	}

	var e = document.getElementById('carousel_img_'+sn);
	var l = e.parentNode.href;
	if (typeof l != 'undefined') {
		location.href = l;
		return;
	}

	GALLERY_CUR_SECTION = sn;
	GALLERY_CUR_IMAGE = sn - (GALLERY_FIRST_SECTION-1);

	document.getElementById('carousel_flash').style.display = 'none';

	var w = window.screen.availWidth;
	var h = window.screen.availHeight;

	document.getElementById('gallery_big_image_wrapper').innerHTML = '\
		<img src="'+name+'" alt="" style="display: none" id="gallery_big_image">\
	';

	$('#galerie_intro').delay(300).fadeOut(GALLERY_FADE_DELAY);

	$('#fullscreen_button').fadeOut();

	GALLERY_STOPPED = 1;

	$('.carousel_img').fadeOut((IE8 ? 0 : GALLERY_FADE_DELAY), function()
	{
		document.getElementById('carousel').className = '';
	});

	GALLERY_LOAD_INT = window.setInterval(gallery_check_loaded, 500);

	// Zoom now in popup
//	GALLERY_ZOOM_INT = window.setInterval(gallery_zoom, 30);
}

function gallery_zoom_update_pos(e)
{
	var pos = gallery_absolutepos(document.getElementById('gallery_big_image'));

	// IE somehow calculates wrong offset, not feeling like fixing it 'the right way'...
	if (navigator.userAgent.indexOf('MSIE')!=-1) {
		pos.y += 500;
	}

	GALLERY_ZOOM_X = e.pageX - pos.x;
	GALLERY_ZOOM_Y = e.pageY - pos.y;
	GALLERY_ZOOM_X_WINDOW = e.pageX;
	GALLERY_ZOOM_Y_WINDOW = e.pageY;
}

function gallery_zoom()
{
	if (!GALLERY_IMAGE_LOADED) return;

	var e = document.getElementById('gallery_zoom');
	var bi = document.getElementById('gallery_big_image');
	var zi = document.getElementById('gallery_zoom_img');

	if (
		((!GALLERY_ZOOM_ACTIVE) &&
		 GALLERY_ZOOM_X > 0 && GALLERY_ZOOM_Y > 0 &&
		 GALLERY_ZOOM_X < bi.width && GALLERY_ZOOM_Y < bi.height - 130)
	) {
		GALLERY_ZOOM_ACTIVE = 1;
		$('#gallery_zoom').fadeIn(300);
	}
	else if (
		(GALLERY_ZOOM_ACTIVE &&
		 GALLERY_ZOOM_X < 0 || GALLERY_ZOOM_Y < 0 ||
		 GALLERY_ZOOM_X > bi.width || GALLERY_ZOOM_Y > bi.height - 130)
	) {
		GALLERY_ZOOM_ACTIVE = 0;
		$('#gallery_zoom').fadeOut(300);
	}

	if (zi.width && zi.complete) {
		if (!GALLERY_ZOOM_IMAGE_LOADED) {
			GALLERY_ZOOM_IMAGE_LOADED = 1;
			$('#gallery_zoom_loading').delay(250).fadeOut();
		}
		else {
			$('#gallery_zoom_loading').hide();
		}
	}
	else GALLERY_ZOOM_IMAGE_LOADED = 0;

	e.style.top = (GALLERY_ZOOM_Y_WINDOW - 100) + 'px';
	e.style.left = (GALLERY_ZOOM_X_WINDOW - 100) + 'px';

	var zoomfac = zi.width / bi.width;

	zi.style.marginTop = Math.ceil(-(GALLERY_ZOOM_Y-50)*zoomfac) + 'px';
	zi.style.marginLeft = Math.ceil(-(GALLERY_ZOOM_X-50)*zoomfac) + 'px';
}

function gallery_mousepos(e)
{
	GALLERY_ZOOM_X = e.pageX - document.getElementById('gallery_big_image').offsetLeft;
	GALLERY_ZOOM_Y = e.pageY - document.getElementById('gallery_big_image').offsetTop;
}

function news_next(dir)
{
	$('#news_'+NEWS_CUR).fadeOut(500);
	NEWS_CUR += dir;
	if (NEWS_CUR < 1) NEWS_CUR = NEWS_TOTAL;
	if (NEWS_CUR > NEWS_TOTAL) NEWS_CUR = 1;

	$('#news_'+NEWS_CUR).delay(600).fadeIn();
	document.getElementById('gallery_cur_img_number').innerHTML = NEWS_CUR;
}

function gallery_next(dir)
{
	// Abused for news switching also
	if (document.getElementById('news_1')) {
		news_next(dir);
		return;
	}

	if (!GALLERY_IMAGE_LOADED) return;

	GALLERY_IMAGE_LOADED = 0;
	GALLERY_ZOOM_IMAGE_LOADED = 0;
	$('#gallery_zoom_loading').show();

	$(document).unbind('mousemove', gallery_zoom_update_pos);

	$('#h2_'+GALLERY_CUR_SECTION).fadeOut(GALLERY_FADE_DELAY);
	$('#text_'+GALLERY_CUR_SECTION).fadeOut(GALLERY_FADE_DELAY);
	$('#gallery_big_image').fadeOut((IE8 ? 0 : GALLERY_FADE_DELAY), function() {

		GALLERY_CUR_SECTION += dir;
		GALLERY_CUR_IMAGE += dir;

		if (GALLERY_CUR_IMAGE > GALLERY_IMAGES) {
			GALLERY_CUR_IMAGE = 1;
			GALLERY_CUR_SECTION = 2;
		}
		else if (GALLERY_CUR_IMAGE < 1) {
			GALLERY_CUR_IMAGE = GALLERY_IMAGES;
			GALLERY_CUR_SECTION = GALLERY_IMAGES+1;
		}

		var e = document.getElementById('gallery_big_image');

		var w = window.screen.availWidth;
		var h = window.screen.availHeight;

		document.getElementById('gallery_big_image_wrapper').innerHTML = '\
			<img src="'+document.getElementById('carousel_img_'+GALLERY_CUR_SECTION).src.replace('/thumb/', '/')+'" alt="" style="display: none" id="gallery_big_image">\
		';

		document.getElementById('gallery_zoom_img_wrapper').innerHTML = '';

		GALLERY_LOAD_INT = window.setInterval(gallery_check_loaded, 100);
	});
}

