$(document).ready(function () {
    
    // ---- Lighbox Fucntionality ----------------------------------------------------------------------------------------------------------

    $(".OutcomesGalleryItemLink").colorbox();

    $(".StaticLogin").colorbox({ iframe: true, innerWidth: 320, innerHeight: 340 });

    //$(".ParticipantColorboxLink").colorbox({ iframe: true, innerWidth: 621, innerHeight: 412 });
	$(".ParticipantColorboxLink").colorbox({ iframe: true, innerWidth: 621, innerHeight: 500 });

    $(".LogonDialog input[type='submit']").val("Login");


    // ---- Generic Stuff ----------------------------------------------------------------------------------------------------------

    $('#footer nav li:last,#news li:last,.news-body aside li:last,#people li:nth-child(4n),.research #more-info li:last,.outcomes #main ul:last,.outcomes-detail #main ul:last,.outcomes li:nth-child(3n),.outcomes-detail li:nth-child(3n)').addClass('last');
    $('.outcomes li:nth-child(3n-2)').addClass('first');
    $('.outcomes-detail li:nth-child(3n-2)').addClass('first');
    $('.outcomes li:nth-child(3n)').addClass('last');

    $('#OutcomesList li:nth-child(4n)').addClass('last');

    // ---- Main Menu ----------------------------------------------------------------------------------------------------------

    $('#main-menu ul ul').addClass('sl-ul');
    $('#main-menu ul ul ul').addClass('tl-ul').removeClass('sl-ul');

    $('#main-menu>ul>li').each(function () {
        $(this).addClass('fl-li');
    });
    $('#main-menu>ul>li>ul>li').each(function () {
        $(this).addClass('sl-li');
    });
    $('#main-menu>ul>li>ul>li>ul>li').each(function () {
        $(this).addClass('tl-li');
    });

    $('#main-menu li').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });

    $('#main-menu .sl-li').each(function () {
        if ($(this).find('ul').length > 0) {
            $(this).addClass('subnav');
        }
    });


    // ---- Random Banner Image   ----------------------------------------------------------------------------------------------------------

    var totalNum = 4; //the total number of images in a folder.
    var rndNum = Math.floor(Math.random() * totalNum);
    $("#banner").css("background-image", "url(images/banner/bnr-" + rndNum + ".jpg)");



    // ---- Outcomes    ----------------------------------------------------------------------------------------------------------
    //$('#OutcomesList li:first').addClass("first");
    //$('#OutcomesList li:last').addClass("last");



    // ---- PreFilled   ----------------------------------------------------------------------------------------------------------

    $.fn.preFilled = function () {
        $(this).focus(function () {
            if (this.value == this.defaultValue) {
                this.value = "";
            }
            $(this).addClass('focus');
        }).blur(function () {
            if (!this.value.length) {
                this.value = this.defaultValue;
                $(this).removeClass('focus');
            }

        });
    };

    $(".search-field,#search-field,#fto,#ffrom").preFilled();


    // ---- Text Slider  ----------------------------------------------------------------------------------------------------------

    /*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
    ***/

    $('#slideshow .caption').hide();
    $('#slideshow div:first').addClass('active').find('.caption').slideDown().delay(4000).slideUp();
    $('<ul id="slideshow-nav"><li id="slide-prev"><a href="#" title="Previous">Previous</a></li><li id="slide-next"><a href="#" title="Next">Next</a></li></ul>').prependTo('#slideshow');

    var currentID = $('#slideshow div.active').attr('id');
    slideSwitch = function () {
        var $active = $('#slideshow div.active');
        if ($active.length == 0) $active = $('#slideshow div:last');
        var $next = $active.next().length ? $active.next() : $('#slideshow div:first');
        $next.css({ opacity: 0.0 })
				.addClass('active')
				.animate({ opacity: 1.0 }, 1000, function () {
				    $active.removeClass('active last-active');
				    $(this).find('.caption').slideDown().delay(3000).slideUp();
				});
        var currentID = $next.attr('id');
    }

    $('#slide-next').click(function () {
        clearInterval(slideshowIntervalID);
        $('#slideshow div').removeClass('last-active');
        var $active = $('#slideshow div.active');
        $('#slideshow div.active').addClass('last-active').find('.caption').slideUp(); ;
        if ($active.length == 0) $active = $('#slideshow div:last');
        var $next = $active.next().length ? $active.next() : $('#slideshow div:first');
        $next.css({ opacity: 0.0 })
				.addClass('active')
				.animate({ opacity: 1.0 }, 1000, function () {
				    $active.removeClass('active');
				    $(this).find('.caption').slideDown();
				});
        return false;
    });

    $('#slide-prev').click(function () {
        clearInterval(slideshowIntervalID);
        $('#slideshow div').removeClass('last-active');
        var $active = $('#slideshow div.active');
        $('#slideshow div.active').addClass('last-active').find('.caption').slideUp(); ;
        if ($active.length == 0) $active = $('#slideshow div:last');
        var $prev = $active.prev('div').length ? $active.prev('div') : $('#slideshow div:last');
        $prev.css({ opacity: 0.0 })
				.addClass('active')
				.animate({ opacity: 1.0 }, 1000, function () {
				    $active.removeClass('active');
				    $(this).find('.caption').slideDown();
				});

        return false;
    });

    var slideshowIntervalID = setInterval("slideSwitch()", 5000);



    // ---- VIDEO SLIDER  ----------------------------------------------------------------------------------------------------------

    //    var currentID = $('#slideshow figure.active').attr('id');
    //    $('#slideshow-nav a[href$=' + currentID + ']').addClass('active');

    //    videoSlideSwitch = function () {
    //        var $active = $('#video-slideshow figure.active');
    //        if ($active.length == 0) $active = $('#video-slideshow figure:last');
    //        var $next = $active.next().length ? $active.next() : $('#video-slideshow figure:first');
    //        $active.addClass('last-active');
    //        $next.css({ opacity: 0.0 })
    //				.addClass('active')
    //				.animate({ opacity: 1.0 }, 1000, function () {
    //				    $active.removeClass('active last-active');
    //				});
    //        $('#slideshow-nav a').removeClass('active');
    //        var currentID = $next.attr('id');
    //        $('#slideshow-nav a[href$=' + currentID + ']').addClass('active');
    //    }

    //    $('#slideshow-nav a').click(function () {
    //        alert("Video");
    //        clearInterval(videoSliderIntervalID);
    //        $('#slideshow-nav a').removeClass('active');
    //        $(this).addClass('active');
    //        var clickedSlide = $(this).attr('href').replace("#", "");
    //        var $active = $('#video-slideshow figure.active');
    //        $('#video-slideshow figure.active').removeClass('active').addClass('last-active');
    //        $('#video-slideshow figure').each(function () {
    //            if ($(this).attr('id') == clickedSlide) {
    //                $(this).css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 1000, function () {
    //                    $active.removeClass('active last-active');
    //                });
    //            }
    //        });

    //        return false;
    //    });

    //    var videoSliderIntervalID = setInterval("videoSlideSwitch()", 6000);



    // ---- Logo Carousel  ----------------------------------------------------------------------------------------------------------


    function mycarousel_initCallback(carousel) {
        // Disable autoscrolling if the user clicks the prev or next button.
        carousel.buttonNext.bind('click', function () {
            carousel.startAuto(0);
        });

        carousel.buttonPrev.bind('click', function () {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function () {
            carousel.stopAuto();
        }, function () {
            carousel.startAuto();
        });
    };

    $('#mycarousel').jcarousel({
        auto: 6,
        wrap: 'circular',
        initCallback: mycarousel_initCallback
    });


    // ---- Tabs ----------------------------------------------------------------------------------------------------------

    $(".tab-content").hide(); // Hide all content

    $("#tabs").each(function () {
        if ($(this).find('.active').size() == 0) {
            $(this).find('.tab-content:first').addClass('active').show();
            $(this).find('ul.tabs li:first').addClass("active");
        } else {
            var activeTab = $(this).find('.active a').attr("href");
            $(activeTab).fadeIn();
        }
    });

    $("ul.tabs li").click(function () {
        $(this).parents('#tabs').find(".active").removeClass("active");
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        $(this).parents('#tabs').find(".tab-content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });


    // ---- Expandable Table  ----------------------------------------------------------------------------------------------------------



    //$('.sort').append('<img src="/crcsi/images/btn-sort.gif" alt="Sort this column" />');

    $(".sortable").not('#events')
			.collapsible("td.collapsible", {
			    collapse: true
			})
			.tablesorter({
			    // don't sort by last column
			    headers: {
			        3: { sorter: false }
			    }
			, debug: false
			});

    $("#events")
			.collapsible("td.collapsible", {
			    collapse: true
			})
			.tablesorter({
			    // don't sort by last column
			    headers: {
			        0: { sorter: false },
			        1: { sorter: false },
			        2: { sorter: false }
			    }
			, debug: false
			});

    // ---- Submenu  ----------------------------------------------------------------------------------------------------------

    $('ul.menu ul').hide();
    $.each($('ul.menu'), function () {
        if ($(this).find('.active').length > 0) {
            $(this).find('.active ul').show();
        }
        else {
            $('#' + this.id + '.expandfirst ul:first').show();
            $('#' + this.id + '.expandfirst li:first').addClass('active');
        }


    });
    $('ul.menu>li>a').click(
			function () {
			    var checkElement = $(this).next();
			    var parent = this.parentNode.parentNode.id;
			    var parentLi = $(this).parent('li');
			    $(this).parents('ul').find('.active').removeClass('active');

			    if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			        if ($('#' + parent).hasClass('collapsible')) {
			            $('#' + parent + ' ul:visible').slideUp('normal');
			            $(parentLi).addClass('active');
			        }
			        return false;
			    }
			    if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			        $('#' + parent + ' ul:visible').slideUp('normal');
			        checkElement.slideDown('normal');
			        $(parentLi).addClass('active');
			        return false;
			    }
			}
		);


    // ---- People Vertical Slider  ----------------------------------------------------------------------------------------------------------

    $('#people-slider').jcarousel({
        vertical: true,
        auto: 0,
        scroll: 5
    });


    // ---- FAQs  ----------------------------------------------------------------------------------------------------------

    $('.faqs ol a').click(function () {
        var clickedItem = $(this).attr('href');
        var targetOffset = $(clickedItem).offset().top;
        $('html, body').animate({ scrollTop: targetOffset }, 'slow');
    });

    $('.faqs .b2t').click(function () {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
        return false;
    });


    // ---- EasyList splitter  ----------------------------------------------------------------------------------------------------------


    $('.participants-cont ul').easyListSplitter({
        colNumber: 3,
        direction: 'horizontal'
    });
    $('.participants-detail .members').easyListSplitter({
        colNumber: 4,
        direction: 'horizontal'
    });


    // ---- Colorbox Stuff ----------------------------------------------------------------------------------------------------------

    $('.participant-link').colorbox({ width: "640px", height: "480px", iframe: true }); //, .signoutLink

});



		
