$(function () {
    // Navigation

    $('ul#main-nav li:not(li li)').hover(
        function () {
            $(this).find('ul').css(
                {
                    'top': '135px',
                    'left': $(this).offset().left - $('div#wrapper').offset().left,
                    'position': 'absolute'
                }
            ).fadeIn();
        },
        function () {
            $(this).find('ul').fadeOut();
        }
    )

    // Contact US
    var $scrollingDiv = $("#contact-us"),
 		offset = +$("#contact-us").css('top').replace(/px/, ''),
 		scrolling = false;

    $(window).scroll(function () {
        scrolling = true;
    });

    setInterval(function () {
        if (scrolling) {
            scrolling = false;
            $scrollingDiv
				.stop()
				.animate({ "top": ($(window).scrollTop() + offset) + "px" }, 500);
        }
    }, 250);

    var now = new Date();
    now = dateFormat(now, "dddd dS mmmm yyyy");
    $('div#clock').append($('<p />').text(now));

    $('body').addClass('hasjs');


    $('[placeholder]').focus(function () {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
            input.val('');
            input.removeClass('placeholder');
        }
    }).blur(function () {
        var input = $(this);
        if (input.val() == '' || input.val() == input.attr('placeholder')) {
            input.addClass('placeholder');
            input.val(input.attr('placeholder'));
        }
    }).blur();

    $('[placeholder]').parents('form').submit(function () {
        $(this).find('[placeholder]').each(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
            }
        })
    });

    // BSP scroller
    var $firstContainer = $('div#bsp-list ul'),
		$secondContainer = $('div#bsp-list ul').clone().appendTo('div#bsp-list div'),
		limit = $firstContainer.height(),
		first = 0, second = limit;

    $secondContainer.css({
        'top': limit
    });

    function slideLogo() {

        $firstContainer.css({
            'top': first--
        });

        $secondContainer.css({
            'top': second--
        });

        if (first * -1 == limit) {

            $firstContainer.css({
                'top': limit
            });
            var $swap = $firstContainer;
            $firstContainer = $secondContainer
            $secondContainer = $swap;
            first = 0, second = limit;
        }

    }
    var interval;

    $('div#bsp-list ul li:last img').load(function () {
        interval = setInterval(slideLogo, 50);
    });

    $('div#bsp-list').hover(function () {

        interval = clearInterval(interval);

    }, function () {

        interval = setInterval(slideLogo, 50);

    });

    // Gallery
    var $gallery = $('<div id="gallery"><a id="gallery-next" href="#">next</a><a id="gallery-prev" href="#">prev</a><a id="gallery-close" href="#">Close</a><div class="images"></div></div>');
    $gallery.appendTo('body');
    var $veil = $('<div id="veil"></div>');
    $veil.appendTo('body');

    var $next = $('a#gallery-next');
    var $prev = $('a#gallery-prev');
    var $close = $('a#gallery-close');
    var $photos;
    var currentPhotoIndex = 0;
    var isVideo = false;
    var isGallery = false;

    $('a#summon-gallery').click(function (e) {
        e.preventDefault();

        closeGallery();

        $('div.images', $gallery).load($(this).attr('href') + ' ul#gallery-images', function () {

            $photos = $('ul#gallery-images li', $gallery);
            $photos.each(function (i) {

                $(this).css({
                    'z-index': $photos.length - i,
                    'position': 'absolute'
                });

            });

            $veil.hide().addClass('active');
            $veil.fadeIn(function () {
                $gallery.hide().addClass('active');
                $gallery.fadeIn(function () {

                    isGallery = true;

                });
            });

        });

    });


    $veil.click(function (e) {
        e.preventDefault();

        closeGallery();

    });

    $close.click(function (e) {
        e.preventDefault();

        closeGallery();

    });

    $next.click(function (e) {
        e.preventDefault();


        var index = currentPhotoIndex;

        var next = index + 1;

        if (next == $photos.length) {
            next = 0;
        }

        var $curr = $($photos[index]);

        animateNextPhoto(index, $curr, function (next) {

            currentPhotoIndex = next;

        });

    });

    $prev.click(function (e) {
        e.preventDefault();

        var previous = currentPhotoIndex - 1;

        if (previous < 0) {
            previous = $photos.length - 1;
        }

        var $curr = $($photos[previous]);

        animatePreviousPhoto(previous, $curr, function (previous) {

            currentPhotoIndex = previous;

        });

    });

    $gallery.hover(
		function () {
		    $next.show();
		    $prev.show();
		},
		function () {
		    $next.hide();
		    $prev.hide();
		}
	)

    function animateNextPhoto(index, $curr, callback) {
        $curr.fadeOut(500, function () {

            $photos.each(function (i) {

                var $this = $(this);

                $this.css('z-index', +($this.css('z-index')) + 1);

            });

            $curr.css('z-index', 0).show(); ;

            var next = index + 1;

            if (next == $photos.length) {
                next = 0;
            }

            callback.call(this, next);

        });

    }

    function animatePreviousPhoto(index, $curr, callback) {

        $curr.hide();

        $photos.each(function (i) {

            var $this = $(this);

            $this.css('z-index', +($this.css('z-index')) - 1);

        });

        $curr.css('z-index', $photos.length).fadeIn(500, function () {

            callback.call(this, index);

        });
    }

    // Banner Gallery

    function animateToPhoto(next, $collection, callback) {

        var $next = $($collection[next]);

        $next.hide();

        $collection.each(function (i) {

            var $this = $(this),
                curr = $this.css('z-index') == 'auto' ? 0 : $this.css('z-index'),
                depth =  +(curr) - 1;
            $this.css('z-index',depth);

        });

        $next.css('z-index', $collection.length).fadeIn(500, function () {

            callback.call(this, next);

        });
    }
    var $bannerImages = $('div#content-flash img');
    var bannerIndex = 0;
    if ($bannerImages.length > 1 && $('body:not(.editmode)').length === 1) {

        $('div#content-flash img:last').imagesLoaded(function (e) {
        
            setTimeout(function () {

                animateToPhoto(bannerIndex, $bannerImages, function doNext(next) {

                    bannerIndex = next + 1;

                    if (bannerIndex > $bannerImages.length - 1) {

                        bannerIndex = 0;

                    }

                    setTimeout(function () {

                        animateToPhoto(bannerIndex, $bannerImages, doNext);

                    }, 2000);

                });

            }, 2000);

        });

    }

    // Video 
    var $video = $('<div id="video-player"><a id="video-close" href="#">Close</a><div id="video-swf"></div></div>');
    $video.appendTo('body');

    $('a.video-link').click(function (e) {

        e.preventDefault();

        closeGallery();

        var src = $(this).attr('href');

        $veil.hide().addClass('active');
        $veil.fadeIn(function () {
            $video.addClass('active');
            $video.fadeIn(function () {

            });
        });

        $('div#video-swf', $video).flashembed({
            width: 620,
            height: 350,
            player: 'swf/videoplayer.swf',
            flashvars: {
                videoSource: src,
                stageWidth: 620,
                stageHeight: 350
            },
            params: {
                wmode: 'transparent',
                allowFullScreen: true,
                scale: 'noscale',
                salign: 'tl',
                bgcolor: '#000000'
            },
            id: 'videoplayer'
        });

        isVideo = true;

    });

    $('a#video-close').click(function (e) {
        e.preventDefault();

        closeGallery();

    });

    function closeGallery() {

        if (isGallery) {

            $gallery.fadeOut(function () {
                $gallery.removeClass('active');
                $veil.fadeOut(function () {
                    $veil.removeClass('active');
                    currentPhotoIndex = 0;
                });
            })
        } else {

            $video.fadeOut(function () {
                $video.removeClass('active');
                $('div#video-swf', $video).html('<span>&nbsp;</span>');
                $veil.fadeOut(function () {
                    $veil.removeClass('active');
                });
            })

        }
        isVideo = false;
        isGallery = false;
    }


});
