function open_close_layer(layerid) {
	$('#' + layerid).toggle();
}


function getPageSize() {
	pw = $(document).width();
	ph = $(document).height();
	ww = $(window).width();
	wh = $(window).height();
	sx = $(document).scrollLeft();
	sy = $(document).scrollTop();
	arrayPageSize = new Array(pw, ph, ww, wh, sx, sy);
	return arrayPageSize;
};

function open_image(src) {
	var imgobj = new Image();
    imgobj.onload = function(){
    	w = this.width;
		h = this.height;
		var arrPageSizes = getPageSize();
		posx = Math.round((arrPageSizes[2]-w)/2);
		if (posx < 0) posx = 0;
		posy = arrPageSizes[5] + Math.round(arrPageSizes[3]/2 - h/2);
		if (posy < 0) posy = 0;
		out = '<img src="' + src + '" width="' + w + '" height="' + h + '" border="1" alt="" id="overlayimage" style="position: absolute; display: none; z-index: 220; left: ' + posx + 'px; top: ' + posy + 'px; " />'; // </div>
		$('body').append('<div id="imgunderlay" class="popupunderlay"></div>');
		$('body').append(out);
		$('#imgunderlay').css({
			width:				arrPageSizes[0],
			height:				arrPageSizes[1]
		}).fadeIn("fast", function() {
			$('#overlayimage').fadeIn("fast");

		});

		$('#imgunderlay').click(function() {
			$('#overlayimage').remove();
			$('#imgunderlay').remove();
		});

		$('#overlayimage').click(function() {
			$('#overlayimage').remove();
			$('#imgunderlay').remove();
		});
	}
	imgobj.src = src;
}


function ajax_send_vars(to, vars, resOk, etc) {
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			resp = xmlhttp.responseText;
			if (etc == "gallery") {
				if (resp != "0") {
					gallery_elements = resp.split(";");
					open_gallery(1);
				} else alert_overlay('Invalid gallery!');
			}
			if (etc == "product_selector") {
				if (resp != "0") $('#productsearch_results').html(resp);
			}
			if (resOk == "1" && resp != "1") alert_overlay(resp);
			return resp;
		}
	}
	xmlhttp.open("POST", to, true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(vars);
}

function product_selector() {
	ev = $('#product_ev').val();
	nem = $('#product_nem').val();
	csoport = $('#product_csoport').val();
	ajax_send_vars("/lib/ajax.php", "func=product_selector&ev=" + ev + "&nem=" + nem + "&csoport=" + csoport, 0, "product_selector");
}


function open_article_gallery(galleryid) {
	ajax_send_vars("/lib/ajax.php", "func=gallery&id=" + galleryid, 0, "gallery");
}


