$(document).ready(function () {
    var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();

    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});

$(function () {
    $('#gallery a').lightBox();
});

$(document).ready(function () {

    // Variables
    var menuId = 'none';

    $('#nav li').hover(
        function () {
            // Get the id from the dir attribute
            menuId = '#' + this.attributes['dir'].value;
            // Check menu exists
            if (menuId.length != 1) {
                // Object
                var menuObject = $(menuId);
                // Check object exists
                if (menuObject.length) {
                    var pos = $(this).position();
                    // Set position
                    menuObject.css('left', pos.left - 5);
                    menuObject.show();
                }
            }
        },

        function () {
            var menuObject = $(menuId);
            if (menuObject.length)
                menuObject.hide();
        }
    )

    $('#popupWindows > div').hover(
            function () {
                var menuObject = $(menuId);
                if (menuObject.length)
                    menuObject.show();
            },
            function () {
                var menuObject = $(menuId);
                if (menuObject.length)
                    menuObject.hide();
            }
        )

});

// Gallery class
$(document).ready(function () {

    var pageGallerys = $("div.gallery");
    pageGallerys.each(initalise);

    function initalise() {
        var id = "#" + $(this).attr("id");
        initaliseGallery(id);
    }

    function initaliseGallery(id) {

        var delay = 5000;
        var delayFade = 1000;
        var galleryPages = new Array();
        var galleryPagesGroup = $(id + " div.page");
        var galleryCurrent;
        var galleryTimeout;

        // Handlers
        $(id + " div.paging a").click(buttonGalleryPage);
        galleryPagesGroup.each(initalise);

        // Inital setup
        galleryPagesGroup.hide().filter(":first").show();

        // Timer for page changing
        galleryTimeout = setTimeout(nextGalleryPage, delay + delayFade)

        // Initalise the items within the gallery
        function initalise() {
            galleryPages.push($(this));

            // Set the current page if not initalised
            if (!galleryCurrent) galleryCurrent = galleryPages[0];
        }

        function nextGalleryPage() {
            // Find the next index
            var nextIndex = galleryPages.indexOf(galleryCurrent) + 1;
            if (nextIndex == galleryPages.length) {
                nextIndex = 0;
            }

            // Change to the next page
            changeGalleryPage(nextIndex);
        }

        function buttonGalleryPage() {
            // Next page from HREF
            var nextIndex = $(this).attr('href').replace('#', '');
            changeGalleryPage(nextIndex);

        }

        function changeGalleryPage(index) {
            try {
                if (galleryCurrent != galleryPages[index]) {
                    // Clear old timeout
                    if (galleryTimeout) clearTimeout(galleryTimeout);

                    // Fade out the current, fade in the next
                    galleryCurrent.fadeOut(delayFade);
                    galleryPages[index].fadeIn(delayFade);

                    // Set the current gallery image
                    galleryCurrent = galleryPages[index];

                    // Next timer slide
                    galleryTimeout = setTimeout(nextGalleryPage, delay + delayFade);
                }
            }
            catch (error) {
                alert(error.toString());
            }
        }

    }

});


