﻿
// this is the interval (in milliseconds) that each image is shown
// this number alwasy has to be higher than (FadeSpeed + 500) to allow the
// overlay time to fade in and out
var FadeInterval = 7500;
// this is the fade speed
// nb this number cannot be less that 500
// as the white overlay needs 500 millisconds to fade out after the image has changed
var FadeSpeed = 1500;



$(document).ready(function() {

    if ($("#ImageSlideShow #ImageSlideHolder .Slide").length > 1) {

        var div = $("<div id=\"FadeWhitePanel\"><!-- --></div>");
        $("#ImageSlideShow #ImageSlideHolder").append(div);
        var NumberOfFadeSlides = $("#ImageSlideShow #ImageSlideHolder .Slide").length - 1;
        var CurrentSlide = 0;

        $("#ImageSlideShow #ImageSlideHolder .Slide").css("display", "none");
        $("#ImageSlideShow #ImageSlideHolder .Slide:eq(" + CurrentSlide + ")")
            .css("display", "block");
        $("#FadeWhitePanel").fadeOut();

        if (NumberOfFadeSlides > 0) {
            setInterval(function() {
                var thisSlide = CurrentSlide;
                CurrentSlide++;
                if (CurrentSlide > NumberOfFadeSlides) { CurrentSlide = 0; }
                // fade out the current slide
                $("#FadeWhitePanel").fadeIn(FadeSpeed, function() {
                    $("#ImageSlideShow #ImageSlideHolder .Slide:eq(" + thisSlide + ")").hide();
                    $("#ImageSlideShow #ImageSlideHolder .Slide:eq(" + CurrentSlide + ")").show();
                    $("#FadeWhitePanel").fadeOut(FadeSpeed + 500);
                });

            }, FadeInterval);
        }

    }

    if ($("#TopTen ol").length > 0) {
        var olListItems = $("#TopTen ol li");

        if (olListItems.length > 4) {

            $("#TopTen ol").remove();

            var pageOne = $("<ol></ol>");

            var count = 5;

            if (olListItems.length > 10) {
                count = (Math.round(olListItems.length / 2));
            }

            for (var i = 0; i < count; i++) {
                pageOne.append(olListItems[i]);
            }

            var pageTwo = $("<ol id=\"pageTwo\"></ol>");
            for (var j = count; j < olListItems.length; j++) {
                pageTwo.append(olListItems[j]);
            }
            
            pageTwo.css("counter-reset", "item " + count);


            $("#TopTen").append(pageOne);
            $("#TopTen").append(pageTwo);
        }
    }





    $("#TopNav li").TopMenuLink();

    $("h2").h2();

    $("#TopNav li a").hover(function() {

        if ($(this).attr("class") != "Selected") {
            $(this).parent().find(".bgTopMenu").fadeIn();
        }
    }, function() {
        if ($(this).attr("class") != "Selected") {
            $(this).parent().find(".bgTopMenu").stop(true, true).fadeOut();
        }
    });

    $("#TopNav li a.Selected").parent().find(".bgTopMenu").css("display", "block");


    var NumberOfSlides = $(".imageClick").length - 1;
    var SlideNumber = 0;

    $("#PopUpImage").css("margin-top", "-300px");
    $("#PopUpImage").css("margin-left", "-250px");


    $(".imageClick").hover(function() {
        $(this).css("background-color", "#F37B90");
        //alert($(this).children().text());
        $(this).children().css("color", "#FFFFFF");
    }, function() {
        $(this).css("background-color", "#FFFFFF");
        $(this).children().css("color", "#F37B90");
    });

    $(".imageClick").click(function(e) {
        e.preventDefault();
        var elmt = $(this);
        $(".BackgroundMask").css("visibility", "visible");
        $(".BackgroundLoading").css("visibility", "visible");

        SlideNumber = $(".imageClick").index($(this));

        $("#PopUpImageInner").html("<img id=\"MyImage\" src=\"" + elmt.attr("large") + "\"/>");

        if (document.images) {
            waitFor(document.images["MyImage"])
        }

    });

    $("#PopUpImage #PopUpLinks .NextPrev").click(function(e) {
        e.preventDefault();
        if ($(this).attr("mode") == "next") {
            SlideNumber++;
            if (SlideNumber > NumberOfSlides) {
                SlideNumber = 1;
            }
        }
        else {
            SlideNumber--;
            if (SlideNumber <= 0) {
                SlideNumber = NumberOfSlides;
            }
        }

        var newSlide = $(".imageClick").eq(SlideNumber);

        $("#PopUpLinks").fadeOut();

        setTimeout(function() {
            $("#PopUpImage").animate({ height: "50px" }, 250);

        }, 500);

        setTimeout(function() {
            $("#PopUpImageInner").html("<img id=\"MyImage\" src=\"" + newSlide.attr("large") + "\"/>");

            if (document.images) {
                waitFor(document.images["MyImage"])
            }
        }, 850);


    });

    $("#PopUpImage #PopUpLinks .Close").click(function(e) {
        e.preventDefault();
        $("#PopUpImage").css("visibility", "hidden");
        $(".BackgroundMask").css("visibility", "hidden");
        $(".BackgroundLoading").css("visibility", "hidden");
        $("#PopUpImage").css("height", "0px").css("width", "0px");
        $("#PopUpImage #PopUpLinks").css("display", "none");
        $("#PopUpImage").css("margin-top", "-300px");
        $("#PopUpImage").css("margin-left", "-250px");
    });

    $("#PopUpImageInner").click(function(e) {
        e.preventDefault();
        SlideNumber++;
        if (SlideNumber > NumberOfSlides) {
            SlideNumber = 1;
        }

        var newSlide = $(".imageClick").eq(SlideNumber);

        $("#PopUpLinks").fadeOut();

        setTimeout(function() {
            $("#PopUpImage").animate({ height: "50px" }, 250);

        }, 500);

        setTimeout(function() {
            $("#PopUpImageInner").html("<img id=\"MyImage\" src=\"" + newSlide.attr("large") + "\"/>");

            if (document.images) {
                waitFor(document.images["MyImage"])
            }
        }, 850);
    });

});


