﻿
var maxScroll = 0;

$(document).ready(function() {
    /* homepage module */
    if (loadHpModule) initHpModule();
    //$('#hp-news li:first').css({top: '60px'}).addClass('active').animate({top: '0'}, 800);
    $('.news-pane > div > div > span > br').remove();
    $('.news-pane .news-item:first').addClass('active').hide().fadeIn(800);
    setInterval("rotateHpNews()", 5000);

    /* faq */
    $('.faq .answer').hide();
    $('.faq .question').click(function() {
        $(this).parent().find('.answer').slideToggle();
    });
    $('.faq-expand-all').click(function() {
        $('.answer').slideDown();
    });
    
    /* photos rotator */
    $('.photo-rotator').each(function() {
        
        var totalPhotos = $(this).children().length;
        var eqi = Math.floor(Math.random() * (totalPhotos));

        $(this).children().eq(eqi).addClass('active');

    });
    if ($('.photo-rotator').length > 0) {
        setInterval("rotatePhotos()", 7000);
    }

    /* newsletter */
    $('.newsletter-subscribe-text').focus(function() {
        if ($(this).hasClass('default-val')) {
            $(this)
                .removeClass('default-val')
                .css({ textAlign: 'left', direction: 'ltr' })
                .attr({ value: '' });
        }
    });





    /* gallery */

    $('.gallery-thumbnails').mezooOneBigGallery();
    $('.gallery-thumbnails img').load(function() {
        setTimeout("resizeGalleryThumbsSlider()", 1000);
    });
    $('.gallery-slider-range').slider({
        orientation: 'horizontal',
        value: 100,
        min: 0,
        max: 100,
        value: 100,
        change: function(event, ui) {
            updateGalleryThumbsScroll();
        },
        slide: function(event, ui) {
            updateGalleryThumbsScroll();
        }
    });
    resizeGalleryThumbsSlider();
    $('a.gallery-slider-left').mousedown(function() {
        pageGalleryThumbs(-1);
    });
    $('a.gallery-slider-right').mousedown(function() {
        pageGalleryThumbs(+1);
    });
    /*$('.listpage2 ul.galleries')
    .hide()
    .parent().find('h2')
    .click(function() {
    $(this).parent().find('ul.items').slideToggle();
    })
    .css({cursor: 'pointer'});*/

    /* staff */

    /* staff list expand/collapse */
    $('.listpage2 ul.staff')
        .hide()
        .parent().parent().parent().find('h2')
            .click(function() {
                $(this).parent().find('ul.items').slideToggle();
            })
            .css({ cursor: 'pointer' });


    

});

function pageGalleryThumbs(direction) {
    var newVal = $('.gallery-slider-range').slider('option', 'value') + (427/maxScroll)*100 * direction;
    newVal = Math.max(Math.min(newVal, 100), 0);
    $('.gallery-slider-range').slider('option', 'value', newVal);
    updateGalleryThumbsScroll();
}
function updateGalleryThumbsScroll() {
    $('.gallery-thumbnails ul.items').css({right: -((100-$('.gallery-slider-range').slider('option', 'value'))/100)*maxScroll+'px'});
}
function resizeGalleryThumbsSlider() {
    var totalWidth = 0;
    $('.gallery-thumbnails img').each(function() {
        totalWidth = totalWidth + $(this).width() + 6;
    });
    $('.gallery-thumbnails li:last').addClass('last');
    totalWidth = totalWidth - 6;
    $('.gallery-thumbnails ul.items').css({width: totalWidth + 'px'});
    maxScroll = totalWidth - $('.gallery-thumbnails').width();
    if (totalWidth > $('.gallery-thumbnails').width()) {
        $('.gallery-slider').show();
    }
    else {
        $('.gallery-slider').hide();
    }
}

jQuery.fn.mezooRotatePhotos = function() {
    return this.each(function(){
    var $active = $(this).find('.active');
    var $next = $active.next();
    if ($next.length == 0) $next = $active.parent().find('.captioned-photo:first');

    $active.addClass('last-active');

    $next
        .hide()
        .addClass('active')
        .fadeIn(1000, function() {
            $active.removeClass('active last-active');
        });
    });
}

function rotatePhotos() {
    $('.photo-rotator').mezooRotatePhotos();
}

$(function() {
    
});

function rotateHpNews() {
    var $active = $('.news-pane .news-item.active')

    $active
        .fadeOut(800, function() {
            $(this).removeClass('active');
            var $next = $active.parent().next().find('.news-item');
            if ($next.length == 0) { $next = $('.news-pane .news-item:first') }
            $next
                .addClass('active')
                .hide().fadeIn(800);
        });


//        .css({top: '60px'})
//        .animate({top: '0'}, 800, function() {
//        .animate({top: '-60px'}, 800, function() {
}