$(document).ready(function() {
    //$('.code,.Code').jCodeViewer();
    //$('img').tooltip();

    $('#featured').slidingBillboard({ width: 712, height: 125, time: 5000 }).show();

    if ($('#latestNews').length > 0) {
        $.get('rss.asp', { TypeId: 1, top: 10 }, function(data) {
            $('#latestNews').empty();
            $('item', data).each(function(i) {
                var txt = $.trim($('title', this).text());
                var lnk = $.trim($('link', this).text());
                var li = $('<li>').appendTo('#latestNews');
                $('<a href="' + lnk + '">' + txt + '</a>').appendTo(li);
            });
            $('#latestNews').ticker(3);
        });
    }

    // fix for ie6 hover
    $('.menu_item').hover(function() {
        $(this).addClass('over');
    }, function() {
        $(this).removeClass('over');
    });

    // accordion
    if ($('.relatedList').length > 0) {
        var d = $('<div class="accordion">');
        $('.relatedList').each(function() {
            var i = $('<div>');
            if ($('h4', this).text() == '') $('h4', this).html('&nbsp;');
            i.append($('<div class="header">').append($('h4', this)));
            i.append($('<div class="content">').append($('ul', this)));
            d.append(i);
        })
        $('.relatedList:first > .top').append('<h4>Related Items</h4>');
        $('.relatedList:first > .middle').css({ padding: 0 }).append(d);
        $('.relatedList:not(:first)').remove();
        d.slideMenu({ event: 'mouseover', delay: 300 });
    }

    if ($('.featured').length > 0) {
        var d = $('<div class="featuredMenu">');
        $('.featured').each(function() {
            var i = $('<div>');
            if ($('h4', this).text() == '') $('h4', this).html('&nbsp;');
            i.append($('<div class="content">').append($('.middle', this).html()));
            i.append($('<div class="header">').append($('h4', this)));
            d.append(i);
        })
        $('.featured:first > .top').append('<h4></h4>');
        $('.featured:first > .middle').remove();  //html('').css({ padding: 0 }).append(d);
        $('.featured:first > .bottom').remove();
        $('.featured:first').append(d);
        $('.featured:not(:first)').remove();
        d.slideMenu({ header: 'last', event: 'mouseover', delay: 300 });
    }

    $.tablesorter.addParser({
        // set a unique id 
        id: 'html',
        is: function(s) {
            // return false so this parser is not auto detected 
            return false;
        },
        format: function(s) {
            // format your data for normalization 
            return s.toLowerCase().replace(/<[^>]*>/gi, '');
        },
        // set type, either numeric or text 
        type: 'text'
    });
    //if ($(".tablesorter th:contains('Serial')").length == 0) {
    //    if ($(".tablesorter th:contains('Invoice')").length == 0) {
    //        $('.tablesorter').tablesorter({ widgets: ['zebra'], sortList: [[1, 1]], headers: { 0: { sorter: 'html' }, 1: { sorter: 'usLongDate'}} }).each(function() {
    //            $(this).tablesorterPager({ container: $(this).next(), positionFixed: false });
    //        });
    //    } else {
    //    $('.tablesorter').tablesorter({ widgets: ['zebra'], sortList: [[3, 1]], headers: { 1: { sorter: 'html' }, 3: { sorter: 'usLongDate'}} }).each(function() {
    //            $(this).tablesorterPager({ container: $(this).next(), positionFixed: false });
    //        });
    //    }
    //} else {
    //$('.tablesorter').tablesorter({ widgets: ['zebra'], sortList: [[2, 1]], headers: { 0: { sorter: 'html' }, 1: { sorter: false }, 2: { sorter: 'usLongDate'}} }).each(function() {
    //        $(this).tablesorterPager({ container: $(this).next(), positionFixed: false });
    //    });
    //}

    //$('form').submit(function() {
    //    $('input[type=submit]').attr('disabled', 'disabled');
    //    $(this).submit();
    //});

    $('.validate').validate();

    $('.tablesorter').tablesorter({ widgets: ['zebra'] }).each(function() {
        $(this).tablesorterPager({ container: $(this).next(), positionFixed: false });
    });

    $('input:text').hint();

    $('input:text').attr('autocomplete', 'off');

    $('a:not(:has(img))[href^=http]:not([href^=' + $('base').attr('href') + '])').addClass('external');

    $('<div id="searchResults"></div>').appendTo('body');
    $('#siteSearch').keyup(function() {
        $('#searchResults').hide();
        if ($(this).val().length > 2) {
            $.post("ajax_search.asp", { STypeId: $('#typeId').val(), searchString: $(this).val() }, function(data, status) {
                if (status = "success") {
                    $('#searchResults').html(data).show();
                    $('body').click(function() {
                        $('#searchResults').hide();
                        $('body').unbind('click', function() { })
                    });
                } else {
                    $('#searchResults').hide();
                }
            }, "html");
        }
    });
});

function goto(url){ document.location.href = url; }

jQuery.fn.hint = function () {
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this); 
    // get it once since it won't change
    var title = t.attr('title'); 
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(title);
          t.addClass('blur');
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
          t.removeClass('blur');
        }
      });

      // clear the pre-defined text when form is submitted
      t.parents('form:first()').submit(function(){
          if (t.val() == title) {
              t.val('');
              t.removeClass('blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
}

function getRadioValue (radioButtonOrGroup) {
  var value = null;
  if (radioButtonOrGroup.length) { // group 
    for (var b = 0; b < radioButtonOrGroup.length; b++)
      if (radioButtonOrGroup[b].checked)
        value = radioButtonOrGroup[b].value;
  }
  else if (radioButtonOrGroup.checked)
    value = radioButtonOrGroup.value;
  return value;
}