function fotoLine(){

	this.preload = [];
	this.preloadCount = 10;
	this.loadedFor = 0;
	this.run;
	this.offset = 0;
	this.settings = {}
	this.binded = false;
	this.sleep = 200;

	/* Static call when creating fotoLine object.. */	
	this.setParams = function(o){
		if(o.id){ this.id = o.id; }
		if(o.type){ this.type = o.type; }
		if(o.inline){ this.inline = o.inline; }
		if(o.imgHost){ this.imgHost = o.imgHost; }
		if(o.handleMouse){ this.handleMouse = o.handleMouse; }
		if(o.noCurrent){ this.noCurrent = o.noCurrent; }
	}

	this.correctOffset = function(){
		if(this.currentIndexOf && this.inline && this.total){
			this.offset = this.currentIndexOf - Math.floor(this.inline / 2);
			if(this.offset + this.inline > this.total - 1){ this.offset = this.offset - (this.offset + this.inline - this.total); }
			if(this.offset < 0){ this.offset = 0; }
			if(this.inline >= this.total){ this.offset = 0; }
		} else {
			this.offset = 0;
		}
	}
	
	this.fillData = function(a,d,c){
		this.img_array = a.slice(0);
		this.img_data = $H(d);
		if(c){
			this.current = c;
		} else {
			this.current = '';
		}
		this.dynInit();
	}
	
	this.dynInit = function(){
		this.box_width = 132;
		this.obj = $(this.id);
		this.boxes = $$('#' + this.id + ' .lImg');
		this.boxes.each( function(s){
			s.removeClassName('cur');
		});		this.total = (this.img_array) ? this.img_array.length : 0;
		if(this.noCurrent){
            this.obj.addClassName('noCurrent');
        } else {
			this.obj.removeClassName('noCurrent');
			this.currentIndexOf = this.img_array.indexOf(this.current);
		}
		this.correctOffset();
		if(this.binded == false && this.handleMouse && !Event.wheel){
			Object.extend(Event, {
                wheel:function (event){
					var delta = 0;
                    if (!event) event = window.event;
                    if (event.wheelDelta) {
                        delta = event.wheelDelta/120;
                    } else if (event.detail) { delta = -event.detail/3;     }
                    return Math.round(delta); //Safari Round
                }
            });
        }

		
		
		if(this.total > 0){
			this.prev_div = $$('#' + this.id + ' .lPrev')[0];
			this.prev_but = $$('#' + this.id + ' .lPrev .lButton')[0];
			this.next_div = $$('#' + this.id + ' .lNext')[0];
			this.next_but = $$('#' + this.id + ' .lNext .lButton')[0];
			if(this.binded == false){ this.bindButtons(); }
			this.updateButtons();
			this.rewriteBoxes();
			this.preloadImages();
        }
		this.binded = true;
		//if(this.resize){ Event.observe(window, 'resize', this.reinit.bindAsEventListener(this)); }
	}

    this.bindButtons = function(){
		Event.observe(this.prev_but, 'mouseup', this.listPrevUp.bindAsEventListener(this));
        Event.observe(this.prev_but, 'mousedown', this.listPrevDown.bindAsEventListener(this));

        Event.observe(this.next_but, 'mouseup', this.listNextUp.bindAsEventListener(this));
        Event.observe(this.next_but, 'mousedown', this.listNextDown.bindAsEventListener(this));
        document.observe('mouseup', this.stopRun.bindAsEventListener(this));

        if(this.handleMouse){
            Event.observe(this.obj, 'mousewheel', this.mouseScroll.bindAsEventListener(this), false );
            Event.observe(this.obj, 'DOMMouseScroll', this.mouseScroll.bindAsEventListener(this), false );
        }
    }

    this.mouseScroll = function(e){
        if(e.preventDefault){
            e.preventDefault();
        }
        var x = Event.wheel(e);
        if(x > 0){
            this.run = true;
            this.listPrev();
            this.run = false;
        } else {
            this.run = true;
            this.listNext();
            this.run = false;
        }
        return false;
    }

    this.stopRun = function(){
        this.run = false;
        if(this.st){ clearInterval(this.st); this.st = null; }
    }

    this.listPrevUp = function(){ this.listPrev(); this.stopRun(); if(this.st){ clearInterval(this.st); } }
    this.listPrevDown = function(){ this.run = true; this.st = setInterval(this.listPrev.bind(this),this.sleep) }
    this.listPrev = function(){
        if(this.offset > 0){
            if(this.run){
                this.offset--;
                this.updateButtons();
                this.rewriteBoxes();
            }
        } else {
            this.stopRun();
        }
    }

    this.listNextUp = function(){ this.listNext(); this.stopRun(); if(this.st){ clearInterval(this.st); } }
    this.listNextDown = function(){ this.run = true; this.st = setInterval(this.listNext.bind(this),this.sleep) }
    this.listNext = function(){
        if(this.offset + this.inline < this.img_array.length){
            if(this.run){
                this.offset++;
                this.updateButtons();
                this.rewriteBoxes();
            }
        } else {
            this.stopRun();
        }
        this.preloadImages();
    }

    this.updateButtons = function(){
		if(this.offset + this.inline < this.img_array.length){ this.next_div.addClassName('lOn'); } else { this.next_div.removeClassName('lOn'); }
        if(this.offset > 0){ this.prev_div.addClassName('lOn'); } else { this.prev_div.removeClassName('lOn'); }
		if(this.total <= this.inline){
			this.prev_div.addClassName('noCount'); 
			this.next_div.addClassName('noCount');
		} else {
			this.prev_div.removeClassName('noCount');
			this.next_div.removeClassName('noCount');
		}
    }

    this.rewriteBoxes = function(){
		for(var bIdx = 0, bLen = this.inline; bIdx < bLen; ++bIdx){
			var bNow = this.boxes[bIdx];

            var curNum = $(bNow).select('div')[0];
			var mvt = $(bNow).select('img.fl-v10')[0];
            var bA = bNow.getElementsByTagName('a');
            var bImg = $(bNow).select('img.fl-img')[0];
            var bA2 = $(bNow).select('a.fl-a2')[0];
			var branch = this.img_data.get(this.img_array[this.offset + bIdx]);
			
			if(branch){
			
				$(bNow).setStyle({width: this.box_width + 'px'});
				if(bImg.src == this.imgHost + branch['UrlJpg']){
                    bNow.writeAttribute('image_id', this.img_array[this.offset + bIdx]);
                } else {
                    if(!branch['preloaded']){
                        var nl = new Image();
                        nl.src = this.imgHost + branch['UrlJpg'];
                        this.preload.push(nl);
                        branch['preloaded'] = true;
                    }
                 
					if(branch['megaVote']){
						if(mvt){
                            mvt.src = 'http://img.imgsmail.ru/mail/ru/images/my/v_10s.png';
							mvt.show();
                        }
                    } else {
                        if(mvt){ 
                            mvt.hide(); 
                            mvt.src = 'http://img.imgsmail.ru/r/video2/spacer.gif';
                        }
                    }
                    for(var fi = 0, fl = bA.length; fi < fl; ++fi){
                        $(bA[fi]).href = '#' + branch['UrlHtml'];
                    }
					bImg.src = this.imgHost + branch['UrlJpg'];
					bImg.width = 120;
					bImg.height = 120;
                    if(bA2){
						bA2.update(branch['Title']);
                    }
                    bNow.writeAttribute('image_id', this.img_array[this.offset + bIdx]);
                }
        
if(bNow.readAttribute('image_id') == this.current){
        curNum.update(spot.o.otherFoto.image_album_index + '/' + spot.o.otherFoto.album_ImagesCount);
//	curNum.update(this.offset + bIdx + 1 + '/' + this.total);
	bNow.addClassName('cur');
} else {
	curNum.update('&nbsp;');
	bNow.removeClassName('cur');
}
			} else {
				bNow.writeAttribute('image_id', '');
				mvt.hide(); 
				mvt.src = 'http://img.imgsmail.ru/r/video2/spacer.gif';
				for(var fi = 0, fl = bA.length; fi < fl; ++fi){
					$(bA[fi]).href = 'javascript:void(0)';
				}
				bImg.src = 'http://img.imgsmail.ru/r/video2/spacer.gif';
				if(bA2){ bA2.update(''); }
				curNum.update('');
			}
		
        }
    }

	this.preloadImages = function(){
        if(this.loadedFor < this.offset + this.inline + this.preloadCount){
            var next5 = this.img_array.slice(this.offset + this.inline, this.offset + this.inline + this.preloadCount);
            for(var nIdx = 0, nLen = next5.length; nIdx < nLen; ++nIdx){
                if(!this.img_data.get(next5[nIdx])['preloaded']){
                    var n5 = new Image();
                    n5.src = this.imgHost + this.img_data.get(next5[nIdx])['UrlJpg'];
                    this.preload.push(n5);
                    this.img_data.get(next5[nIdx])['preloaded'] = true;
                }
            }
            if(this.offset + this.inline + this.preloadCount > this.loadedFor){
                this.loadedFor = this.offset + this.inline + this.preloadCount;
            }
        }
    }

//    this.init();

}

