var jQueryScriptOutputted = false;

function initJQuery() {

    //if the jQuery object isn't available
    if (typeof (jQuery) == 'undefined') {

        if (!jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;

            //output the script (load it from google api)
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {

        $(function () {
            enhancedUI();
        });
    }

}
initJQuery();


function enhancedUI() {

    //  ---------------- Alternating rows --------------------
    //$('.library tbody tr:even td').addClass('alt');
    $('.library tr').each(function () {
        $('td:first,th:first', this).addClass('first');
    });

    $('.library h3:first, .library tr:last td').css('border', '0px');
    $('.library h3')
        .hover(function () {
            $(this).toggleClass('over');
        })
        .click(function () {

            var openNew = $(this).is('.open') ? false : true;
            $('.library div.open').slideToggle(600).removeClass('open');
            $('.library h3.open').removeClass('open');

            if (openNew) {
                $(this).next().slideToggle(600).addClass('open');
                $(this).toggleClass('open');
            }
        });

    $('.library h3:first').trigger('click');

    //  ---------------- Tabs --------------------
    // Fade tabs and buttons on mouseover
    $('#tabs li').hover(
        function () {
            if (!$(this).is('.tabOn')) $(this).fadeTo(200, 0.5);
        },
        function () {
            if (!$(this).is('.tabOn')) $(this).fadeTo(10, 1);
        })
		.click(function () {
		    $(this).fadeTo(10, 1);
		});


    //  ---------------- jCarousel --------------------
    if ($('#carousel').length) {

        // Append next / prev buttons
        $('#carousel').prepend('<div class="slidenav prev disabled"><a href="#"></a></div>');
        $('#carousel').append('<div class="slidenav next"><a href="#"></a></div>');

        var currSlide = 0,
            totalSlides = $('#carousel li').length,
            slideW = $('#carousel li:first').width();

        // Set carousel with to the sum of its slides
        $('#carousel ul').css('width', totalSlides * slideW + 'px');

        // Move slides as per user click
        $('#carousel .slidenav a').click(function () {
            if (!$(this).parent().is('.disabled')) {
                var newSlide = $(this).parent().is('.prev') ? currSlide - 2 : currSlide + 2;

                // Toggle buttons on start / end
                if (newSlide > 0) $('#carousel .slidenav.prev').removeClass('disabled');
                else $('#carousel .slidenav.prev').addClass('disabled');

                if (newSlide < totalSlides - 2) $('#carousel .slidenav.next').removeClass('disabled');
                else $('#carousel .slidenav.next').addClass('disabled')

                currSlide = newSlide;
                $('#carousel ul').animate({ left: -slideW * currSlide + 'px' }, 1000);
            }
            return false;
        });

    }


    //  ---------------- News scroller --------------------

    if ($('#newsticker').length) {
        var $n = $('#newsticker ul');
        var newsW = $n.width();

        // Duplicate the scroller to remove any blank views 
        $n.append($n.html()).css('width', newsW * 2 + 'px');

        // Do animation
        function scrollStory() {
            pos = $n.position();
            // Start again if we reach the original width
            if (Math.abs(pos.left) > newsW) {
                $n.css('left', 0 + 'px');
                scrollStory();
                return;
            }
            $n.animate({ left: (pos.left - 1) + 'px' }, 1, function () {
                if (!pauseScroll) scrollStory();
            });
        }

        // Pause when mouse goes over a lunk
        var pauseScroll = false;
        $('#newsticker a')
		    .mouseover(function () {
		        pauseScroll = true;
		    })
		    .mouseout(function () {
		        pauseScroll = false;
		        scrollStory();
		    });

        scrollStory();
    }

    // Minified jQuery cookie plugin
    eval(function (p, a, c, k, e, d) { e = function (c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) }; if (!''.replace(/^/, String)) { while (c--) { d[e(c)] = k[c] || e(c) } k = [function (e) { return d[e] } ]; e = function () { return '\\w+' }; c = 1 }; while (c--) { if (k[c]) { p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]) } } return p } ('m.b=i(c,3,0){7(p.v>1&&(3===6||d 3!=="q")){0=m.u({},0);7(3===6){0.2=-1}7(d 0.2===\'o\'){g e=0.2,t=0.2=l r();t.z(t.C()+e)}4(n.b=[9(c),\'=\',0.j?h(3):9(h(3)),0.2?\'; 2=\'+0.2.A():\'\',0.5?\'; 5=\'+0.5:\'\',0.8?\'; 8=\'+0.8:\'\',0.f?\'; f\':\'\'].w(\'\'))}0=3||{};g a,k=0.j?i(s){4 s}:x;4(a=l y(\'(?:^|; )\'+9(c)+\'=([^;]*)\').B(n.b))?k(a[1]):6};', 39, 39, 'options||expires|value|return|path|null|if|domain|encodeURIComponent|result|cookie|key|typeof|days|secure|var|String|function|raw|decode|new|jQuery|document|number|arguments|object|Date|||extend|length|join|decodeURIComponent|RegExp|setDate|toUTCString|exec|getDate'.split('|'), 0, {}))

};


