function Gallery(gallery) {
	this.gallery = gallery;	
}
Gallery.prototype = {
	init: function() {
		this.initVars();
		this.decorate();
		this.setDefault();
		this.initScroll();
		this.initPictureChanging();
		this.initClosing();
	},
	initVars: function() {
		this.lp = as.getBTN("img",as.getBCN("lp-container","div",this.gallery)[0])[0];
		this.list = as.getBTN("ul",this.gallery)[0];
		this.items = as.getBTN("li",this.list);
		this.imgs = as.getBTN("img",this.list);
		this.closer = as.getBCN("back","a")[0];
		this.lpHash = [];
		
		this.sswidth = this.list.parentNode.offsetWidth+7;
		this.wlwidth = 90*this.items.length;
		this.vlwidth = this.list.parentNode.offsetWidth;
		this.scrollSpeed = 25;
		if (this.vlwidth+10 > this.wlwidth) {
			this.noListScroll = true;
			this.gallery.className += " no-scroll-list";
		}
		(this.items.length > 1) || (this.gallery.className += " no-scroll-image");
	},
	decorate: function() {
		this.gallery.className = this.gallery.className.replace(/\bundecorated\b/,"");
		as.style(this.list,{width: this.wlwidth+"px",left: "0px"});
	},
	initScroll: function() {
		as.e.click(as.$$("div.lp-wrapper a.scroller-left"),function(e) {this.preScrollPicture("l",e)},this);
		as.e.click(as.$$("div.lp-wrapper a.scroller-right"),function(e) {this.preScrollPicture("r",e)},this);
		as.e.click(as.$$("div.sp-wrapper a.scroller-left"),function(e) {this.preScrollList("l",e)},this);
		as.e.click(as.$$("div.sp-wrapper a.scroller-right"),function(e) {this.preScrollList("r",e)},this);
	},
	initPictureChanging: function() {
		var _self_ = this;
		as.addEvent(
			this.list,
			"click",
			function(e) {
				e = e || window.event;
				var target = e.target || e.srcElement;
				if (target.tagName == "IMG") {
					as.cancelEvent(e);
					_self_.changePicture(target.parentNode.parentNode);
				}
			}
		)
	},
	initClosing: function() {
		this.closer.onclick = function(e) {
			e = e || window.event;
			as.cancelEvent(e);
			window.close();
			window.opener.focus();
		}
	},
	setDefault: function() {
		var twidth = 0;
		as.foreach(
			this.items,
			function(item,i) {
				item.src = as.getBTN("a",item)[0].href;	
				item.oleft = twidth;
				item.countnum = i;
				twidth += 90;
				item.oright = twidth;
			}
		);
		if (location.hash) {
			var titem, hash = location.hash.substring(1,location.hash.length);
			as.foreach(
				this.imgs,
				function(img) {
					if (img.src.indexOf(hash) != -1) {
						titem = img.parentNode.parentNode;
					}	
				}
			);
			if (titem) {
				this.activeItem = titem;
				this.changePicture(titem);
			}
			else {
				this.activeItem = this.items[0];
				this.changePicture(this.items[0]);
			}
		}
		else {
			this.activeItem = this.items[0];
			this.changePicture(this.items[0]);
		}
	},
	changePicture: function(item) {
		this.setPictureSize(item);
		this.activeItem.className = this.activeItem.className.replace(/\bactive\b/,"");
		item.className += " active";
		this.activeItem = item;		
		
		var l = -parseInt(this.list.style.left);
		//alert(l+" "+item.oleft+" "+this.vlwidth+l+" "+item.oright);
		
		if (item.oright > l + this.vlwidth) {
			this.scrollList(-this.scrollSpeed,item.oright - (l + this.vlwidth),-l);
		}
		if (item.oleft < l) {
			this.scrollList(this.scrollSpeed,(l - item.oleft),-l);	
		}
	},
	setPictureSize: function(item) {
		var num = item.countnum, _self_ = this;
		as.style(this.lp,{position: "absolute",width: "",height: "",visibility: "hidden"});
		this.lp.src = item.src;
		if (this.lpHash[num]) {
			as.style(this.lp,this.lpHash[num]);
			setTimeout(
				function() {
					as.style(_self_.lp,{position: "static",visibility: "visible"});
					var ieTop = (_self_.lp.parentNode.offsetHeight - _self_.lp.offsetHeight)/2+"px";
					as.style(_self_.lp,{top: ieTop});
				},500
			);
		}
		else {
			this.lp.onload = function() {
				setTimeout(
					function() {
						if (_self_.lp.offsetWidth > 640) {
							as.style(_self_.lp,{width: "640px"});
						}
						if (_self_.lp.offsetHeight > 420) {
							as.style(_self_.lp,{height: "420px"});
							as.style(_self_.lp,{width: "auto"});
						}
						_self_.lpHash[num] = {width: _self_.lp.offsetWidth+"px", height: _self_.lp.offsetHeight+"px"};
						_self_.lp.onload = "";
						as.style(_self_.lp,{position: "static",visibility: "visible"});
						var ieTop = (_self_.lp.parentNode.offsetHeight - _self_.lp.offsetHeight)/2+"px";
						as.style(_self_.lp,{top: ieTop});
					},100
				);
			}
		}
	},
	preScrollPicture: function(d,e) {
		e.preventDefault();
		var cItem = this.activeItem, index, cIndex = cItem.countnum;
		if (d=="l") {
			index = (cIndex == 0) ? (this.items.length - 1) : (--cIndex);
		}
		if (d=="r") {
			index = (cIndex == this.items.length-1) ? (0) : (++cIndex);
		}
		this.changePicture(this.items[index]);
	},
	preScrollList: function(d,e) {
		as.cancelEvent(e);
		var l = -parseInt(this.list.style.left);
		if (d=="l") {
			this.scrollList(this.scrollSpeed,Math.min(l,this.sswidth),-l);
		}
		if (d=="r") {
			this.scrollList(-this.scrollSpeed,Math.min(this.wlwidth-l-this.vlwidth-7,this.sswidth),-l);
		}
	},
	
	
	
	
	
	getTrifle: function(speed,sign,width) {
		var trifle = width - (Math.floor(width/speed)*speed);
		if (sign < 0) { trifle = -trifle }
		return trifle;
	},
	scrollList: function(speed,width,al) {
		if (width <= 0 || this.noListScroll) { return; }
		var _self_ = this;
		var twidth = 0;
		var awidth = Math.abs(width);
		var aspeed = Math.abs(speed);
		var trifle = this.getTrifle(aspeed,speed,width);
		var atrifle = Math.abs(trifle);
		
		//alert(speed+" "+width+" "+al+" "+trifle);
		
		var sli = setInterval(
			function() {
				if (twidth + aspeed > awidth) {
					clearInterval(sli);
					al += trifle;
					as.style(_self_.list,{left: al+"px"});
					return;
				}
				twidth += aspeed;
				al += speed;
				as.style(_self_.list,{left: al+"px"});
			},10
		);
	}
}