function alert_overlay(msg) {
   	w = 400;
	h = 100;
	var arrPageSizes = getPageSize();
	posx = Math.round((arrPageSizes[2]-w)/2);
	if (posx < 0) posx = 0;
	posy = arrPageSizes[5] + Math.round(arrPageSizes[3]/2 - h/2);
	if (posy < 0) posy = 0;
	out = '<div class="warning" id="overlaywarning" style="position: absolute; display: none; z-index: 250; left: ' + posx + 'px; top: ' + posy + 'px; width: 400px; text-align: center; ">'; // </div>
	out += '<p align="center">' + msg + '</p><p><input type="button" name="" value="OK" onclick="$(\'#popupunderlay\').click();" class="button" /></p></div>';
	$('body').append(out);
	$('#popupunderlay').css({
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn("fast", function() {
		$('#overlaywarning').fadeIn("fast");

	});

	$('#popupunderlay').click(function() {
		$('#overlaywarning').remove();
		$('#popupunderlay').fadeOut("fast");
	});
}






var gallery_elements = Array();
var gallery_descriptions = Array();
var maxImg = 0;
var actImg = 1;
var closebuttontxt = "Bezárás";
function gallery_advance(d) {
	nextImg = actImg;
	maxImg = gallery_elements.length - 1;
	nextImg += d;
	if (nextImg > maxImg) nextImg = 1;
	if (nextImg < 1) nextImg = maxImg;
	if (nextImg != actImg) {
		actImg = nextImg;
		imgdesc = "<br /><br />";
		if (typeof(gallery_descriptions[actImg]) != "undefined") imgdesc = "<br />" + gallery_descriptions[actImg] + "<br />";
		var nextImage = new Image();
		document.getElementById('gallerycontainer').innerHTML = '<table style="width: 100%; height: 100%; "><tr><td align="center"><img src="images/loading.gif" width="32" height="32" border="0" alt="Töltés..." title="Töltés..." /></td></tr></table>';
		// document.getElementById('gallerycontainer').innerHTML = '';
		nextImage.onload = function() {
			var w = this.width;
			var h = this.height;
			var arrPageSizes = getPageSize();
			var posx = Math.abs(Math.round((arrPageSizes[2] - w-80)/2));
			var posy = arrPageSizes[5] + Math.abs(Math.round(arrPageSizes[3]/2 - h/2 - 30));
			$('#gallerycontainer').animate({ width: (w+80), height: (h+50), left: posx, top: posy}, "normal", function(){
				innercontainerstring = '<div align="center"><a href="javascript:gallery_advance(-1)"><img src="images/slide_prev2.png" width="20" height="20" alt="" border="0"  align="absmiddle" /></a> ' +
				'<a href="javascript:gallery_advance(1)"><img src="' + gallery_elements[actImg] + '" width="' + w + '" height="' + h + '" border="0" alt="" align="absmiddle" /></a> ' +
				'<a href="javascript:gallery_advance(1)"><img src="images/slide_next2.png" width="20" height="20" alt="" border="0"  align="absmiddle" /></a>' + imgdesc +
				'<input type="button" class="button" value="' + closebuttontxt + '" onclick="$(\'#galleryunderlay\').click();" /></div>';
				$('#gallerycontainer').html(innercontainerstring);
			});
		}
		nextImage.src = gallery_elements[actImg];
	}
}



function open_gallery() {
	var nextImage = new Image();
	var imgdesc = "<br /><br />";
	if (typeof(gallery_descriptions[actImg]) != "undefined") imgdesc = "<br />" + gallery_descriptions[actImg] + "<br />";
	nextImage.onload = function(){
		var w = this.width;
		var h = this.height;
		var arrPageSizes = getPageSize();
		posx = Math.abs(Math.round((arrPageSizes[2]-w-44)/2));
		posy = arrPageSizes[5] + Math.abs(Math.round(arrPageSizes[3]/2 - h/2 - 30));
		out = '<div id="gallerycontainer" style="position: absolute; left: ' + posx + 'px; top: ' + posy + 'px; width: ' + (w+80) + 'px; height: ' + (h+50) + 'px; background-color: black; z-index: 100; display: none; border: 1px solid gray; padding: 10px; ">';
		out += '</div>'
		$('body').append('<div id="galleryunderlay" class="popupunderlay"></div>');
		$('body').append(out);

		$('#galleryunderlay').css({
			width:				arrPageSizes[0],
			height:				arrPageSizes[1]
		}).fadeIn("fast", function() {
			$('#gallerycontainer').fadeIn("fast", function(){
				innercontainerstring = '<div align="center"><a href="javascript:gallery_advance(-1)"><img src="images/slide_prev2.png" width="20" height="20" alt="" border="0"  align="absmiddle" /></a> ' +
				'<a href="javascript:gallery_advance(1)"><img src="' + gallery_elements[actImg] + '" width="' + w + '" height="' + h + '" border="0" alt="" align="absmiddle" /></a> ' +
				'<a href="javascript:gallery_advance(1)"><img src="images/slide_next2.png" width="20" height="20" alt="" border="0"  align="absmiddle" /></a>' + imgdesc +
				'<input type="button" class="button" value="' + closebuttontxt + '" onclick="$(\'#galleryunderlay\').click();" /></div>';
				$('#gallerycontainer').html(innercontainerstring);
			});
		});

		$('#galleryunderlay').click(function() {
			$('#gallerycontainer').remove();
			$('#galleryunderlay').remove();
			$(document).unbind('keydown');
		});
		$(document).keydown(function(event) {
			if (event.keyCode == 27) {
				$('#gallerycontainer').remove();
				$('#galleryunderlay').remove();
				$(document).unbind('keydown');
			}
		});

	}
	nextImage.src = gallery_elements[actImg];
}

