var obj = new Array(),
	objMorph = new Array(),
	n = -1,
	onL = -1;
	

function open_big_pic() {
	alert('jg');
}


function init_all(){
	
		Fx.Morph = Fx.Styles.extend({
			
			start: function(className){
					
				var to = {};
					
				$each(document.styleSheets, function(style){
					var rules = style.rules || style.cssRules;
					$each(rules, function(rule){
						if (!rule.selectorText.test('\.' + className + '$')) return;
						Fx.CSS.Styles.each(function(style){
							if (!rule.style || !rule.style[style]) return;
							var ruleStyle = rule.style[style];
							to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
						});
					});
				});
				return this.parent(to);
			}
				
		});
		
		Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
			
		Fx.CSS.Styles.extend(Element.Styles.padding);
		Fx.CSS.Styles.extend(Element.Styles.margin);
			
		Element.Styles.border.each(function(border){
			['Width', 'Color'].each(function(property){
				Fx.CSS.Styles.push(border + property);
			});
		});
			
			
		var slides = $$('.slide');
		
		for (var i = 0; i < slides.length; i++) {
			var idName = slides[i].id;
			
			slides[i].style.display = "block";
			
			var idName2;
			
			idName2 = idName.replace('open_', '');
			
			idCount = idName.replace('block_sl', '');
			idCount = idCount.replace('open_', '');
			obj[idCount] = new Fx.Slide(idName, {mode: 'horizontal', duration: 200, wait:false});
			
			if (idName != idName2) { n = idCount; }
			
			idName = idName.replace('block_sl', 'openB_sl');
			idName = idName.replace('open_', '');
			
			objMorph[idCount] = new Fx.Morph(idName, {duration: false, wait: false});
			
			
			obj[idCount].hide();
			
			$(idName).addEvent('click', function(e){
				e = new Event(e);
				var id = this.id.replace('openB_sl', '');
				if ((onL !=-1) && id != onL) {
					obj[onL].slideOut();
					objMorph[onL].start('slideOut');
					var idd = 'openB_sl'+onL;
					document.getElementById(idd).style.fontweight = "bold";
					
				}
				e.stop();
				
				e2 = new Event(e);
				obj[id].slideIn();
				
				objMorph[id].start('slideIn');
				
				onL = id;
				var idd = 'openB_sl'+onL;
					document.getElementById(idd).style.fontweight = "normal";
				e2.stop();
			});
			
			
		}
		if (n != -1) {onL=n; obj[n].show();}
		
		
		$$('.add_cart').addEvent('click', function(e) {
				e = new Event(e).stop();
				var d = this.getProperty('href');
				var title = this.getProperty('title');
				
				poss = d.lastIndexOf('pid=');
				d = d.substring(poss);

				var url = "/ajax.php?" + d;
				
				var coord = this.getCoordinates();
	
    			var wind = new Element('div', {
    				'class': 'already_in_cart',
    				'styles': {
				        'margin-top': 55,
				  	 	'left': coord['left']+20
					}
    			}).setHTML('Загрузка...').injectBefore(this);
    			
				new Ajax(url, {
					method: 'get',
					update: $('cart-count'),
					onComplete: function(){
						
						title = '«<span class="item_capt">'+ title +'</span>»<br/>добавлен в заявку'
						wind.setHTML(title);
						
						(function(){ 
							wind.setStyles({
								display: 'none'
							});
							
							wind.remove();
						}).delay(4000);
					}
				}).request();
				
			});
		
		
		$$('.openpic').addEvent('click', function(e) {
				e = new Event(e).stop();
				
				var g = this.firstChild.innerHTML;
				
				var coord = this.getCoordinates();
				
				var wind = new Element('div', {
    				'class': 'already_in_cart',
    				'styles': {
    					'position': 'absolute',
				        'margin-top': 55,
				  	 	'left': coord['left']+20
					},
					'events': {
				        'click': function(){
				            // alert('clicked');
				            wind.remove();
				        }
				    }

    			}).setHTML(g).injectBefore(this);
				
				// alert(g);
				
		});
		
		Lightbox.init(Lightbox);
		/// calc
		
		
		
		
		if ($('myForm')) {
			
			
			var base_data = new Fx.Slide('base_data');
			var myFx = new Fx.Scroll(window);
			
			document.getElementById('log').style.display = 'none';
			$('izmenit').addEvent('click', function(e) {
				new Event(e).stop();
				document.getElementById('log').style.display = 'none';
				
				base_data.slideIn();
			});
			$('myForm').addEvent('submit', function(e) {
			
				new Event(e).stop();
			 
				var log = $('log_res').empty();
				var stat = $('status');
				stat.innerHTML="Загрузка...";
				
				this.send({update: log}).chain(function() {
					if(log.innerHTML != 'false') {
						stat.innerHTML = "";
						myFx.toElement('cart-count', {duration: 50 ,wait: false}).chain(
							function () {
								base_data.slideOut();
								document.getElementById('log').style.display = 'block';		
							}
						);
					} else {stat.innerHTML="Не все поля заполнены!";}
				});
			});	 
			
			
			// описание датчика
			$$('.qwe_temp').addEvent('click', function(e) {
				
				URL = '/calc.php?desc=k&did='+this.id;
				
				$('t_desc').setHTML('Загрузка...');
				new Ajax(URL, {
					method: 'get',
					update: $('t_desc'),
					onComplete: function(){}
				}).request();
			});
		}
};

