var photos;
var active = 0;
var previous_active = 0;

function addPhotos(photos) {
	this.photos = photos;
}

function setActive(active) {

	if(active > 0 && active <= photos.length) {
		previous_active = this.active;
		this.active = active;

		// Disable previous button when active = 1
		if(active == 1) {
			button = document.getElementById('previous_button');
			button.className = "disabled";
		}

		// Disable next button when active = photos.length
		if(active == photos.length) {
			button = document.getElementById('next_button');
			button.className = "disabled";
		}

		// Enable previous button when active != 1
		if(previous_active == 1) {
			if(active != 1) {
				button = document.getElementById('previous_button');
				button.className = "";
			}
		}

		// Enable next button when active != photos.length
		if(previous_active == photos.length) {
			if(active != photos.length) {
				button = document.getElementById('next_button');
				button.className = "";
			}
		}

		return true;
	}

	return false;
}

function change(picture, el) {

	change_back(el);

	document.big_photo.src = picture;

	if (el){
		if(el.className.indexOf("active") == -1) {
			el.className += " active";
		}
	}

	// Check which one is active
	if (picture){
		for(var i = 0; i < photos.length; i++) {

			if(picture.indexOf(photos[i]) != -1) {

				setActive(i+1);
			}
		}
	}
}

function change_back(el) {

	var containerDiv = document.getElementById("photos_small");

	if (containerDiv){
		var images = containerDiv.getElementsByTagName('img');

		if (images){
			for(var i = 0; i < images.length; i++) {
				images[i].className = images[i].className.replace(/active/, "");
			}
		}
	}

}


function next() {

	if(setActive((this.active - 0) + 1)) {
		var offset = (window.event) ? -1 : 1;
		var children = document.getElementById('photos_small').getElementsByTagName("img");

		el = children[this.active - 1];
		change(photos[this.active - 1], el);
	}

}

function previous() {

	if(setActive((this.active - 0) - 1)) {
		var offset = (window.event) ? -1 : 1;
		var children = document.getElementById('photos_small').getElementsByTagName("img");

		el = children[this.active - 1];
		change(photos[this.active - 1], el);
	}

}

function start(photoAlbumID) {

	xajax_updatePhotos(photoAlbumID, 1);
	combo_select_value(document.getElementById('selectbox'),photoAlbumID);

}

function combo_select_value(c, val){
	if (c){
		var ops = c.getElementsByTagName("option");
		for (var i = ops.length; --i >= 0;) {
			var op = ops[i];
			op.selected = (op.value == val);
		}
		c.value = val;
	}
}
