/*

	>
	>	Class SearcBox for page Catalogo.aspx
	>	

*/
var SearchBox = new Class({

	initialize:function() {
		this.status = false;
		this.bg = new Element('div',{
			'class':'bg_overlay',
			'styles':{
				'opacity':0,
				'background':'#E9EDF2 url(images/bg_sub_nav_bar.gif)',
				'position':((!Browser.Engine.trident4) ? 'fixed' : 'absolute'),
				'top':0,
				'left':0,
				'width':'100%',
				'height':'100%',
				'z-index':100
			},
			'tween':{
				duration:150
			},
			'events':{
				'click':this.close.bind(this)
			}
		});
		this.container_box = new Element('div', {
			'class':'container_box',
			'styles': {
				'position':((!Browser.Engine.trident4) ? 'fixed' : 'absolute'),
				'opacity':0,
				'top':((document.getElement('table.item_list')) ? document.getElement('table.item_list').getPosition().y : 0),
				'left':((document.getElement('table.item_list')) ? document.getElement('table.item_list').getPosition().x : 0),
				'z-index':101,
				'overflow':'hidden'
			},
			'tween':{
				duration:200
			},
			'morph':{
				duration:5650,
				link:'chain',
				transition:Fx.Transitions.Back.easeIn
			},
			'html':'<h2>Cerca nel nostro catalogo prodotti</h2>'
			
		});
		
		this.container_box_open_fx = new Fx.Morph(this.container_box,{
				duration:450,
				link:'chain'/*,
				transition:Fx.Transitions.Back.easeOut*/
			});
		this.container_box_close_fx = new Fx.Morph(this.container_box,{
				duration:450,
				link:'chain'/*,
				transition:Fx.Transitions.Back.easeIn*/
			});
		

		this.closeButton = new Element('a',{
			'class':'closeBtn',
			'href':'#',
			'text':'X',
			'title':'Chiudi finestra',
			'morph':{duration:200},
			'events':{
				'click':this.close.bind(this),
				'mouseover':function() {this.morph({'color':'#333'})},
				'mouseout':function() {this.morph({'color':'#BFCAD9'})}
			}
		});
		
		this.bg.inject($E('body'));
		this.container_box.inject($E('form'));
		if($('search_fields'))  $('search_fields').set('styles',{display:'block'}).inject(this.container_box);
		this.closeButton.inject(this.container_box.getElement('h2'));

		this.container_box.store('fHeight',this.container_box.getSize().y.toInt()-54)
		this.containerBoxStatus = {
			opened: {
				'width':600,
				'height':this.container_box.retrieve('fHeight'),
				'top':((this.container_box.retrieve('fHeight')> window.getSize().y.toInt() - document.getElement('#search_box h2').getPosition().y.toInt() ) ? (window.getSize().y.toInt()-this.container_box.retrieve('fHeight'))/2 : document.getElement('#search_box h2').getPosition().y.toInt()  ),
				'left':document.getElement('#search_box h2').getPosition().x.toInt(),
				'opacity':1
			},
			closed: {
				'width':0,
				'height':0,
				/*'height':this.container_box.retrieve('fHeight'),*/
				'left':document.getElement('#search_box h2').getPosition().x.toInt(),
				'opacity':0
			}
		}
		
		if (document.getElement('.open_box')) {
			this.openButton = $E('.open_box');
			this.openButton.addEvent('click', this.open.bind(this));
		} else this.openButton = false;
		
		this.dynamicSelectHandler();
		
		window.addEvent('scroll', this.bgResizeAndScroll.bind(this));
		window.addEvent('resize', this.bgResizeAndScroll.bind(this));
		window.addEvent('keydown',(function(evt) {
			if ((evt.key == "esc")&&(this.status)) this.close();
		}).bind(this));
	},
	close:function() {
		this.bg.get('tween','opacity').start(.85,0);
		//this.container_box.get('tween','opacity').start(1,0);
		this.container_box_close_fx.start(this.containerBoxStatus.closed);

		this.status = false;
		return false;
	},
	open:function() {
		this.bgResizeAndScroll();
		this.bg.get('tween','opacity').start(0,.8);
		
		this.container_box.set('styles',this.containerBoxStatus.closed);
		this.container_box_open_fx.start(this.containerBoxStatus.opened);
		this.status = true;
	},
	bgResizeAndScroll:function() {
		
		this.containerBoxStatus.opened.top = ((this.container_box.retrieve('fHeight')> window.getSize().y.toInt() - document.getElement('#search_box h2').getPosition().y.toInt() ) ? (window.getSize().y.toInt()-this.container_box.retrieve('fHeight'))/2 : document.getElement('#search_box h2').getPosition().y.toInt()  );

		
		if (Browser.Engine.trident4) {
			this.bg.set('styles',{
				'top':window.getScroll().y.toInt(),
				'left':window.getScroll().x.toInt()
			});
		}
	},
	dynamicSelectHandler:function() {
				
		Req = new Request.JSON({
			
			url:'ajax/ajax.aspx',
			onComplete:function(opts) {
				$('ctl00_MasterBody_search_criterio_2').empty().erase('disabled');
				if ($type(opts.options)=='array') {
				
					new Element('option', {
						'value':'',
						'text':'-- Scegli criterio Specifico --'
					}).inject($('ctl00_MasterBody_search_criterio_2'));
					opts.options.each(function(criterio) {
						new Element('option', {
							'value':criterio,
							'text':criterio
						}).inject($('ctl00_MasterBody_search_criterio_2'));
					})
					
				}				
			}
			
		});
		
		$('ctl00_MasterBody_search_criterio').addEvent('change', function() {
			Req.post({'action':'list_dropdown_criterio_2','tipo_criterio':$('ctl00_MasterBody_search_criterio').get('value')})
		});
				
	}

});
window.addEvent('domready',function() {
		
	
	document.getElements('#viewAllProducts').addEvent('click', function() {
		
		$$('fieldset select','fieldset input[type=text]','fieldset input[type=hidden]').set('value','');
		document.getElement('input.submit').click();
		
		return false;
	});
	
	$$('select.multiSelectBox').each(function(s) {
		
		s.store('CriterioList',  new Element('ul',{
				'class':s.get('class')
			})
		);
		
		s.store('CriterioValue', new Element('input',{
				'id':s.get('id'),
				'name':s.get('name'),
				'type':'hidden',
				'value':s.get('value'),
				'style':s.getProperty('style')
			})
		)
		
		
		s.retrieve('CriterioList').store('select',function(evt) {
			s.retrieve('CriterioList').getElements('li').removeClass('selected');
			evt.target.toggleClass('selected');
			s.retrieve('CriterioValue').set('value',evt.target.retrieve('value').trim());
		});
		
		s.getElements('option').each(function(o,i) {
			
			new Element('li',{
				'text':o.get('text'),
				'class':((o.getProperty('selected')) ? 'selected' : ''),
				'events':{
					'click':s.retrieve('CriterioList').retrieve('select')		
				}
			})
			.store('value',o.get('value'))
			.inject(s.retrieve('CriterioList'))
			
		});
		s.retrieve('CriterioList').inject($('ctl00_MasterBody_search_criterio'),'after');
		$('ctl00_MasterBody_search_criterio').dispose();
		s.retrieve('CriterioValue').inject(s.retrieve('CriterioList'),'after');
		
	});
	
	if ($('search_box')) new SearchBox();

	
});