var CMainSlider = Class.create({
	initialize: function() {
		var obj = this;
		this.mainLayer = $('content').down('.container'),
		this.slider = $('main_slider'),
		this.sliderHandle = this.slider.down('.handle');
		
		if($('home')) this.topMargin += $('home').getHeight();
		if($('submenu')) this.topMargin += $('submenu').getHeight();
		$('main_slider').setStyle({marginTop: this.topMargin + 'px'});
		
		// --- Set onservers ---
		Event.observe(window, 'resize', onWindowResize = function(event){
			obj.onWindowResize();
		});
		
		Event.observe('content', 'mousewheel', function(event){
			obj.onScroll(event);
		});
		Event.observe('content', 'DOMMouseScroll', function(event){
			obj.onScroll(event);
		});
		
		$$('a[href="#top"]').each( function(elem){
			Event.observe(elem, 'click', function(event){
				if(obj.csSlider) obj.csSlider.setValue( 0 );
			});
		} );
		
		this.onWindowResize();
	},//---------------------------------------------------------------------
	
	onWindowResize: function() {
		var obj = this,
			contentHeight = $('content').getHeight();
		
		this.sliderRange = this.mainLayer.getHeight() - contentHeight;
		if(this.sliderRange < 3) {
			this.slider.hide();
			return;
		} else this.slider.show();
		this.trackLength = contentHeight - this.topMargin;
		this.slider.setStyle({'height': this.trackLength + 'px'});	// уменьшаем высоту дорожки ползунка
		if(this.csSlider) {
			this.csSlider.range.end = this.sliderRange;
			this.csSlider.trackLength = this.trackLength;
			this.csSlider.setValue( this.csSlider.value );
		}
		else this.csSlider = new Control.Slider(this.sliderHandle, this.slider, {
			axis: 'vertical',
			range: $R(0, this.sliderRange),
			sliderValue: this.sliderValue,
			onSlide: function(value) {
				obj.sliderValue = value;
				obj.mainLayer.setStyle({ marginTop: -value + 'px' });
			},
			onChange: function(value) {
				obj.sliderValue = value;
				obj.mainLayer.setStyle({ marginTop: -value + 'px' });
			}
		});
	},//---------------------------------------------------------------------
	
	onScroll: function(event) {
		var step = event.detail? event.detail * 120 : -event.wheelDelta;
		step = (step > 0)? 100 : -100;	// из-за разного шага в разных браузерах приводим его к единому значению
		this.csSlider.setValue( this.csSlider.value + step );
		//disable default wheel action of scrolling page
		if(event.preventDefault) event.preventDefault();
		else return false;
	},//---------------------------------------------------------------------
	
	slider: null,
	sliderHandle: null,
	csSlider: null,
	sliderValue: 0,
	sliderRange: 0,
	trackLength: 0,
	mainLayer: null,
	topMargin: 0
});
/////////////////////////////////////////////////////////////////////////////


var CContentListImageIterator = Class.create({
	initialize: function(listItem) {
		var obj = this;
		this.listItem = listItem;
		this.header = listItem.down('h2 a');
		this.image = listItem.down('.photo');
		
		// highlight the photo when move mouse under header
		Event.observe(this.header, 'mouseover', function(){
			obj.image.addClassName('photo-hover');
		});
		Event.observe(this.header, 'mouseout', function(){
			obj.image.removeClassName('photo-hover');
		});
		// highlight the  header when move mouse under photo
		Event.observe(this.image, 'mouseover', function(){
			obj.header.addClassName('hover');
		});
		Event.observe(this.image, 'mouseout', function(){
			obj.header.removeClassName('hover');
		});
	},//---------------------------------------------------------------------
	
	listItem: null,
	header: null,
	image: null
});
/////////////////////////////////////////////////////////////////////////////


Event.onReady( function(){
	var slider = new CMainSlider();
	
	$$('#content .pagelist-img-item').each( function(listItem){
		new CContentListImageIterator(listItem);
	} );
	
	// Переключение языков
	$$('#lang-menu a').each( function(elem){
		Event.observe( elem, 'click', function(event){
			var btn = this, url = this.href;
			this.href = 'javascript:;';
			var conn = new CSrvConnect(null, '/srv/lang/get_url/', '', true);
			conn.onLoad = function(t){
				var json;
				try { json = eval('(' + t.responseText + ')'); }
				catch(e) { return; }
				if(typeof(json)!='undefined' && json.url) {
					document.location = json.url;
					Event.stop(event);
					btn.href = 'javascript:;';
					return false;
				}
				else {
					btn.href = url;
				}
			}
			conn.load( {'url':url} );
		} );
	} );
} );
//---------------------------------------------------------------------------
