
$(document).ready(function () {

    /* Homepage slideshow */
    $("div#slideshow").cycle({
        "timeout": 8000,
        "fx": "uncover",
        "pause": 1,
        "pager": "#pager"
    });

    /* Clear inputs */
    $('.focus-clear').focus(function () {
        ($(this).val() == $(this).attr('title')) ? $(this).val('') : null;
    }).blur(function () {
        ($(this).val() == '') ? $(this).val($(this).attr('title')) : null;
    });

    /* Homepage info box tabs */
    $("div#info-box .section").hide();
    $("div#info-box .section:first").show();

    $("ul#nav-tabs li a").mouseover(function () {
        if (!$(this).hasClass('selected')) {
            $("div#info-box .section").hide();

            var theTab = $(this).attr("rel");
            $("div#info-box").find(theTab).fadeIn("slow");
            $("ul#nav-tabs a").removeClass("selected");
            $(this).addClass("selected");
        }
        return false;
    });

    /* Homepage events tabs */
    $("div#news-events-container .news-events-list").hide();
    $("div#news-events-container .news-events-list:first").show();

    $("div#news-events ul li a").click(function () {
        $("div#news-events-container .news-events-list").hide();
        var theTab = $(this).attr("href");
        $("div#news-events-container").find(theTab).show();
        $("div#news-events ul li a").removeClass("selected");
        $(this).addClass("selected");
        return false;
    });

    /* Locations list toggle */
    $("ul.locations-list ul").hide();
    $("ul.locations-list li a.first").click(function () {
        $(this).css("width", $(this).width()); // Stops IE7 from flickering
        $(this).hasClass("selected") ? $(this).removeClass("selected") : $(this).addClass("selected");
        $(this).parent().find("ul").slideToggle();
        return false;
    });

    /* Drop down menu */
    $("ul.sf-menu").superfish({ "autoArrows": false, speed: "fast" });
    $("ul.sf-menu li ul li:first a").css("border-top", "none");
    $("ul.sf-menu li ul li:last a").css("border-bottom", "none")

    /* Subnav toggle */
    //    $("ul#subnav ul").hide();
    //    $("ul#subnav li a").click(function () {

    //        $(this).css("width", $(this).width()); // Stops IE7 from flickering

    //        $(this).parent().find("ul").slideToggle(function () {

    //            $(this).css("display") == 'block' ? $(this).parent().find("a:first").css("background-image", "url('../images/bg-subnav-link-selected.gif')") : $(this).parent().find("a:first").css("background-image", "url('../images/bg-subnav-link.gif')");

    //        });
    //        //$(this).parent().find("ul").toggle();
    //        return false;
    //    });

    /* Remove courses */
    //    $("a.remove-course").click(function () {
    //        /* Ajax call here... */
    //        /* on success function ... */
    //        $(this).parent().remove();
    //    });

    // Target _blank for external links
    $("a[rel='external']").attr("target", "_blank");


    $("ul.course-options li a[rel='tab-course-information']").click(function () {
        $(this).parents("ul").find("a").removeClass("selected");
        $(this).addClass("selected");
        $("div.course-info-box #tab-enrol-now").hide();
        $("div.course-info-box #tab-course-information").show();
    });

    $("ul.course-options li a[rel='tab-enrol-now']").click(function () {
        $(this).parents("ul").find("a").removeClass("selected");
        $(this).addClass("selected");
        $("div.course-info-box #tab-enrol-now").show();
        $("div.course-info-box #tab-course-information").hide();
    });

    // Email course options expander
    $(".course-email-options").hide();
    $(".course-email, .course-email-cancel").click(function () {
        $(".course-email-options").slideToggle();
        return false;
    });

    /* Popup windows */
    $(".pop-up").colorbox({
        iframe: true,
        width: "470px",
        height: "580px",
        scrolling: false,
        innerHeight: "482px"
    });


    /* Image gallery */
    $(".gallery-thumbs").cycle({
        fx: "scrollLeft",
        timeout: 0,
        after: function (currSlideElement, nextSlideElement, options) {
            slideIndex = options.currSlide;
            nextIndex = slideIndex + 1;
            prevIndex = slideIndex - 1;


            if (slideIndex == options.slideCount - 1) {
                nextIndex = 0;
            }
            if (slideIndex == 0) {
                prevIndex = options.slideCount - 1
            }
        }
    });

    /* Init */
    $(".gallery-thumbs img:first").addClass("selected");

    var total = $(".gallery-thumbs div").length;
    if (total > 1) {

        $(".gallery-nav-left").click(function () {
            $(".gallery-thumbs").cycle(prevIndex, "scrollRight");
        });

        $(".gallery-nav-right").click(function () {
            $(".gallery-thumbs").cycle(nextIndex, "scrollLeft");
        });

    } else {

        $(".gallery-nav-left, .gallery-nav-right").each(function () {
            $(this).css("background-position", "0 -73px");
        }).click(function () {
            return false;
        });
        ;
    }

    $(".gallery-thumbs a").click(function () {
        /* Add selected state */
        $(".gallery-thumbs img").removeClass("selected");
        $(this).children("img").addClass("selected");
        /* Change larger img */
        var med = $(this).attr("href");
        var lg = med.replace(/medium/gi, '');
        $(".gallery img").fadeOut("fast", function () {
            $(this).parent().remove();
            $("<a href='" + lg + "' class='gallery-pop'><img src='" + med + "' alt='' /></a>").appendTo(".gallery");
            $(".gallery img").css("display", "none").fadeIn("slow");
        });


        var caption = $(this).attr("title");
        $("div.caption p").text(caption);
        return false;
    });

    $(".gallery img").live("mouseover mouseout", function (e) {
        if (e.type == 'mouseover') {
            $("div.caption").fadeIn("slow");
        } else if (e.type == 'mouseout') {
            $("div.caption").fadeOut("slow");
        }
    });

    $(".gallery a.gallery-pop").live("mouseover", function () {
        var url = $(this).attr("href");
        $(this).colorbox({
            href: url,
            title: false
        });
        return false;
    });
});
