//
// ZEN POPUP GALLERY 1.0
// Written by Blain Hosford
// Copyright 2010, ZenWebware
// www.zenwebware.com
//
// REQUIRES zen_library.js AND image.php
//

var popup_gallery = null;


function Popup_Gallery(popup_element_id, popup_horizontal_padding, image_element_id, image_max_height, title_element_id, description_element_id, counter_element_id, shade_element_id) {
	this.popup_element_id = popup_element_id;
	this.popup_horizontal_padding = popup_horizontal_padding;
	this.image_element_id = image_element_id;
	this.image_max_height = image_max_height;
	this.title_element_id = title_element_id;
	this.description_element_id = description_element_id;
	this.counter_element_id = counter_element_id;
	this.shade_element_id = shade_element_id;
	this.images = [];
	this.image_index = 0;
}


function popup_gallery_add_image(url, width, height, title, description) {
	var image = [];
	image['url'] = url;
	image['width'] = width;
	image['height'] = height;
	image['title'] = title;
	image['description'] = description;
	this.images[this.images.length] = image;
}


function popup_gallery_fetch_image_index(url) {
	for(var i = 0; i < this.images.length; i++) {
		if(this.images[i]['url'] == url) {
			return i;
		}
	}
}


function popup_gallery_open(url) {
	popup_gallery = this;
	
	this.image_index = this.fetch_image_index(url);
	var image = this.images[this.image_index];
	
	var aspect_ratio = image['width'] / image['height'];
	if(image['height'] > this.image_max_height) {
		var height = this.image_max_height;
	} else {
		var height = image['height'];
	}
	var width = height * aspect_ratio;

	var shade_element = document.getElementById(this.shade_element_id);
	shade_element.style.width = f_clientWidth() + "px";
	shade_element.style.height = f_clientHeight() + "px";

	var popup_element = document.getElementById(this.popup_element_id);
	popup_element.style.width = width + this.popup_horizontal_padding + "px";
	popup_element.style.left = (f_clientWidth() - width - this.popup_horizontal_padding) / 2;
	
	var image_element = document.getElementById(this.image_element_id);
	image_element.innerHTML = "";
	image_element.style.width = width + "px";
	image_element.style.height = height + "px";

	if(this.title_element_id && image['title']) {
		show_layer(this.title_element_id);
		var title_element = document.getElementById(this.title_element_id);
		title_element.innerHTML = image['title'];
	} else {
		hide_layer(this.title_element_id);
	}

	if(this.description_element_id && image['description']) {
		show_layer(this.description_element_id);
		var description_element = document.getElementById(this.description_element_id);
		description_element.innerHTML = image['description'];
	} else {
		hide_layer(this.description_element_id);
	}
	
	if(this.counter_element_id) {
		show_layer(this.counter_element_id);
		var counter_element = document.getElementById(this.counter_element_id);
		counter_element.innerHTML = (this.image_index + 1) + " of " + this.images.length;
	} else {
		show_layer(this.counter_element_id);
	}
	
	show_layer(this.shade_element_id);
	show_layer(this.popup_element_id);
	load_xml('javascript/zen_popup_gallery_image.php?url=' + url + "&height=" + height, this.image_element_id);
}


function popup_gallery_close() {
	var image_element = document.getElementById(popup_gallery.image_element_id);
	image_element.innerHTML = "";
	hide_layer(popup_gallery.popup_element_id);
	hide_layer(popup_gallery.shade_element_id);
}


function popup_gallery_next() {
	if(popup_gallery.image_index == popup_gallery.images.length - 1) {
		popup_gallery.image_index = 0;
	} else {
		popup_gallery.image_index++;
	}
	popup_gallery.open(popup_gallery.images[popup_gallery.image_index]['url']);
}


function popup_gallery_previous() {
	if(popup_gallery.image_index == 0) {
		popup_gallery.image_index = popup_gallery.images.length - 1;
	} else {
		popup_gallery.image_index--;
	}
	popup_gallery.open(popup_gallery.images[popup_gallery.image_index]['url']);
}


Popup_Gallery.prototype.add_image = popup_gallery_add_image;
Popup_Gallery.prototype.fetch_image_index = popup_gallery_fetch_image_index;
Popup_Gallery.prototype.open = popup_gallery_open;
