$(document).ready(function() {
// Select Replace
	$('select').addClass('select-js');
	$(".select-js").css("visibility","visible");

	$('.select-js').each(function(i, e)
	{
		var select = $(e);
		
		var replaceText = select.children('option[data-html-text]').val();
		
		var selectsBoxContainer = $('<div>',{
			html		: '<div class="select-replace">' + replaceText + '</div>'
		}).addClass('select-change');
	
		var dropdownWrapper = $('<div class="pane"></div>');
		var dropsDown = $('<ul>');
		dropsDown.addClass('dropdown');
		var selectsBox = selectsBoxContainer.children('.select-replace');
		
		select.find('option').each(function(i, e){
			var option = $(e);
			
			if(i==select.attr('selectedIndex')){
				selectsBox.html(option.text());
			}
			
			if(option.data('skip')){
				return true;
			}
			
			var li = $('<li>',{
				html:'<span>'+option.data('html-text')+'</span>'
			});
			
			if (! dropsDown.find('li').length)
				li.addClass('first-child');
					
			li.click(function(){
				if (! li.is('.first-child'))
				{
					selectsBox.html(option.text());
				}
				dropsDown.trigger('hide');
				select.val(option.val());
				return false;
			});
			
			dropsDown.append(li);
		});
		
		selectsBoxContainer.append(dropdownWrapper);
		dropdownWrapper.append(dropsDown);
		select.hide().after(selectsBoxContainer);
	});
	
	$('.dropdown').bind('show',function(){
		
		if($(this).is(':animated')){
			return false;
		}
		
		$(this).parents('.select-change').find('.select-replace').addClass('expanded');
		$(this).show();
		$(this).parents('.pane').show();
		
	}).bind('hide',function(){
		
		if($(this).is(':animated')){
			return false;
		}
		
		$(this).parents('.select-change').find('.select-replace').removeClass('expanded');
		$(this).parents('.pane').hide();
		
	}).bind('toggle',function(){
		if($(this).parents('.select-change').find('.select-replace').hasClass('expanded')){
			$(this).trigger('hide');
		}
		else $(this).trigger('show');
	});
		
	$('.select-replace').click(function(){
		$('.dropdown').trigger('hide');
		$(this).parent('.select-change').find('.dropdown').trigger('show');
		return false;
	});

	$('.pane').hide();
	
	$('.pane').each(function(i, e)
	{
		$(e).css('height', Math.min($(e).innerHeight(), 190));
	});
	
	$(".dropdown li").hover(
	  function () {
		$(this).addClass('hover');
	  }, 
	  function () {
		$(this).removeClass('hover');
	  }
	);
	
	$(document).click(function() {
		$('.dropdown').trigger('hide');
	});
	
	$('.pane').show().jScrollPane().hide();
	
	$('.jspVerticalBar').click(function() {
		return false;
	});

});
