function fotoLine(){
    if(arguments[0]){ this.settings = arguments[0] }
    this.preload = [];
    this.preloadCount = 10;
    this.loadedFor = 0;
	if(this.settings.spawn){ this.resize = true; }
    this.run;
    this.handleMouse = (typeof this.settings.handleMouse != 'undefined' && this.settings.handleMouse == false) ? false : true;
    this.sleep = (this.settings.sleep) ? parseInt(this.settings.sleep) : 150;
    this.noCurrent = (!this.settings.type) ? true : (this.settings.type == 'simple') ? true : false;
    this.imgHost = (this.settings.imgHost) ? this.settings.imgHost : 'http://content.foto.mail.ru';
    this.offset = 0;
    this.current = (this.settings.current) ? this.settings.current : '';
    if(this.settings.array && window[this.settings.array] && Object.isArray(window[this.settings.array])){
        this.img_array = window[this.settings.array].slice(0);
    } else { this.img_array = new Array(); }
	this.total = (this.img_array) ? this.img_array.length : 0;
	this.currentIndexOf = this.img_array.indexOf(this.current);
    if(this.settings.data && window[this.settings.data] && Object.isHash($H(window[this.settings.data]))){
        this.img_data = $H(window[this.settings.data]);
    } else { this.img_data = new Hash(); }
	
	this.init = function(){
		if(this.settings.id){ this.id = this.settings.id; } else { this.notInited = true; return false; }
		if(this.settings.album_ImagesCount){ this.imagTotal = this.settings.album_ImagesCount; }
		if(this.settings.image_album_index){ this.imagCurrent = this.settings.image_album_index; }
		if(this.settings.spawn){
		    if(this.settings.parent){
				this.parent_obj = $(this.settings.parent);
				if(this.parent_obj){
					this.work_width = this.parent_obj.getWidth();
					this.inline = Math.floor(this.work_width / 132);
					this.box_width = Math.floor((this.work_width - 60) / this.inline);
					if(this.inline > 0){
						this.spawn();
					} else {
						this.notInited = true; return false;
					}
				} else {
					this.notInited = true; return false;
				}
			} else {
				this.notInited = true; return false;
			}
		}
		this.box_width = (this.box_width) ? this.box_width : 132;
		this.obj = $(this.id);
		this.boxes = $$('#' + this.id + ' .lImg');
		this.inline = this.boxes.length;
		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; }
		if(this.noCurrent && !this.obj.hasClassName('noCurrent')){
            this.obj.addClassName('noCurrent');
        }
		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.total > this.inline){
				//if(this.prev_but && this.next_but && this.img_array.length > this.inline){
					this.bindButtons();
					this.updateButtons();
				//}
			//}
			this.rewriteBoxes();
			this.preloadImages();
        } else {
            this.tryAjaxLoad();
        }
		if(this.settings.spawn){
		    this.obj.show();
		}
        if(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;
//                        if (window.opera) delta = -delta;
                    } else if (event.detail) { delta = -event.detail/3;     }
                    return Math.round(delta); //Safari Round
                }
            });
        }
		if(this.resize){ Event.observe(window, 'resize', this.reinit.bindAsEventListener(this)); }
    }
	
	this.reinit = function(){
		this.work_width = this.parent_obj.getWidth();
		this.inline = Math.floor(this.work_width / 132);
		this.box_width = Math.floor((this.work_width - 60) / this.inline);
		if(this.inline > 0){
			if(this.inline > this.boxes.length){
				for(var _i1 = 0, _l1 = this.inline - this.boxes.length; _i1 < _l1; ++_i1){
					this.addBox();
				}
				this.boxes = $$('#' + this.id + ' .lImg');
				this.inline = this.boxes.length;
					
				this.offset = (this.offset > this.total - this.inline) ? this.total - this.inline : this.offset;
				if(this.inline >= this.total){ this.offset = 0; }					
					
				this.rewriteBoxes();
				this.updateButtons();
				
			} else if (this.inline < this.boxes.length){

				var _td = this.boxes.slice(this.boxes.length-(this.boxes.length - this.inline), this.boxes.length);
				for(var _i1 = 0, _l1 = _td.length; _i1 < _l1; ++_i1){
				    _td[_i1].remove();
				}
			
				this.boxes = $$('#' + this.id + ' .lImg');
				this.inline = this.boxes.length;
					
				this.offset = (this.offset > this.total - this.inline) ? this.total - this.inline : this.offset;
				if(this.inline >= this.total){ this.offset = 0; }					
					
				this.rewriteBoxes();
				this.updateButtons();
				
			} else if (this.inline == this.boxes.length){
				this.rewriteBoxes();
			}
			
			
		} else {
			this.notInited = true; return false;
		}		
	}

    this.spawn = function(){
		var ul = new Element('ul', {'id': this.id}).addClassName('fotoLine').setStyle({'display': 'none'});
		if(this.noCurrent) { ul.addClassName('noCurrent'); }
		var lPrev = new Element('li').addClassName('lPrev');
		var l_lButton = new Element('div').addClassName('lButton');
		l_lButton.appendChild(new Element('img', {'src': 'http://img.imgsmail.ru/r/video2/spacer.gif'}));
		var l_lSpacer = new Element('div').addClassName('lSpacer');
		l_lSpacer.appendChild(new Element('img', {'src': 'http://img.imgsmail.ru/r/video2/spacer.gif'}));
		lPrev.appendChild(l_lButton);
		lPrev.appendChild(l_lSpacer);
		ul.appendChild(lPrev);
		if(this.inline >= this.total) { this.inline = this.total; }
		
		for(var uidx = 0, ulen = this.inline; uidx < ulen; ++uidx){
		    var lImg = new Element('li').addClassName('lImg');
			lImg.appendChild(new Element('div').update('&nbsp;'));
			lImg.appendChild(new Element('img', {'src': 'http://img.imgsmail.ru/r/video2/spacer.gif'}).addClassName('fl-v10'));
			
			var la1 = new Element('a', {'href': '#'}).addClassName('fl-a1');
			var li1 = new Element('img', {'src' : 'http://img.imgsmail.ru/r/video2/spacer.gif'}).addClassName('fl-img');
			la1.appendChild(li1);
			lImg.appendChild(la1);

			var p1 = new Element('p');
			p1.appendChild(new Element('a', {'href': '#'}).addClassName('fl-a2').update(''));
			lImg.appendChild(p1);

			ul.appendChild(lImg);
		}
		
		var lNext = new Element('li').addClassName('lNext');
		var r_lButton = new Element('div').addClassName('lButton');
		r_lButton.appendChild(new Element('img', {'src': 'http://img.imgsmail.ru/r/video2/spacer.gif'}));
		var r_lSpacer = new Element('div').addClassName('lSpacer');
		r_lSpacer.appendChild(new Element('img', {'src': 'http://img.imgsmail.ru/r/video2/spacer.gif'}));
		lNext.appendChild(r_lButton);
		lNext.appendChild(r_lSpacer);
		ul.appendChild(lNext);
		
		this.parent_obj.appendChild(ul);
	}
	
	this.addBox = function(){
		var beforeMe = this.obj.select('li.lNext')[0];
		Element.insert(beforeMe, {'before': '<li class="lImg" style="width: ' + this.boxWidth + 'px"><div>&nbsp;</div><img class="fl-v10" style="display: none" src="http://img.imgsmail.ru/r/video2/spacer.gif" /><a class="fl-a1" href="#"><img class="fl-img" src="#" /></a><p><a class="fl-a2" href="#"></a></p></li>'});
	}
	
	this.tryAjaxLoad = function(){
    }

    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]);
			
			$(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 = branch['PWidth'];
				bImg.height = branch['PHeight'];
                if(bA2){
					bA2.update(branch['Title']);
                }
                bNow.writeAttribute('image_id', this.img_array[this.offset + bIdx]);
            }

                        try{
			if(branch['PHeight'] < 120){
				var _mar = Math.floor((120 - branch['PHeight']) / 2);
				
				$(bImg).setStyle({'marginTop': _mar + 'px', 'marginBottom': _mar + 'px'});
			} else {
				$(bImg).setStyle({'marginTop': '0', 'marginBottom': '0'})
			}
			} catch(e){}








            if(bNow.readAttribute('image_id') == this.current){
if(this.imagCurrent && this.imagCurrent){
				curNum.update(this.imagCurrent + '/' + this.imagTotal);
} else {
				curNum.update(this.offset + bIdx + 1 + '/' + this.total);
}
                bNow.addClassName('cur');
            } else {
				curNum.update('&nbsp;');
                bNow.removeClassName('cur');
            }

        }
    }

	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();

}
