﻿$(document).ready(function() {

    DD_roundies.addRule("a.button", "4px");

    $.fn.delay = function(time, callback) {
        // Empty function:
        jQuery.fx.step.delay = function() { };
        // Return meaningless animation, (will be added to queue)
        return this.animate({ delay: 1 }, time, callback);
    }

    $("ul#sponsors a").roll({
        speed: 400
    });

    $("div#feature .slideshow").append('<img src="/images/header-images/header-mask.png" class="mask" alt="" />');

    $("div#feature .slideshow ul, div#feature .splash ul").cycle({
        timeout: 6000,
        speed: 1000
    });

    $("ul.slideshow").each(function(i) {
        $(this).height($(this).find("img:first").height());
        $(this).parent().after('<div class="module caption"><p id="caption" class="caption"></p></div>');
        if ($(this).find('li').length > 1) {
            $(this).after('<a class="previous">Prev</a> <a class="next">Next</a>');
            $(this).cycle({
                delay: 0,
                timeout: 6000,
                speed: 1000,
                sync: 0,
                prev: $(this).parent().find('.previous'),
                next: $(this).parent().find('.next'),
                before: onBefore
            });
        }
    });

    function onBefore(current, next, options, forward) {
        $('#caption').html($(this).find("img").attr("title"));
        $(next).css("display", "block");
        var h = $(next).find("img").height();
        $(this).parent().delay(500).animate({ height: h }, 500);
    }

    var aImages = new Array();
    var aTitles = new Array();
    var aAlts = new Array();
    var nCurrent = 0;

    function showImage(n) {
        if (n >= aImages.length) {
            n = 0;
        } else if (n < 0) {
            n = aImages.length - 1;
        }
        if ($('img.large').length > 0) {
            function fadeBack() {
                $('img.large').attr("src", aImages[n]);
                $('h2#title').html(aTitles[n]);
                $('p#description').html(aAlts[n]);
                $('img.large').animate({ opacity: "1" }, 500);
            }
            $.scrollTo(123, 1000);
            $('img.large').animate({ opacity: "0" }, 500, fadeBack);
        }
        nCurrent = n;
    }

    $(".module.thumbs li").each(function(i) {
        if ($("img.large").length > 0) {
            var sSrc = $(this).find('a').attr('href');
            var sTitle = $(this).find("img").attr("title");
            var sAlt = $(this).find("img").attr("alt");
            var img = new Image();
            img.src = sSrc;
            aImages.push(sSrc);
            aTitles.push(sTitle);
            aAlts.push(sAlt);
            $(this).data("i", i);
            $(this).click(function(e) {
                showImage(i);
                e.preventDefault();
                return false;
            })
        }
    });

    if ($("img.large").length > 0) {
        var sSrc = $(".module.thumbs li a:first").attr("href");
        var sTitle = $(".module.thumbs li a:first").parents().find("img").attr("title");
        $('img.large').attr("src", sSrc);
        $('h2#title').html(sTitle);
        $('img.large').after("<a class='previous' title='Previous'>Previous</a><a class='next' title='Next'>Next</a>");
        $("a.previous").click(function() {
            showImage(nCurrent - 1);
        })
        $("a.next").click(function() {
            showImage(nCurrent + 1);
        })
    }

    $(".module.thumbs li").hover(
		function() {
		    $(this).css("position", "relative");
		    $(this).animate({ top: "-5px" }, { queue: false, duration: 150 })
		    $(this).find("p").animate({ top: "96px" }, { queue: false, duration: 150 })
		},
		function() {
		    $(this).animate({ top: "0px" }, { queue: false, duration: 150 })
		    $(this).find("p").animate({ top: "91px" }, { queue: false, duration: 150 })
		}
	);

    $("input.search").focus(function() {
        var val = $(this).val();
        var str = $(this).attr("title");
        if (val == str) { $(this).val(""); }
    });

    $("input.search").blur(function() {
        var val = $(this).val();
        if (val == "") { $(this).val($(this).attr("title")); }
    });

    $(".module.training").data("height", $('.module.training').height());
    $(".module.training").addClass("jquery");
    $(".module.training").after("<a class='module read'>Read more</a>");
    $("a.module.read").click(function() {
        var h = $(".module.training").data("height") + 36;
        $(".module.training").animate({ height: h }, 500);
        $(this).remove();
        return false;
    });

    $("ul#sub_menu li.selected").parent().parent().not("ul#sub_menu").addClass("has-sub");
    $("ul#sub_menu li.selected").parent().parent().removeClass("selected");
});