//REPLACE GRAVITY SELECT DROPDOWNS
function replaceGravitySelects(){
var optionValues = [];
var optionHTML = [];
var selected = [];
var selectedHTML = [];
var selectParent = [];
var inputName = [];
var tabIndex = [];
var numSelects = 0;
//get options
jQuery('.gfield_select').each(function() {
optionValues[numSelects] = [];
optionHTML[numSelects] = [];
jQuery('option', this).each(function() {
optionValues[numSelects].push(jQuery(this).val());
optionHTML[numSelects].push(jQuery(this).html());
if(jQuery(this).is(':selected')){
selected[numSelects] = jQuery(this).val();
selectedHTML[numSelects] = jQuery(this).html();
}
});
selectParent[numSelects] = jQuery(this).parent();
inputName[numSelects] = jQuery(this).attr('name');
tabIndex[numSelects++] = jQuery(this).attr('tabindex');
});
for(var j = 0; j < numSelects; j++){
var buildSelect = '<div class="dropdown subject" tabindex="'+tabIndex[j]+'"><span id="search-text">'+selectedHTML[j]+'</span>'+
'<input id="'+inputName[j]+'" type="hidden" value="'+selected[j]+'" name="'+inputName[j]+'"></input><ul style="">';
var isSelected = '';
for (var i in optionValues[j]) {
if(optionValues[j][i] == selected){
isSelected = 'selected';
}else{
isSelected = '';
}
buildSelect += '<li id="'+optionValues[j][i]+'" class="'+isSelected+'">'+optionHTML[j][i]+'</li>';
}
buildSelect += '</ul></div>';
// remove the old and set the new select in place
selectParent[j].html(buildSelect);
}
// SET UP FUNCTIONALITY
//MOBILE CHECK
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
binding = 'touchstart';
//close the menu if it is open and the user clicks / touches ANYWHERE but the menu
$(document).bind(binding, function(e){
var container = $(".dropdown.open");
if (! container.is(e.target) && ! container.children('ul').is(e.target) && ! container.children('ul').children('li').is(e.target) )
{
container.children('ul').hide();
container.removeClass('open');
}
});
}else{
binding = 'click';
}
//dropdown controller
$('.dropdown').bind(binding, function(e) {
//dropdownOpenClose(this);
if( $(this).children('ul').is( ":hidden" )){
$(this).children('ul').show();
$(this).addClass('open');
}
});
$('.dropdown').mouseleave(function(e){
if($(this).children('ul').is( ":visible" )){
$(this).children('ul').hide();
$(this).removeClass('open');
}
});
$('.dropdown ul li').click( function(e) {
e.stopPropagation();
var thisLI = $(this);
var sitem = thisLI.html();
var sid = thisLI.attr('data-id');
var parentUL = thisLI.parent('ul');
parentUL.siblings('span.display-text').html(sitem); //add text to display area
parentUL.siblings('input').val(sid); //add to hidden field
parentUL.hide(); //add to hidden field
//wait just a little longer than the slide up time to put in the new text. Looks weird otherwise.
setTimeout(function(){ thisLI.addClass('selected'); thisLI.siblings('li').removeClass('selected'); }, 100);
});
}