var cur = 0;
var _total = 1;
var sl_paused = false;
var _width, _height;
var _onlyOne;

getWH = function(photo_width, photo_height) {
	var width, ratio, height;

	width = 500;
	ratio = 500 / photo_width;
	height = photo_height * ratio;
	if(height > 400) {
		height = 400;
		ratio = 400 / photo_height;
		width = photo_width * ratio;
	}
	width = Math.floor(width);
	height = Math.floor(height);

	return [width, height];
}

sl_update = function(_path, _caption, _position, _total) {
	var i = document.getElementById("slide_img");
	i.onload = function() {
		var wh = getWH(_width, _height);
		this.style.width = wh[0] + "px";
		this.style.height = wh[1] + "px";
		notify_loading(0);
		document.getElementById("slide_caption").innerHTML = _caption;
		document.getElementById("slide_position").innerHTML = _position;
		document.getElementById("slide_total").innerHTML = _total;

		_delay = document.getElementById("slide_delay").value;
		_delay = (_delay >= 1000) ? _delay : 1000;

		document.getElementById("sl_progress_bar").style.width = (_position * 100 / _total) + "px";

		if(_onlyOne != true)
			window.setTimeout(sl_next, _delay);
		_onlyOne = false;
	}
	i.onerror = function() {alert("error" + this.src)};
	i.src = _path;
}

notify_loading = function(status_loading) {
	var st_l = document.getElementById("msg_loading");
	var st_t = document.getElementById("msg_loading_txt");
	switch(status_loading) {
		case 0:
			st_l.style.visibility = "hidden";
			break;

		case 1:
			st_t.innerHTML = "Loading...";
			st_l.style.visibility = "visible";
			break;

		case 2:
			st_t.innerHTML = "Processing...";
			st_l.style.visibility = "visible";
			break;

	}
}

sl_getNodeValue = function(obj, _tag) {
	var t = obj.getElementsByTagName(_tag);
	return t[0].childNodes[0].nodeValue;
}

sl_process = function(msg, obj) {
	switch(msg) {
		case "loading":
			notify_loading(1);
			return;

		case "success":
			notify_loading(2);
			var x = obj.cn.responseXML;

			_path = sl_getNodeValue(x, "src");
			_caption = sl_getNodeValue(x, "name");
			_position = sl_getNodeValue(x, "pos");
			_total = sl_getNodeValue(x, "total");
			_width = sl_getNodeValue(x, "width");
			_height = sl_getNodeValue(x, "height");

			sl_update(_path, _caption, _position, _total);
			break;

		case "error":
			break;

		case "browserNotEnabled":
			break;
	}
	notify_loading();
}

sl_go = function(d, oo) {
	if(sl_paused && !oo)
		return;

	_onlyOne = oo;
	var _cur = cur + d;

	if(_cur < _total && _cur > -1) {
		cur = _cur;
		var u = "egallery/sl_gallery.php?cur=" + cur + "&album=" + album;
		var cn = new httpConnection(u, sl_process);
		cn.getUrl();
	}
}
sl_next = function() {
	sl_go(1);
}
sl_prev = function() {
	sl_go(-1);
}
sl_next1 = function() {
	sl_paused = true;
	sl_go(1, true);
}
sl_prev1 = function() {
	sl_paused = true;
	sl_go(-1, true);
}
sl_pause = function() {
	sl_paused = true;
}
sl_play = function() {
	sl_paused = false;
	sl_go(1);
}
