var CB_Gallery = [];
var current_gallery = '';
var CB_galeriak = [];

$(function() {
	$('.gallery a').each(function(i, e) {
		$(e).click(function() {
			var patt = new RegExp('gallery=([^,\\]]+)');
			current_gallery = patt.exec($(this).attr('rel'))[1];
			for(var x in CB_galeriak) {
				if (CB_galeriak[x].nev == current_gallery) {
					CB_galeriak[x].load();
				}
			}
			CB_Open('gallery=' + current_gallery + ',,href=' + $(this).attr('href'));
		});
	});
});

function Galeria(neve, helye, thumbok)
{
	this.nev = neve;
	this.mappa = helye;
	this.thumbs = thumbok;
	
	this.ajaxScriptPath = 'ajax/';
	this.ajaxScript = 'fromfile.php';
	
	this.data = [];
	
	CB_galeriak.push(this);
	
	this.getPics = function() {
		if (this.data.length == 0) {
			if (typeof(this.mappa) == 'number') {
				this.ajaxScript = 'fromdb.php';
			}
			
			
			if (this.thumbs === undefined) {
				this.thumbs = '';
			}
			
			$.ajax({
				url: this.ajaxScriptPath + this.ajaxScript,
				data: {'mappa': this.mappa, 'thumbs': this.thumbs},
				async: false,
				context: this,
				contentType: 'json',
				success: function(msg) {
					if (typeof(msg) == 'string') {
						alert(msg);
					} else {
						var gyujto = [this.nev];
						$(msg).each(function(i, e) {
							var temp = [e.kep, 'image', e.title];
							temp[5] = e.thumb;
							gyujto.push(temp);
						});
						this.data = gyujto;
					}
				}
			});
		}
	};
	
	this.load = function() {
		this.getPics();
		current_gallery = this.nev;
		CB_Gallery = this.data;
	};
	
	this.addPic = function(kepHelye, thumbHelye) {
		if (this.data.length == 0) {
			this.getPics();
		}
		var ujkep = [kepHelye, 'image', kepHelye];
		if (thumbHelye === undefined) {
			ujkep[5] = kepHelye;
		} else {
			ujkep[5] = thumbHelye;
		}
		this.data.push(ujkep);
	};
	
	this.setAjaxFilePath = function(path) {
		this.ajaxScriptPath = path;
	};
}
