$(document).ready(function () {

    initSearchButton();
    sortStartPageCourseListIfExists();
    checkForTopLink();
    checkFooter();

});

function initSearchButton() {

    $('#Text1:input').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            var searchValue = $('#Text1').val();
            if (searchValue.length > 2) {
                var location = $('a#search_link').attr('href');
                document.location = location + "?q=" + searchValue;
            }
        }
    });

    $('a#search_link').click(function (event) {
        event.preventDefault();
        var searchValue = $('#Text1').val();
        if (searchValue.length > 2) {
            var location = $(this).attr('href');
            document.location = location + "?q=" + searchValue;
        }
    });

}

function sortStartPageCourseListIfExists()
{
  if($('ul#startpage-categories li').length > 0)
  {
    //alert($('ul#startpage-categories li').length / 3);
    var cols = Math.round($('ul#startpage-categories li').length / 3);
    var hits = 1;
    var selector = 'start_';
    $('ul#startpage-categories').hide();
    $('ul#startpage-categories li').each(function (index) {
        if (((index) % cols == 0) && index > 0) {
            hits++
        }

        if (hits > 3) {
            $('ul#' + selector + "1").append('<li>' + $('ul#' + selector + "2 li:first").html() + '</li>');
            $('ul#' + selector + "2 li:first").remove();
            $('ul#' + selector + "2").append('<li>' + $('ul#' + selector + "3 li:first").html() + '</li>');
            $('ul#' + selector + "3 li:first").remove();
            $('ul#' + selector + "3").append('<li>' + $(this).html() + '</li>');
        }
        else
            $('ul#' + selector + hits).append('<li>' + $(this).html() + '</li>');
    });
  }

}

function checkForTopLink() {

   if($('#article a.buttontotop').length > 0 || $('#article_content').length > 0) {
     
       if ($('#article_content').height() > 625)
           $('#article a.buttontotop').show();
   }

}

function checkFooter() {

  $('#footer article.footer_content').each(
    function () {
      if($(this).html().length == 0)
        $(this).attr('style', 'visibility:hidden');
    });
}

function checkTx() {
    if ($('#tx_1_show').length > 0) {

        if ($('#tx_1_show').html() == '1' && $('#puffsection article.about_puff:last').length > 0) {

            $('#puffsection article.about_puff:last').html($('#tx_1_text').html());
            $('#puffsection article.about_puff:last').addClass('tx_content');
            $('#tx_1_head').attr('style', 'top:' + $('#puffsection h2:first').position().top + 'px;' + 'left:' + $('#puffsection article.about_puff:last').position().left + 'px');
            $('#tx_1_head').show();
        
        }
    
    }
}

function checkCatMenu(nameIn) {

    $('ul#kurser li').each(function () {
        if ($(this).find('a').html() == nameIn)
            $(this).addClass('current');
    });

}