function waitFor(img) {
    if (!img.complete) {
        imgWait = setTimeout('waitFor(document.images["MyImage"])', 250);
    }
    else {
        $(".BackgroundLoading").css("visibility", "hidden");
        //var image = document.getElementById("MyImage"); 

        var ScreenWidth = $(window).width();
        var ScreenHeight = $(window).height() - 80;

        var image = $("#MyImage");
        var newHeight = image.height();
        var newWidth = image.width();



        if (((newWidth + 140) >= ScreenWidth) | ((newHeight + 180) >= ScreenHeight)) {
            // RESIZE IMAGE
            if (newWidth > ScreenWidth) {
                image.width(ScreenWidth - 110);
            }
            else {
                image.height(ScreenHeight - 130);
            }

            newHeight = image.height();
            newWidth = image.width();
        }


        $("#PopUpImage")
                .css("visibility", "visible")
                .animate(
                {
                    marginTop: "-" + (newHeight / 2) + "px",
                    marginLeft: "-" + (newWidth / 2) + "px",
                    width: (newWidth + 70) + "px",
                    height: (newHeight + 90)
                }
                , 400);

        setTimeout(function() { $("#PopUpLinks").fadeIn() }, 500);
    }
    ;
};



jQuery.fn.h2 = function() {

    this.each(function() {

        var img = $("<img src=\"img/UlyBug.gif\" class=\"h2UlyBug\"/>");

        $(this).append(img);

    });

};

jQuery.fn.TopMenuLink = function() {

    this.each(function() {
        var linkWidth = $(this).children(0).width() + 30;
        var bgdiv = $("<div class=\"bgTopMenu\"  style=\"width:" + linkWidth + "px;\"><img src=\"img/btn-left.jpg\"/></div>");

        $(this).prepend(bgdiv);

    });

};