﻿var parts = false;

Slimbox.scanPage = function(moduls) {
	var filtered = moduls.filter(function(el) {
		return el.rel && el.rel.test(/^lightbox/i);
	});
	filtered.each(function(item){
		if (item.get('rel').test(/^lightbox-parts-modul-/i)) {
			parts = true;
			item.set('href', Host + 'ajax/parts_modul/' + item.get('rel').replace('lightbox-parts-modul-', '') + '/' + item.get('href').replace(new RegExp(Host), ''));
		}
		else {
			item.set('href', Host + 'ajax/mebel_modul/' + item.get('rel').replace('lightbox-modul-', '') + '/' + item.get('href').replace(new RegExp(Host), ''));
		}
	});
	
	filtered.slimbox({ }, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
};

window.addEvent('domready',function(){
	Slimbox.scanPage($$('ul.modul_list a'));
	
	var headings = $$('a.heading');
	var img_headings = $$('img.img_heading');
	var list = $$('.slide .container');
	var collapsible = [];
	
	headings.each( function(heading, i) {
		
		collapsible[i] = new Fx.Slide(list[i]);
		
		heading.onclick = function(e) {
			
			new Event(e).stop();
			
			if ( ! $chk ( list[i].hasClass('open') )) 
			{
				collapsible[i].hide();
				
				img_headings[i].set('src', '/templates/prosmebel/img/loader.gif');
				
				var rel = this.get('rel').replace(/mg_id_/, '');
				var parentLink = this.get('href').replace(new RegExp(Host), '');
				new Request.JSON({
					url: Host + 'json/get_price_list/' + rel + '/'+ parentLink, 
		            method: 'get',
					onComplete: function(jsonObj) {
						list[i].empty();
						new Element('div', {'class': 'mg_opis', 'html': jsonObj.desc}).inject( list[i] ); 
						
						var ul = new Element('ul', {'class': 'list modul_list'}).inject( list[i] );
						
						jsonObj.list.each(function(item) {
							var li = new Element('li').inject(ul);
							new Element ('img', {'src' : '/templates/prosmebel/img/1px.gif', 'alt' : item.pict_alt, 'title' : item.pict_alt, 'style' : 'background: url(/images/' + ( parts ? 'parts/' : '' ) + 'modul/' + item.pict + ') no-repeat center;' } ).inject(
								new Element ('a', {'href' : Host + jsonObj.url + '/' + item.link, 'rel' : 'lightbox-modul-' + item.id, 'title' : item.pict_alt } ).inject( li )
							);
							var h3 = new Element ('h3', {'class' : 'name'}).inject(li);
							new Element ('img', {'src' : '/templates/prosmebel/img/cik.gif'}).inject(h3);
							h3.appendText(' ');
							new Element ('a', {'href' : '/' + jsonObj.url + '/' + item.link, 'text' : item.articul, 'rel' : 'lightbox-modul-' + item.id, 'title' : item.pict_alt } ).inject(h3);
							
							new Element ('div', {'class' : 'complect', 'text' : item.name}).inject(li);
							
                            /*
                            if (item.desc)
                                new Element ('div', {'class' : 'complect', 'html' : item.desc}).inject(li);
                            */
                                
							if (item.size)
								new Element ('div', {'class' : 'complect', 'html' : item.size}).inject(li);
							if (item.diametr)
								new Element ('div', {'class' : 'complect', 'html' : item.diametr}).inject(li);
							
							if (item.price)
								new Element ('div', {'class' : 'price', 'html' : '<img src="/templates/prosmebel/img/punct_line_s.gif" />цена: <strong>' + item.price + '</strong> руб' + 
									
									(item.dealer_price ? '<br/>для дилеров: <strong>' + item.dealer_price + '</strong> руб' : '') + 
									
									'<img src="/templates/prosmebel/img/punct_line_s.gif" />'}).inject(li);
							
							if (item.novinka == "1")
								new Element ('div', {'class' : 'sklad', 'text' : 'новинка'}).inject(li);
							
							if (item.price)
								new Element ('div', {'class' : 'buy', 'html' : '<noindex><img src="/templates/prosmebel/img/arrr.gif" /> <a rel="nofollow" href="' + '/' + jsonObj.url + '/' + item.link + '/buy' + '">купить</a></noindex>'}).inject(li);
							
						});
						
						list[i].addClass('open');
						
						img_headings[i].set('src', '/templates/prosmebel/img/tri4.gif');
						/*
						$$('ul.modul_list a img').each( function(el) {
							el.addEvents({
								'mouseover': function(){ this.set( 'src', '/templates/prosmebel/img/rama_modul_on.gif'); },
								'mouseout': function(){ this.set( 'src', '/templates/prosmebel/img/rama_modul.gif'); }
							});
						});
						*/ 
						Slimbox.scanPage( $$('ul.modul_list a') );
						ScanLbLinks( $$('a.lb_add') );
						
						collapsible[i].slideIn();
						//collapsible[i].toggle();
					}
				}).send();
		
				
			}
			else
			{	
				if ( img_headings[i].get('src') == '/templates/prosmebel/img/tri3.gif' )
					img_headings[i].set('src', '/templates/prosmebel/img/tri4.gif');
				else
					img_headings[i].set('src', '/templates/prosmebel/img/tri3.gif');
				collapsible[i].toggle();
			}
		};
	});
	
	
});

