var devel = 1;
var ticker;
var ajax = {
	queue:[],
	busy:0,

	req:function(q) {
		this.queue.push(q);
		if (this.busy == 0) this.get();
	},

	get:function() {
		var URL = 'lib/ajaxData.php';
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (self.xmlHttpReq != null) {
			self.xmlHttpReq.open('POST', URL, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() {
				if (self.xmlHttpReq.readyState == 4) {
					ret = self.xmlHttpReq.responseText;
					var index = ret.indexOf('\n');
					var div =  ret.substring(4, index);
					if (ret.substr(0, 3) == "div") {
						document.getElementById(div).innerHTML = ret.substr(index);
					} else if (ret.substr(0, 4) == "eval") {
						eval(ret.substr(4));
					}
					if (ajax.queue.length > 0) ajax.get();
					else ajax.busy = 0;
				}
			}
			self.xmlHttpReq.send(this.queue.pop());
			ajax.busy = 1;
		} else {
			document.getElementById('page').innerHTML = "ERROR: Your broser doesn't support Ajax.";
		}
	}
}

var gal = {
	samm:3,
	delta:0,
	kaugus:0,

	click:function(e) {
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		// vahetame foto ara
		document.getElementById('photo').src = targ.id;
		// kerimise osa
		var thumbs = document.getElementById('thumbline');
		var off = targ.offsetLeft - thumbs.scrollLeft;
		if (off < 200) this.keri(thumbs, 0, targ.width);
		else if (off > 500) this.keri(thumbs, 1, targ.width);
	},

	keri:function(obj, suund, width) {
			clearInterval(ticker);
			this.delta = 30;
			//this.kaugus = obj.scrollLeft;
		if (suund == 1) {
			//obj.scrollLeft = obj.scrollLeft + width;
			ticker = setInterval("gal.keriSujuv(1);", 50);
		} else if (suund == 0) {
			//obj.scrollLeft = obj.scrollLeft - width;
			ticker = setInterval("gal.keriSujuv(0);", 50);
		}
	},

	keriSujuv:function(suund) {
		if (this.delta <= this.samm) clearInterval(ticker);
		else {
			var obj = document.getElementById('thumbline');
			this.delta -= this.samm;
			if (suund == 1) obj.scrollLeft = obj.scrollLeft + this.delta;
			else if (suund == 0) obj.scrollLeft = obj.scrollLeft - this.delta;
		}
	}

}

// slideshow avalehel
var ss = {
	opacity:1,
	p:"",

	init:function() {
		setTimeout("ajax.req('q=nextimage')", 5000);
	},

	nextimage:function(photo) {
        	//var p =  document.getElementById('home_image');
		this.p = document.getElementById('home_image');
	        if (this.p) {
	                this.p.src = photo;
			/*
			this.p.style.display = 'none';
			this.opacity = 1;
			this.fadein();
			*/
	                setTimeout("ajax.req('q=nextimage')", 5000);
	        }
	},

	fadein:function() {
                if (this.opacity < 99 && this.p && this.p.style) {
			this.p.style.display = '';
                        this.opacity += 5;
                        this.p.style.MozOpacity = this.opacity / 30;
                        this.p.style.filter = 'alpha(opacity=' + this.opacity + ')';
                        setTimeout("ss.fadein()", 50);
		}                
	}
}

/*
var slideshow = {
	images:[],
	thumbs:[],
	thumb_width:[],
	delay:3000,
	slide:0,
	timeout:0,
	photo_div:"",
	thumb_div:"",
	opacity:1,

	init:function() {
		// nullime vanad asjad ara
		this.images = [];
		this.thumbs = [];
		this.slide = 0;
		this.photo_div = document.getElementById('photo');
		this.thumb_div = document.getElementById('thumbline');
		this.thumb_div.scrollLeft = 0;
		clearTimeout(this.timeout);

		for (x = 0; x < document.images.length; x++) {
			if (document.images[x].id.substr(0, 7) == "photos/") {
				this.images.push(document.images[x].id);
				this.thumbs.push(document.images[x].src);
				this.thumbs.push(document.images[x].width);
			}
		}
		this.run();
	},

	run:function() {
		this.timeout = setTimeout("slideshow.change()", this.delay);
	},

	change:function() {
		if (this.slide == this.images.length - 1) {
		} else {
			this.slide++;
			this.photo_div.src = this.images[this.slide];
			this.opacity = 1;
			this.fadein();
			gal.keri(this.thumb_div, 1, this.thumb_width[x] - 20);
			this.timeout = setTimeout("slideshow.change()", this.delay);
		}
	},

	fadein:function() {
		//if (this.opacity < 0.99 && this.photo_div && this.photo_div.style && typeof this.photo_div.style.MozOpacity != 'undefined') {
		if (this.opacity < 99 && this.photo_div && this.photo_div.style) {
			this.opacity += 5;
			this.photo_div.style.MozOpacity = this.opacity / 30;
			this.photo_div.style.filter = 'alpha(opacity=' + this.opacity + ')';
			setTimeout("slideshow.fadein()", 50);
		}
	}
}
*/

function init() {
	ajax.req('q=mm&a=1');
	ajax.req('q=home');
	//ss.init();
}

function changepic(pic_id, new_pic) {
	document.getElementById(pic_id).src = new_pic;
}

function openpic(arg) {
	document.getElementById('pic').style.visibility="visible";
	document.getElementById('pic').style.top = (document.body.scrollTop + 330)+"px";
	document.getElementById('pic').style.left = (document.body.clientWidth/2-230)+"px";
	document.getElementById('pic').innerHTML='<a href="javascript:closepic();"><img src="'+arg+'" border="0">';
}

function closepic() {
        document.getElementById('pic').style.visibility="hidden";
}

/*
function align() {
	e = document.getElementById('sisu');
	e.style.left = (document.body.clientWidth / 2) - (e.offsetWidth / 2);
	e.style.top = (document.body.clientHeight / 2) - (e.offsetHeight / 2);
}

function naitaNooli(olek) {
	if (olek == 0) {
		clearInterval(ticker);
		ticker = setInterval("__naitaNooli(0);", 30);
	} else {
		clearInterval(ticker);
		ticker = setInterval("__naitaNooli(1);", 30);
	}
}

function __naitaNooli(suund) {
	if (suund == 1) {
		if (opa < 50) opa += 10;
		else clearInterval(ticker);
	} else {
		if (opa > 0) opa -= 10;
		else clearInterval(ticker);
	}
	nv = document.getElementById('nool-v');
	np = document.getElementById('nool-p');
	nv.style.opacity = opa / 100;
	np.style.opacity = opa / 100;
	nv.style.filter = 'alpha(opacity=' + opa + ')';
	np.style.filter = 'alpha(opacity=' + opa + ')';
}
*/
/*
function over(id) {
	document.getElementById('el_' + id).className = 'highlight';
}

function out(id) {
	document.getElementById('el_' + id).className = 'element';
}


function debug(text) {
	document.getElementById('debug').innerHTML = text;
}

var album = {
	kaugus:0,
	delta:30,
	//kauguse_muut:30,
	//uus_kaugus:0,
	//temp:0,
	suund:'',
	samm:2,

	kuva:function(pilt) {
		var _text = "<img src = '" + pilt + "' width = '600' height = '400' class = 'pilt'>";
		_text += "<div id = 'kate' style = 'position:absolute; left:0px; top:0px; height: 100%; width: 100%;'></div>";
		document.getElementById('pilt').innerHTML = _text;
		//this.pane_keskele();
	},
	keri:function(suund) {
		if (ticker) clearInterval(ticker);
		this.suund = suund;
		t = document.getElementById('thumb');
		if ((suund == 'v' && this.kaugus >= this.delta) || (suund == 'p' && this.kaugus < t.scrollWidth - t.offsetWidth)) {
			this.delta = 30;
			ticker = setInterval("album.keri_x()", 50);
		}
		//if (this.kaugus > 0) document.getElementById('nool-v').style.display = '';
		//if (this.kaugus >= t.scrollWidth - t.offsetWidth) document.getElementById('nool-p').style.display = 'none';
	},

	keri_x:function() {
		//alert(this.suund);
		t = document.getElementById('thumb');
		if (this.delta <= this.samm) {
			clearInterval(ticker);
		} else {
			this.delta -= this.samm;
			if (this.suund == 'p') this.kaugus += this.delta;
			else this.kaugus -= this.delta;
			if (this.kaugus > t.scrollWidth - t.offsetWidth) this.kaugus = t.scrollWidth - t.offsetWidth;
			else if (this.kaugus < 0) this.kaugus = 0;
			t.scrollLeft = this.kaugus;
		}
	}
}
*/
