var oPhoto = {
	create: function(oParams){
		if(window.location.hash){
			window.location.href = window.location.href.replace(/(\?.*)?\#/, '?');
		}else{
			this.eNavigation = oParams.eNavigation;
			this.eContent = oParams.eContent;
			var oThis = this;
			Common.Event.add(window, 'load', function(){
				oThis.init(oParams);
			});
		}
	}
	, init: function(oParams){
  		var eVisible_area = this.eNavigation;
		var eNavigation_control = document.createElement('div');
		eNavigation_control.id = 'scroll';
		this.eNavigation.appendChild(eNavigation_control);
		eNavigation_control.innerHTML = '<div class="button left"></div><div class="button right"></div><div class="handler"><div class="left"></div><div class="right"></div></div><div class="getter"></div><div class="marks_container"></div>';
		this.iImg_spacer = 20;
		var aePhoto = this.eNavigation.getElementsByTagName('img');
		for (var i = 1, eSpacer; i < oParams.aoLabel.length; i++) {
			eSpacer = document.createElement('i');
			eSpacer.style.paddingRight = this.iImg_spacer + 'px';
			aePhoto[0].parentNode.parentNode.insertBefore(eSpacer, aePhoto[oParams.aoLabel[i].iPosition + 1].parentNode);
			//this.eNavigation.getElementsByTagName('img')[oParams.aoLabel[i].iPosition].parentNode.style.paddingRight = this.iImg_spacer + 'px';
		}
		this.oContent.create(oParams);
		if(oParams.bVertical){
			oPhoto.oContent.oControl.vertical_mode();
		}
		if(oParams.aoLabel.length > 1){
			for(var i = 0; i < oParams.aoLabel.length; i++){
				var eNavigation_mark = document.createElement('div');
				eNavigation_mark.className = 'mark';
				eNavigation_mark.style.left = oParams.aoLabel[i].iPosition / this.oContent.iCount * 100 + '%';
				eNavigation_control.lastChild.appendChild(eNavigation_mark);
				eNavigation_mark.innerHTML = oParams.aoLabel[i].sTitle;
			}
		}
		this.Scroll.create(oParams);
	}
};

oPhoto.oContent = {
	iCount: 0
	, create: function(oParams){
		var eItems = Common.Dom.getElementsByClassName(oPhoto.eNavigation, 'reducer')[0], oPage;
		this.hPage = [];
		for(var i = 0; i < eItems.childNodes.length; i++){
			if(eItems.childNodes[i].nodeType != 3 && (eItems.childNodes[i].tagName.toLowerCase() == 'a' || eItems.childNodes[i].tagName.toLowerCase() == 'b')){
				this.iCount++;
				oPage = new oPhoto.oContent.Page({
					eOn: eItems.childNodes[i]
				});
				this.hPage[oPage.sURL] = oPage;
			}
		}
		Common.Event.add(document, 'keydown', function(oEvent) {
				var oEvent = Common.Event.normalize(oEvent);
				if(!oEvent.ctrlKey){
					return true;
				}
				if(oEvent.iKeyCode == 0x27){
					if(oPhoto.oContent.oCurrent.oNext){
						oPhoto.oContent.oCurrent.oNext.click();
					}
				}else if(oEvent.iKeyCode == 0x25){
					if(oPhoto.oContent.oCurrent.oPrev){
						oPhoto.oContent.oCurrent.oPrev.click();
					}
				}
		});
		Common.Event.add(window, 'resize', function(){
			oPhoto.oContent.resize();
		});
		Common.Event.add(oPhoto.eContent, 'mousemove', function(oEvent){
			oPhoto.oContent.mousemove(oEvent);
		});
		Common.Event.add(document.body, 'mouseout', function(oEvent){
			oPhoto.oContent.hide_pager(Common.Event.getAbsoluteCoords(oEvent));
		});
		Common.Event.add(oPhoto.eContent, 'mouseout', function(oEvent){
			oPhoto.oContent.mouseout(oEvent);
		});
		this.oControl.create(oParams);
	}
	, set_current: function(oContent){
		this.oCurrent = oContent;
		this.resize();
	}
	, mousemove: function(oEvent){
		this.oCoords = Common.Event.getAbsoluteCoords(oEvent);
		this.show_pager(this.oCoords);
		this.hide_pager(this.oCoords);
	}
	, show_pager: function(oCoords){
		if(oCoords && oCoords.iTop >= this.oImage_coords.iTop && oCoords.iTop <= this.oImage_coords.iTop + this.oCurrent.eImage.offsetHeight){
			var iLeft = this.iContent_half_width - oCoords.iLeft;
			var iDelta = 1 - (this.iImage_half_width - Math.abs(iLeft)) / this.iImage_half_width;
			iDelta = iDelta <= 1? iDelta: 1;
			if(iLeft > 0){
				if(this.oCurrent.oPrev){
					iLeft = this.iContent_half_width - this.iImage_half_width - iDelta * iDelta * this.iPager_half_width;
					this.oCurrent.oPrev.iLeft = iLeft < -this.iPager_half_width?  -this.iPager_half_width: iLeft;
					this.oCurrent.oPrev.show();
				}
			}else if(this.oCurrent.oNext){
				iLeft = this.iImage_half_width + iDelta * iDelta * this.iPager_half_width - this.iPager_width;
				this.oCurrent.oNext.iLeft = iLeft < 0 ? this.iContent_width - this.iPager_half_width: iLeft;
				this.oCurrent.oNext.show();
			}
		}
	}
	, mouseout: function(oEvent){
		this.hide_pager(Common.Event.getAbsoluteCoords(oEvent));
	}
	, hide_pager: function(oCoords, bNow){
		if(!oCoords){
			if(this.oCurrent.oPrev){
				this.oCurrent.oPrev.hide(0);
			}
			if(this.oCurrent.oNext){
				this.oCurrent.oNext.hide(0);
			}
		}else if(oCoords.iTop < this.oImage_coords.iTop || oCoords.iLeft <=0 || oCoords.iLeft >= this.iContent_width){
			if(this.oCurrent.oPrev){
				this.oCurrent.oPrev.hide(1);
			}
			if(this.oCurrent.oNext){
				this.oCurrent.oNext.hide(-1);
			}
		}
	}
	, resize: function(){
		if(this.oCurrent.eReducer){
			this.oImage_coords = Common.Dom.getAbsoluteCoords(this.oCurrent.eImage);
			var sHeight = this.oControl.bVertical? document.body.offsetHeight - this.oImage_coords.iTop + 'px': (this.oCurrent.eImage.offsetHeight? this.oCurrent.eImage.offsetHeight - 24 + 'px': 'auto');
			this.oCurrent.eReducer.style.height = sHeight;
			if(this.oCurrent.oNext){
				this.oCurrent.oNext.eOn.style.height = sHeight;
			}
			if(this.oCurrent.oPrev){
				this.oCurrent.oPrev.eOn.style.height = sHeight;
			}
			this.iContent_width = document.body.offsetWidth;
			this.iContent_half_width = this.iContent_width / 2;
			this.iImage_half_width = this.oCurrent.eImage.offsetWidth / 2;
			if(this.oCurrent.eImage_ghost){
				this.oCurrent.eImage_ghost.style.left = this.oCurrent.eImage.offsetLeft + 'px';
				this.oCurrent.eImage_ghost.style.width = this.oCurrent.eImage.offsetWidth + 'px';
				this.oCurrent.eImage_ghost.style.height = this.oCurrent.eImage.offsetHeight + 'px';
			}
			this.hide_pager(null);
		}
	}
};

oPhoto.oContent.oControl = {
	create: function(oParams){
		var eParent = document.createElement('div');
		eParent.className = 'control';
		oPhoto.eContent.appendChild(eParent);
		eParent.innerHTML = '<span class="button horizontal selected" id="horizont" title="' + oParams.hLabel.sHorizontal + '"><ins></ins></span><div class="hvtext"  onclick="oPhoto.oContent.oControl.horizontal_mode();"> по ширине </div><span id="vertical" class="button vertical" title="' + oParams.hLabel.sVertical + '"><ins></ins></span><div class="hvtext"  onclick="oPhoto.oContent.oControl.vertical_mode();"> по высоте </div>';
		var aeButton = eParent.getElementsByTagName('span');
		this.eHorizontal = aeButton[0];
		this.eVertical = aeButton[1];
		oMain.fix_hover(this.eHorizontal);
		oMain.fix_hover(this.eVertical);
		Common.Event.add(this.eHorizontal, 'click', function(){
			oPhoto.oContent.oControl.horizontal_mode();
		});
		Common.Event.add(this.eVertical, 'click', function(){
			oPhoto.oContent.oControl.vertical_mode();
		});
	}
	, horizontal_mode: function(){
		this.bVertical = false;
		Common.Class.remove(this.eVertical, 'selected');
		Common.Class.add(this.eHorizontal, 'selected');
		Common.Class.remove(oPhoto.eContent, 'vertical');
		oPhoto.oContent.resize();
	}
	, vertical_mode: function(){
		this.bVertical = true;
		Common.Class.remove(this.eHorizontal, 'selected');
		Common.Class.add(this.eVertical, 'selected');
		Common.Class.add(oPhoto.eContent, 'vertical');
		oPhoto.oContent.resize();
	}
}

oPhoto.oContent.Page = function(oParams){
	var oThis = this;
	this.eOn = oParams.eOn;
	if(this.eOn.tagName.toLowerCase() == 'b'){
		this.sURL = location.href.replace( /^http:\/\/[^\/]+/, '');
		this.bLoaded = true;
		this.eContent = Common.Dom.getElementsByClassName(oPhoto.eContent, 'content')[0];
		if(oMain.need_fix_hover()){
			oMain.fix_hover(this.eOn);
		}
		this.init();
		oPhoto.oContent.set_current(this);
	}else{
		this.sURL = this.eOn.href.replace( /^http:\/\/[^\/]+/, '');
		this.eContent = document.createElement('div');
		this.eContent.className = 'content hidden';
		oPhoto.eContent.appendChild(this.eContent);
		this.oLoader = new Common.Ajax.Helper('', function(o){oThis.onload(o)});
	}
	var iQuery = this.sURL.indexOf('?');
	this.sQuery = iQuery >= 0? this.sURL.substr(iQuery + 1): 0;
	Common.Event.add(oParams.eOn, 'click', function(oEvent){
		oThis.show();
		Common.Event.cancel(oEvent);
	});
};

oPhoto.oContent.Page.prototype = {
	show: function(){
		if(this.bLoaded){
			this.select();
		}else{
			this.oLoader.sUrl = oMain.URL_append(this.eOn.href, 'AJAX=true');
			this.oLoader.send();
		}
	}
	, select: function(){
		this.start_animation();
		window.location.hash = this.sQuery;
	}
	, start_animation: function(){
		this.iStep = 0;
		this.iSteps = 10;
		this.iHalf_step = this.iSteps / 2;
		this.animation();
	}
	, animation: function(){
		this.iStep++;
		if(this.iStep <= this.iSteps){
			if(this.iStep < this.iHalf_step){
				var iOpacity = (this.iHalf_step - this.iStep) / this.iHalf_step;
				oPhoto.oContent.oCurrent.eContent.style.opacity = iOpacity;
				oPhoto.oContent.oCurrent.eContent.style.filter = 'alpha(opacity=' + iOpacity * 100 + ')';
			}else if(this.iStep == this.iHalf_step){
				Common.Class.remove(oPhoto.oContent.oCurrent.eOn, 'selected');
				Common.Class.add(oPhoto.oContent.oCurrent.eContent, 'hidden');
				this.eContent.style.opacity = 0;
				this.eContent.style.filter = 'alpha(opacity=0)';
				Common.Class.remove(this.eContent, 'hidden');
				oPhoto.oContent.set_current(this);
				Common.Class.add(this.eOn, 'selected');
				oPhoto.oContent.show_pager(oPhoto.oContent.oCoords);
				this.resize();
			}else if(this.iStep >= this.iHalf_step){
				var iOpacity = (this.iStep - this.iHalf_step) / this.iHalf_step;
				this.eContent.style.opacity = iOpacity;
				this.eContent.style.filter = 'alpha(opacity=' + iOpacity * 100 + ')';
			}
			var oThis = this;
			setTimeout(function(){oThis.animation();}, 0);
		}
	}
	, resize: function(){
		if(this.eImage.offsetHeight < 30){
			var oThis = this;
			setTimeout(function(){oThis.resize();}, 200);
		}else{
			oPhoto.oContent.resize();
			oPhoto.oContent.show_pager(oPhoto.oContent.oCoords);
		}
	}
	, onload: function(oResponse){
		var sHTML = oResponse.responseText;
		
		//var liveInternetCounter = "<a href='http://www.liveinternet.ru/click' target=_blank><img src='http://counter.yadro.ru/hit?t52.6;r" + escape(document.referrer) + ((typeof(screen)=="undefined")?"":";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth?screen.colorDepth:screen.pixelDepth)) + ";u" + escape('http://"'+ this.eOn.href +'"') + ";" + Math.random() + "' border=0 width=1 height=1 alt='' title='LiveInternet: показано число просмотров и посетителей за 24 часа'></a>";

		this.eContent.innerHTML = oResponse.responseText;

		this.bLoaded = true;
		this.init();
		this.select();
	}
	, init: function(){
		this.eReducer = Common.Dom.getElementsByClassName(this.eContent, 'reducer')[0];
		this.eImage = this.eReducer.getElementsByTagName('img')[0];
		if(this.eImage.runtimeStyle){
			Common.Dom.setStyle(
				this.eImage
				, 'z-index: 2; filter: alpha(opacity=0);'
			);
			var eImage = document.createElement('div');
			Common.Dom.setStyle(
				eImage
				, 'position: absolute; z-index: 1; top: 0; left: 0; width: 100%; height: 1px;'
			);
			this.eReducer.appendChild(eImage);
			this.eImage_ghost = document.createElement('span');
			eImage.appendChild(this.eImage_ghost);
			Common.Dom.setStyle(
				this.eImage_ghost
				, 'position: absolute; z-index: 1; width: ' + this.eImage.offsetWidth + 'px; height: ' + this.eImage.offsetHeight + 'px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+this.eImage.src+'", sizingMethod="scale")'
			);
		}
		var aeA = this.eReducer.getElementsByTagName('a'), oPager;
		for(var i = 0; i < aeA.length; i++){
			oPager = new oPhoto.oContent.Pager({eOn: aeA[i]});
			if(Common.Class.match(aeA[i], 'next')){
				this.oNext = oPager
			}else{
				this.oPrev = oPager;
			}
		}
	}
};

oPhoto.oContent.Pager = function(oParams){
	this.eOn = oParams.eOn;
	oParams.eOn.innerHTML = '<ins></ins>';
	this.eMove = oParams.eOn.firstChild;
	var iWidth = this.eMove.offsetWidth;
	if(iWidth){
		oPhoto.oContent.iPager_width = iWidth;
		oPhoto.oContent.iPager_half_width = iWidth / 2;
	}
	Common.Dom.setStyle(this.eMove, 'display: none;');
	var oThis = this;
	Common.Event.add(oParams.eOn, 'click', function(oEvent){
		oThis.click();
		Common.Event.cancel(oEvent);
	});
};

oPhoto.oContent.Pager.prototype = {
	show: function(iLeft){
		this.eMove.style.display = 'block';
		this.eMove.style.left = this.iLeft + 'px';
		this.iPosition = 0;
		this.iDX = 0;
	}
	, hide: function(iDX){
		if(!iDX){
			this.iDX = 0;
			this.eMove.style.display = 'none'
		}else if(!this.iDX){
			this.iDX = iDX;
			this.iPosition = oPhoto.oContent.iPager_half_width;
			if(this.iLeft || this.iLeft == 0){
				this.hiding();
			}
		}
	}
	, hiding: function(){
		if(this.iPosition){
			this.iPosition--;
			this.iLeft += 2* this.iDX;
			this.eMove.style.left = this.iLeft + 'px';
			var oThis = this;
			setTimeout(function(){oThis.hiding();}, 0);
		}else if(this.iDX){
			this.iDX = 0;
			this.hide(0);
		}
	}
	, click: function(){
		oPhoto.oContent.hPage[this.eOn.href.replace( /^http:\/\/[^\/]+/, '')].show();
	}
};

oPhoto.Scroll = {
	iScroll_button_step: 10,
	timer: 0,
	create: function(oParams) {
		var iMouseX = 0;
		var iHandleX = 0;
		var bIs_drag = false;

		this.eVisible_area = oParams.eNavigation;
		this.eContent = Common.Dom.getElementsByClassName(this.eVisible_area, 'content')[0];
		this.eSlider = document.getElementById('scroll');
		this.eGetter = Common.Dom.getElementsByClassName(this.eSlider, 'getter')[0];
		this.eHandle = Common.Dom.getElementsByClassName(this.eSlider, 'handler')[0];
		this.iCurrent_position = 0;
		this.iMax_displacement = 0;

		this.iButton_width = Common.Dom.getElementsByClassName(this.eSlider, 'button')[0].offsetWidth;
		this.scroll_bar_recalculate();
		this.get_selected_photo();

		this.eGetter.unselectable = "on";

		this.slider();

		var oThis = this;
		Common.Event.add(window, 'resize', function(){
			oThis.scroll_bar_recalculate();
			oThis.content_position_recalculate();
		});
		Common.Class.add(this.eVisible_area, 'inited');
		this.content_position_recalculate();
	},

	buttons: function(oEvent) {
		if(oEvent.clientX < (this.eVisible_area.offsetLeft + this.iButton_width)) {
			this.iNew_position = this.iCurrent_position - this.eHandle.offsetWidth;//this.iScroll_button_step;
		}else if(oEvent.clientX > this.eGetter.offsetWidth) {
			this.iNew_position = this.iCurrent_position + this.eHandle.offsetWidth;//this.iScroll_button_step;
		}else if(oEvent.clientX > (this.eHandle.offsetWidth + 8 + this.iButton_width*2 + this.iCurrent_position)) {
			this.iNew_position = oEvent.clientX - this.iButton_width * 2 - this.eHandle.offsetWidth / 2;
		}else if(oEvent.clientX < (this.iCurrent_position + this.iButton_width*2 - 8)) {
			this.iNew_position = oEvent.clientX - this.iButton_width * 2 - this.eHandle.offsetWidth / 2;
		}else{
			this.start_drag(oEvent);
		}
		if(this.iNew_position < 0 ) {
			this.iNew_position = 0;
		}else if(this.iNew_position > this.iMax_displacement){
			this.iNew_position = this.iMax_displacement;
		}
		this.start_content_scrolling();
	},

	start_content_scrolling: function(){
		this.iSteps = 30;
		this.iStep = 0;
		this.iOld_position = this.iCurrent_position;
		this.iDX = (this.iNew_position - this.iCurrent_position) / this.iSteps;
		this.content_scrolling_animation();
	},

	content_scrolling_animation: function(){
		if(!this.bIs_drag && this.iStep < this.iSteps){
			this.iStep++;
			this.iCurrent_position = this.iOld_position + this.iDX * Math.sqrt(this.iSteps * this.iSteps - (this.iSteps - this.iStep) * (this.iSteps - this.iStep));
			this.content_scrolling();
			var oThis = this;
			setTimeout(function(){oThis.content_scrolling_animation();}, 10);
		}
	},

	content_scrolling: function() {
		this.scroll_bar_recalculate();
		this.eContent.style.left = - this.iCurrent_position * this.correction_calculate() + 'px';
	},

	correction_calculate: function() {
		return this.eContent.offsetWidth / (this.eVisible_area.offsetWidth - 2 * this.iButton_width);
	},

	scroll_bar_recalculate: function() {
		if(this.eVisible_area.offsetWidth >= this.eContent.offsetWidth) {
			var aeButton = Common.Dom.getElementsByClassName(this.eSlider, 'button');
			for(var i=0; i < aeButton.lenght; i++){
				aeButton[i].style.visibility = 'hidden';
			}
			this.eSlider.style.visibility = 'hidden';
			this.eHandle.style.width = '100px';
		}
		else {
			var iCorrection = this.correction_calculate();
			this.eSlider.style.visibility = 'visible';
			this.iSlider_width = this.eVisible_area.offsetWidth - 2 * this.iButton_width;
			this.iHandle_width = this.eVisible_area.offsetWidth * this.iSlider_width / this.eContent.offsetWidth;
			this.eHandle.style.width = this.iHandle_width + 'px';
			this.iMax_displacement = (this.eContent.offsetWidth - this.eVisible_area.offsetWidth) / iCorrection;
			if(this.iCurrent_position > this.iMax_displacement) {
				this.iCurrent_position = this.iMax_displacement;
			}
			else if(!this.iCurrent_position || this.iCurrent_position < 0) {
				this.iCurrent_position = 0;
			}
			this.eHandle.style.left = this.iCurrent_position + 'px';
		}
	},

	content_position_recalculate: function() {
		var iVisible_area_width = this.eVisible_area.offsetWidth;
		var iContent_width = this.eContent.offsetWidth;
		if(iContent_width <= iVisible_area_width) {
			this.eContent.style.left = 0;
		}
		else if(iVisible_area_width > (iContent_width - iVisible_area_width + this.eContent.offsetLeft)) {
			var iCorrection = this.correction_calculate();
			this.eContent.style.left = - this.iCurrent_position * iCorrection + 'px';
		}
	},

	get_selected_photo: function() {
		var eSelected_photo = Common.Dom.getElementsByClassName(this.eContent, 'selected')[0];
		var iLeft_position = eSelected_photo.offsetLeft + eSelected_photo.offsetWidth/2;
		var iVisible_area_width = this.eVisible_area.offsetWidth;
		if(iVisible_area_width/2 < iLeft_position) {
			var iCorrection = this.correction_calculate();
			this.iCurrent_position = (iLeft_position - iVisible_area_width/2) / iCorrection;
			if(this.iCurrent_position > this.iMax_displacement) {
				this.iCurrent_position = this.iMax_displacement;
 			}
		}
		else if(iVisible_area_width/2 > (this.eContent.offsetWidth - iLeft_position)) {
			this.iCurrent_position = this.iMax_displacement;
		}

		this.content_scrolling();
		this.content_position_recalculate();
		this.scroll_bar_recalculate();
	},

	slider: function() {
		var oThis = this;
		Common.Event.add(document, 'mousemove', function(oEvent){
			oThis.drag_handle(oEvent);
		});
		Common.Event.add(document, 'mouseup', function(oEvent){
			oThis.stop_drag(oEvent);
		});
		Common.Event.add(oThis.eGetter, 'mousedown', function(oEvent){
			oThis.buttons(oEvent);
			Common.Event.cancel(oEvent);
		});
	},

	start_drag: function(oEvent) {
		iMouseX = oEvent.clientX;
		iHandleX = this.eHandle.offsetLeft - this.iButton_width;
		this.bIs_drag = true;
	},

	drag_handle: function(oEvent) {
		if(this.bIs_drag) {
			if(oEvent.clientX < 2 || oEvent.clientX > document.body.offsetWidth) {
				this.stop_drag();
			}
			var iDX = oEvent.clientX - iMouseX;
			this.iCurrent_position = iHandleX + iDX;
			if (this.iCurrent_position < 0) {
				this.iCurrent_position = 0;
			}
			else if (this.iCurrent_position > this.iMax_displacement) {
				this.iCurrent_position = this.iMax_displacement;
			}
			this.scroll_bar_recalculate();
			this.content_scrolling();
		}
	},

	stop_drag: function(oEvent) {
		this.bIs_drag = false;
	}
};

