﻿//
wr = {};
//scale image to fit div
wr.fitDivImg = function(div) {
    //get elems
    var $div = $(div);
    var $img = $div.find('img');
    var img = $img[0];
    //get img size + ratios
    var imgCopy = new Image();
    imgCopy.src = $img.attr('src');
    var imgWidth = imgCopy.width;
    var imgHeight = imgCopy.height;
    var imgWHRatio = imgWidth / imgHeight;
    //get div size + ratios
    var divWidth = $div.width();
    var divHeight = $div.height();
    var divWHRatio = divWidth / divHeight;
    //determine ratio
    if (imgWHRatio > divWHRatio) {//use div height
        imgWidth = divHeight * imgWHRatio;
        imgHeight = divHeight;
    } else {//use div width
        imgWidth = divWidth;
        imgHeight = divWidth / imgWHRatio;
    }
    //set img dims
    $img.width(imgWidth);
    $img.height(imgHeight);
    $img.css('top', '0px');
    $img.css('left', '0px');
    $img.css('margin-top', (divHeight - imgHeight) / 2);
}

//collapse animation
wr.iconBorder = 5;
wr.iconSpacing = 85;
wr.iconSize = 75;
//wr.iconCount = 0;
wr.collapsePhoto = function(div, callback) {
    //get elems
    var $div = $(div);
    var $img = $div.find('img');
    var $photoFrame = $('#photo_frame');
    $div.removeClass('fullsize').removeClass('iconsize');
    //show prev
    //$div.prev().show();
    //v2.1
    //var $fulls = $('.fullsize');
    //var $next = $fulls.eq(Math.floor(Math.random() * $fulls.length));
    //$div.css('z-index', parseInt($next.css('z-index')) + 1);
    //$next.show();
    //calc
    var $icons = $('.iconsize');
    var targetTop = $icons.length * wr.iconSpacing;
    targetTop = 50; //v2.1:force to 50px

    //fade
    console.log("StartCollapse");
    $div.fadeOut(3000, callback);
    
    //animate div
//    $div.animate({//shorten height  + (2 * wr.iconBorder)
//        top: targetTop + 'px',
//        right: '0px', //v2.1:remove immediately
//        height: wr.iconSize
//    }, 2000);
//    $div.animate({//narrow to target width
//        width: wr.iconSize + (2 * wr.iconBorder)
//    }, {
//        complete: function() {
//            //fit img
//            wr.fitDivImg(div);
//            $div.addClass('iconsize');
//            //v2.1:remove immediately
//            wr.removeIcon($div);
//            //            //remove icon
//            //            var $icons = $photoFrame.find('.iconsize');
//            //            if ($icons.length > 5) {
//            //                wr.removeIcon($icons.last());
//            //            }
//            //            //slide icons
//            //            wr.slideIcons();
//            //callback
//            if (callback) callback();
//        },
//        duration: 2000
//    });
//    //animate img
//    $img.animate({//during shorten: keep steady
//        top: '-' + targetTop + 'px'
//    }, 2000);
};
wr.expandPhoto = function(div) {
    //get elems
    var $photoFrame = $('#photo_frame');
    var $div = $(div);
    var $img = $div.find('img');
    //fade out
    $div.fadeOut(500, function() {
        //slide icons up
        wr.removeIcon($div);
        wr.slideIcons();
        //push div backwards
        var topIndex = $photoFrame.find('.fullsize').length - 1;
        $div.insertAfter($photoFrame.children().eq(topIndex));
        //make full size
        $div.css({
            top: 0,
            width: $photoFrame.width(),
            height: $photoFrame.height()
        });
        //scale img
        wr.fitDivImg(div);
        //fade in
        $div.fadeIn(2000);
        //set state
        $div.addClass('fullsize');
    });
}
wr.removeIcon = function($div) {
    //
    $div.hide();
    $div.removeClass('iconsize');
}
wr.slideIcons = function() {
    //get elems
    var $photoFrame = $('#photo_frame');
    var $icons = $photoFrame.find('.iconsize');
    //slide icons
    $icons.each(function(index, div) {
        var $icon = $(div);
        var targetTop = ($icons.length - index - 1) * wr.iconSpacing;
        if ($icon.css('top') == targetTop) return;
        $icon.animate({
            top: targetTop
        }, 1000);
    });
}
wr.showingPhotoIndex = -1;
wr.autoCollapseTimeout = null;
wr.autoCollapseNext = function() {
    wr.autoCollapseTimeout = null;
    //var $fulldivs = $('.fullsize');
    //if ($fulldivs.length > 1) {//v2.1: dont collapse last one
    $photoDivs = $('.photo_div');
    var $showingPhotoDiv = $photoDivs.eq(wr.showingPhotoIndex);
    var $nextPhotoDiv = null;
    //
    if (wr.showingPhotoIndex == 0) {
        wr.showingPhotoIndex = $photoDivs.length - 1;
    }
    if (wr.showingPhotoIndex > 0) {//dont collapse last one
        wr.showingPhotoIndex--;
    } else {
        //wr.clearAutoCollapse(); //v2.1: dont collapse last one
    }
    //show next
    $nextPhotoDiv = $photoDivs.eq(wr.showingPhotoIndex);
    $nextPhotoDiv.show();
    //collapse curent   
    wr.collapsePhoto($showingPhotoDiv, function() {
        if (!wr.autoCollapseTimeout) {
            console.log("Set Next AutoCollapse");
            wr.autoCollapseTimeout = setTimeout(wr.autoCollapseNext, 4000);
        }
    });
}
wr.resetAutoCollapse = function() {
    if (wr.autoCollapseTimeout) clearTimeout(wr.autoCollapseTimeout);
    wr.autoCollapseTimeout = setTimeout(wr.autoCollapseNext, 6000);
}
wr.clearAutoCollapse = function() {
    if (wr.autoCollapseTimeout) clearTimeout(wr.autoCollapseTimeout);
}
wr.resizeFullPhotos = function() {
    var $photoFrame = $('#photo_frame');
    var $fullDivs = $('.fullsize');
    $fullDivs.css({
        width: $photoFrame.width() - (2 * wr.iconBorder),
        height: $photoFrame.height() - (2 * wr.iconBorder)
    });
    //fit to full size
    $fullDivs.each(function(index, div) {
        wr.fitDivImg(div);
    });
}
//v2.1: randomize imgs
wr.randomizePhotos = function() {
    var $photoFrame = $('#photo_frame');
    var $photoDivs = $('.photo_div');
    var $aerial = $photoDivs.last();
    //sort
    $photoDivs.sort(function() {
        var r = (Math.random() - 0.5);
        return (r);
    });
    //remove all
    $photoFrame.remove('.photo_div');
    //re-append
    for (var i = 0; i < $photoDivs.length; i++) {
        $photoFrame.append($photoDivs[i]);
    }
    //remove+re-append aerial
    $aerial.remove();
    //$photoFrame.append($aerial);
};


