function addSelectOption(selectObj, text, value) {
	selectObj.append($("<option></option>").attr("value",value).text(text));
}
function resetSelects(list) {
	if(typeof(list) == 'string'){
		list = new Array(list);
	}
	for(var i=0; i<list.length; i++){
		$('#'+list[i]).empty().append('<option value="">All...</option>');
	}
}
function populateSelect(selectID, pathOb, eventSel){
	if(eventSel.attr("selectedIndex") > 0){
		if(pathOb.parent != '' && $(pathOb.parent) != null){
			var p = '#'+pathOb.src +' #' + pathOb.type + $('#'+pathOb.parent+" option:selected").attr('value') + pathOb.node + eventSel.attr('value')+ ' li a.lvl' + pathOb.level;
		}else{
			var p = '#'+pathOb.src +' #' + pathOb.type + pathOb.node + eventSel.attr('value')+ ' li a.lvl' + pathOb.level;
		}
		$( p ).each(			
			function(){addSelectOption($( '#'+selectID ), $(this).html(), $(this).attr('entity'), true) }
		);
	}
}
function attachSelectEvt(id, resets, linkedID, formID, pathOb, parentelementid){
	$('#'+id).change(function(){
		resets.push(linkedID);
		resetSelects( resets );
		populateSelect(linkedID, pathOb, $(this));
		if($("#"+$(this).attr("id")+" option:selected").attr('value') == '' && parentelementid){
			$('#'+formID+' #'+[pathOb.formElement]).attr( 'value', $('#'+formID+' #'+parentelementid).attr('value') );
		}else{
			$('#'+formID+' #'+[pathOb.formElement]).attr( 'value', $('#'+$(this).attr('id')+' option:selected').attr('value') );
		}
		if(typeof(linkedID) == 'string'){
			$('#'+linkedID).focus();
		}
	});
}
function attachSimpleEvt(id, linkedValue, formID, linkedID, parentelementid){
	$('#'+id).change(function(){
		if($(this).attr('value') == '' && parentelementid){
			$('#'+formID+' #'+[linkedValue]).attr( 'value',  $('#'+formID+' #'+parentelementid).attr('value') );
		}else{
			$('#'+formID+' #'+[linkedValue]).attr( 'value',  $(this).attr('value') );
		}
		if(typeof(linkedID) == 'string')
			$('#'+linkedID).focus(); 
	});
}

productSearch = ({

	load:function(args){
		
		// Tours
		$('#tourslist .lvl1').each(function(){ addSelectOption($('#hgroup'), $(this).html(), $(this).attr('entity'), true) });
		attachSelectEvt('hgroup', new Array('hregion1'), 'hcountry', 'holidaysearchform', {src:'tourslist', formElement:'group', type:'holiday', node:'category', parent:'', level:2});
		attachSelectEvt('hcountry', new Array(), 'hregion1', 'holidaysearchform', {src:'tourslist', formElement:'region', type:'holiday', node:'region', parent:'hgroup', level:3});
		attachSimpleEvt('hregion1', 'region', 'holidaysearchform', 'hsorting', 'hcountry');
		attachSimpleEvt('hsorting', 'sorting', 'holidaysearchform');
		
		// Accommodation
		$('#accomlist .lvl1').each(function(){ addSelectOption($('#acountry'), $(this).html(), $(this).attr('entity'), true) });
		attachSelectEvt('acountry', new Array('aregion2', 'agroup'), 'aregion1', 'accommodationsearchform', {src:'accomlist', formElement:'region', type:'accommodation', node:'region', parent:'', level:2});
		attachSelectEvt('aregion1', new Array('agroup'), 'aregion2', 'accommodationsearchform', {src:'accomlist', formElement:'region', type:'accommodation', node:'region', parent:'', level:3}, 'acountry');
		attachSelectEvt('aregion2', new Array(), 'agroup', 'accommodationsearchform', {src:'accomlist', formElement:'region', type:'accommodation', node:'category', parent:'', level:4}, 'aregion1');
		attachSimpleEvt('agroup', 'group', 'accommodationsearchform', 'asorting');
		attachSimpleEvt('asorting', 'sorting', 'accommodationsearchform');
		
	},
	
	init:function(args){
		if(args['type'] == 'holiday'){
			$('#tours-search-panel').slideDown('slow');
			$('#tours-search-link').removeClass('tours-closed');
			$('#tours-search-link').addClass('tours-open');
			if(args['group'] != null && args['group'] != ''){
				$("#hgroup").val(args['group'])
				$("#hgroup").trigger('change');
			}
			if(args['firstlevelregion'] != null && args['firstlevelregion'] != ''){
				$("#hcountry").val(args['firstlevelregion'])
				$("#hcountry").trigger('change');
			}
			if(args['secondlevelregion'] != null && args['secondlevelregion'] != ''){
				$("#hregion1").val(args['secondlevelregion'])
				$("#hregion1").trigger('change');
			}
		}
		if(args['type'] == 'accommodation'){
			$('#accomodation-search-panel').slideDown('slow');
			$('#accomodation-search-link').removeClass('accomodation-closed');
			$('#accomodation-search-link').addClass('accomodation-open');
			if(args['firstlevelregion'] != null && args['firstlevelregion'] != ''){
				$("#acountry").val(args['firstlevelregion'])
				$("#acountry").trigger('change');
			}
			if(args['secondlevelregion'] != null && args['secondlevelregion'] != ''){
				$("#aregion1").val(args['secondlevelregion'])
				$("#aregion1").trigger('change');
			}
			if(args['thirdlevelregion'] != null && args['thirdlevelregion'] != ''){
				$("#aregion2").val(args['thirdlevelregion'])
				$("#aregion2").trigger('change');
			}
			if(args['group'] != null && args['group'] != ''){
				$("#agroup").val(args['group'])
				$("#agroup").trigger('change');
			}
		}		
	},
	
	validate:function(formID){
		check( $('#'+formID).get(0) );
	}
});

$(document).ready(function(){
	productSearch.load();
	$('#tours-search-link').mouseover(function(){ 
		if($(this).hasClass('tours-closed')){
			$(this).removeClass('tours-closed');
			$(this).addClass('tours-open');
			$('#tours-search-panel').slideDown('slow');
			$('#accomodation-search-link').removeClass('accomodation-open');
			$('#accomodation-search-link').addClass('accomodation-closed');
			$('#accomodation-search-panel').slideUp('slow');
		}
	});
	$('#accomodation-search-link').mouseover(function(){ 
		if($(this).hasClass('accomodation-closed')){
			$(this).removeClass('accomodation-closed');
			$(this).addClass('accomodation-open');
			$('#accomodation-search-panel').slideDown('slow');
			$('#tours-search-link').removeClass('tours-open');
			$('#tours-search-link').addClass('tours-closed');
			$('#tours-search-panel').slideUp('slow');
		}
	});
	$('#accomodation-search-btn').click(function(){ 
		productSearch.validate('accommodationsearchform');
	});
	$('#tours-search-btn').click(function(){ 
		productSearch.validate('holidaysearchform');
	});
	$('#sorting').change(function(){ 
		productSearch.validate('sortingform');
	});
});