
function $(id) {
	
	return document.getElementById(id);
	
}

function Animation() {
	
	this.id = null;
	
	this.objects = Array();
	
	this.status = 0;
	
	this.opacityCurrent = 0;
	this.opacityStep = 10;
	this.opacityMax = 100;
	this.opacityMin = 0;
	
	this.delay = 50;
	
}

Animation.prototype.init = function(id, objects) {
	
	this.id = id;
	
	this.objects = Array();
	
	objects = objects.split(',');
	
	for (var i = 0; i < objects.length; i += 1) {
		var object = $(objects[i]);
		if (object == null) {
			continue;
		}
		this.objects.push(object);
	}
	
}

Animation.prototype.filterObjects = function() {
	
	for (var i = 0; i < this.objects.length; i += 1) {
		if (this.objects[i].runtimeStyle) {
			this.objects[i].runtimeStyle.filter = 'alpha(opacity=' + this.opacityCurrent + ')';												
		}
		else {
			this.objects[i].style.opacity = this.opacityCurrent / 100;
		}
		this.objects[i].style.visibility = this.opacityCurrent > 0 ? 'visible' : 'hidden';
	}
	
}

Animation.prototype.stop = function() {
	
	clearTimeout(this.timeout);
	
	this.status = 0;
	
}

Animation.prototype.play = function() {
	
	clearTimeout(this.timeout);
	
	if (this.status == -1) {
		return;
	}
	
	this.status = 1;
	
	this.opacityCurrent += this.opacityStep;
	
	if (this.opacityCurrent > this.opacityMax) {
		this.opacityCurrent = this.opacityMax;
		this.stop();
		return;
	}
	
	this.filterObjects();
	
	this.timeout = setTimeout("dd('" + this.id + "', '')", this.delay);
	
}

Animation.prototype.back = function() {
	
	clearTimeout(this.timeout);
	
	if (this.status == 1) {
		return;
	}
	
	this.status = -1;
	
	this.opacityCurrent -= this.opacityStep;
	
	if (this.opacityCurrent < this.opacityMin) {
		this.opacityCurrent = this.opacityMin;
		this.stop();
		return;
	}
	
	this.filterObjects();
	
	this.timeout = setTimeout("db('" + this.id + "', '')", this.delay);
	
}

var animation = new Animation();

var timeout = null;

function dd(id, objects) {
	
	if (animation.id == null) {
		animation.init(id, objects);
		animation.play();
	}
	else {
		if (animation.id == id) {
			animation.play();
		}
		else {
			if (animation.status == 0) {
				animation.init(id, objects);
				animation.play();
			}
			else {
				animation.back();
				timeout = setTimeout("dd('" + id + "', '" + objects + "')", animation.delay);
			}
		}
	}
	
}

function db(id, objects) {
	
	if (animation.id == null) {
		animation.init(id, objects);
		animation.back();
	}
	else {
		if (animation.id == id) {
			animation.back();
		}
		else {
			if (animation.status == 0) {
				animation.init(id, objects);
				animation.back();
			}
			else {
				animation.play();
				timeout = setTimeout("db('" + id + "', '" + objects + "')", animation.delay);
			}
		}
	}
	
}

function window_open(url, win_width, win_height) {
	
	var screen_width = window.screen.width;
	var screen_height = window.screen.height;
	
	var win_left = screen_width / 2 - win_width / 2;
	var win_top = screen_height / 2 - win_height / 2;
	
	var win = window.open(url, "popup", "width=" + win_width + ", height=" + win_height + ", scrollbars=1, location=0, menubar=0, resizable=0, status=0, titlebar=0, toolbar=0, screenX=100, left=" + win_left + ", screenY=30, top=" + win_top);
 	
	win.focus();
	
}

function popupWindow(w, h) {
	
	var sw = window.screen.width;
	var sh = window.screen.height;
	
	var l = sw / 2 - w / 2;
	var t = sh / 2 - h / 2;
	
	window.resizeTo(w, h);
	window.moveTo(l, t);
	
}

var curid = 0;

function selectImage(id) {
	if (curid == id) {
		return;
	}
	var img = document.getElementById('images[' + id + ']');
	if (img) {
		img.className = 'image_block';
	}
	var img = document.getElementById('images[' + curid + ']');
	if (img) {
		img.className = 'image_none';
	}
	curid = id;
}

function configureSubmit() {
	
	var action = '';
	
	var args = configureSubmit.arguments;
	
	for (var i = 1; i < args.length; i += 1) {
		
		var elems = document.getElementsByName(args[i]);
		var values = Array();
		
		for (var j = 0; j < elems.length; j += 1) {
			if (elems[j].type == "checkbox" && elems[j].checked) {
				values[j] = elems[j].value;
			}
			else if (elems[j].type == "text") {
				values[j] = elems[j].value;
			}
			else if (elems[j].type == "select-one") {
				values[j] = elems[j].value;
			}
		}
		
		var value = values.join(";");
		
		if (value.length >= elems.length) {
			action += args[i] + "/" + value + "/";
		}
		
	}
	
	window.location = args[0] + action;
	
	return false;

}

function changeImage(i) {
	
	var txtElem = document.getElementById('ctxt');
	
	if (txtElem == null) {
		return;
	}
	
	var imgElem = document.getElementById('cimg');
	
	if (imgElem == null) {
		return;
	}
	
	var infElem = document.getElementById('itxt');
	
	if (infElem != null) {
		infElem.style.display = 'block';
		imgElem.className = 'smooth';
	}
	
	var imgObj = new Image();
	imgObj.src = photoSrc[i];
	
	imgObj.onload = function() {
		imgElem.src = this.src;
		imgElem.className = '';
		if (infElem != null) {
			infElem.style.display = 'none';
		}
	};
	
	txtElem.innerHTML = photoTxt[i];
	
}