function inPageGallery(gallery) {
	this.gallery = gallery;
}
inPageGallery.prototype = {
	init: function() {
		this.handleClick();
		
	},
	handleClick: function() {
		var _self_ = this;
		as.addEvent(
			this.gallery,
			"click",
			function(e) {
				e = e || window.event;
				as.cancelEvent(e);
				var target = e.target || e.srcElement;
				var offsetLeft = (document.body.offsetWidth - 730)/2;
				var offsetTop = (document.documentElement.offsetHeight - 700)/2;
				switch (target.tagName) {
					case "IMG":
					_self_.galleryWindow = window.open(target.parentNode.href,"galleryWindow","resize=0,scrollbars=0,width=730,height=700,screenX="+offsetLeft+",screenY=150");
					break;
					
					case "A":
					_self_.galleryWindow = window.open(target.href,"galleryWindow","resize=0,scrollbars=0,width=730,height=700,screenX="+offsetLeft+",screenY=150");
					break;
				}
			}
		)
	}
}
function scarfPopup(){
	var scarfLink = document.getElementById('scarf-popup');
	if (!scarfLink) return;
	
	var offsetLeft = (document.body.offsetWidth - 730)/2;
	
	scarfLink.onclick = function(e){
		window.open("http://formica-spb.ru/","_blank");
		return false;
		/*
		var event = e || window.event;
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		window.open("/scarf-popup.html","galleryWindow","resize=0,scrollbars=0,width=730,height=700,screenX="+offsetLeft+",screenY=150");
		*/
	}
}
scarfPopup();
