$(function() {
    //add last-child psuedo class
    $('.msie td:last-child,.msie div:last-child,.msie span:last-child,#dnn_ctr380_Links_lstLinks  tr td:last-child').each(//hack psuedo class for lame, pathetic ie
        function() {
            $(this).addClass('last-child');
        }
    );
    $('.msie td:first-child,.msie div:first-child,.msie span:first-child').each(//hack psuedo class for lame, pathetic ie
        function() {
            $(this).addClass('first-child');

        }
    );

    //JAVASCRIPT REORDER/RANDOM LIST SHOWING
    //shuffle
    $('.reorder').reorder();
    $('.reorder').each(//hack psuedo class for lame, pathetic ie
        function() {
            $(this).children('table:first').addClass('firstChild');
        }
    );


    $('#secondary-links ul').hide();
    //STYLE SECONDARY LINKS BASED ON HOW MANY THERE ARE
    var tclass = '';
    if ($('#secondary-links ul').children().size() <= 2) {
        tclass = 'large';
    } else {
        tclass = 'small';
    }
    $('#secondary-links').addClass(tclass);
    $('#secondary-links ul').show();

    //LOGIN BOX TEXT HANDLING
    $('div.black-box input').click(
        function() {
            if ($(this).val() == 'Login' || $(this).val() == 'Password') {
                $(this).val('');
            }
        }
    );



    //SEARCH BOX
    if ($('#dnn_SEARCH_txtSearch').length > 0) {
        $('#dnn_SEARCH_txtSearch').val('Search');
    }

    $('#dnn_SEARCH_txtSearch').click(
            function() {
                if ($(this).val() == 'Search') {
                    $(this).val('');
                }
            }
    );

    $('#dnn_SEARCH_txtSearch').blur(
            function() {
                if ($(this).val() == '') {
                    $(this).val('Search');
                }
            }
    );

    //add firstChild class to first-child to help browsers that don't support the psuedo class
    $('.inventua_sidemenu table table tr:first').addClass('firstChild');

    //special secondary link image container handling
    //this div needs to be hidden by default, but in order to edit it, it has to be shown.
    //this was the quickest way to get that done.
    if ($('#dnn_SecondaryNavLinkImage div.c_footer .Head').children().size() > 1) {
        $('#dnn_SecondaryNavLinkImage').show();
    }

    //style psuedo last child of footer menu td to not have border right black
    $('#footer-menu table td:nth-child(' + ($('#footer-menu table').size() - 1) + ')').css('border', 'none');

    //NEWS HANDLING
    //stripe certain tables
    $('table.newscontainer').attr('cellspacing', '0');
    $('table.newscontainer').attr('cellspacing', '0');
    $('table.newscontainer').attr('border', '0');
    $('table.newscontainer').attr('rules', '');



    $('table.newscontainer tr:odd').addClass('alt');
    //$('#dnn_ctr423_viewNukeNews_ctl00_dlNews tr:first').removeClass('alt');
    //$('#dnn_ctr423_viewNukeNews_ctl00_dlNews tr:first').addClass('tr-header');
    $('a.newstitle').click(
        function() {
            $('table.press-release-header').parent().parent().parent().load($(this).attr('href') + ' #dnn_contentpane');
            return false;
        }
    );
    //END NEWS HANDLING


    //Sorted UL Handling
    //LEVEL 3
    if ($('ul.sorted-expandable-3').length > 0) {

        //sort first level
        var mylist = $('ul.sorted');
        var listitems = mylist.children('li').get();
        listitems.sort(function(a, b) {
            var compA = $(a).text().toUpperCase();
            var compB = $(b).text().toUpperCase();
            return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
        })
        $.each(listitems, function(idx, itm) { mylist.append(itm); });

        //sort second level
        $('ul.sorted-expandable-3 li ul:not(ul li ul li ul)').each(
            function() {

                var mylist1 = $(this);
                var listitems1 = mylist1.children('li').get();
                listitems1.sort(function(a, b) {
                    var compA = $(a).text().toUpperCase();
                    var compB = $(b).text().toUpperCase();
                    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
                })
                $.each(listitems1, function(idx, itm) {
                    mylist1.append(itm);
                });
            }
        );

        $('ul.sorted-expandable-3 li ul li ul').hide();
        $('ul.sorted-expandable-3 li ul li').click(
            function() {
                $(this).children('ul').toggle(250);
            }
        );
        $('ul.sorted-expandable-3 li ul li ul,ul.sorted-expandable-3 li ul li ul li').click(
            function() {
                return false;
            }
        );
        $('ul.sorted-expandable-3').show();
    }

    //LEVEL 2
    if ($('ul.sorted-expandable-2').length > 0) {

        //sort first level
        var mylist = $('ul.sorted');
        var listitems = mylist.children('li').get();
        listitems.sort(function(a, b) {
            var compA = $(a).text().toUpperCase();
            var compB = $(b).text().toUpperCase();
            return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
        })
        $.each(listitems, function(idx, itm) { mylist.append(itm); });

        //sort second level
        $('ul.sorted-expandable-2 li ul').each(
            function() {

                var mylist1 = $(this);
                var listitems1 = mylist1.children('li').get();
                listitems1.sort(function(a, b) {
                    var compA = $(a).text().toUpperCase();
                    var compB = $(b).text().toUpperCase();
                    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
                })
                $.each(listitems1, function(idx, itm) {
                    mylist1.append(itm);
                });
            }
        );

        $('ul.sorted-expandable-2 li ul').hide();

        $('ul.sorted-expandable-2 li:not(ul li ul li)').click(
            function() {
                $(this).children('ul').toggle(250);
            }
        );
        $('ul.sorted-expandable-2 li ul,ul.sorted-expandable-2 li ul li').click(
            function() {
                return false;
            }
        );
		$('ul.sorted-expandable-2 li ul li a').click(function(){
			window.open(this.href);
			return false;
		});


        $('ul.sorted-expandable-2').show();
        //		$('ul.sorted-expandable-2 li ul').hide();

    }


    //handle hovershow functions
    $('div.hovershow').parent().hover(
        function() {
            $(this).children('div.hovershow').css('visibility', 'visible');
        },
        function() {
            $(this).children('div.hovershow').css('visibility', 'hidden');
        }
    );
});

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16539762-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga=document.createElement('script');
  ga.type='text/javascript';
  ga.async=true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s=document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(ga, s);
})();
