var ie5 = (navigator.userAgent.indexOf("MSIE 5") != -1) ? true : false;
var ie6 = (navigator.userAgent.indexOf("MSIE 6") != -1) ? true : false;
var ie7 = (navigator.userAgent.indexOf("MSIE 7") != -1) ? true : false;
var ie8 = (navigator.userAgent.indexOf("MSIE 8") != -1) ? true : false;
var ff1 = (navigator.userAgent.indexOf("Firefox/1") != -1) ? true : false;
var ff2 = (navigator.userAgent.indexOf("Firefox/2") != -1) ? true : false;
var ff3 = (navigator.userAgent.indexOf("Firefox/3") != -1) ? true : false;
var op9 = (navigator.userAgent.indexOf("Opera/9") != -1) ? true : false;

var stepTimer = {};
var imgpre = { // NAMESPACE
	archive : {},
	getObject : function(id) {
		if (typeof this.archive[id] == "undefined") {
			this.archive[id] = new this.previewList(id);
		}
		return this.archive[id];
	}
};

imgpre.previewList = function(id) { // CONSTRUCTOR
	// Vars and hash
	this.id = id;
	this.inited = false;
	this.image_id;
	this.prevIdArray = [];
	this.prevAlbumArray = [];
	this.prevNameArray = [];
	this.prevDurationArray = [];
	this.prevRateArray = [];
	this.prevJpgArray = [];
	this.prevHtmlArray = [];
	this.prevHDArray = [];
	this.prevNumArray = [];
	this.prevPathJpg = "";
	this.prevPathLnk = "";
	this.curPrevId = null;
	this.curPos = 0;
	this.prevPath = "";
	this.prevExt = "";
	this.linkPath = "";
	this.linkExt = "";
	this.isRight = 0;
	this.isLeft = 0;
	this.JSONLoad = false;
	this.cellTotal = 4;
	this.curLeft = 0;
	this.frontendCopy = "";
	this.querySearch = "";
	this.theme = "";
	this.themeEng = "";
	this.image_ThemeId = "";
	this.prevTableClass = "p-table";
	this.totalBlockClass = "p-total-video";
	this.namePopClass = "p-name";
	this.allowName = true;
	this.imagesCount = 0;
	this.albumIndex = false;
	// Const
	this.prevWidth = 98;
	this.prevHeight = 96;
	this.prevPaddingRight = 10;
	this.curLeftInterval = 0;
	this.errorArray = {
		1 : "Object initialisation error",
		2 : "NOSCRIPT ID not found",
		3 : "Table of preview list ID is not found",
		4 : "ID of DIV with list is not found",
		5 : "Right scroll ID is not found",
		6 : "Left scroll ID is not found",
		7 : "Preview list is empty",
		8 : "Current preview ID is not set",
		9 : "Add handler is not supported11",
		10: "",
		11: "Preload error"
	};
	// Scroll

	this.resizeTimer = [];
	this.resizeInterval = 100;
	this.initTimeout = 300;
	this.stepTimeout = 110;
	this.stepDirection = "";
	// Prefixes
	this._noscript = "noscript_";
	this._script = "script_";
	this._list = "list_";
	this._goRight = "goright_";
	this._goLeft = "goleft_";
	// HTML-elements
	this.noscript = null;
	this.script = null;
	this.list = null;
	this.goRight = null;
	this.goLeft = null;
	this.preloadDiv = null;
	// Load
	this.alreadyLoaded = {};
	this.loadSpeed = 0;
}

