// JavaScript Document

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
        }
});

var
BannersRotator = Class.create({
	'initialize' : function(prop) {
		
		if (prop){
			
			this.bannersContainers = prop;
			
			this.bannersInitilizer();
			
		} else {
		
			alert('No params for bannersRotator');
		
		}
		
	},
	
	'bannersInitilizer' : function(){
	
		var
			bR = this,
			i,
			bannersContsSize = this.bannersContainers.size(),
			cInterval  = 10000;
		
		for (i = 0; i < bannersContsSize; i ++){
		
			bR.bannersContainers[i].banners = bR.bannersContainers[i].select('[title="banner_item"]');
			
			if (bR.bannersContainers[i].banners.size() > 0){
				bR.bannersContainers[i].style.display = "block";
				bR.bannersContainers[i].banners[0].style.display = 'block';
				bR.bannersContainers[i].interval = setInterval(
					function(){
					
						var
							j,
							bannersSize = this.banners.size();

						for (j = 0; j < bannersSize; j++){
							this.banners[j].style.display = 'none';
						}
						
						this.banners[(Math.floor(Math.random()*bannersSize))].style.display = 'block';
					
					}.bind(bR.bannersContainers[i]), 
					cInterval
				);
			
			}
			
		}
	
	}
}),
Gallerator = Class.create({
	'initialize' : function(prop) {
		this.errorsInit();
		
		if (prop){
			
			this.contentContainer = prop.contentContainer ? $(prop.contentContainer) : false;			
			
			this.imagesArray = [];
			
			if (!this.contentContainer){
				this.errorsController.pushError('Undefined content container. Set it in property!');
			} else {
			
				this.content = this.contentContainer ? this.contentContainer.innerHTML : false;
				this.galleratorContainer = new Element('div', {'class' : 'gallerator'});
				this.galleratorViewerContainer = new Element('div', {'id' : 'gallerator_viewer_container'});
				this.galleratorViewerImage = new Element('img', {'id' : 'gallerator_viewer_image'});
				this.galleratorNavigatorPrev = new Element('div', {'id' : 'gallerator_navigator_prev'});
				this.galleratorNavigatorNext = new Element('div', {'id' : 'gallerator_navigator_next'});
				this.galleratorInformer = new Element('div', {'id' : 'gallerator_informer'});
				this.galleratorImageContainer = new Element('div', {'id' : 'gallerator_image_container'});
				this.galleratorPreviewerContainer = new Element('div', {'id' : 'gallerator_previewer_container'});
				
				this.galleratorThumbsParentContainer = new Element('div', {'id' : 'gallerator_thumbs_parent_container'});
				this.galleratorThumbsContainer = new Element('div', {'id' : 'gallerator_thumbs_container'});
				
				this.galleratorPreviewerNavigatorPrev = new Element('div', {'id' : 'gallerator_previewer_navigator_prev'});
				this.galleratorPreviewerNavigatorNext = new Element('div', {'id' : 'gallerator_previewer_navigator_next'});
				
				this.currentFrame = 0;
				
				this.frameMoverEffect = null;
				this.scaleEffect = null;
				this.informerMoveEffect = null;
				this.thumbsMoverEffect = null;
			
			}
			
		} else {
			this.errorsController.pushError('Undefined properties for Gallerator!');
		}
		
		if (this.errorsController.isErrors()){
			return false;
		} else {
			this.construct();
		}
	
	},
	'tagsCuter' : function(content){
		
		var
			tagsArray = content.match(/<img[^>]+>/gi),
			images = [],
			i,
			src,
			alt,
			title;
		
		//alert(tagsArray);
		
		for (i = 0; i < tagsArray.length; i++){
			
			src = tagsArray[i].match(/(src\=)([\"|\'][^"]*[\'|\"])/gi),
			alt = tagsArray[i].match(/(alt\=)([\"|\'][^"]*[\'|\"])/gi),
			title = tagsArray[i].match(/(title\=)([\"|\'][^"]*[\'|\"])/gi);
				
			src = (src && src[0]) ? src[0].replace(/(src=)?[\"\']/gi, '') : '';
			alt = (alt && alt[0]) ? alt[0].replace(/(alt=)?[\"\']/gi, '') : '';
			title = (title && title[0]) ? title[0].replace(/(title=)?[\"\']/gi, '') : '';

			if (src != ''){
				images.push({
				
					'id' : ('gallerator_image_' + i),
					'name' : i,
					'src' : src,
					'alt' : alt,
					'title' : title
				
				});
			}		
		}
		
		return images;
	},
	'errorsInit' : function(){
		
		var
			gallerator = this;
			
		gallerator.errorsController = {
			
			'errors' : [],
			
			'pushError' : function(error){
				if (error && error != ''){
					gallerator.errorsController.errors.push(error);
				}
			},
			
			'isErrors' : function(){
				if (gallerator.errorsController.errors.size() > 0){
					gallerator.errorsController.showErrors();
					return true;
				}
				return false;
			},
			
			'showErrors' : function(){
				var
					errors = gallerator.errorsController.errors,
					errorsSize = gallerator.errorsController.errors.size(),
					errorsStringCollector = '',
					i;
				
				for (i = 0; i < errorsSize; i ++){
					errorsStringCollector += errors[i] + '\n';
				}
				
				alert(errorsStringCollector);
			}
			
		};	
	},
	'scrollToFrame' : function(num){
	
		var
			gallerator = this,
			current = gallerator.galleratorThumbsParentContainer.scrollLeft + (num * 130);
		
		if (gallerator.thumbsMoverEffect){
			gallerator.thumbsMoverEffect.cancel();
		}
		gallerator.thumbsMoverEffect = new Effect.Tween(null, gallerator.galleratorThumbsParentContainer.scrollLeft, current, {duration : 0.5, transition: Effect.Transitions.spring}, function(p){ gallerator.galleratorThumbsParentContainer.scrollLeft = p;});
		
	
	},
	'gotoFrame' : function(num){
		
		var
			gallerator = this,
			imagesArraySize = gallerator.imagesArray.size(),
			num = num && num > 0 ? ((num <= imagesArraySize) ? num : imagesArraySize) : 1,
			src = gallerator.imagesArray[num - 1] ? gallerator.imagesArray[num - 1].image.src : false;
		
		if (num != gallerator.currentFrame && src && src != gallerator.galleratorViewerImage.src){
			
			if (gallerator.frameMoverEffect){
				
				gallerator.frameMoverEffect.cancel();
			
			}
			
			if (gallerator.imagesArray[gallerator.currentFrame - 1]){
				gallerator.imagesArray[gallerator.currentFrame - 1].image.setOpacity(0.5);
			}
			
			gallerator.currentFrame = num;
			gallerator.imagesArray[num - 1].image.setOpacity(1);
			
			if (gallerator.informerMoveEffect){
				gallerator.informerMoveEffect.cancel();
			}
			
			//gallerator.galleratorInformer.style.top = (gallerator.galleratorViewerContainer.getHeight() - gallerator.galleratorInformer.getHeight()) + 'px';
			
			///gallerator.informerMoveEffect = new Effect.Move(gallerator.galleratorInformer, { x: -2000, y: 0, mode: 'relative', afterFinish: function(){
				/*gallerator.informerMoveEffect = new Effect.Morph(gallerator.galleratorInformer, {
					  style: 'left: -300px;', // CSS Properties
					  duration: 0.3,// Core Effect properties
				afterFinish: function(){*/
				gallerator.galleratorInformer.style.left = '-350px';
				gallerator.galleratorViewerImage.setOpacity(0);
				gallerator.galleratorViewerImage.name = num;
				if (gallerator.imagesArray[gallerator.currentFrame - 1].title != '' || gallerator.imagesArray[gallerator.currentFrame - 1].alt != ''){					
					gallerator.galleratorInformer.update('<span>Название:</span>&nbsp;' + gallerator.imagesArray[gallerator.currentFrame - 1].title + '<br/><span>Описание:&nbsp;</span>' + gallerator.imagesArray[gallerator.currentFrame - 1].alt);
				} else {
					gallerator.galleratorInformer.update();
				}
				gallerator.galleratorViewerImage.src = src;
		/*		
			}});*/
			
			
			/*gallerator.frameMoverEffect = new Effect.Opacity(gallerator.galleratorViewerImage, { from: 1, to: 0, duration: 0.5, delay: 0.5, afterFinish: function(){
				
				//gallerator.galleratorViewerImage.src = src;
				
			}});*/
			
		}
		//alert(gallerator.imagesArray[num - 1].image.src);
		
	
	},
	'construct' : function(){
	
		var
			gallerator = this,
			imagesArray = gallerator.tagsCuter(gallerator.content),
			imagesArraySize = imagesArray.size(),
			i,
			df = document.createDocumentFragment(),
			onclickThumbsEvent = function(){
				gallerator.gotoFrame((parseInt(this.name, 10) + 1));
				return false;
			},
			onWheelEvent = function(event){
				
				var
					e = event ? event : window.event,
					delta = Event.wheel(e) < 0 ? 75 : 150;
				
				if (gallerator.scaleEffect){
				
					gallerator.scaleEffect.cancel();
				
				}
				
				gallerator.scaleEffect = new Effect.Scale(gallerator.galleratorViewerImage, delta, {'duration' : 0.8});
				
				if (e.preventDefault) e.preventDefault();
				e.returnValue = false;
			},
			galleratorThumbsWidth = 0,
			onLoadFunc = function(imga){
				
				//gallerator.galleratorThumbsContainer.style.width = (20000) + 'px';
				
				var
					imga = imga && imga.src ? imga : (this && this.src ? this : false),
					height = imga ? imga.getHeight() : null,
					width = imga ? imga.getWidth() : null;
				
				if (!imga){
					return;
				}
				
				if (height > width){
					imga.style.width = '70px';
					imga.style.height = 'auto';
				} else {
					imga.style.width = 'auto';
					imga.style.height = '70px';
				}
			};
		
		for (i = 0; i < imagesArraySize; i ++){
			
			imagesArray[i].container = new Element('a', {'class' : 'thumb', 'name' : imagesArray[i].name});
			imagesArray[i].container.onclick = onclickThumbsEvent;
			
			imagesArray[i].containerImage = new Element('div');
			
			imagesArray[i].image = new Element('img', imagesArray[i]);
			imagesArray[i].image.setOpacity(0.5);
			
			imagesArray[i].containerImage.appendChild(imagesArray[i].image);
			imagesArray[i].container.appendChild(imagesArray[i].containerImage);
			
			//if (imagesArray[i].image.complete){
				//onLoadFunc(imagesArray[i].image);
			//} else {
				imagesArray[i].image.onload = onLoadFunc;
			//}
			
			df.appendChild(imagesArray[i].container);
		}
		
		gallerator.imagesArray = imagesArray;
		
		gallerator.contentContainer.update();
		
		gallerator.galleratorThumbsContainer.style.width = (i *  90) + 'px';
		
		gallerator.galleratorViewerImage.observe('load', function(){
			
			gallerator.galleratorImageContainer.style.position = 'absolute';
			gallerator.galleratorViewerImage.style.width = 'auto';
			gallerator.galleratorViewerImage.style.height = 'auto';
			
			var
				imageContainerWidth = gallerator.galleratorViewerContainer.getWidth(),
				imageContainerHeight = gallerator.galleratorViewerContainer.getHeight(),
				imageWidth = gallerator.galleratorViewerImage.getWidth(),
				imageHeight = gallerator.galleratorViewerImage.getHeight(),
				newWidth = imageWidth,
				newHeight = imageHeight,
				delta = 100;
				
			if ((imageContainerWidth - delta) < imageWidth){
				newWidth = imageContainerWidth - delta;
				newHeight = Math.round(imageHeight * newWidth / imageWidth);
			} else if ((imageContainerHeight - delta) < imageHeight) {
				newHeight = imageContainerHeight - delta;
				newWidth =Math.round(imageWidth * newHeight / imageHeight);				
			}
			
			gallerator.galleratorViewerImage.style.width = newWidth + 'px';
			gallerator.galleratorViewerImage.style.height = newHeight + 'px';
			
			gallerator.galleratorImageContainer.style.left = ((imageContainerWidth / 2) - (gallerator.galleratorImageContainer.getWidth()/ 2))  + 'px';
			gallerator.galleratorImageContainer.style.top = ((imageContainerHeight / 2) - (gallerator.galleratorImageContainer.getHeight()/ 2))  + 'px';
			
			gallerator.frameMoverEffect = new Effect.Opacity(gallerator.galleratorViewerImage, { from: 0, to: 1, duration: 0.5, afterFinish : function(){
				
				if (gallerator.informerMoveEffect){
					gallerator.informerMoveEffect.cancel();
				}
				
				if (gallerator.galleratorInformer != ''){
					//gallerator.informerMoveEffect = new Effect.Move(gallerator.galleratorInformer, { x: 2000, y: 0, duration: 0.5, mode: 'relative' });
					gallerator.informerMoveEffect = new Effect.Morph(gallerator.galleratorInformer, {
					  style: 'left: 0px;', // CSS Properties
					  duration: 0.2
					});

				}
			
			}});
			
			
			
		});
		
		new Draggable(gallerator.galleratorImageContainer, {'starteffect' : null, 'endeffect' : null, 'onStart' : function(){
			
			if (gallerator.scaleEffect){
			
				gallerator.scaleEffect.cancel();
			
			}
			
		}});
		
		if (window.addEventListener){
			gallerator.galleratorImageContainer.addEventListener('DOMMouseScroll', onWheelEvent, true);
		}
		gallerator.galleratorImageContainer.onmousewheel = onWheelEvent;
		
		
		gallerator.galleratorViewerImage.setOpacity(0);
		gallerator.galleratorImageContainer.appendChild(gallerator.galleratorViewerImage);
		
		gallerator.galleratorNavigatorPrev.onclick = function(){
			gallerator.gotoFrame(gallerator.currentFrame - 1);
		};
		
		gallerator.galleratorNavigatorNext.onclick = function(){
			gallerator.gotoFrame(gallerator.currentFrame + 1);
		};
		
		gallerator.galleratorPreviewerNavigatorPrev.onclick = function(){
			gallerator.scrollToFrame(-1);
		};
		
		gallerator.galleratorPreviewerNavigatorNext.onclick = function(){
			gallerator.scrollToFrame(1);
		};
		
		gallerator.galleratorViewerContainer.appendChild(gallerator.galleratorNavigatorPrev);
		gallerator.galleratorViewerContainer.appendChild(gallerator.galleratorNavigatorNext);
		gallerator.galleratorViewerContainer.appendChild(gallerator.galleratorInformer);
		gallerator.galleratorViewerContainer.appendChild(gallerator.galleratorImageContainer);
		
		gallerator.galleratorThumbsContainer.appendChild(df);
		gallerator.galleratorThumbsParentContainer.appendChild(gallerator.galleratorThumbsContainer);
		
		gallerator.galleratorPreviewerContainer.appendChild(gallerator.galleratorPreviewerNavigatorPrev);
		gallerator.galleratorPreviewerContainer.appendChild(gallerator.galleratorPreviewerNavigatorNext);
		gallerator.galleratorPreviewerContainer.appendChild(gallerator.galleratorThumbsParentContainer);
		
		gallerator.galleratorContainer.appendChild(gallerator.galleratorViewerContainer);
		gallerator.galleratorContainer.appendChild(gallerator.galleratorPreviewerContainer);
		
		gallerator.contentContainer.appendChild(gallerator.galleratorContainer);
		
		gallerator.gotoFrame(1);
		
		//alert(imagesArray.toSource());
	}
}),

Paginator = Class.create({
	'initialize' : function(prop) {
		
		this.errorsInit();
		if (prop){
			
			this.uri = 'http://' + window.location.host + '' + window.location.pathname;
			
			this.contentContainer = prop.contentContainer ? $(prop.contentContainer) : false;
			this.paginatorContainer = prop.paginatorContainer ? $(prop.paginatorContainer) : false;
			//this.contentHeight = prop.contentHeight ? prop.contentHeight : 1080;
			this.pagesContainer = new Element('div', {'style': 'display: inline-block; max-width: 365px; position: relative;'});
			this.paginatorSub = new Element('div', {'style': 'display:inline-block; position: relative;'});
			this.content = this.contentContainer ? this.contentContainer.innerHTML : false;
			this.contentSub = prop.contentSub ? $(prop.contentSub) : false;
			this.paginatorPreloader = $('paginator_cp_' + this.uri + '_preloader');
			
			this.pagesCount = $('paginator_cp_contents_count') && $('paginator_cp_contents_count').innerHTML ? parseInt($('paginator_cp_contents_count').innerHTML, 10) : 0;
			this.currentPage = 0;
			this.pages = [];
			this.pagesControllersBtns = {};
			
			this.perPage = 11000;
			
			this.hrefChangerHackTimeout = null;
			
			this.fooloverTomeout = null;
			
			this.onConstructed = prop.onConstructed ? $(prop.onConstructed) : false;
			
			if (!this.contentContainer){
				this.errorsController.pushError('Undefined content container. Set it in property!');
			}
			if (!this.paginatorContainer){
				this.errorsController.pushError('Undefined paginator container. Set it in property!');
			}
		} else {
			//alert('o_O');
			this.errorsController.pushError('Undefined properties for paginator!');
		}
		
		if (this.errorsController.isErrors()){
			return false;
		} else {
			var
				paginator = this,
				contentContainerParent = paginator.contentContainer.parentNode;
			
			if (!paginator.paginatorPreloader){
				
				paginator.paginatorPreloader = new Element('div', {'id' : ('paginator_cp_' + this.uri + '_preloader'), 'style' : 'font-family: Arial; font-size: 10px; color: #666; text-decoration: blink; text-align: center;'}).update('Загрузка...');
				contentContainerParent.insertBefore(paginator.paginatorPreloader,paginator.contentContainer);
				//paginator.contentContainer.style.display = 'none';
			} else {
				paginator.construct();
			}
			
			
			Event.observe(window, 'load', function() {
				paginator.paginatorPreloader.style.display = 'none';
				paginator.construct();
			});
		}
		
	},
	
	'errorsInit' : function(){
		
		var
			paginator = this;
			
		paginator.errorsController = {
			
			'errors' : [],
			
			'pushError' : function(error){
				if (error && error != ''){
					paginator.errorsController.errors.push(error);
				}
			},
			
			'isErrors' : function(){
				if (paginator.errorsController.errors.size() > 0){
					paginator.errorsController.showErrors();
					return true;
				}
				return false;
			},
			
			'showErrors' : function(){
				var
					errors = paginator.errorsController.errors,
					errorsSize = paginator.errorsController.errors.size(),
					errorsStringCollector = '',
					i;
				
				for (i = 0; i < errorsSize; i ++){
					errorsStringCollector += errors[i] + '\n';
				}
				
				alert(errorsStringCollector);
			}
			
		};
		
	},
	
	'htmlPusher' : function(html, pushstr, salt){
		
		var
			paginator = this,
			firstOpenTag = '',
			pushstr = pushstr ? pushstr : '',
			salt = salt ? salt : '',
			isStartFromOpenedTag = html.match(/^(<\/\w+>)|(^([?:\s]+<|<)?(tr|td|th|li|area|col|colgroup|dt|dd|option|param|tfoot|thead|tbody|ins\b)?(\s+("[^"]*"|'[^']*'|[^>])+)>)/gi);
			
		if (!isStartFromOpenedTag){
			
			html = salt + pushstr + html;
		
		} else if (isStartFromOpenedTag && isStartFromOpenedTag[0]){
			
			salt = salt + html.substr(0, isStartFromOpenedTag[0].length);
			
			//alert(isStartFromOpenedTag[0].toSource());
			
			html = paginator.htmlPusher(html.substr(isStartFromOpenedTag[0].length), pushstr, salt);
		}
		
		return html;
	
	},
	
	'anchorsFiller' : function(){
		
		var
			paginator = this,
			links = paginator.contentSub.getElementsByTagName('a'),
			linksSize = links.length,
			i,
			j,
			anchorsArray = [],
			currentAnchor;
		
		for (i = 0; i < linksSize; i ++){
			
			if (links[i].href){
				currentAnchor = (new RegExp('[^?#/]*$').exec(links[i].href))[0].toQueryParams();
				if (currentAnchor && currentAnchor.anchor){
					
					
					links[i].anchor = currentAnchor.anchor;
					
					
					links[i].onclick = function(){
						paginator.goToAnchor(this.anchor);
						return false;
					};
					
					for (j = 0; j < linksSize; j++){
					
						if (links[j].name && links[j].name == ('anchor='+links[i].anchor)){
							links[j].name = 'anch_' + links[i].anchor;
							anchorsArray['anch_' + links[i].anchor] = links[j];
						}
					}
				}				
			}
		}
		
		paginator.anchorsArray = anchorsArray;
		
	},
	
	'goToAnchor' : function(anchor){
		
		var
			paginator = this,
			anchorPosition,
			pagesCount = paginator.pagesCount,
			page = 1,
			i,
			pagePos;
			
		if (paginator.anchorsArray['anch_' + anchor]){
			
			anchorPosition = paginator.anchorsArray['anch_' + anchor].positionedOffset()[1];
			
			for (i = 0; i < pagesCount; i++){
			
				if (paginator.pages[i]){
				
					pagePos = paginator.pages[i].pageContainer.positionedOffset()[1];
					
					if (anchorPosition > pagePos){
						page = i + 1;
					}
					
				}
			
			}
			
			paginator.goToPage(page, false, function(){
				new Effect.ScrollTo(paginator.anchorsArray['anch_' + anchor], { duration:'0.2'});
			});
		}
		
	},
	
	'goToPage' : function(pageNum, isnotAnimate, afterFunc){
		
		var
			pageNum = parseInt(pageNum, 10),
			paginator = this,
			pages = paginator.pages,
			pagesCount = pages.size(),
			/*scrollTop = ((pageNum - 1) * paginator.contentHeight) - ((paginator.currentPage - 1) * paginator.contentHeight),*/
			scrollTop = 0,
			i,
			backBtnHref,
			forwardBtnHref,
			isnotAnimate = isnotAnimate ? true : false,
			nextPagePos,
			afterFunc = afterFunc ? afterFunc : false;
		
		if (pageNum <= 0){
			pageNum = 1;
		}
		if (pageNum > pagesCount){
			pageNum = pagesCount;
		}
		
		for (i = 0; i < pagesCount; i ++){
			
			paginator.pages[i].page.removeClassName('selected');
			
			/*if (i < (pageNum - 1)){
				scrollTop += paginator.pages[i].pageContainer.getHeight();
			}*/
			paginator.pages[i].pageContainer = $('paginator_cp_' + i);
			
		}
		
		paginator.pages[(pageNum - 1)].page.addClassName('selected');
		
		//alert(paginator.pages[(pageNum - 1)].pageContainer.id + '__' + paginator.pages[(pageNum - 1)].pageContainer.positionedOffset());
		
		scrollTop = paginator.pages[(pageNum - 1)].pageContainer.positionedOffset();
		nextPagePos = paginator.pages[(pageNum)] ? paginator.pages[(pageNum)].pageContainer.positionedOffset() : $('paginator_cp_' + pageNum).positionedOffset();
		//alert(scrollTop);		
		
		if (pageNum == paginator.currentPage){
			if (afterFunc){
				afterFunc();
			}
			return;
		}
		
		paginator.currentPage = pageNum;
		
		if (!isnotAnimate){
		
			//new Effect.Opacity(paginator.contentSub, { from: paginator.contentSub.getOpacity(), to: 0, duration: 0.4, afterFinish: function(){
				//paginator.contentSub.style.top = -scrollTop + "px";
				new Effect.ScrollTo(paginator.contentContainer, { duration:'0.2', offset:-600});
			//}});
		}
		
		paginator.contentContainer.scrollTop = 0;
		paginator.contentSub.style.top = -scrollTop[1] + "px";
		paginator.contentContainer.style.height = (nextPagePos[1] - scrollTop[1]) + 'px';
		
		backBtnHref = (pageNum - 1) > 0 ? pageNum - 1 : 1;
		forwardBtnHref = (pageNum + 1) < pagesCount ? pageNum + 1 : pagesCount;
		if (backBtnHref == paginator.currentPage){
			paginator.pagesControllersBtns._back.style.visibility = 'hidden';
		} else {
			paginator.pagesControllersBtns._back.style.visibility = 'visible';
		}
		if (forwardBtnHref == paginator.currentPage){
			paginator.pagesControllersBtns._forward.style.visibility = 'hidden';
		} else {
			paginator.pagesControllersBtns._forward.style.visibility = 'visible';
		}
		
		document.fire('paginator:' + paginator.uri, {'pageNum' : pageNum});
		
		if (paginator.hrefChangerHackTimeout && paginator.hrefChangerHackTimeout != null){
		
			clearTimeout(paginator.hrefChangerHackTimeout);
		
		}
		
		paginator.hrefChangerHackTimeout = setTimeout(function(){			
			paginator.pagesControllersBtns._back.setAttribute('name', backBtnHref);
			paginator.pagesControllersBtns._back.setAttribute('href', (paginator.uri +'#page=' + backBtnHref));
			paginator.pagesControllersBtns._forward.setAttribute('name', forwardBtnHref);
			paginator.pagesControllersBtns._forward.setAttribute('href', (paginator.uri +'#page=' + forwardBtnHref));
		}, 100);
		
		/*new Effect.Move(paginator.contentSub, { y: -scrollTop, duration:'0.3', afterFinish: function(){
			new Effect.ScrollTo(paginator.contentContainer, { duration:'0.2', offset: -60});}});*/
	
	},

	'construct' : function(){
		var
			paginator = this,
			df = document.createDocumentFragment(),
			df_c = document.createDocumentFragment(),
			pageClickEvent = function(){
				var
					name = this.name;
				
				if (paginator.fooloverTomeout && paginator.fooloverTomeout != null){
					clearTimeout(paginator.fooloverTomeout);
				}
				paginator.fooloverTomeout = setTimeout(function(){paginator.goToPage(name);}, 300);
			},
			pageNum,
			i = 0,
			uriObj = (new RegExp('[^?#/]*$').exec(window.location.hash))[0].toQueryParams(),
			currentPage = uriObj && uriObj.page ? parseInt(uriObj.page, 10) : 1,
			backBtnHref,
			forwardBtnHref,
			currentPageContent,
			isFillSubContent = false,
			lastOpenTag,
			lastCloseTag,
			lastContentAppendix = '<!-- Generated by Paginator from U.JorJ (ajat.com.ua, uncle.jorj@gmail.com) -->',
			lastContentOpenedTags = [],
			lastSpace,
			contentSubHTML = '',
			//pageContentsArray = paginator.content.split("<!-- pagebreak -->")
			approximatePagesCount = paginator.pagesCount;
		//paginator.contentParser();
		
		//paginator.contentContainer.style.visibility = 'hidden';
		//paginator.contentContainer.style.height = paginator.contentHeight + 'px';
		
		if (approximatePagesCount <= 1){
			paginator.contentContainer.style.display = 'block';
			return;
		}
		
		paginator.contentContainer.style.overflow = 'hidden';		
		
		if (paginator.contentSub == undefined || paginator.contentSub == null){
			
			paginator.pagesCount = approximatePagesCount;
			
			//paginator.contentSub = new Element('div', {'style' : 'position: relative; top: 0px; z-index: 0;', 'id' : 'paginator_content_' + this.uri});
			
			//paginator.contentContainer.update('');
		
			//paginator.contentSub.update(paginator.content);
			//paginator.contentContainer.appendChild(paginator.contentSub);
			
			//paginator.contentSub = paginator.contentContainer;
			
		//	paginator.contentSub.setOpacity(0);
			
			
			isFillSubContent = true;
			
			paginator.contentSub.pagesCount = paginator.pagesCount;
		
		} else {
			paginator.pagesCount = paginator.contentSub.pagesCount;
		
		}
		
		currentPageContent = paginator.content;
		
		var
			index = 0,
			nextStrPos,
			first,
			pre = paginator.content,
			last;
			
		for (i = 0; i < approximatePagesCount; i++){
			
			/*if (i == 0){
				contentSubHTML += '<div id="' + 'paginator_cp_' + paginator.uri + '_' + i + '" style="display:block; height:1px; background-color:none;"></div><br/>';
			}
			
			contentSubHTML += pageContentsArray[i] + '<div id="' + 'paginator_cp_' + paginator.uri + '_' + (i + 1) + '" style="display:block; height:1px; background-color:none;"></div><br/>';*/
			
			pageNum = (i + 1);
			paginator.pages[i] = {};
			paginator.pages[i].page = new Element('a').update(pageNum);
			paginator.pages[i].page.setAttribute('name', pageNum);
			paginator.pages[i].page.setAttribute('href', (paginator.uri +'#page=' + pageNum));
			paginator.pages[i].page.setAttribute('title', "Перейти на страницу " + pageNum);
			paginator.pages[i].page.observe('click', pageClickEvent);
			df.appendChild(paginator.pages[i].page);
		}
		
		if (isFillSubContent){
		//	paginator.contentSub.update(contentSubHTML);
			paginator.anchorsFiller();
		}
		//alert(i+1);
		//paginator.pagesCount = i;
		
		backBtnHref = (currentPage - 1) > 0 ? currentPage - 1 : 1;
		forwardBtnHref = (currentPage + 1) < paginator.pagesCount ? currentPage + 1 : paginator.pagesCount;
		
		paginator.pagesControllersBtns = {
		
			'_back' : new Element('a', {'class': 'controllers', 'href': (paginator.uri + '#page=' + backBtnHref), 'name': backBtnHref, 'id' : 'back'}).update('назад'),
			'_forward' : new Element('a', {'class': 'controllers', 'href': (paginator.uri +'#page=' + forwardBtnHref), 'name': forwardBtnHref, 'id' : 'forward'}).update('вперед')
		
		};
		
		if (backBtnHref == currentPage){
			paginator.pagesControllersBtns._back.style.visibility = 'hidden';
		} else {
			paginator.pagesControllersBtns._back.style.visibility = 'visible';
		}
		if (forwardBtnHref == currentPage){
			paginator.pagesControllersBtns._forward.style.visibility = 'hidden';
		} else {
			paginator.pagesControllersBtns._forward.style.visibility = 'visible';
		}
		
		paginator.pagesControllersBtns._back.observe('click', pageClickEvent);
		paginator.pagesControllersBtns._forward.observe('click', pageClickEvent);
		
		
		paginator.pagesContainer.appendChild(df);
		paginator.paginatorSub.appendChild(paginator.pagesControllersBtns._back);
		paginator.paginatorSub.appendChild(paginator.pagesContainer);
		paginator.paginatorSub.appendChild(paginator.pagesControllersBtns._forward);
		
		paginator.paginatorContainer.appendChild(paginator.paginatorSub);		
		paginator.goToPage(currentPage,!isFillSubContent);
		
		document.observe('paginator:' + paginator.uri, function(event){
			paginator.goToPage(event.memo.pageNum, true);
		});
		
		if (paginator.onConstructed){
		
			paginator.onConstructed();
		
		}
		paginator.contentContainer.style.display = 'block';
	}});
