var intWidth;
var intHeight;
var hcWidth;
var hcHeight;
var swpWidth;
var move = 0;
var bottomPaddingTop = 60;

window.onresize = onResize;
window.onload = onLoad;

GetWindowSize();

function onLoad() {
	GetWindowSize();	
	hcResize();
	swpResize();
	CatalogPreviewResize();
	CatalogMenuResize();
	UpdateControls();
	UpdateUserForms();
	BottomResize();
}

window.addEvent('domready', function(){
	/*$('creation_button').addEvent('click', function(event){
		window.location = "/darko/creation/";
	});
	$('video_button').addEvent('click', function(event){
		window.location = "/darko/creation/video/";
	});
	$('audio_button').addEvent('click', function(event){
		window.location = "/darko/creation/audio/";
	});*/
});

function GetWindowSize() {
	if (typeof(window.innerWidth) == "number") {  
		intWidth = window.innerWidth;  
   		intHeight = window.innerHeight;
  	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {  
    	intWidth = document.documentElement.clientWidth;  
    	intHeight = document.documentElement.clientHeight;  
  	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {  
    	intWidth = document.body.clientWidth;  
    	intHeight = document.body.clientHeight;
  	}
	intWidth -= 5;
	if (!Browser.Engine.trident)
		intWidth -= 17;
}

function onResize() {
	GetWindowSize();	
	hcResize();
	swpResize();
	CatalogPreviewResize();
	BottomResize();
	//alert(ResizeLayers);
	//ResizeLayers();
}

function swpResize() {
	var m = 10, r;
	if (hcWidth <= 1020)
		m = Math.round((hcWidth - 1000)/2);
	swpWidth = hcWidth - m*2 - 482;
	//hcHeight = 0;
	$$('.wrapper').each(function(wrapper, index){
		wrapper.getElements('.sworkplace').each(function(item, index){
			w = item.getStyle('width').toInt();
			r = item.getStyle('margin-left').toInt();
			if (w != swpWidth) {
				item.setStyle('width', swpWidth);
			}
			if (r != m) {
				item.setStyle('margin-left', m);	
				item.setStyle('margin-right', m);	
			}
			//hcHeight += item.getStyle('height').toInt() + 40;
		});
	});
}

function hcResize() {
	var w, d, x, y = 27;
	$('head_line').setStyle('width', '100%');
	if (intWidth <= 1600) {
		if (intWidth < 1000) {
			hcWidth = 1000;
			w = hcWidth;
			if (!Browser.Engine.trident)
				w += 4;
			$('head_line').setStyle('width', w);
		} else {
			hcWidth = intWidth;
		}
	} else {
		hcWidth = 1600;
	}
	
	hcHeight = 0;
	$$('.hcontent').each(function(item, index){
		w = item.getStyle('width').toInt();
		if (w != hcWidth) {
			item.setStyle('width', hcWidth);
			w = hcWidth;
		}
		if (hcWidth <= 1000) {
			x = 0;
		} else {
			x = Math.round((intWidth - w) / 2);
		}
		item.setStyle('left', x);
		hcHeight += item.getStyle('height').toInt();
	});		
}

function CatalogMenuResize() {
	var h, a, tween;
	$$('.catalog_menu_item').each(function(item, index){
		h = item.getStyle('height').toInt();
		if (h > 0 && item.get('targetHeight') == null) {
			item.set('targetHeight', h);
			item.setStyle('overflow', 'hidden');
			a = item.getElement('a');
			if (!a.hasClass('expanded')) {
s = a.getDimensions();
s.height += 15;
item.set('minHeight', s.height);
				item.setStyle('height', s.height);
			}
e = item.getElements('.sub_items');
if (e != null && e.getElement('a').toString().length > 0)
			a.addEvent('click', function(event){
				$$('.catalog_menu_item').each(function(item, index){
					//item.tween('height', 40);
					tween = item.get('tween', { duration: 'long'});
					tween.start('height', item.get('minHeight'));
					a = item.getElement('a');
					a.removeClass('expanded');
				});
				//this.tween('height', this.get('targetHeight'));
				tween = this.get('tween', { duration: 'long'});
				tween.start('height', this.get('targetHeight'));
				tween.addEvent('complete', function(event){
					BottomResize();
				});
				a = this.getElement('a');
				a.addClass('expanded');
				event.stop();
			}.bind(item));
		}
	});
}

function CatalogMoveContent(catalog, delta) {
	var content = catalog.getElement('.section_content');
    var x = content.getStyle('left').toInt() + delta;
	var w = content.getStyle('width').toInt();
	var a, d = catalog.get('target_width').toInt();
	if (x < 0) {
		if (w+x > d) {
			content.setStyle('left', x);
			a = catalog.getElement('.arrow_right');
			a.setProperty('src', '/bitrix/templates/default/img/arrow1_right.png');
		} else {
			//alert(d + ' '+w+' '+x);
			content.setStyle('left', d-w);
			a = catalog.getElement('.arrow_right');
			a.setProperty('src', '/bitrix/templates/default/img/arrow2_right.png');	
			$clear(move);
			move = 0;
		}
		if (w > d) {
			a = catalog.getElement('.arrow_left');
			a.setProperty('src', '/bitrix/templates/default/img/arrow1_left.png');
		}
	} else {
			//catalog.get('arrow_right').setProperty('src', '/bitrix/templates/default/img/arrow2_right.png');
			content.setStyle('left', 0);
			a = catalog.getElement('.arrow_left');
			a.setProperty('src', '/bitrix/templates/default/img/arrow2_left.png');
			if (w > d) {
				a = catalog.getElement('.arrow_right');
				a.setProperty('src', '/bitrix/templates/default/img/arrow1_right.png');
			}
			$clear(move);
			move = 0;
		}
};

function CatalogPreviewResize() {
	var w = swpWidth - 80;
	var mask, mw, content, al, ar, d, x, timer;
	$$('.catalog_section').each(function(catalog, index){
		mask = catalog.getElement('.mask');
		mw = mask.getStyle('width').toInt();
		mask.setStyle('width', w);
		content = mask.getElement('.section_content');
		al = catalog.getElement('.arrow_left');
		ar = catalog.getElement('.arrow_right');
		d = content.getStyle('width').toInt();
		x = content.getStyle('left').toInt();
		
		catalog.set('target_width', w);
		
		content.getElements('.section_element').each(function(item, index) {
			var h = item.getElement('div');
			var f = item.getElement('.more');
			if (f != null) {
				f = f.getElement('a');
				if (f != null && h != null && h.get('clickEvent') == null) {
					h.set('clickEvent', true);
					h.addEvent('click', function(event){
						if (this.get('target') == '_blank')
							open(this.getProperty('href'));
						else
							window.location = this.getProperty('href');
					}.bind(f));
				}
			}				
		});
		
		if (d <= w) {
			x = Math.round((w-d)/2);
			al.setProperty('src', '/bitrix/templates/default/img/arrow2_left.png');
			ar.setProperty('src', '/bitrix/templates/default/img/arrow2_right.png');
			ar.removeEvents();
			ar.set('timer', null);
			al.removeEvents();
			al.set('timer', null);
			mask.set('wheelEvent', null);
			mask.removeEvents();
		} else {
			if (x > 0) {
				x = 0;
				ar.setProperty('src', '/bitrix/templates/default/img/arrow1_right.png');
			}
			
			if (x < 0) {
				al.setProperty('src', '/bitrix/templates/default/img/arrow1_left.png');
			} else {
				al.setProperty('src', '/bitrix/templates/default/img/arrow2_left.png');	
			}
			
			if (d+x <= w) {
				x += w - mw;
				ar.setProperty('src', '/bitrix/templates/default/img/arrow2_right.png');
			} else {
				ar.setProperty('src', '/bitrix/templates/default/img/arrow1_right.png');
			}
			
			if (mask.get('wheelEvent') == null) {
				mask.set('wheelEvent', true);
				mask.addEvent('DOMMouseScroll', function(event) {
					/*var rolled = 0;
					if (event.wheelDelta === undefined)
						rolled = -40 * event.detail;
					else
						rolled = event.wheelDelta;
					alert(rolled);*/
					var i = -100;
					if (event.wheel > 0) i = 100;
					CatalogMoveContent(this, i);
					event.stop();
				}.bind(catalog));
				mask.addEvent('mousewheel', function(event) {
					var i = -100;
					if (event.wheel > 0) i = 100;
					CatalogMoveContent(this, i);
					event.stop();
				}.bind(catalog));
			}

			if (ar.get('timer') == null) {
				ar.set('timer', true);
				ar.addEvent('mousedown', function(event) {
					this.setProperty('src', '/bitrix/templates/default/img/arrow1_left.png');
					CatalogMoveContent(catalog, -100);
				}.bind(al));
				
			}
			
			if (al.get('timer') == null) {
				al.set('timer', true);
				al.addEvent('mousedown', function(event) {
					this.setProperty('src', '/bitrix/templates/default/img/arrow1_right.png');
					CatalogMoveContent(catalog, 100);
				}.bind(ar));
			}
			
		}
		
		content.setStyle('left', x);
	});
}

function BottomResize() {
	hcHeight = 0;
	$$('.hcontent').each(function(item, index){
		hcHeight += item.getStyle('height').toInt();
	});	
	
	var h = hcHeight+bottomPaddingTop;
	//if (Browser.Engine.trident)
		//h += 20;
	$('bottom').setStyle('top', h);
	$('bottom').setStyle('left', 0);
	$('bottom').setStyle('display', 'inline');
	var w = intWidth;
	if (w < 1000)
		w = 1000;
	$('bottom').setStyle('width', w);
	$('bottom_content').setStyle('width', hcWidth);
	hcHeight += 90 + 40 + 1;
}

function UpdateButtons() {
	var d, t, w;
	$$('.js_input_button').each(function(item, index){
		if (item.get('updated') == null) {
			t = item.get('text');
			item.set('text', '');
			item.addClass('input_button');
			item.set('updated', true);
			item.set('html', '<div class="lc"></div><div class="caption"><div class="shadow">'+t+'</div><div style="color:#ffffff;">'+t+'</div></div><div class="rc"></div>');
			/*item.addEvents({
				'mouseover':function(e){ this.removeClass("input_button"); this.addClass("input_button_hover"); },
				'mouseout':function(e){  this.removeClass("input_button_hover"); this.addClass("input_button"); }
			});*/
			//d = item.getElement('caption')
			//alert(item.get('html'));
		}
	});	
	
	$$('.js_input_button2').each(function(item, index){
		if (item.get('updated') == null) {
			w = item.getStyle('width').toInt();
			if (w > 0) {
				item.setStyle('width', 'auto');
				w = 'width:'+w+'px';
			} else {
				w = '';	
			}
			t = item.get('text');
			item.set('text', '');
			item.addClass('input_button2');
			item.set('updated', true);
			item.set('html', '<div class="lc"></div><div class="caption" style="'+w+'"><div class="shadow" style="'+w+'">'+t+'</div><div style="'+w+'">'+t+'</div></div><div class="rc"></div>');
			/*item.addEvents({
				'mouseover':function(e){ this.addClass("input_button2_hover"); },
				'mouseout':function(e){ this.removeClass("input_button2_hover"); }
			});*/
		}
	});	
}

function UpdateControls() {
	UpdateButtons();
	var s, t, w, input;
	$$('.js_input_select').each(function(item, index){
		if (item.get('updated') == null) {
			s = item.getElement('select');
			s.getElements('option').each(function(option, index){
				t = option.get('text');
				w = option.getStyle('width').toInt();
				if (w > 0) {
					w = 'width:'+w+'px';
					//item.setStyle('width', 'auto');
				} else {
					w = '';	
				}
			});
			s.setStyle('display', 'none');
			item.addClass('input_select');
			item.set('updated', true);
			item.set('html', '<div class="lc"></div><div class="caption" style="'+w+'">'+t+'</div><div class="button"></div>');
		}
	});

	$$('.input_counter2, .input_counter').each(function(item, index){
		if (item.get('updated') == null) {
			input = item.getElement('input');

			var a = .5;
			if (input.getProperty('value').toInt() > item.get('min').toInt()) a = 1;
			item.getElements('.button_dec').each(function(b, index){
				b.set('opacity', this);
				b.setStyle('color', '#ffffff');
			}.bind(a));

			a = .5;
			if (input.getProperty('value').toInt() < item.get('max').toInt()) a = 1;
			item.getElements('.button_inc').each(function(b, index){
				b.set('opacity', this);
				b.setStyle('color', '#ffffff');
			}.bind(a));
		}
	});
	
	$$('.js_input_counter').each(function(item, index){
		if (item.get('updated') == null) {
			
			input = item.getElement('input');
			item.set('html', '');
			item.addClass('input_counter');
			item.set('html', '<div class="lc"></div><div class="caption"><div class="button_dec">-</div></div><input id="'+input.get('id')+'" type="text" name="'+input.get('name')+'" value="'+input.get('value')+'" size="2" maxlength="2" class="caption" /><div class="caption"><div class="button_inc">+</div></div><div class="rc"></div>');
			input = item.getElement('input');

			var a = .5;
			if (input.getProperty('value').toInt() > item.get('min').toInt()) a = 1;
			item.getElements('.button_dec').each(function(b, index){
				b.set('opacity', this);
				b.setStyle('color', '#ffffff');
			}.bind(a));

			a = .5;
			if (input.getProperty('value').toInt() < item.get('max').toInt()) a = 1;
			item.getElements('.button_inc').each(function(b, index){
				b.set('opacity', this);
				b.setStyle('color', '#ffffff');
			}.bind(a));
	
			item.getElement('.button_dec').addEvent('click', function(event){
				var input = this.getElement('input');
				var min = this.get('min').toInt();
				var inc_display = .5;
				i = input.getProperty('value').toInt()-1;
				if (i >= min) {
					input.setProperty('value', i);
					if (i == min)
						event.target.tween('opacity', .5);//event.target.setStyle('display', 'none');
					else
						event.target.tween('opacity', 1);//event.target.setStyle('display', 'inline-block');
				} else
					event.target.tween('opacity', .5);//event.target.setStyle('display', 'none');
				if (input.getProperty('value').toInt() < this.get('max').toInt()) inc_display = 1;
				this.getElements('.button_inc').each(function(b, index){
					b.tween('opacity', this);
				}.bind(inc_display));
				event.stop();
			}.bind(item));
			item.getElement('.button_inc').addEvent('click', function(event){
				var input = this.getElement('input');
				var max = this.get('max').toInt();
				var dec_display = .5;
				i = input.getProperty('value').toInt()+1;
				if (i <= max) {
					input.setProperty('value', i);
					if (i == max)
						event.target.tween('opacity', .5);//event.target.setStyle('display', 'none');
					else
						event.target.tween('opacity', 1);//event.target.setStyle('display', 'inline-block');
				} else
					//event.target.setStyle('display', 'none');
					event.target.tween('opacity', .5);
				if (input.getProperty('value').toInt() > this.get('min').toInt()) dec_display = 1;
				this.getElements('.button_dec').each(function(b, index){
					b.tween('opacity', this);
				}.bind(dec_display));
				event.stop();
			}.bind(item));
			
			
		}
	});

	$$('.js_input_counter2').each(function(item, index){
		if (item.get('updated') == null) {
			
			input = item.getElement('input');
			item.set('html', '');
			//item.addClass('input_text');
			item.set('html', '<div class="caption"><div class="button_dec">-</div></div><input type="text" name="'+input.get('name')+'" value="'+input.get('value')+'" size="2" maxlength="2" class="caption" /><div class="caption"><div class="button_inc">+</div></div>');
			input = item.getElement('input');

			var a = .5;
			if (input.getProperty('value').toInt() > item.get('min').toInt()) a = 1;
			item.getElements('.button_dec').each(function(b, index){
				b.set('opacity', this);
				b.setStyle('color', '#ffffff');
			}.bind(a));

			a = .5;
			if (input.getProperty('value').toInt() < item.get('max').toInt()) a = 1;
			item.getElements('.button_inc').each(function(b, index){
				b.set('opacity', this);
				b.setStyle('color', '#ffffff');
			}.bind(a));
	
			item.getElement('.button_dec').addEvent('click', function(event){
				var input = this.getElement('input');
				var min = this.get('min').toInt();
				var inc_display = .5;
				i = input.getProperty('value').toInt()-1;
				if (i >= min) {
					input.setProperty('value', i);
					if (i == min)
						event.target.tween('opacity', .5);//event.target.setStyle('display', 'none');
					else
						event.target.tween('opacity', 1);//event.target.setStyle('display', 'inline-block');
				} else
					event.target.tween('opacity', .5);//event.target.setStyle('display', 'none');
				if (input.getProperty('value').toInt() < this.get('max').toInt()) inc_display = 1;
				this.getElements('.button_inc').each(function(b, index){
					b.tween('opacity', this);
				}.bind(inc_display));
				event.stop();
			}.bind(item));
			item.getElement('.button_inc').addEvent('click', function(event){
				var input = this.getElement('input');
				var max = this.get('max').toInt();
				var dec_display = .5;
				i = input.getProperty('value').toInt()+1;
				if (i <= max) {
					input.setProperty('value', i);
					if (i == max)
						event.target.tween('opacity', .5);//event.target.setStyle('display', 'none');
					else
						event.target.tween('opacity', 1);//event.target.setStyle('display', 'inline-block');
				} else
					//event.target.setStyle('display', 'none');
					event.target.tween('opacity', .5);
				if (input.getProperty('value').toInt() > this.get('min').toInt()) dec_display = 1;
				this.getElements('.button_dec').each(function(b, index){
					b.tween('opacity', this);
				}.bind(dec_display));
				event.stop();
			}.bind(item));
			
			
		}
	});
	
	$$('.js_input_text').each(function(item, index){
		if (item.get('updated') == null) {
			input = item.getElement('input');
			s = input.getDimensions();
			//item.set('html', '');
			item.grab(new Element('div', {'class':'lc'} ), 'top');
			item.grab(new Element('div', {'class':'rc'} ), 'bottom');
			//item.set('html', '<div class="lc"></div><input type="text" name="'+input.get('name')+'" value="'+input.get('value')+'" class="caption" alt="'+input.getProperty('alt')+'" title="'+input.getProperty('title')+'"/><div class="rc"></div>');
			input.addClass('caption');
			item.addClass('input_text');
			item.set('updated', true);
			input = item.getElement('input');
			//input.setStyle('width', s.width);
			if (Browser.Engine.presto) {
				input.setStyle('padding-bottom', 2);
				input.setStyle('padding-top', 2);
			}
			item.addEvent('click', function(event){
				this.getElement('input').focus();
				event.stop();
			}.bind(item));
			//item.set('html', '<div class="lc"></div><div class="caption" style="'+w+'">'+t+'</div><div class="rc"></div>');
		}
	});
	
	$$('.js_input_text2').each(function(item, index){
		if (item.get('updated') == null) {
			input = item.getElement('input');
			s = input.getDimensions();
			//item.set('html', '');
			item.grab(new Element('div', {'class':'lc'} ), 'top');
			item.grab(new Element('div', {'class':'rc'} ), 'bottom');
			//item.set('html', '<div class="lc"></div><input type="text" name="'+input.get('name')+'" value="'+input.get('value')+'" class="caption" alt="'+input.getProperty('alt')+'" title="'+input.getProperty('title')+'"/><div class="rc"></div>');
			input.addClass('caption');
			item.addClass('input_text2');
			item.set('updated', true);
			input = item.getElement('input');
			//input.setStyle('width', s.width);
			if (Browser.Engine.presto) {
				input.setStyle('padding-bottom', 2);
				input.setStyle('padding-top', 2);
			}
			item.addEvent('click', function(event){
				this.getElement('input').focus();
				event.stop();
			}.bind(item));
			//item.set('html', '<div class="lc"></div><div class="caption" style="'+w+'">'+t+'</div><div class="rc"></div>');
		}
	});
	
	$$('input').each(function(item, index){
		var t = item.get('type');
		//item.set('real_type', t);
		if (t == 'password') {
			t = 'text';
			//item.set('type', 'text');
		}
		
		if (t == 'text' ) {
			title = item.getProperty('title');
			if (title == null)
				title = item.getProperty('alt');
			if (title != null && title.length > 0) {
				
				var l = new Element('div', {'styles':{
												'position':'relative',
												'left': item.getStyle('padding-left').toInt(),
												'top': item.getStyle('padding-top').toInt()+item.getStyle('margin-top').toInt()
											},
											'html':'<div class="overtext">'+title+'</div>',
											'z-index': item.getStyle('z-index').toInt()+1
									});
				l.inject(item, 'before');
				if (item.getProperty('value').length > 0) l.setStyle('display', 'none');
				l.addEvent('click', function(event){
					this.focus();
				}.bind(item));
				
				item.addEvent('blur', function(event){
					if (event.target.getProperty('value').length == 0 && event.target.get('hide_title') == null) {
						this.setStyle('display', 'block');
					}
				}.bind(l));
				
				item.addEvent('focus', function(event){
					this.setStyle('display', 'none');
				}.bind(l));				
			}
		}
	});
	
	$$('.js_input_radio').each(function(item, index){
		if (item.get('rel') == null) {
			i = item.getElement('input');
			item.set('rel', i.getProperty('name'));
			i.setStyle('display', 'none');
			s = item.getElement('a');
			
			if (i.getProperty('checked')) {
				item.addClass('input_radio_checked');
			} else
				item.addClass('input_radio');
				
			s.addEvent('click', function(event){
				i = this.getElement('input');
				$$('div.js_input_radio[rel='+i.getProperty('name')+']').each(function(item, index){
					item.removeClass('input_radio_checked');
					item.addClass('input_radio');
				});
				i.setProperty('checked', true);
				item.addClass('input_radio_checked');
				item.removeClass('input_radio');
				event.stop();
			}.bind(item));
			
		}
	});
	
	UpdateInputSelect();
}

function UpdateInputSelect() {
	var reset_selects = function(s, index){
			sub = s.getElements('.sub');
			sub.setStyle('height', 0);
			s.setStyle('z-index', s.get('initZ'));
		}
	
	$$('.js_select').each(function(s, index){
		s.removeClass('js_select');
		d = new Element('div', {'class':'input_select'});
		t = new Element('div', {'class':'control'});
		var c = new Element('div', {'class':'caption'});
		t.adopt(new Element('div', {'class':'lc'}), c, new Element('div', {'class':'button'}));
		d.adopt(t, new Element('div', {'class':'control'}), new Element('div', {'styles':{'clear':'both'} }));
		table = new Element('table', {
					'border':0,
					'cellpadding':0,
					'cellspacing':0,
					'width':'100%',
					'class':'items'
				});
		sub = new Element('div', {'class':'sub'});
		d.adopt(sub.adopt(table));
		
		head = new Element('td', {'html':'&nbsp;'});
		table.adopt(new Element('thead').adopt(new Element('tr').adopt(head)));
		b = new Element('tbody');
		table.adopt(b);
		
		d.inject(s, 'after');
		s.setStyle('display', 'none');
		
		width = s.getDimensions().width+7;
		g = false;
		var text;
		var pl = 0;
		selected = false;
		s.getElements('').each(function(o, index){
			tag = o.get('tag');
			td = new Element('td');
			if (tag == 'optgroup') {
				text = o.get('label');
				if (text.length > 0) {
					td.set('text', text);
					td.addClass('group');
					b.adopt(new Element('tr').adopt(td));
				}
				g = true;
			} else if (tag == 'option') {
				
				if (pl > 0)	td.setStyle('padding-left', pl);
				if (o.hasClass('group')) {
					td.setStyle('padding-left', 8);
					pl = 24;
				}
				if (o.hasClass('ungroup')) pl = 0;
				
				text = o.get('text');
				if (text.length > 0) {
					a = new Element('a', {'text':text});
					b.adopt(new Element('tr').adopt(td.adopt(a)));
					if (o.get('rel') != null) {
						//c.set('text', text);
						//a.addClass('selected');
						selected = true;
//alert(o.getProperty('selected'));
					}				
					a.set('rel', o.index);
					a.set('value', o.value);
					a.addEvent('mousedown', function(e){
						c.set('text', e.target.get('text'));
						this.getElements('a[class=selected]').each(function(i, index){
							i.removeClass('selected');
						});
						s.selectedIndex = e.target.get('rel').toInt();
						if (s.onchange != null)
							s.onchange(null);
						e.target.addClass('selected');
					}.bind(table));
				}
				
			}
		});

		td.setStyle('width', width+21);
		d.setStyle('width', width);
		c.setStyle('width', width-37);
		
		//head.setStyle('width', width-1);
		table.adopt(new Element('tfoot').adopt(new Element('tr').adopt(new Element('td', {'width':width+26}).adopt(new Element('div').adopt(new Element('div'))))));
		
		title = s.get('title');
		if (!selected /*&& s.selectedIndex == 0*/ && title != null && title.length > 0) {
			c.set('text', title);
			s.selectedIndex = -1;
		} else {
			table.getElements('a[rel='+s.selectedIndex+']').each(function(i, index){
				i.addClass('selected');
				c.set('text', i.get('text'));
			});
		}
			
		h = sub.getDimensions().height;
		sub.set('initHeight', h);
		d.set('initZ', d.getStyle('z-index'));
		sub.setStyle('height', 0);
		t.addEvent('mousedown', function(e){
			e.stop();
			$$('.input_select').each(reset_selects);
			this.setStyle('z-index', '1000');
			sub = this.getElements('.sub')[0];
			sub.tween('height', sub.get('initHeight').toInt());
		}.bind(d));
		
		s.addEvent('change', function(e){

			table.getElements('a[class=selected]').each(function(i, index){
				i.removeClass('selected');
			});
			table.getElements('a[rel='+s.selectedIndex+']').each(function(i, index){
				i.addClass('selected');
				c.set('text', i.get('text'));
			});
		});
		
	});
	
	if (Browser.Engine.trident) {
		target = document;
	} else
		target = window;
	target.addEvent('mousedown', function(e){
		$$('.input_select').each(reset_selects);
	}.bind(d));	
}

function RequestContent(target, url, params) {
	$(target).set('html', '<img src="/bitrix/images/install/wait.gif" />');
	new Request.HTML({'url':url, 'update':$(target)}).get(params);
}

function UpdateUserForms() {
	var h, a;
	UpdateButtons();
	$$('.user_form').each(function(item, index){
		h = item.getStyle('height').toInt();
		if (h > 0 && item.get('targetHeight') == null) {
			item.set('targetHeight', h);
			item.setStyle('overflow', 'hidden');
			if (item.get('rel') != 'show')
				item.setStyle('height', 0);
		}
	});
	a = $('default_user_form');
	if (a != null) {
		h = a.getStyle('height').toInt();
		a.set('targetHeight', h);
		a.setStyle('overflow', 'hidden');
		a.addClass('user_form');
	}
}

function ShowUserForm(id) {
	var tween;
	$$('.user_form').each(function(item, index){
		//item.tween('height', 0);
		tween = item.get('tween', { duration: 'long'});
		tween.start('height', 0);
	});
	//$(id).tween('height', $(id).get('targetHeight'));
	tween = $(id).get('tween', { duration: 'long'});
	tween.start('height', $(id).get('targetHeight'));
	tween.addEvent('complete', function(event){
		BottomResize();
	});
}

function ApllyFilter(filters) {
  var l = new Array();
  var p = new Array();
  var v = '', a='';
	$(filters).getElements('select').each(function(s, index){
		if (s.selectedIndex > -1) {
	    	if (s.options[s.selectedIndex].value.length > 0)
    		    v = s.options[s.selectedIndex].value;
    		else
    	    	v = s.options[s.selectedIndex].get('text');
	    	if (v.length > 0)
    		    a += '&'+s.getProperty('name') + '=' + v;
    		    l.push(v);
    		    p.push(s.getProperty('name'));
		}
    });
	if (l.length > 0)
    	//window.location = '/catalog/search/?'+l;
    	    	//new Request.HTML({'url':'/catalog/search/content.php', 'update':$('catalog_section')}).get('a'+l);
//$('catalog_section').load('/catalog/search/content.php?a'+l);
//alert(l.toString() +"\n"+p.toString());
RequestContent('workplace', '/catalog/search/content.php', l.associate(p));
	//alert(l.length + ' '+l);
}

function SendQuestion(target) {
  	if ($(target) == null) return false;
  	GlobalHideLayers = false;
	jsAjaxUtil.ShowLocalWaitWindow('order_wait', target, true);
	var req = new Request.HTML({'url':'/personal/send_question.php', 'update':$(target)}).post($(target));
	req.addEvent('success', function(e){
		GlobalHideLayers = true;
		jsAjaxUtil.CloseLocalWaitWindow('order_wait', this);
		UpdateControls();
	}.bind(target));
}

function PostContainer(target, url, params) {
  	if ($(target) == null) return false;
  	GlobalHideLayers = false;
	jsAjaxUtil.ShowLocalWaitWindow('container_wait', target, true);
	var req = new Request.HTML({'url':url, 'update':$(target)}).post(params);
	req.addEvent('success', function(e){
		GlobalHideLayers = true;
		jsAjaxUtil.CloseLocalWaitWindow('container_wait', this);
		UpdateControls();
	}.bind(target));
}

function RequestProduct(target, id, template) {
  	if ($(target) == null) return false;
  	GlobalHideLayers = false;
	jsAjaxUtil.ShowLocalWaitWindow('container_wait', target, true);
	var req = new Request.HTML({'url':'/catalog/product.php', 'update':$(target)}).post({'Product[id]':id, 'Product[template]':template});
	req.addEvent('success', function(e){
		GlobalHideLayers = true;
		var title = $(target).getElement('div').getElement('h1').get('text');
		if ($('page_title') != null)
			$('page_title').set('text', title);
		document.title = title;
		jsAjaxUtil.CloseLocalWaitWindow('container_wait', this);
		UpdateControls();
		milkbox = false;
		DescLoad();
		CatalogElementResizeDesc();
	}.bind(target));
}

function multi_basket(authorized){

	if( authorized != true ){
		alert('Пожалуйста авторизуйтесь, войдите под своим логином и паролем.');
		return;
	}

	var arr = $$('.size_value input');

	var items = [];

	var regexp = new RegExp('item_(\\d+)', 'i');


	for( var i in arr ){
		var input = arr[ i ];

		var value = parseInt(input.value);
		var name = input.name;


		var matches = name.match( regexp );


		if( input.value > 0 )
			// items[ items.length ] = [ matches[1], value ];
			items[ items.length ] = matches[1] + '=' + value;

	}



	var str = items.join('&');

	// console.log( items );
	//console.log( str );

	var myRequest = new Request({
		url: '/personal/basket/multi_add.php',
		method: 'post',
		data: str,
		onSuccess: function(html){

			document.getElementById('basket').innerHTML = html;


		}
	});

	myRequest.send();





}



function show_price(id){
//	console.log(id);
	var el_id = 'price_' + id;
	$(el_id).addClass('current');
	return true;
}


function hide_price(id){
	//$$('price').removeClass('current');

	$$('.price').removeClass('current');

	//$('body_wrap').getElement('.special_anchor_class');

	return true;
}