//handle resize
$(window).resize(function() {
    wr.resizeFullPhotos();
});

//ready
var cnt = 0;
$(document).ready(function() {
    //v2.1: randomize imgs
    //wr.randomizePhotos();
    //get elems
    var $photoFrame = $('#photo_frame');
    var $photoDivs = $('.photo_div');

    //make photodivs full size
    wr.resizeFullPhotos();
    //onload - handle where imgs are cached
    $('.photo_div img').one('load', function() {
        //get elems
        var $div = $(this).parent();
        console.log("Loaded: " + $div.index());
        //fit to full size
        wr.fitDivImg($div);
        //show first
        if ($div.index() == $photoDivs.length - 1) {
            $div.show();
            wr.showingPhotoIndex = $photoDivs.length - 1;
            wr.autoCollapseTimeout = setTimeout(wr.autoCollapseNext, 5000);
        }
    }).each(function(index, img) {
        if (img.complete) {
            cnt++;
            $(img).load();
        }
    });

    //ie fix: ie does not issue load event when an image is cached.  this forces the event.
    if ($.browser.msie) {
        $('.photo_div img').each(function(index, img) {
            var src = $(img).attr('src');
            $(img).attr('src', '');
            src = $(img).attr('src', src);
        });
    }

    //clicks
    //    $photoDivs.click(function(event) {
    //        if ($(this).hasClass('fullsize')) {
    //            var $fulldivs = $('.fullsize');
    //            if ($fulldivs.length > 1) {//v2.1: dont collapse last one
    //                wr.clearAutoCollapse();
    //                wr.collapsePhoto(this);
    //            }
    //        } else if ($(this).hasClass('iconsize')) {
    //            wr.resetAutoCollapse();
    //            wr.expandPhoto(this);
    //        }
    //    });
    
    //nav frame
    var $navMask = $('#nav_mask');
    var $navFrame = $('#nav_frame');
    var $navSlide = $('#nav_slide');
    $navMask.mouseenter(function(event) {
        $navFrame.animate({ width: 320 });
        $navSlide.animate({ left: -75 });
    });
    $navFrame.mouseleave(function(event) {
        $navFrame.animate({ width: 95 });
        $navSlide.animate({ left: 225 });
    });
    //nav list
    $("#navAccordion").accordion({
        header: '.accordian_header',
        //autoHeight: false,
        event: 'mouseover'
    });
    //about box
    var hideAbout = function(event) {
        var $about = $("#about");
        if ($about[0] == event.target) return;
        if (jQuery.contains($about[0], event.target)) return;
        $about.hide();
        $(window).unbind('mousedown', hideAbout);
    }
    $("#about_nav").click(function(event) {
        $("#about").show();
        $(window).bind('mousedown', hideAbout);
    });

});



//MM functions
function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}
function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function prepCivPage() {
    if (location.href.indexOf('photographs.aspx') != -1 || location.href.indexOf('guests.aspx') != -1) toggleSlideshow();
    MM_preloadImages('images/mission.gif', 'images/history.gif', 'images/selection.gif', 'images/fellows.gif', 'images/resources.gif', 'images/photos.gif', 'images/video.gif', 'images/home.gif', 'images/board_staff.gif', 'images/contact_us.gif');
}

