		$(document).ready(function(){
			var $trueform = $('#trueform');
			var $falseform = $('.sfa-cont, .sfm-cont, .sfs-cont');
			var falseFields = {};
			var trueFields = {};
			var currConfig = {};
			
			$('html *')
				.click(function(e){
					if(!$(e.target).is('.sf-combo *'))
					{$('.sf-combo-list').hide();}
				});
			
			
			//init combos
			var $truecombos = $('select',$trueform);
			var $falsecombos = $('.sf-combo',$falseform);
			
			$falsecombos.each(function(){
				var $this = $(this);
				var cb_name = $this.attr('id').substring(4)
				var $true_sel = $truecombos.filter('[name='+cb_name+']');
				trueFields[cb_name] = $true_sel;
				$this.find('.sf-combo-selected')
					.text($(':selected',$true_sel).text())
					.click(function(){
						
						$('.sf-combo-list').not($('.sf-combo-list',$this)).hide();
						$('.sf-combo-list',$this).toggle();
						/*$this
							//.attr('z-index',300)
							.find('.sf-combo-list').toggle();*/
					});
				var combo_list_output = '';
				falseFields[cb_name] = {};
				$('option',$true_sel).each(function(){
					/*$this.find('.sf-combo-list')
						.append('<div>'+$(this).text()+'</div>')*/
					combo_list_output+='<div>'+$(this).val()+'</div>';
				});
				$this.find('.sf-combo-list')
					.html(combo_list_output);
				$this.find('.sf-combo-list div').each(function(){
						falseFields[cb_name][$(this).text()] = $(this);
						$(this)
							.click(function(){
								if(!$(this).is('.disabled')){
									$('.sf-combo-list').hide();
									$this
										//.attr('z-index',100)
										.find('.sf-combo-selected')
											.text($(this).text());
									$true_sel.find('option[value='+$(this).text()+']')
										.attr('selected','selected')
									currConfig[cb_name] = $this.find('.sf-combo-selected').text();
									selectPosibleValues(getPossibleValues(currConfig));
								}
							})
							.hover(function(){
								if(!$(this).is('.disabled')){$(this).css('background','#9fbbf2')}
							},function(){
								if(!$(this).is('.disabled')){$(this).css('background','none').removeAttr('style')}
							})
						currConfig[cb_name] = $this.find('.sf-combo-selected').text();
					});
			});
			
			//init ch-b
			var $truecheckboxes = $(':checkbox',$trueform);
			var $falsecheckboxes = $('.checkboxes div',$falseform);
			
			$falsecheckboxes.each(function(){
				var $this = $(this);
				var cb_name = $this.attr('id').substring(7);
				var $true_cb = $truecheckboxes.filter('[name='+cb_name+']');
				trueFields[cb_name] = $true_cb;
				if($true_cb.is(':checked'))
				{
					$this.addClass('checked')
				}
				
				$this.click(function(e){
					if(!$(this).is('.disabled')){
						$this.toggleClass('checked');
						$this.is('.checked')?$true_cb.attr('checked','checked'):$true_cb.removeAttr('checked')
						currConfig[cb_name] = $this.is('.checked')?1:0;
						selectPosibleValues(getPossibleValues(currConfig))
					}
				});
				currConfig[cb_name] = $this.is('.checked')?1:0;
				falseFields[cb_name] = $this;
			})
			
			//console.log(falseFields,'\n',currConfig,'\n',getPossibleValues(currConfig));
			// console.log(currConfig,getPossibleValues(currConfig))

			selectPosibleValues(getPossibleValues(currConfig));
			
function selectPosibleValues(possibleValues,isSetDefault){
/*
	console.log(
		'currConfig\t',currConfig,
		'\npossibleValues\t',possibleValues,
		'\nfalseFields\t',falseFields,
		'\ntrueFields\t',falseFields
	);
*/
	for(var currField in falseFields){
		//console.log(currField,typeof falseFields[currField].get == 'undefined',falseFields[currField],possibleValues[currField])
		if(typeof falseFields[currField].get == 'undefined' ){ //combos
			//console.log(currField,falseFields[currField],possibleValues[currField])
			if(typeof isSetDefault != 'undefined'){
					
					var $currTrue = trueFields[currField]
						.find('option:first')
							.attr('selected','selected');
					
					var $currFalse = falseFields[currField]['All']
						.parents('.sf-combo')
						.find('.sf-combo-selected')
							.text($currTrue.text());
							
					currConfig[currField] = $currTrue.text();
					
					//console.log('reset combos', $currTrue, $currFalse)
							
			}
			
			for(var currOption in falseFields[currField]){
				//console.log('\t',currOption,falseFields[currField][currOption],possibleValues[currField][currOption])
				if((typeof isSetDefault != 'undefined') || (typeof possibleValues[currField][currOption] != 'undefined' && possibleValues[currField][currOption] == 1)){
					falseFields[currField][currOption].removeClass('disabled');
				} else {
					falseFields[currField][currOption].addClass('disabled');
				}
			}
		} else { //ch-boxes
			//console.log(currField,falseFields[currField],possibleValues[currField])
			if(typeof isSetDefault != 'undefined'){
				falseFields[currField].removeClass('disabled').removeClass('checked');
				trueFields[currField].removeAttr('checked');
				currConfig[currField] = 0;
			} else if(possibleValues[currField] == 0){
				falseFields[currField].addClass('disabled');
			} else {
				falseFields[currField].removeClass('disabled');
			}
		}
	}
}			


function serializeCurrConfig(){
	var tmpArr = [];
	for(var type in currConfig){
		tmpArr.push(''+type+'='+currConfig[type]);
	}
	return tmpArr.join('~');
}
			
			
			$('#sfa-search, #sfm-search, #sfs-search').click(function(e){
				try{
					var actionsArr = {
						'sfa-search': 'advanced',
						'sfm-search': 'main',
						'sfs-search': 'sidebar'
					}
					trackAction('slots-finder', actionsArr[e.target.id], serializeCurrConfig())
				}catch(err){}
				$trueform.submit();
			})
			$('#sfa-reset, #sfm-reset, #sfs-reset').click(function(e){
				selectPosibleValues(null,true);
				e.preventDefault();
			})

			if ($.browser.msie && $.browser.version<7){
				$('.sfs-cont').css('margin-bottom','-234px')
			}
		}); //end of document ready


