/**
* Live Listing Pics (slideshow), by Sergey Shuchkin <shuchkin@mail.ru>
* 1) Just add "LiveListing" param to "class" attribute of the listing image
* Example:
* <img src="{$listing_info.image}" width="150" height="100" class="LiveListing={$listing_info.id_listing_info}" />
*
* 2) If used AJAX,
*    a) just call LLPics.update(); after ajax loading (recommended)
*    b) insert this code to ajax body:
*      <script type="text/javascript">
*      <!--
*      LLPics.update();
*      -->
*      </script>
*/
if (typeof($) == "undefined") {
	$ = function (id) {
		return document.getElementById(id);
	}
}
if (typeof($$) == "undefined") {
	$$ = function (tag) {
		return document.getElementsByTagName(tag);
	}
}
LLPics = {};
LLPics.loaded = [];
LLPics.data = [];
LLPics.images = [];
LLPics.initialized = false;
LLPics.init = function() {
	LLPics.initialized = true;
	LLPics.update();
}
LLPics.update = function() {
	if (!LLPics.initialized) return;
	if (LLPics.loadData()) LLPics.setImages();
}
LLPics.searchIDs = function() {
	var ids = [];
	var im = $$("IMG");
	if (!im) return ids;
	for (var i = 0; i < im.length; i++) {
		if (r = im[i].className.match(/LiveListing=(\d+)/i)) {
			if (!LLPics.inArray(r[1],ids)) ids[ids.length] = r[1];
		}
	}
	return ids;
}
LLPics.in_loaded = function(id) {
	for (var i = 0; i<LLPics.length; i++) if (LLPics.loaded[i].id == id) return true;
	return false;
}
LLPics.loadData = function() {
	//if (typeof(https) == "undefined") { alert("live_listing_pics.js: xml_http_request.js is requred!"); return; }
	var req = [];
	// check new
	ids = LLPics.searchIDs();
	for (var i = 0; i<ids.length; i++)
		if (!LLPics.inArray(ids[i],LLPics.loaded)) req[req.length] = ids[i];
	if (req.length > 0) {
		var url = "get_pics.php?ids[]=" + req.join("&ids[]=");
		if (typeof(https) != "undefined") { // is xml_http_request ?
			var cfg = {
	       	  url: url,
        	  method: "get",
			  callback: LLPics.onLoad
		    }
	        https.get_object(cfg);
		} else if (typeof(TransAjax) != 'undefined') { // try TransAjax
			TransAjax.exec({url: url, onLoadObject: LLPics.onLoad});
		}
		return false;
	} else {
		return true;
	}
}
LLPics.onLoad = function(data) {
	for (var i=0; i<data.length; i++) {
		LLPics.loaded[LLPics.loaded.length] = data[i].id;
		LLPics.data[LLPics.data.length] = data[i];
	}
	LLPics.setImages();
}
LLPics.getPics = function(id) {
	//alert(LLPics.data);
	for (var i = 0; i<LLPics.data.length; i++) if (id == LLPics.data[i].id) return LLPics.data[i].pics;
	return [];
}
LLPics.setImages = function() {
	var im = $$("IMG");
	if (!im) return;
	//LLPics.images = [];
	for (var i = 0; i <im.length; i++) {
		if (r = im[i].className.match(/LiveListing=(\d+)/i)) {
			if (typeof(im[i].livePics) == "undefined") { //new
				idx = LLPics.images.length;
				LLPics.images[idx] = im[i];
				im[i].liveIndex = idx;
				im[i].livePics = LLPics.getPics(r[1]);
				im[i].liveCur = 0;
				//console.log(im[i].livePics.toString());
				if (im[i].livePics.length > 0) {
					im[i].onmouseover = function() {
						this.liveTimer = setInterval("LLPics.show("+this.liveIndex+")",700);
						//console.log('1');
					}
					im[i].onmouseout = function() {
						//console.log('2');
						clearTimeout(this.liveTimer);
					}
				}
			}
		}
	}
}
LLPics.show = function(idx) {
	if (typeof(LLPics.images[idx]) == "undefined") return;
	var im = LLPics.images[idx];
	im.liveCur++;
	if (im.liveCur == im.livePics.length) im.liveCur = 0;
	im.src = baseurl+'/pics/pre_'+im.livePics[im.liveCur];
}
//utils
LLPics.inArray = function(id, arr) {
	for (var i = 0; i<arr.length; i++) if (id == arr[i]) return true;
	return false;
}
addLoadEvent(LLPics.init);