	function scrollIntoView(container, elem) {
		var c = Ext.getDom(container) || Ext.getBody().dom,
			elem = Ext.get(elem),
        	el = elem.dom,
        	o = elem.getOffsetsTo(c),
            l = o[0] + c.scrollLeft,
            t = o[1] + c.scrollTop,
            b = t + el.offsetHeight,
            r = l + el.offsetWidth,
        	ch = c.clientHeight,
        	ct = parseInt(c.scrollTop, 10),
        	cl = parseInt(c.scrollLeft, 10),
        	cb = ct + ch,
        	cr = cl + c.clientWidth;

        if (el.offsetHeight > ch || t < ct) {
        	c.scrollTop = t;
        } else if (b > cb){
            c.scrollTop = b-ch;
        }
        c.scrollTop = c.scrollTop; // corrects IE, other browsers will ignore
	};

	function ifocus(el, val){
		if (el.value == val) {
			el.value='';
		}
	}
	function iblur(el, val){
		if (el.value == '') {
			el.value=val;
		}
	}
	function ifocusc(el, c1, c2){
		Ext.fly(el).addClass(c1);
		Ext.fly(el).removeClass(c2);
	}
	function iblurc(el, c1, c2){
		if (el.value == '') {
			Ext.fly(el).addClass(c2);
			Ext.fly(el).removeClass(c1);
		} else {
			Ext.fly(el).addClass(c1);
			Ext.fly(el).removeClass(c2);
		}
	}

	function iclear(form){
		var iclstr = [];
		for (i = 1; i < iclear.arguments.length; i++){
			iclstr[i-1] = iclear.arguments[i];
		}
		Ext.each(Ext.query('input', form), function(el,i,a){
			if (iclstr.indexOf(el.value) != -1) {
				el.value='';
			}
		},this);
	}
	function change_tag(el, base) {
		var tag = el.options[el.selectedIndex].value;
		location = base + '?tags=' + tag;
	}
//-----------------------
	imgGallery.matchHref = new RegExp('(.*)\?iid=(.*)&w=(.*)&h=(.*)', 'im');
	imgGallery.blankImg = '/images/blank.gif';
	function imgGallery (i, w) {
		this.galId = i;
		this.imgDiv = document.getElementById(i);
		this.imgImg = this.imgDiv.getElementsByTagName('img')[0];
		this.imgAnc = this.imgImg.parentNode;
		this.descrDiv = document.getElementById(i+'_d');
		this.thumbs = document.getElementById(i+'_t');
		this.galWidth = w;
		this.preload = document.createElement('IMG');
		this.preload.gallery = this;
		this.preload.onload = function(){
			this.gallery.imgImg.src = this.src;
			this.gallery.imgDiv.className = 'image';
		};

	};
	imgGallery.prototype.show = function (el) {
		var load_img = el.href.match(imgGallery.matchHref);
		this.descrDiv.innerHTML = el.title;
		el.className = 's';
		scrollIntoView(this.thumbs, el);
		if (this.curImg) {
			this.curImg.className = '';
		}
		this.curImg = el;
		this.imgDiv.className = 'image loading';
		this.imgImg.src = imgGallery.blankImg;
		this.imgImg.width = load_img[3] > this.galWidth ? this.galWidth : load_img[3];
		this.imgImg.height = load_img[3] > this.galWidth ? (Math.round((load_img[4] * this.galWidth)/load_img[3])) : load_img[4];
		this.imgDiv.style.height = this.imgImg.height+'px';
		this.preload.src = '/pic.php?iid='+load_img[2]+'&w='+this.imgImg.width+'&h='+this.imgImg.height;
		this.imgAnc.href = el.href;
		return false;
	};
	imgGallery.prototype.prevnext = function (el) {
		var tl = this.thumbs.getElementsByTagName('a');
		for (var i=0; i < tl.length; i++){
			if (tl[i].id && tl[i].id == this.curImg.id) {
				break;
			}
		}
		if (el.id == this.galId+'_p') {
			this.show(i > 0 ? tl[i-1] : tl[tl.length-1]);
		} else {
			this.show(tl[i+1] ? tl[i+1] : tl[0]);
		}
		el.blur();
		return false;
	};
