var custom_ob = {
	'name' : ['TheDropDowns'],
	'namehash' : {
		'category1' : {'columns' : [], 'next' : 'category2'},
		'category2' : {'columns' : ['category1'], 'next' : 'category3'},
		'category3' : {'columns' : ['category1', 'category2'], 'next' : 'name'},
		'name' 		: {'columns' : ['category1','category2', 'category3'], 'next' : 'sku'},
		'sku' 		: {'columns' : ['category1','category2', 'category3','name'], 'next' : ''}
	},
	'skiphash' : {
		
 'category2' : { 'Dishwashers' : 'name'}
        
 
	}
};

var Custom = Class.create({});
Custom.TheDropDowns = Class.create(Custom, 
{
	initialize : function(locator){
		this.locator = locator;
		this.dropChange($('category1_row'), false);
	},
	
	dropChange : function(el, updateFire){
		$('local_comp_panel').innerHTML = '';
		var ob = (el.target) ? el.target : el;
		var name = '';
		var currentname = '';
		var value = '';
		if(el.target){
			if(ob && ob.readAttribute('name'))
				name = ob.readAttribute('name');
			else
				return false;
			if(name == 'pname') name = 'name';
			currentname = name;
			name = custom_ob['namehash'][name]['next'];
		}else{
			if(ob && ob.readAttribute('name'))
				name = ob.readAttribute('name');
			else
				return false;
			if(name == 'pname') name = 'name';
			currentname = name;
		}
		var inner = ''
		//if ($(currentname)) {
			//value = $(currentname).value
			//inner = ob[ob.selectedIndex].innerHTML
			
		//}

		
		//check custom skips
		var check1 = custom_ob['skiphash'][currentname];
		if(check1)
			var check2 = custom_ob['skiphash'][currentname][value];
		if (value != '' && check1 && check2 != null) {
			name = custom_ob['skiphash'][currentname][value];
		}
		
		//hide all next category rows
		$H(custom_ob['namehash']).each(function(e){
			if (e.value['columns'].indexOf(currentname) != -1 && $(e.key)) {
				//var key2 = (e.key == 'name') ? e.key : e.key;
				$(e.key + '_row').hide();

				var drop = $(e.key + '_row').select('.dropdown');
				
				if (drop && drop[0]) {
					drop[0].innerHTML = '';
				}
			} 
		}.bind(this));
		
		if(name == ''){
			document.fire('w2gi:mapFormSearch');
			return 'no next';
		}
		
		var params = {}

		//if ($(currentname) && $(currentname).value == 'Laundry Detergents') {
			//params['cleaning_essentials'] = 'y';
		//}else{
			var cats = custom_ob['namehash'][name]['columns'];
			cats.each(function(e){
				if ($(e)) {
					var t = '';
					if(e == 'name')
						t = 'name'
					else
						t = e;
					params[t] = $(e).value;
				}
			}.bind(this));
		//}
		
		var options = {
			'distinct' : 1,
			'columns' : name,
			'objectName' : 'Product',
			'tagName' : 'where',
			'formdataID' : 'getlist'
		}
		udpateFire = false;

		ace.request(params, this.processResponse.bindAsEventListener(this, updateFire, name, currentname), options);
	},
	
	processResponse : function(xml, updateFire, name, currentname){
		var data = ace.xml.parse(xml);
		var address_input = $('inputaddress');
		var address_value = address_input.value;
		if(name == 'sku' && 0){
			if(!data[0])
				alert('no sku');
			var input = document.createElement("input");
			input.setAttribute('type', 'hidden');
			input.setAttribute('name', name);
			input.setAttribute('value', data[0][name.toUpperCase()]);
			input.setAttribute('class', 'dd_input');
			var nextlist = $(name+'_row');
			var drop = nextlist.select('.dropdown');
		
			if(drop && drop[0]){
				drop[0].innerHTML = '';
				drop[0].appendChild(input);	
			}
			if(updateFire && address_value != ''){
				document.fire('w2gi:mapFormSearch');
			}
			return;
		}
		var list = document.createElement("select");
		list.setAttribute('id', name);
		var sname = '';
		(name == 'name') ?  sname = 'pname' : sname =name;
		list.setAttribute('name', sname);
		list.setAttribute('class', 'dd_list');
		
		var option = document.createElement("option");
			option.setAttribute('value', '');
			option.appendChild(document.createTextNode('--Select--'));
		list.appendChild(option);

		data.each(function(e){
			if (e[name.toUpperCase()].replace(/^\s+|\s+$/g, '').length > 0) {
				var option = document.createElement("option");
				option.setAttribute('value', e[name.toUpperCase()]);
				option.appendChild(document.createTextNode(e[name.toUpperCase()]));
				list.appendChild(option);
			}
			
		}.bind(this));
		/*if(currentname == 'category1' && !updateFire){
			var option = document.createElement("option");
			option.setAttribute('value', '');
			//option.appendChild(document.createTextNode('Laundry Detergents'));
			list.appendChild(option);
		}*/
		var nextid = custom_ob['namehash'][name]['next'] ;
		var nextlist = $(name+'_row');
		
		if(custom_ob['namehash'][nextid]){ // && name != 'sku'){
			var next = nextid;
			while(next && next != ''){
				$(next+'_row').select('.dd_list').each(function(e){
					Event.stopObserving(list, 'change', this.dropChange.bind(this));
				}.bind(this));
				$(next+'_row').hide();
				next = custom_ob['namehash'][next]['next'];
			}
			
		}
		var drop = nextlist.select('.dropdown');
		
		if(drop && drop[0]){
			drop[0].innerHTML = '';
			drop[0].appendChild(list);	
		}
		Event.observe(list, 'change', this.dropChange.bindAsEventListener(this, true));
		nextlist.show();

		if(updateFire && address_value != '')
			document.fire('w2gi:mapFormSearch');
	}
	
	
});

