$(document).ready(function() {
   makeSelectComponents();
   makeRadioComponents();
   makeSelectComponentsPressroom();
   //debug();
});

// FUNCTION
// makeSelectComponents
// Replaces all select components with an html alternate consisting of a div containing an unsorted list.
function makeSelectComponents() {
    $('.custom-select-component').each(function(index) {
        var selectComponent = $(this);
        selectComponent.hide();
        selectComponent.after('<div id="tic-custom-' + this.id + '" class="tic-custom-select"></div>');

        var container = $('#tic-custom-' + this.id);
        container.html('<a href="#" class="current-value">' + selectComponent.find(':selected').text() + '</a><span></span><ul class="options"></ul>');

        selectComponent.find('option').each(function(index) {
            //container.find('ul').append('<li class="custom-select-option"><a href="#" title="' + $(this).val() + '">' + $(this).text() + '</a></li>');
            container.find('ul').append('<li class="custom-select-option"><a href="#" name="' + $(this).val() + '" title="' + $(this).text() + '">' + $(this).text() + '</a></li>');
        });

        container.click(function() {
            $('.tic-custom-select.active:not(#' + this.id + ')').removeClass('active');
            if (container.find('ul').is(':visible')) {
                container.removeClass('active');
            } else {
                container.addClass('active');
            }
        });

        container.find('.custom-select-option a').click(function() {
            container.find('.current-value').text($(this).text());
            //selectComponent.val(this.title);
            selectComponent.val(this.name);
            setTimeout('__doPostBack(\'selectComponent.attr("name")\',\'\')', 0);
            container.removeClass('active');
            return false;
        });
    });
}


// FUNCTION
// makeRadioComponents
// Replaces all radio input components with an html alternate consisting of a div containing an anchor.
function makeRadioComponents() {
    $('.custom-radio-component').each(function(index) {
        var selectComponent = $(this).hide();
        var selectLabel = $('label[for="' + this.id + '"]').hide();

        var selectedLink = $(this.id);
        var useLink = "javascript: radSearch1_Click();";
        if (this.id != "ctl00_contentPlaceHolderMain_radSearch1") {
            useLink = "javascript: radSearch2_Click();";
        }

        selectComponent.after('<a href="#" onclick="' + useLink + '" id="tic-custom-' + this.id + '" title="' + selectComponent.val() + '" class="tic-custom-radio-input" target="' + this.name + '">' + selectLabel.text() + '</a>');
        if (selectComponent.is(':checked')) {
            $('#tic-custom-' + this.id).addClass('active');
        }
    });

  $('.tic-custom-radio-input').click(function() {
      $('.tic-custom-radio-input.active[target="' + this.target + '"]:not(#' + this.id + ')').removeClass('active');
      $(this).addClass('active');
      $('#' + this.id.replace('tic-custom-', '')).attr('checked', 'checked');
      return false;
  });
}

// FUNCTION
// makeSelectComponentsPressroom
// Replaces all select components with an html alternate consisting of a div containing an unsorted list.
function makeSelectComponentsPressroom() {
    $('.custom-select-component-pressroom').each(function(index) {
        var selectComponent = $(this);
        selectComponent.hide();
        selectComponent.after('<div id="tic-custom-' + this.id + '" class="tic-custom-select-pressroom"></div>');

        var container = $('#tic-custom-' + this.id);
        container.html('<a href="#" class="current-value">' + selectComponent.find(':selected').text() + '</a><span></span><ul class="options"></ul>');

        selectComponent.find('option').each(function(index) {
            //container.find('ul').append('<li class="custom-select-option"><a href="#" title="' + $(this).val() + '">' + $(this).text() + '</a></li>');
            container.find('ul').append('<li class="custom-select-option"><a href="#" name="' + $(this).val() + '" title="' + $(this).text() + '">' + $(this).text() + '</a></li>');
    });

        container.click(function() {
            $('.tic-custom-spaceavailabletall-select.active:not(#' + this.id + ')').removeClass('active');
            if (container.find('ul').is(':visible')) {
                container.removeClass('active');
            } else {
                container.addClass('active');
            }
        });

        container.find('.custom-select-option a').click(function() {
            container.find('.current-value').text($(this).text());
            //selectComponent.val(this.title);
            selectComponent.val(this.name);
            setTimeout('__doPostBack(\'selectComponent.attr("name")\',\'\')', 0);
            container.removeClass('active');
            return false;
        });
    });
}

// FUNCTION
// debug
// Sends form as ajax request.
function debug() {
   $('form').submit(function() {
      alert($("form").serialize());
      return false;
   });
}
