/**
 * Klasa obsługująca pojedyńczą galerię.
 * @param _id
 * @param _title
 * @param _icon
 * @param _position
 * @param _orientation
 * @returns {Gallery}
 */
function Gallery(_id, _title, _icon, _position, _orientation) {
	this.id = _id;
	this.title = _title;
	this.icon = _icon;
	this.position = _position;
        this.orientation = _orientation;
	this.list = new Array();
	this.listMovie = new Array();
	this.photoContainer = null;
}

Gallery.prototype.setContainer = function(photoContainer) {
	this.photoContainer = photoContainer;
};

/**
 * Odrysowuje box galerii wraz ze zdjęciem startowym.
 */
Gallery.prototype.draw = function(parentContainer) {
	var gallery = document.createElement("div");
	gallery.id = "gallery_" + this.id;
	gallery.onclick = this.show;
	gallery.className = "galleryBox";
	gallery.onmouseover = this.showCover;
	gallery.onmouseout = this.hideCover;
	var gallery_title = document.createElement("div");
	gallery_title.id = gallery.id + '_cover';
	gallery_title.className = "galleryBoxCoverHidden";
	gallery_title.innerHTML = this.title;
	gallery.appendChild(gallery_title);
	if (this.icon != null) {
		var gallery_icon = document.createElement("img")
		gallery_icon.width = "210";
		gallery_icon.height = "210";
		gallery_icon.className = "galleryImageHidden";
		gallery_icon.onload = function() { this.className = "galleryImage"; };
		gallery_icon.src = this.icon;
		gallery_icon.title = this.title;
		gallery.appendChild(gallery_icon);
	}
	if (parentContainer != null) {
		parentContainer.appendChild(gallery);
	}
};

Gallery.prototype.drawMenu = function(menuContainer, selectedItem) {
	var item = document.createElement("div");
	item.id = "menu_gallery_" + this.id;
	item.className = "menuItem";
	if (selectedItem == this.id) {
		item.className = "menuItemSelected";
	}
	item.onclick = this.show;
	item.innerHTML = this.title;
	if (menuContainer) {
		menuContainer.appendChild(item);
	}
};

/**
 * Rysuje zdjęcia galerii.
 */
Gallery.prototype.drawPhotos = function() {
	if (this.photoContainer) {
		for (var i=0;i<this.list.length;i++) {
			var photo = this.list[i];
			photo.draw(this.photoContainer);
		}
	}
};

/**
 * Ryzuje film galerii.
 */
Gallery.prototype.drawMovies = function() {
	if (this.photoContainer) {
		this.listMovie[0].draw(this.photoContainer);
	}
}

/**
 * Wyświetla zawartość galerii (zdjęcia).
 */
Gallery.prototype.show = function() {
	var regex = /gallery_(\d+)/;
	if (!this.id || !this.id.match(regex)) return;
	var match = regex.exec(this.id);
	var gallery = Galleries.get(match[1]);
	gallery.removeContent();
	$.post(templatePath + "servlet.php", {action : "getGalleryPhotos", parent : gallery.id}, gallery.setPhotos);
	$.post(templatePath + "servlet.php", {action : "getGalleryMovies", parent : gallery.id}, gallery.setMovies);
}

/**
 * Dodaje zdjęcie do list wszystkich zdjęć galerii.
 */
Gallery.prototype.addPhoto = function(photo) {
	this.list[this.list.length] = photo;
};

/**
 * Dodaje film do listy filmów
 */
Gallery.prototype.addMovie = function(movie) {
	this.listMovie[this.listMovie.length] = movie;
}

/**
 * Usuwa wszystkie zdjęcia z listy zdjęć galerii
 */
Gallery.prototype.removeContent = function() {
	this.list = new Array();
	while(this.photoContainer.childNodes.length > 0) {
		this.photoContainer.removeChild(this.photoContainer.childNodes[0]);
	}
        if (this.orientation == "horizontal") {
            this.photoContainer.style.width = '0px';
        } else {
            this.photoContainer.style.height = '0px';
        }
};

Gallery.prototype.changePhotos = function() {
	this.drawPhotos();
	$('.galleryImages a').lightBox();
	Galleries.drawMenu(this.id);
},

Gallery.prototype.changeMovies = function() {
	this.drawMovies();
	/* więcej akcji */
},

/**
 * Uzupełnia obrazki znajdujące się w galerii.
 */
Gallery.prototype.setPhotos = function(galleryJSON) {
	var photos = eval(galleryJSON);
	if (!photos) return;
	var gallery = null;
	for (var i=0;i<photos.length;i++) {
		var params = photos[i];
		var paramString = Toolbox.escapeParams(params);
		var photo = eval("new Photo(" + paramString + ");");
		if (!gallery) {
			gallery = Galleries.get(photo.getParent());
		}
		if (gallery) {
			gallery.addPhoto(photo);
		}
	}
	if (gallery) {
		gallery.changePhotos();
	}
};

/**
 * Ustawa video w galerii.
 */
Gallery.prototype.setMovies = function(moviesJSON) {
	var movies = eval(moviesJSON);
	if (!movies) return;
	var gallery = null;
	for (var i=0;i<movies.length;i++) {
		var params = movies[i];
		var paramString = Toolbox.escapeParams(params);
		var movie = eval("new Movie(" + paramString + ");");
		if (!gallery) {
			gallery = Galleries.get(movie.getParent());
		}
		if (gallery) {
			gallery.addMovie(movie);
		}
	}
	if (gallery) {
		gallery.changeMovies();
	}
}

/**
 * Zwraca id galerii.
 */
Gallery.prototype.getId = function() {
	return this.id;
};

/**
 * Wyświetla przesłonę galerii.
 */
Gallery.prototype.showCover = function() {
	if (!this || !this.id) return;
	var cover = document.getElementById(this.id + "_cover");
	if (!cover) return;
	cover.className = "galleryBoxCoverVisible";
};

/**
 * Ukrywa przesłonę galerii.
 */
Gallery.prototype.hideCover = function() {
	if (!this || !this.id) return;
	var cover = document.getElementById(this.id + "_cover");
	if (!cover) return;
	cover.className = "galleryBoxCoverHidden";
};
