var WinX = {
	init: function(){
		this.loader = new Element('div', {'id': 'loader'}).injectInside($('header'))
		this.popup = new Element('div', {'id': 'window'}).injectInside($('header'));
		this.popup.innerHTML = '<div id="wtp"></div><div id="wmain"></div><div id="wft"></div><div id="wcbtn"></div>';

		addBuilding = this.addBuilding.bind(this)
		this.step = 1;
		resize = this.resize.bind(this)
		this.fx = this.loader.effects({duration: 600, onComplete: resize ,transition: Fx.Transitions.Quart.easeOut});
		
		$('Map1').onclick = this.open.pass('1')
		$('Map2').onclick = this.open.pass('2')
		//$('Map3').onclick = this.loader.pass('3')
		$('b1_y').firstChild.onclick = this.open.pass('1')
		$('b2_y').firstChild.onclick = this.open.pass('2')
		$('wcbtn').onclick = this.close.pass();
	},
	open: function(build){
		WinX.resize();
		$('wmain').innerHTML = '<div id="popup_build"><div class="pcol1"><h1 id="centr_name"></h1><div id="galery">'+
		'<div id="g_overlay"></div><div id="g_navigation"><div id="g_slide_s"></div></div><div id="g_window"></div>'+
		'</div></div><div class="pcol2"><h3><a id="centr_page" href="#">более подробная информация о центре посмотреть на карте услуг</a></h3>'+
		'<div id="wtext"></div></div></div>';
		LimeBox.init();
		new Ajax(sitelink+'data.php', { method: 'post', onComplete: addBuilding }).request('build='+build);
		return false;
	},
	close: function(){
		if(periodical) clearTimeout(periodical)
		$('window').style.left = '-5000px';
		return false;
	},
	addBuilding: function(build) {
		build = Json.evaluate(build);
		$('g_window').empty();
		build.images.each(function(img){
			var img = new Element('img', {'src': sitelink+'slideshow/' + img[0] , 'alt' : img[1]}).injectInside($('g_window'));
		})
		$('centr_name').innerHTML = build.centr_name;
		$('wtext').setHTML(build.text)
		$('centr_page').href = build.centr_page
		this.resize()
		return LimeBox.setup();
	},
	resize: function(){
		switch (this.step++){
		case 1:
			this.loader.style.display = 'block';
			this.loader.className = 'loading';
		break;
		case 2:
			this.loader.className = 'load';
			this.fx.start({'height': this.popup.offsetHeight-39 })
		break;
		case 3:
			this.fx.start({'width': '778','margin-left': '-390'});
		break;
		case 4:
			this.loader.style.display = 'none';
			this.popup.style.left = '50%';
			this.step = 1;
			this.loader.setStyles({
			'display' : 'none',
			'width': 100,
			'height': 100,
			'margin-left': '-50px'
			});
		break;
		}
	}
}

var SliderX = {

	init: function(id,options){
		this.options = $extend({
			posOn: 342,
			posOff: 110,
			toggler: $('showhide')
		}, options || {});
		
		this.container = $(id);
		this.options = options ? options : this.options;
		this.slidefx = this.container.effect('height', {'duration' : 600, 'fps' : 300});
		if(this.options.toggler){
		this.options.toggler.onclick = this.slider.bind(this);
			if(Storage.get('layer')=='off'){
				$$('div#build_area div.layerOn').each(function(el){ el.className = 'layerOff'; });
				this.slidefx.set(this.options.posOff);
				this.options.toggler.style.backgroundPosition = '0 -23px';
			}
		}
		return this;
	},
	slider: function(){
		if(Storage.get('layer')=='off'){
			this.slidefx.start(this.options.posOn).chain(function(){
				$$('div#build_area div.layerOff').each(function(el){ el.className = 'layerOn'; });	
			});
			this.options.toggler.style.backgroundPosition = '0 0';
			Storage.remove('layer');
		} else {
			$$('div#build_area div.layerOn').each(function(el){ el.className = 'layerOff'; })
			this.slidefx.start(this.options.posOff);
			this.options.toggler.style.backgroundPosition = '0 -23px';
			Storage.put('layer','off');
		}
		
	}
}

window.addEvent('domready', function(){
	sitelink = 'http://mo-zdorovie.tomsk.ru/';
	Storage.init();
	WinX.init();
	SliderX.init('build_area');
	if(window.ie){$$('.noblur').each(function(el){ el.addEvent('focus',function(){el.blur();}); });}
	if($('rubrics')){
		var count = -1;
		$$('ul#rubrics li.toggler').each(function(el,i){
			if(el.getElement('a#active_menu')){
				count = i;
			}
		});
		var accordion = new Accordion('li.toggler span', 'ul.element', {
			show: count,
			onActive: function(toggler, element){
			toggler.parentNode.className = 'toggler active';
			element.className = 'element active';
			},
			onBackground: function(toggler, element){
			toggler.parentNode.className = 'toggler inactive';
			element.className = 'element';
			}
		}, $('rubrics'));
	}
});