// JavaScript Document
$(function()
{
	
	setListScroll = function(){
		$('#scrollPane').jScrollPane({
			showArrows:true, 
			scrollbarWidth: 7, 
			arrowSize:7
		});
	}
	
	ListFilter = function(val){
		$("#scrollPane .resultList li").show();
		$("#scrollPane .resultList li").each(function(){
			$("dd",this).eq(1).hide();
		});
		if (val.toLowerCase()!='all'){ 
			$("#scrollPane .resultList li").each(function(){
				if ($("dd",this).eq(1).text().toLowerCase().replace(/[ ]/g,"") != val.toLowerCase().replace(/[ ]/g,"")){
					$(this).hide();
				}
			});
		}
		$('.resultTitle>.resultNumber').html($("#scrollPane .resultList li:visible").length+' result(s)')
		setListScroll();
	}
	
	/*fauxSelect*/
    /*<div class="pullDownContainer">
        <div class="optionSelect">&nbsp;</div>
        <div class="optionsWrap">
          <div class="options">
            <div class="optionsContainer"></div>
          </div>
        </div>
     </div>*/
	 
	$('select').each(function(){
		$(this).after('<div class="pullDownContainer"><div class="optionSelect">&nbsp;</div><div class="optionsWrap"><div class="options"><div class="optionsContainer"><ul></ul></div></div></div></div>');
		$(this).find('option').each(function(){
			$(this).parent().siblings('.pullDownContainer').find('ul').append('<li><a href="#">'+$(this).text()+'</a></li>');
			var optionsContainer =  $(this).parent().siblings('.pullDownContainer').find('.optionsContainer');
			if ( optionsContainer.height() > 80 ){
				 optionsContainer.css('height',80);
				 optionsContainer.jScrollPane({
					showArrows:true,
					scrollbarWidth: 13, 
					arrowSize:7
				});
		    }
		});
	});

	$('select').change(function(){
		var optionIndex = $(this).find('option').index($(this).find('option:selected'));
		var selectedValue = $(this).find('option:selected').val() ? $(this).find('option:selected').val() : $(this).val(); 
		$(this).siblings('.pullDownContainer').find('ul').find('a').removeClass('selected');
		$(this).siblings('.pullDownContainer').find('ul').find('a').eq(optionIndex).addClass('selected');
		$(this).siblings('.pullDownContainer').find('.optionSelect').html($(this).children('option').eq(optionIndex).text());
		ListFilter(selectedValue);
	});
	
	setPulldown = function(e){
		var optionIndex = e.parent().parent().find('a').index(e);
		e.parents('.pullDownContainer').siblings('select').find('option').removeAttr('selected') 
		e.parents('.pullDownContainer').siblings('select').find('option').eq(optionIndex).attr('selected','selected');
		e.parents('.pullDownContainer').siblings('select').change();
	}
	
	showfauxoption = function(e){
		e.find('.options').addClass('optionsShow');
	}
	hidefauxoption = function(){
		$('.options').removeClass('optionsShow');	
	}
	$('.pullDownContainer').bind(
		'click',
		function(){
			showfauxoption($(this));
			return false;
		}
	);
	$('body').click(function(){
		$('.options').removeClass('optionsShow');
	});
	
	$('.optionsContainer ul').find('a').bind(
		'click',
		function(){
			setPulldown($(this));
			hidefauxoption();
			return false;
		}
	);
	
	/*Init*/
	$('select').change();
	//alert($('.resultList').find('li').length)
	
});