//-------------------------
	imgBox.matchHref = new RegExp('(.*)\?iid=(.*)&w=(.*)&h=(.*)', 'im');
	function imgBox(){
	};
	imgBox.prototype.init = function(id){
		this.imgBox = Ext.get(id);
		this.id = id;
		this.imgBoxInfo = Ext.get(id+'_info');
		this.imgBoxImgDiv = Ext.get(id+'_img');
		this.imgBoxImgImg = this.imgBoxImgDiv.select('img').item(0);
		this.imgBoxDescr = Ext.get(id+'_descr');
		this.imgBoxPrev = Ext.get(id+'_prev');
		this.imgBoxNext = Ext.get(id+'_next');
		this.imgBoxClose = Ext.get(id+'_close');
		this.imgBox.on('click', imgBox.prototype.stop, this);
		this.imgBoxPrev.on('click', imgBox.prototype.prevnext, this);
		this.imgBoxNext.on('click', imgBox.prototype.prevnext, this);
		this.imgBoxClose.on('click', imgBox.prototype.hide, this);
		this.preload = Ext.get(document.createElement('IMG'));
		this.preload.on('load', function(){
			this.imgBoxImgImg.dom.src = this.preload.dom.src;
			this.imgBoxImgDiv.className = 'image';
			this.imgBoxImgImg.show();
		},this);
	}	
	imgBox.prototype.assign = function(id){
		Ext.get(id).select('a').on('click', function(e,t,o){
			e.preventDefault();
			var el = Ext.get(t).findParent('a');
			this.show(el, el.getAttribute('gal'))
		},this);
	}
	imgBox.prototype.show = function(img, gal){
		this.gallery = gal || null;
		this.imgBoxPrev.dom.style.display = gal ? 'block' : 'none';
		this.imgBoxNext.dom.style.display = gal ? 'block' : 'none';
		this.curImg = img || this.gallery.curImg || null;
		this.curImg = Ext.get(this.curImg);
		Ext.getBody().mask();
		setTimeout(function(){
			Ext.getBody().on('click', imgBox.prototype.hide,this);
		}.createDelegate(this),200);
		this.imgBox.dom.style.display = 'block';
		this.showCur();
		return false;
	};
	imgBox.prototype.showCur = function(){
		if (this.gallery) {
			var tl = Ext.get(this.gallery.thumbs).select('a');
			this.imgBoxInfo.dom.innerHTML = 'Изображение '+(tl.indexOf(this.curImg)+1)+' из '+tl.getCount();
			this.imgBoxInfo.dom.style.display = 'visible';
		} else {
			this.imgBoxInfo.dom.style.visibility  = 'hidden';
		}
		this.imgBoxImgDiv.dom.className = 'image loading';
		this.imgBoxDescr.dom.innerHTML = this.curImg.dom.title;
		var load_img = this.curImg.dom.href.match(imgBox.matchHref);
		this.curWidth = load_img[3];
		this.curHeight = load_img[4];
		this.imgBoxImgDiv.setSize(this.curWidth, this.curHeight);
		this.imgBoxImgImg.setSize(this.curWidth, this.curHeight);
		this.imgBoxImgImg.hide();
		this.imgBox.center();
		this.imgBox.repaint();
		this.preload.dom.src = '/pic.php?iid='+load_img[2];
	};
	imgBox.prototype.hide = function(){
		var body = Ext.getBody();
		body.un('click', imgBox.prototype.hide,this);
		this.imgBox.dom.style.display = 'none';
		body.unmask();
		return false;
	};
	imgBox.prototype.stop = function(e){
		e.stopEvent();
		return false;
	};
	imgBox.prototype.prevnext = function(e){
		var el = e.getTarget('a');
		var tl = Ext.get(this.gallery.thumbs).select('a');
		var i = tl.indexOf(this.curImg);
		var c = tl.getCount();
		if (el.id == this.id+'_prev') {
			this.curImg = i > 0 ? tl.item(i-1) : tl.item(c-1);
		} else {
			this.curImg = i+1 >= c ? tl.item(0) : tl.item(i+1);
		}
		//alert(this.curImg);
		el.blur();
		this.showCur();
		return false;
	};
//-------------------------

