var SearchBox = function(options){

	var box = $('search_form'),
		search_catalog = box.getElement('#search_catalog'),
		countries = box.getElement('#countries'),
		price = box.getElement('#price'),
		settings = box.getElement('#settings'),
		results_container = box.getElement('#results_container'),
		results = box.getElement('#results'),
		loader = box.getElement('#search_loader'),
		form_select = box.getElement('#form_select'),
		search_send = box.getElement('#search_send');
		
	var center = $('lbCenter'),
		image = center.getElement('#lbImage'),
		bottom = $('lbBottom'),
		bottomContainer = $('lbBottomContainer');
	
	var middle = window.getScrollTop() + (window.getHeight() / 2);
	
	var animateCaption = function () {
		fxBottom.set(-bottom.offsetHeight).start(0);
		bottomContainer.style.visibility = "";
	}
	
	var stop = function () {
		center.className = "lbLoading";
		fxResize.cancel();
		fxImage.cancel();
		fxBottom.cancel();
		$$(image, bottomContainer).setStyle("display", "none");
	}
	
	var fxResize = new Fx.Morph(center, $extend({duration: options.resizeDuration, link: "chain"}, options.resizeTransition ? {transition: options.resizeTransition} : {}));
	var fxImage = new Fx.Tween(image, {property: "opacity", duration: options.imageFadeDuration, onComplete : animateCaption});
	var fxBottom = new Fx.Tween(bottom, {property: "margin-top", duration: options.captionAnimationDuration});
	
	var animateBox = function () {
		center.className = "";
		fxImage.set(0);
		image.setStyle('display', '');
		
		var centerWidth = image.offsetWidth,
			centerHeight = image.offsetHeight,
			top = Math.max(0, middle - (centerHeight / 2)),
			check = 0, 
			fn;
		if (center.offsetHeight != centerHeight) {
			check = fxResize.start({height: centerHeight, top: top});
		}
		if (center.offsetWidth != centerWidth) {
			check = fxResize.start({width: centerWidth, marginLeft: -centerWidth/2});
		}
		fn = function() {
			bottomContainer.setStyles({width: centerWidth, top: top + centerHeight, marginLeft: -centerWidth/2, visibility: "hidden", display: ""});
			fxImage.start(1);
		};
		if (check) {
			fxResize.chain(fn);
		}
		else {
			fn();
		}
		
	}
	
	form_select
		.set('send', {
			onComplete: function(text) { 
				results.set('text', text);
				if (text == '0') {	
					search_send.setStyle('visibility', 'hidden');
				}
				else {
					search_send.setStyle('visibility', 'visible');
				}
				animateBox();
			},
			onFailure: function() {
				results.set('text', 0);
				search_send.setStyle('visibility', 'hidden');
				animateBox();
			},
			onCancel: function() {
				results.set('text', 0);
				search_send.setStyle('visibility', 'hidden');
				animateBox();
			}
		});
			
	search_catalog
		.set ('value', 0)
		.onchange = function() {
			
			if (this.get('value') == 0)
			{	
				results_container.setStyle('display', 'none');
				countries.empty();
				price.empty();
				settings.empty();
				return;
			}

			stop();
			
			new Request.JSON({
				
				url: Host + 'json/get_parameters/' + this.get('value'), 

				method: 'get',
						
				onComplete: function(jsonObj) {
					
					countries.empty();
					if ( $chk(jsonObj.countries_list) )
					{	var h2 = new Element('h2', {'text' : jsonObj.common_settings_list.country.name}).inject(countries);
						if (jsonObj.common_settings_list.country.tip)
						{	h2.appendText(' [');
							new Element('a', { 'href': '#', 'class' : 'tip', 'rel' : jsonObj.common_settings_list.country.tip, 'text' : '?'}).inject(h2);
							h2.appendText(']');
						}
						jsonObj.countries_list.each(function(country) {
							new Element ('div', {'html' : '<input id="search_country[' + country.id + ']" type="checkbox" value="1" name="country[' + country.id + ']" /> <label for="search_country[' + country.id + ']">' + country.name + '</label>'}).inject(countries);	
						});
					}
					
					price.empty();
					if (jsonObj.min_price > 0 && jsonObj.max_price > 0)
					{	var h2 = new Element('h2', {'text' : jsonObj.common_settings_list.price.name}).inject(price);
						if (jsonObj.common_settings_list.price.tip)
						{	h2.appendText(' [');
							new Element('a', { 'href': '#', 'class' : 'tip', 'rel' : jsonObj.common_settings_list.price.tip, 'text' : '?'}).inject(h2);
							h2.appendText(']');
						}
						new Element ('div', {'html' : '<input id="search_price[0]" type="checkbox" value="0_'+ jsonObj.min_price + '" name="price[]" /> <label for="search_price[0]">' + 'до ' + jsonObj.min_price + ' руб' + '</label>'}).inject(price);
						
						new Element ('div', {'html' : '<input id="search_price[1]" type="checkbox" value="' + jsonObj.min_price + '_'+ jsonObj.max_price + '" name="price[]" /> <label for="search_price[1]">' + 'от ' + jsonObj.min_price + ' до ' + jsonObj.max_price + ' руб' + '</label>'}).inject(price);
						
						new Element ('div', {'html' : '<input id="search_price[2]" type="checkbox" value="' + jsonObj.max_price + '_0" name="price[]" /> <label for="search_price[2]">' + 'от ' + jsonObj.max_price + ' руб</label>'}).inject(price);
						
					}
					
					settings.empty();
					jsonObj.settings_list.each(function(parameter) {
						var h2 = new Element('h2', {'text' : parameter.name}).inject(settings);
						if (parameter.tip)
						{	h2.appendText(' [');
							new Element('a', { 'href': '#', 'class' : 'tip', 'rel' : parameter.tip, 'text' : '?'}).inject(h2);
							h2.appendText(']');
						}
						parameter.array.each(function(value) {
							new Element ('div', {'html' : '<input id="search_setting[' + parameter.id + '][' + value.id + ']" type="checkbox" value="1" name="setting[' + parameter.id + '][' + value.id + ']" /> <label for="search_setting[' + parameter.id + '][' + value.id + ']">' + value.name + '</label>'}).inject(settings);						
						});
					});
					
					results.set('text', jsonObj.count);
					if (jsonObj.count == '0') {	
						search_send.setStyle('visibility', 'hidden');
					}
					else {
						search_send.setStyle('visibility', 'visible');
					}
					
					$$('input[type=checkbox]').each(function( el ) {
						el.addEvent('click', function() { 
							stop();
							form_select.send();
						});
					});
					/*
					$$('select').each(function( el ) {
						el.addEvent('change', function() { 
							//loader.setStyle('visibility', 'visible');
							center.className = "lbLoading";
							form_select.send();
						});
					});
					*/
					search_send.addEvent('click', function(e) { 
						new Event(e).stop();
						
						form_select
							.set('action', Host + 'search/selection_get')
							.submit();
							
					});
					
					var myTips = new Tips('a.tip', {
						'className' : 'my_tip'
					});
					
					myTips.addEvent('show', function(tip, el){
						tip.fade('in');
					});
					myTips.addEvent('hide', function(tip, el){
						tip.fade('out');
					});
					/**/
					results_container.setStyle('display', 'block');
					animateBox();
					
				}
			}).send();
				
	};
	
};
