function setUpProductsFinderBottom() {
  $.originalCategoryListBtm = $('#bottom-products-list').clone();
  $.originalProductsListsBtm = $('#bottom-solutions-list-container ul').clone();
  $.originalApplicationListBtm = $('#bottom-applications-list').clone();
  
  $('#btn-bottom-expand-products').click(function() {
      //restore original uls
      $('#bottom-products-list li').remove();
      $('#bottom-products-list').prepend($.originalCategoryListBtm.clone().children());

      //Add Event-Listeners to the links
      addClickEventsBottom();

      $('#bottom-products-list').toggleClass('open');
      return false;
  });
  
  $('#btn-bottom-expand-solutions').click(function() {
      //restore active ul
      $('#bottom-solutions-list-container ul').each(function(index, value) {
          if ($(this).hasClass('active')) {
              $(this).find('li').remove();
              $(this).append($.originalProductsListsBtm.clone().eq(index).find('li'));

              addClickEventsBottom();
          }
      });

      $('#bottom-solutions-list-container .active').toggleClass('open');
      return false;
  });
  
  $('#btn-bottom-expand-applications').click(function() {
      //restore original ul
      $('#bottom-applications-list li').remove();
      $('#bottom-applications-list-container ul').prepend($.originalApplicationListBtm.clone().children());

      addClickEventsBottom();

      $('#bottom-applications-list').toggleClass('open');
      return false;
  });
  
  $('#btn-bottom-products-solutions-go').click(function() {
      redirectBrowser($('#bottom-solutions-list-container .active li:first-child a').attr('href'));
      return false;
  });

  $('#btn-bottom-applications-solutions-go').click(function() {
      redirectBrowser($('#bottom-applications-list li:first-child a').attr('href'));
      return false;
  });
  
}

function setUpProductsFinder() {

    $.originalCategoryList = $('#products-list').clone();
    $.originalProductsLists = $('#solutions-list-container ul').clone();
    $.originalApplicationList = $('#applications-list').clone();

    $('#product-finder-showme').click(function() {
        $('#product-finder-options').toggleClass('open');

        //close all open scrollable uls
        closeUls();

        return false;
    });

    $('#btn-expand-products').click(function() {
        //restore original uls
        $('#products-list li').remove();
        $('#products-list').prepend($.originalCategoryList.clone().children());

        //Add Event-Listeners to the links
        addClickEvents();

        $('#products-list').toggleClass('open');
        return false;
    });

    $('#btn-expand-solutions').click(function() {
        //restore active ul
        $('#solutions-list-container ul').each(function(index, value) {
            if ($(this).hasClass('active')) {
                $(this).find('li').remove();
                $(this).append($.originalProductsLists.clone().eq(index).find('li'));

                addClickEvents();
            }
        });

        $('#solutions-list-container .active').toggleClass('open');
        return false;
    });

    $('#btn-expand-applications').click(function() {
        //restore original ul
        $('#applications-list li').remove();
        $('#applications-list-container ul').prepend($.originalApplicationList.clone().children());

        addClickEvents();

        $('#applications-list').toggleClass('open');
        return false;
    });

    $('#product-finder-container').bind('click',
    function() {
        closeUls();
    });

    $('#btn-products-solutions-go').click(function() {
        redirectBrowser($('#solutions-list-container .active li:first-child a').attr('href'));
        return false;
    });

    $('#btn-applications-solutions-go').click(function() {
        redirectBrowser($('#applications-list li:first-child a').attr('href'));
        return false;
    });

}

function redirectBrowser(link) {
    if (link) {
        window.location.replace(link);
    }
}

function closeUls() {
    $('.products-finder-container ul').each(function() {
        $(this).scrollTop(0);
        $(this).removeClass('open');
    });
}

function addClickEventsBottom() { 
  $('#bottom-products-list a').each(function(index, value) {
      $(this).click(function() {
          //When Category Link is clicked then move it on top of the ul
          resortUl($('#bottom-products-list'), this);

          //Show product associated product-ul
          $('#bottom-solutions-list-container ul').removeClass('active');
          $('#bottom-solutions-list-container ul:nth-child(' + (index + 1) + ')').addClass('active');

          return false;
      });
  });

  $('#bottom-solutions-list-container ul a').each(function(index, value) {
      $(this).click(function() {        
          resortUl($(this).parent().parent(), this);
          return false;
      });
  });

  $('#bottom-applications-list a').each(function() {
      $(this).click(function() {
          resortUl($('#bottom-applications-list'), this);
          return false;
      });
  });
}

function addClickEvents() {
    //Make Category Links clickable
    $('#products-list a').each(function(index, value) {
        $(this).click(function() {
            //When Category Link is clicked then move it on top of the ul
            resortUl($('#products-list'), this);

            //Show product associated product-ul
            $('#solutions-list-container ul').removeClass('active');
            $('#solutions-list-container ul:nth-child(' + (index + 1) + ')').addClass('active');

            return false;
        });
    });

    $('#solutions-list-container ul a').each(function(index, value) {
        $(this).click(function() {
            resortUl($(this).parent().parent(), this);
            return false;
        });
    });

    $('#applications-list a').each(function() {
        $(this).click(function() {
            resortUl($('#applications-list'), this);
            return false;
        });
    });
}

function resortUl(ulElement, aElement) {
    var liElement = $(aElement).parent().clone();
    $(aElement).parent().detach();
    closeUls();
    ulElement.prepend($(liElement));
}

function setUpProductsRotator() {
    $('#product-rotator-buttons li').each(function() {
        $(this).bind('mouseover',
        function() {
            $('#product-rotator-buttons li').removeClass('active');
            $('#products-rotator > div').css('display', 'none');
            $('#rotator-info-' + $(this).attr('id').split('-').pop()).css('display', 'block');
            $(this).addClass('active');
        });
    });
}

function setUpCareRotator() {
    $('#care-rotator-buttons li').each(function() {
        $(this).bind('mouseover',
        function() {
            $('#care-rotator-buttons li').removeClass('active');
            $('#care-rotator > div').css('display', 'none');
            $('#rotator-info-care-' + $(this).attr('id').split('-').pop()).css('display', 'block');
            $(this).addClass('active');
        });
    });
}