function getPossibleValues(config){
	var result = {
		software: {"All":1},
		type: {"All":1},
		paylines: {"All":1},
		theme: {"All":1},
		progressive: 0,
		bonus_round: 0,
		free_spins: 0,
		skill_stop: 0,
		scatter: 0,
		wild: 0,
		multiplier: 0,
		autoplay: 0,
		fun_money: 0,
		instant_play: 0
	};
	
	function fillSlot(slot) {
		var result = {
			software: slot[0],
			reels: slot[1],
			paylines: slot[2],
			theme: slot[3],
			'i-slots': slot[4],
			progressive: slot[5],
			bonus_round: slot[6],
			free_spins: slot[7],
			skill_stop: slot[8],
			scatter: slot[9],
			wild: slot[10],
			multiplier: slot[11],
			autoplay: slot[12],
			fun_money: slot[13],
			instant_play: slot[14]
		};
		
		if (result['i-slots']) {
			result.type = 'i-slots';
		} else {
			result.type = result.reels+'-reel';
		}
		
		return result;
	}
	
	function isValidBooleans(config, slot) {
		if (config.progressive && !slot.progressive) return false;
		if (config.bonus_round && !slot.bonus_round) return false;
		if (config.free_spins && !slot.free_spins) return false;
		if (config.skill_stop && !slot.skill_stop) return false;
		if (config.scatter && !slot.scatter) return false;
		if (config.wild && !slot.wild) return false;
		if (config.multiplier && !slot.multiplier) return false;
		if (config.autoplay && !slot.autoplay) return false;
		if (config.fun_money && !slot.fun_money) return false;
		if (config.instant_play && !slot.instant_play) return false;
		
		return true;
	}

	var len = slotsList.length;
	for (var i=0; i<len; i++) {
		var slot = fillSlot(slotsList[i]);

		if (config.software!="All" && config.software!=slot.software) continue;
		if (config.paylines!="All" && config.paylines!=slot.paylines) continue;
		if (config.theme!="All" && config.theme!=slot.theme) continue;
		if (config.type!="All") {
			if (config.type!="i-slots") {
				if (config.type!=slot.type) continue;
			} else {
				if (!slot['i-slots']) continue;
			}
		}
		
		if (!isValidBooleans(config, slot)) continue;

		result.software[slot.software] = 1;
		result.type[slot.reels+'-reel'] = 1;
		if (slot['i-slots']) {
			result.type["i-slots"] = 1;
		}
		result.paylines[slot.paylines] = 1;
		result.theme[slot.theme] = 1;
		
		if (slot.progressive) result.progressive = 1;
		if (slot.bonus_round) result.bonus_round = 1;
		if (slot.free_spins) result.free_spins = 1;
		if (slot.skill_stop) result.skill_stop = 1;
		if (slot.scatter) result.scatter = 1;
		if (slot.wild) result.wild = 1;
		if (slot.multiplier) result.multiplier = 1;
		if (slot.autoplay) result.autoplay = 1;
		if (slot.fun_money) result.fun_money = 1;
		if (slot.instant_play) result.instant_play = 1;
	}

	if (config.software!="All") {
		for (var i=0; i<len; i++) {
			var slot = fillSlot(slotsList[i]);

			//if (config.software!="All" && config.software!=slot.software) continue;
			if (config.paylines!="All" && config.paylines!=slot.paylines) continue;
			if (config.theme!="All" && config.theme!=slot.theme) continue;
			if (config.type!="All") {
				if (config.type!="i-slots") {
					if (config.type!=slot.type) continue;
				} else {
					if (!slot['i-slots']) continue;
				}
			}
			
			if (!isValidBooleans(config, slot)) continue;

			result.software[slot.software] = 1;
			//result.type[slot.reels+'-reel'] = 1;
			//if (slot['i-slots']) result.type["i-slots"] = 1;
			//result.paylines[slot.paylines] = 1;
			//result.theme[slot.theme] = 1;
		}
	}
	
	if (config.paylines!="All") {
		for (var i=0; i<len; i++) {
			var slot = fillSlot(slotsList[i]);

			if (config.software!="All" && config.software!=slot.software) continue;
			//if (config.paylines!="All" && config.paylines!=slot.paylines) continue;
			if (config.theme!="All" && config.theme!=slot.theme) continue;
			if (config.type!="All") {
				if (config.type!="i-slots") {
					if (config.type!=slot.type) continue;
				} else {
					if (!slot['i-slots']) continue;
				}
			}
			
			if (!isValidBooleans(config, slot)) continue;

			//result.software[slot.software] = 1;
			//result.type[slot.reels+'-reel'] = 1;
			//if (slot['i-slots']) result.type["i-slots"] = 1;
			result.paylines[slot.paylines] = 1;
			//result.theme[slot.theme] = 1;
		}
	}
	
	if (config.theme!="All") {
		for (var i=0; i<len; i++) {
			var slot = fillSlot(slotsList[i]);

			if (config.software!="All" && config.software!=slot.software) continue;
			if (config.paylines!="All" && config.paylines!=slot.paylines) continue;
			//if (config.theme!="All" && config.theme!=slot.theme) continue;
			if (config.type!="All") {
				if (config.type!="i-slots") {
					if (config.type!=slot.type) continue;
				} else {
					if (!slot['i-slots']) continue;
				}
			}
			
			if (!isValidBooleans(config, slot)) continue;

			//result.software[slot.software] = 1;
			//result.type[slot.reels+'-reel'] = 1;
			//if (slot['i-slots']) result.type["i-slots"] = 1;
			//result.paylines[slot.paylines] = 1;
			result.theme[slot.theme] = 1;
		}
	}
	
	if (config.type!="All") {
		for (var i=0; i<len; i++) {
			var slot = fillSlot(slotsList[i]);

			if (config.software!="All" && config.software!=slot.software) continue;
			if (config.paylines!="All" && config.paylines!=slot.paylines) continue;
			if (config.theme!="All" && config.theme!=slot.theme) continue;
			/*
			if (config.type!="All") {
				if (config.type!="i-slots") {
					if (config.type!=slot.type) continue;
				} else {
					if (!slot['i-slots']) continue;
				}
			}
			*/
			
			if (!isValidBooleans(config, slot)) continue;

			//result.software[slot.software] = 1;
			result.type[slot.reels+'-reel'] = 1;
			if (slot['i-slots']) result.type["i-slots"] = 1;
			//result.paylines[slot.paylines] = 1;
			//result.theme[slot.theme] = 1;
		}
	}

	return result;
}
		