imgpre.previewList.prototype = { // PROTOTYPE
	// ----------- Default -----------
	gebi : function(id) {
		return document.getElementById(id);
	},
	ce : function(name) {
		return document.createElement(name);
	},
	browserDefine : function() {
		this.ua = navigator.userAgent.toLowerCase();
		this.isGecko = this.ua.indexOf("gecko") != -1;
		this.isFF = this.ua.indexOf("firefox") != -1;
	},
	newImg : function(path) {
		var image = new Image();
		image.src = path;
		return image;
	},
	addHandler : function(object, event, handler, useCapture) { 
		if (object.addEventListener) {
			object.addEventListener(event, handler, useCapture ? useCapture : false);
		} else if (object.attachEvent) {
			object.attachEvent('on' + event, handler);
		} //else alert(this.errorArray[9]);
	},
	screenSize : function() { 
		var w = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth);
		var h = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight);
		return {w:w, h:h}; 
	},
	contentCell : function(index,curPos,id,aHref,imgSrc) { 
							
		var pic = [];
			
		if(this.prevIdArray[index] != "new" ){
			var num = (id == 'listId') ? this.prevNumArray[index] : index + 1;
			var cur = "", hd = "", hd_ico = "";
			if (id != 'likeId') cur = this.curPos == index ? ' cur' : ''; 
				
			if (this.prevHDArray[index] == "1") { hd = " HD_cont"; hd_ico = "<div class=\"HD_ico\"></div>"; }
			
			if(this.prevIdArray[index] == "empty") aHref = "http://" + VideoPerlHost() + "/catalog/static/wait.html?с=1"; 
			
			var re = /\&amp;/g;
			this.prevNameArray[index] = this.prevNameArray[index].replace(re,"\&");
			
			var mr = "";
			if (num >= 10) mr = "margin-left: " + parseInt(num.toString().length-1)  + "5px" ; 
					
			pic.push('<div class="cont '+ cur + hd +'"');
			//if(!op9) pic.push('onmouseover=\'imgpre.getObject("' + this.id + '").showName(' + this.prevIdArray[index] + ')\' onmouseout=\'imgpre.getObject(\"' + this.id + '\").hideName()\'');
			pic.push('>'); 
			pic.push('<a href="' + aHref + '" class="pv" onmouseover="pv.getObj(this).display({width : \'118\', height : \'88\', dynamic : true})" title="' + (this.prevNameArray[index] != "" ? this.prevNameArray[index] : "") + '"><img src="' + imgSrc + '" width="118" height="88" alt="' + (this.prevNameArray[index] != "" ? this.prevNameArray[index] : "") + '" /></a>');
			pic.push('<div class="p-prevname"><a href="' + aHref + '">' + num + '. ' + (this.prevNameArray[index] != "" ? this.prevNameArray[index] : "Без названия") + '</a></div>'); 
			if(hd_ico!='') pic.push(hd_ico);
			//if(this.prevNameArray[index] != '') pic.push('<div id="name-' + this.prevIdArray[index] + '-pop" class="p-name"><div class="t"></div><div class="b">' + this.prevNameArray[index] + '</div></div>');
			//if(op9) pic.push('<div onclick="window.location=\''+aHref+'\'" class="over" onmouseover=\'imgpre.getObject("' + this.id + '").showName(' + this.prevIdArray[index] + ')\' onmouseout=\'imgpre.getObject(\"' + this.id + '\").hideName()\'></div>');
			pic.push('</div>');
		}
		else {
			pic.push('<div class="cont p-album">');
			pic.push('<a href="' + this.prevPathLnk + this.prevAlbumArray[index + 1] + '">Альбом<br/>&laquo;' + aNames[this.prevAlbumArray[index + 1]] + '&raquo;</a>');
			pic.push('</div>');
		
		}
		
		return pic.join("");

	},
	
	showName : function(id) {
		if(this.allowName) {
			var h = -20;
			this.hideName();
			var el = gebi("name-" + id + "-pop");
			if(el) {
				el.style.display = "block";
				h = (-1) * el.offsetHeight + 20;
				el.style.top = h + "px"; 
			}
		}
	},
	
	hideName : function() {
		var els = gebc(this.namePopClass);
		forEach(els,function(a) {a.style.display = "none";});
	},
	
	// ----------- Init -----------
	findElements : function() {
		this.noscript = this.gebi(this._noscript + this.id);
		if (this.noscript == null) {
			if(console) console.log(this.errorArray[1] + " : " + this.errorArray[2]);
			return;
		}
		this.script = this.gebi(this._script + this.id);
		if (this.script == null) {
			if(console) console.log(this.errorArray[1] + " : " + this.errorArray[3]);
			return;
		}
		this.list = this.gebi(this._list + this.id);
		if (this.list == null) {
			if(console) console.log(this.errorArray[1] + " : " + this.errorArray[4]);
			return;
		}
		this.goRight = this.gebi(this._goRight + this.id);
		if (this.goRight == null) {
			if(console) console.log(this.errorArray[1] + " : " + this.errorArray[5]);
			return;
		}
		this.goLeft = this.gebi(this._goLeft + this.id);
		if (this.goLeft == null) {
			if(console) console.log(this.errorArray[1] + " : " + this.errorArray[6]);
			return;
		}
	},
	init : function() { 
		if(!this.inited) {
			//try {
				this.inited = true;
				if(this.id == 'listId') if(this.albumIndex) this.createNumbers();
				this.findElements();
				this.browserDefine();
				if (this.JSONLoad) this.curPrevId = null;
				this.drawList();
				this.wheelInit();
			//} catch(e) {alert(this.errorArray[1]);}

			var id = this.id;
			this.addHandler(document, "mouseup", function() {imgpre.getObject(id).mouseUpHandler()});
			this.addHandler(this.goLeft, "mousedown", function(evt) {
				evt = evt || window.event;
				if (evt.preventDefault) evt.preventDefault();
			});
			this.addHandler(this.goRight, "mousedown", function(evt) {
				evt = evt || window.event;
				if (evt.preventDefault) evt.preventDefault();
			});
			this.addHandler(this.goLeft, "dragstart", function() {return false;});
			this.addHandler(this.goRight, "dragstart", function() {return false;});
		}
	},
	
	createNumbers : function() { 
		var seek = this.curPrevId;
		var pos = this.prevIdArray.indexOf(seek); 
		var i, n;
		
		if (pos < 0) pos = 1;
		this.prevNumArray[pos] = this.albumIndex;
		n = this.albumIndex;
		for(i = (pos - 1) ; i >= 0 ; i--) if(this.prevIdArray[i] != "new") this.prevNumArray[i] = --n; else this.prevNumArray[i] = "new";
		n = this.albumIndex;
		for(i = (pos + 1) ; i < this.prevIdArray.length ; i++) if(this.prevIdArray[i] != "new") this.prevNumArray[i] = ++n; else this.prevNumArray[i] = "new";
	},
	// ----------- Functional -----------
	drawList : function() {
		if (this.JSONLoad) {
			if(!!this.querySearch){
				var _this = this; 
				var _url = 'http://' + VideoPerlHost() + '/ajaxsearch?q=' + this.querySearch + '&theme=' + this.themeEng + '&image_ThemeId=' + this.image_ThemeId + '&page=1&from=' + escape(window.location);
				
				ajax_call_static(_url,function(FromUri,arr){ 
					var f=0;
					for (var i = 0; i < arr.length; i++) {
						if(FromUri != arr[i]["UrlHtml"] && arr[i]["UrlJpg"] != "") {

						var RTC = document.getElementById("RelatedTotalCount");
						var UNSET = true;

						for(var j in arr[i]){
							
							if(arr[i]){
							if(RTC && UNSET){
							    UNSET = false;
							    if(typeof arr[i]["RTC"] != undefined && arr[i]["RTC"]){
							        RTC.innerHTML = arr[i]["RTC"];
							    }
							}
							_this.prevIdArray[f] = f;
							_this.prevHtmlArray[f] = "http://r.mail.ru/cln5322/" + VideoPerlHost() + arr[i]["UrlHtml"];
							_this.prevJpgArray[f] = _this.prevPathJpg + arr[i]["UrlJpg"];
							_this.prevRateArray[f] = (arr[i]["Rate"]) ? arr[i]["Rate"] : "0";
							_this.prevDurationArray[f] = (arr[i]["Duration"]) ? arr[i]["Duration"] : "00:00";
							_this.prevNameArray[f] = arr[i]["Title"];
							_this.prevHDArray[f] = arr[i]["HDexist"];
							}
							}
						f++;
						}
					}
					if(f > 0) {
						_this.JSONLoad = false;
						_this.drawList();
						_this.displayTotal();
					}else  {switchPrevline('listId');deletePrevline('likeId');}
				});
			}else {switchPrevline('listId');deletePrevline('likeId');}
		}

		// Detect curPos
		for (var i = 0; i < this.prevIdArray.length; i++) {
			if (this.prevIdArray[i] == this.curPrevId) {
				this.curPos = i;
				break;
			}
		}
		// Draw table
		this.resizeHandler();
	},
	setCentral : function() {
		this.curLeft = this.curPos - Math.floor(this.cellTotal / 2);
		this.curLeft = this.curLeft < 0 ? 0 : this.curLeft;
		var curRight = this.curLeft + this.cellTotal - 1;
		var rem = this.prevIdArray.length - 1 - curRight;

		if (rem < 0) {
			while (rem < 0 && this.curLeft > 0) {
				rem++;
				this.curLeft--;
			}
		}

		var VideoPerlHostPreview = VideoPerlHost();
		var VideoContentHostPreview = VideoContentHost();
		var imgSrc, aHref, index;
		var table = this.list.firstChild;
		for (var i = 0; i < this.cellTotal; i++)
		{ 
			if(table.rows) {
				var row = table.rows[i]; 
				index = this.curLeft + i;
				if (typeof this.prevJpgArray[index] == "undefined") continue;
				if (!this.frontendCopy) imgSrc = this.prevJpgArray[index];// для всего кроме this.frontendCopy = true
				else imgSrc = this.prevJpgArray[index];// для this.frontendCopy = true
				if (this.prevHtmlArray[index]) aHref = this.prevHtmlArray[index];// для ajax
				else aHref = this.prevPathLnk + this.prevAlbumArray[index] + "/" + this.prevIdArray[index] + '.html';// для обычных альбомов и избранного

				if(this.id == 'likeId'){ aHref = (aHref.indexOf('?') == -1) ? aHref + '?liked=1' : aHref + '&liked=1'; }
				row.cells[0].innerHTML = this.contentCell(index,this.curPos,this.id,aHref,imgSrc);
			}
		}
		this.activeLeftRightAngle();

	},
	resizeHandler : function() { //draw table
		this.goRight.style.display = this.goLeft.style.display = "";
		
		var html = [];
		html.push('<table>');
		for (var i = 0; i < this.cellTotal; i++) {
			html.push('<tr><td><div class="cont"><img src="http://img.imgsmail.ru/0.gif" width="118" height="88" alt="" /></div></td></tr>');
		}
		html.push('</table>');
		html = html.join('');
		this.list.innerHTML = html;
		// Show preview
		this.loadPreview(this.curPos);
		this.setCentral();
	},
	loadPreview : function(pos)
	{
		// Load DIV init
		pos = pos || this.curLeft;
		var preloadDiv = gebi('prevLineDiv');
   		preloadDiv.innerHTML = '';
		var extra = 2;
		var rightLoad = Math.floor(pos + extra * this.cellTotal) < this.prevIdArray.length ? Math.floor(pos + extra * this.cellTotal) : this.prevIdArray.length - 1;
		var leftLoad = Math.floor(pos - this.cellTotal) > 0 ? Math.floor(pos - this.cellTotal) : 0;
		var VideoContentHostPreview = VideoContentHost();
		for (var i = leftLoad; i <= rightLoad; i++)
		{
			if (typeof this.alreadyLoaded[i] != "undefined") continue;
			index = i;
			if (!this.frontendCopy) imgSrc = this.prevJpgArray[index];// для всего кроме this.frontendCopy = true
			else imgSrc = this.prevJpgArray[index];// для this.frontendCopy = true
			if (this.prevHtmlArray[index]) aHref = this.prevHtmlArray[index];// для ajax
			else aHref = this.prevPathLnk + this.prevIdArray[index] + '.html';// для обычных альбомов и избранного

			preloadDiv.innerHTML += '<img src="' + imgSrc + '" width="118" height="88" alt="" border="0" />';
			this.alreadyLoaded[index] = true;
		}
	},
	activeLeftRightAngle : function() {
		if (this.cellTotal >= this.prevIdArray.length) {
			this.goRight.style.display = this.goLeft.style.display = "none";
		} else {
			this.goRight.style.display = this.goLeft.style.display = "";
			// Right
			if (this.curLeft + this.cellTotal + 1 > this.prevIdArray.length) {
				this.goRight.innerHTML = "";
				this.isRight = false;
			} else {
				this.goRight.innerHTML = "<a class=\"d\" onmousedown=\"imgpre.getObject('" + this.id + "').scrollInit('r');\" onclick=\"return false;\" href=\"#\"><img src=\"http://img.mail.ru/0.gif\" height=\"22\" width=\"100%\" alt=\"\" /></a>";
				this.isRight = true;
			}
			// Left
			if (this.curLeft <= 0) {
				this.goLeft.innerHTML = "";
				this.isLeft = false;
			} else {
				this.goLeft.innerHTML = "<a class=\"u\" onmousedown=\"imgpre.getObject('" + this.id + "').scrollInit('l');\" onclick=\"return false;\" href=\"#\"><img src=\"http://img.mail.ru/0.gif\" height=\"22\" width=\"100%\" alt=\"\" /></a>";
				this.isLeft = true;
			}
		}
	},
	scrollInit : function(order) {
		this.stepDirection = order;
		var id = this.id; // IE timer fix
		stepTimer[id] = setInterval("imgpre.getObject('" + id + "').scrollRepeat()", this.initTimeout);
	},
	scrollRepeat : function() {
		this.doStep();
		var id = this.id; // IE timer fix
		if (stepTimer[id] != null) {
			clearInterval(stepTimer[id]);
			stepTimer[id] = null;
			stepTimer[id] = setInterval("imgpre.getObject('" + id + "').scrollRepeat()", this.stepTimeout);
		}
	},
	mouseUpHandler : function() {
		var id = this.id; // IE timer fix
		if (stepTimer[id] == null) return;
		clearInterval(stepTimer[id]);
		stepTimer[id] = null;
		this.doStep();
	},
	doStep : function() {
		var VideoPerlHostPreview = VideoPerlHost();
		var VideoContentHostPreview = VideoContentHost();
		var imgSrc, aHref, index;
		
		if (this.stepDirection == "l") {
			this.curLeft--;
			var index = this.curLeft;
			this.list.firstChild.deleteRow(this.cellTotal-1);
			if (!this.frontendCopy) imgSrc = this.prevJpgArray[index];// для всего кроме this.frontendCopy = true
			else imgSrc = this.prevJpgArray[index];// для this.frontendCopy = true
			if (this.prevHtmlArray[this.curLeft]) aHref = this.prevHtmlArray[this.curLeft];// для ajax
			else aHref = this.prevPathLnk + this.prevAlbumArray[index] + "/" + this.prevIdArray[this.curLeft] + '.html';// для обычных альбомов и избранного

			if(this.id == 'likeId'){ aHref = (aHref.indexOf('?') == -1) ? aHref + '?liked=1' : aHref + '&liked=1'; }
			this.list.firstChild.insertRow(0).insertCell(0).innerHTML = this.contentCell(index,this.curPos,this.id,aHref,imgSrc);
		} else if (this.stepDirection == "r") {
			this.curLeft++;
			this.list.firstChild.deleteRow(0);
			var curRight = this.curLeft + this.cellTotal - 1;
			var index = curRight;
			if (!this.frontendCopy) imgSrc = this.prevJpgArray[index];// для всего кроме this.frontendCopy = true
			else imgSrc = this.prevJpgArray[index];// для this.frontendCopy = true
			if (this.prevHtmlArray[curRight]) aHref = this.prevHtmlArray[curRight];// для ajax
			else aHref = this.prevPathLnk + this.prevAlbumArray[index] + "/" + this.prevIdArray[curRight] + '.html';// для обычных альбомов и избранного

			if(this.id == 'likeId'){ aHref = (aHref.indexOf('?') == -1) ? aHref + '?liked=1' : aHref + '&liked=1'; }
			this.list.firstChild.insertRow(this.cellTotal-1).insertCell(-1).innerHTML = this.contentCell(index,this.curPos,this.id,aHref,imgSrc);
		}
		this.loadSpeed++;
		if (this.loadSpeed % 5 == 0) this.loadPreview();
		this.activeLeftRightAngle();
		if (this.stepDirection == "l" && !this.isLeft || this.stepDirection == "r" && !this.isRight) {
			var id = this.id; // IE timer fix
			clearInterval(stepTimer[id]);
			stepTimer[id] = null;
		}
	},
	
		createQuery : function(name,theme,descr,tag) {
			var a,re,q,i = 0,arr = [];
			
			if (name != "") { arr[i] = name; i++;}
			else if (tag != "") { arr[i] = tag; i++;} 
			else if (descr != "") { arr[i] = this.cutFirst(descr); i++;}
			else return;
					
			q = arr.join(" ");
			
			q = q.toLowerCase();
			q = q.replace(/\&quot\;/,"");
			q = q.replace(/\&laquo\;/,"");
			q = q.replace(/\&raquo\;/,"");
			q = q.replace(/\&amp\;/,"");
			q = q.replace(/[\;\:\,\.\"\'\?\!\<\>\#\$\%\^\&\*\\]+/,"");
			
			this.querySearch = q; 
	},
	
	countWords :  function(str) {
		var arr = [];
		arr = str.split(" ");
		return arr.length;
	},
	
	cutFirst : function(str){
		var re,q,a;
		re = /([а-яА-Яa-zA-Z0-9_\-\,\:\.\"\'\?\!]+)\s/;
		if (re.test(str)) {
			a = re.exec(str);
			q = a[1];
			return q;
		}else return str;
	},
	
	encodeUTF : function(string) {
	
	 string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
	},
	
	displayTotal : function() { 
		var c ;
		var el = gebi("total_" + this.id);
		
		if (this.id == 'listId') 
			c = this.imagesCount; 
		else 
			c = this.prevIdArray.length;
		
		if(el){ 
			if(el) el.style.display = "block";
			if(c > 0) 
				el.childNodes[1].innerHTML = c;
			else 
				if (this.id == 'listId') {
					el.innerHTML = "Других: <span>0</span>"; 
					el = gebc("prevline-box")[0];
					var nv = gebi("novideo_listId");
					if(!nv) {
						var novideo = document.createElement("div");
						novideo.id = "novideo_listId";
						novideo.className = "p-novideo";
						novideo.innerHTML = "У пользователя<br/>нет других роликов";
						el.appendChild(novideo);
					}
				}
		}
	}

}

function initAlbumPrevline() { 
	// Init
	imgpre.getObject('listId').init();
	return false;
}

	
var switchPrevline = function(id) {
		hidePrevline();
		var el = gebi("script_" + id); 
		if(el) el.style.display = "block"; 
		var el = gebi("total_" + id);
		if(el) el.style.display = "block";
		var ul = gebc("p-switch")[0];
		addClass(ul, id+"-bg");
		setClass(gebi("switch_"+id).firstChild, "act");
		
		var el = gebi("novideo_" + id);
		if(el) el.style.display = "block";
				
		if(id == 'listId') if(gebi("script_listId")) initAlbumPrevline();
		
		imgpre.getObject(id).displayTotal();
		
		return false;
	};
	
var hidePrevline = function() {
		var els = gebc("p-table");
		forEach(els,function(a) {a.style.display = "none";});
		var els = gebc("p-total-video");
		forEach(els,function(a) {a.style.display = "none";});
		var ul = gebc("p-switch")[0];
		setClass(ul, "p-switch");
		setClass(gebc("act", "a", ul)[0], "");
		var els = gebc("p-novideo");
		forEach(els,function(a) {a.style.display = "none";});
	};
	
var deletePrevline = function(id) {
	var el;
	el = gebi("total_" + id);
	if(el) el.parentNode.removeChild(el);
	el = gebi("script_" + id);
	if(el) el.parentNode.removeChild(el);
	el = gebi("switch_" + id);
	if(el) el.parentNode.removeChild(el);
	};


//Wheel plugin
imgpre.previewList.prototype.wheelState = false;
imgpre.previewList.prototype.wheelLength = 1;
imgpre.previewList.prototype.absPosition = function(obj) { 
	var x = y = 0; 
	while(obj) { 
		x += obj.offsetLeft; 
		y += obj.offsetTop; 
		obj = obj.offsetParent; 
	} 
	return {x:x, y:y}; 
}
imgpre.previewList.prototype.defPosition = function(event) { 
	var x = y = 0; 
	if (document.attachEvent != null) { // Internet Explorer & Opera 
		x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); 
		y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
	} else if (!document.attachEvent && document.addEventListener) { // Gecko 
		x = event.clientX + window.scrollX; 
		y = event.clientY + window.scrollY; 
	} // else { // do nothing }
	return {x:x, y:y}; 
}
imgpre.previewList.prototype.scrollMode = 0;
imgpre.previewList.prototype.scrollModeTimer = null;
imgpre.previewList.prototype.scrollModeInterval = 1000;
imgpre.previewList.prototype.clearScrollMode = function() {
	this.scrollMode = 0;
	clearTimeout(this.scrollModeTimer);
	this.scrollModeTimer = null;
}
imgpre.previewList.prototype.wheelInit = function() {
	var _this = this;
	var id = this.id;
	var wheel = function(event) {
		_this.allowName = false;
		if (document.attachEvent) {
			var x_1 = _this.absPosition(_this.list.parentNode).x;
			var x_2 = _this.absPosition(_this.list.parentNode).x + _this.list.parentNode.offsetWidth;
			var y_1 = _this.absPosition(_this.list.parentNode).y;
			var y_2 = _this.absPosition(_this.list.parentNode).y + _this.list.parentNode.offsetHeight;
			var mx = _this.defPosition(event).x;
			var my = _this.defPosition(event).y;
			if ((mx < x_1 || mx > x_2) || (my < y_1 || my > y_2)) { // Указатель вне превлайна
				if (!_this.scrollMode) {
					_this.scrollMode = 1;
					_this.scrollModeTimer = setTimeout("imgpre.getObject('"+id+"').clearScrollMode()", _this.scrollModeInterval);
				}
				if (_this.scrollMode != 2) return;
			}
		} else if (!_this.wheelState) {
			if (!_this.scrollMode) {
				_this.scrollMode = 1;
				_this.scrollModeTimer = setTimeout("imgpre.getObject('"+id+"').clearScrollMode()", _this.scrollModeInterval);
			}
			if (_this.scrollMode != 2) return;
		}
		if (_this.scrollMode == 1) return;
		_this.scrollMode = 2;
		_this.scrollModeTimer = setTimeout("imgpre.getObject('"+id+"').scrollMode=0", _this.scrollModeInterval);
		var delta; // -N down, N up
		event = event || window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta / 120;
			//if (window.opera) delta = -delta;
		} else if (event.detail) delta = -event.detail / 3;
		if (event.preventDefault)  event.preventDefault();
		event.returnValue = false;
		_this.wheelStep(delta, event);
	}
	this.addHandler(window, 'DOMMouseScroll', wheel); /* Gecko */
	this.addHandler(window, 'mousewheel', wheel); /* Opera */
	this.addHandler(document, 'mousewheel', wheel); /* IE */
	
	this.addHandler(this.list, 'mouseover', function() {
		_this.wheelState = true;
	});
	this.addHandler(this.list, 'mouseout', function() {
		_this.wheelState = false;
		_this.allowName = true;
	});
}
imgpre.previewList.prototype.wheelStep = function(delta, event) { 
	if(this.elementInLine(event.target || event.srcElement)) {
		this.stepDirection = delta < 0 ? "r" : "l";
		//прокручивать на каждый 2й скролл
		if (this.wheelLength % 2 == 0) if (this.stepDirection == "l" && this.isLeft || this.stepDirection == "r" && this.isRight) this.doStep();
		this.wheelLength++;
	}
	else {
		this.wheelState = false;
		this.allowName = true;
	}
},

imgpre.previewList.prototype.elementInLine = function(el) {
	while(el) { 
		if (el.id == this._list + this.id) return true;
		el = el.parentNode;
	}
	return false;
}
//end Wheel plugin