﻿///  <reference path="../Shared/scripts/jquery-1.3.1-vsdoc.js" />
function log(l) { if (typeof console != "undefined" && typeof console.debug != "undefined") { console.log(l); } else { alert(l); } }
var slimboxOptions = {
    loop: true,
    overlayOpacity: 0.6,
    overlayFadeDuration: 0,
    captionAnimationDuration: 0,
    resizeDuration: 0,
    imageFadeDuration: 0,
    onClose: true
}
var mainPhotos = [];
function slimboxANETClose() {
    mainPhotos.getConf().keyboard = true;
    mainPhotos.reload();
    playShow(true);
}
var slimboxMax;
function setslimboxMax() {
    var winHeight = $(window).height() - 60 - 16;
    var winWidth = $(window).width() - 260;
    var maxDim;
    if (winHeight < winWidth) maxDim = winHeight;
    else maxDim = winWidth;
    if (maxDim < 200) slimboxMax = 160;
    else if (maxDim < 288) slimboxMax = 200;
    else if (maxDim < 320) slimboxMax = 288;
    else if (maxDim < 400) slimboxMax = 320;
    else if (maxDim < 512) slimboxMax = 400;
    else if (maxDim < 576) slimboxMax = 512;
    else if (maxDim < 640) slimboxMax = 576;
    else if (maxDim < 720) slimboxMax = 640;
    else if (maxDim < 800) slimboxMax = 720;
    else if (maxDim >= 800) slimboxMax = 800;
    else slimboxMax = 400;
}
function captionBuilder(photoID, selector, selector2) {
    var caption = [];
    var currentPhoto = photos.photoData[photoID];
    caption[0] = '';
    if (currentPhoto[2]) {
        caption[0] = '<span style="float:right;position:absolute;right:2px;bottom:0px;">' + photos.meets[currentPhoto[2]][1] + '</span>';
    }
    if (currentPhoto[5]) caption[0] += photos.photoData[photoID][5] + '<br /><br />';
    if (currentPhoto[3] && photos.results[currentPhoto[3]]) {
        caption[0] += photos.results[currentPhoto[3]][0] + ' - ';
        if (photos.results[currentPhoto[3]][2]) {
            caption[0] += photos.results[currentPhoto[3]][2] + '. ';
        }
        caption[0] += photos.athletes[html_Athlete] + ' ' + photos.results[currentPhoto[3]][1];
        if (photos.results[currentPhoto[3]][4] === 1) {
            caption[0] += " - Personal Record";
        }
        else if (photos.results[currentPhoto[3]][3] === 1) {
            caption[0] += " - Season Record";
        }
        caption[0] += "<br />";
    }
    caption[0] += '<b><a href="../Photo/Download.ashx?photoID=' + photoID + '">Download Original</a></b>';
    try {
        caption[1] = '';
        $.each(currentPhoto[1], function(i, n) {
            caption[1] += photos.athletes[n] + ' <a href="Athlete.aspx?AID=' + n + '" title="Athlete Profile" target="_blank"><img src="../images/Icons/Inline/ExternalLink.gif" /></a><br />';
        });
    }
    catch (err) { return true; }
    if (currentPhoto[2]) {
        caption[2] = '<a href="Results/Meet.aspx?Meet=' + currentPhoto[2];
        if (currentPhoto[3] && photos.results[currentPhoto[3]]) {
            caption[2] += '#' + photos.results[currentPhoto[3]][5];
        }
        caption[2] += '">' + photos.meets[currentPhoto[2]][0] + '</a>';
    }
    return caption;
}
function allPhotoBuilder() {
    var photoArray = [];
    var x = 0;
    $('#scrItems  img').each(function() {
        photoArray[x] = [$(this).attr('src').split('?')[0], captionBuilder($(this).attr('id').split('_')[1])];
        x += 1;
    });
    return photoArray;
}
var lastCaptionID;
var currentTabID;
function changeCaption() {
    var currentImageID = $('img.active').attr('id').split('_')[1];
    if ($('#scrItems img').index($('img.active')) != lastCaptionID) {
        var currentPhoto = photos.photoData[currentImageID];
        var caption = '';
        if (currentPhoto[2]) {
            caption += '<a href="Results/Meet.aspx?Meet=' + currentPhoto[2];
            if (currentPhoto[3] && photos.results[currentPhoto[3]]) {
                caption += '#' + photos.results[currentPhoto[3]][5];
            }
            caption += '">' + photos.meets[currentPhoto[2]][0] + '</a>' + ' - ' + photos.meets[currentPhoto[2]][1] + '<br />';
        }
        if (currentPhoto[3] && photos.results[currentPhoto[3]]) {
            caption += photos.results[currentPhoto[3]][0] + ' - ';
            if (photos.results[currentPhoto[3]][2]) {
                caption += photos.results[currentPhoto[3]][2] + '. ';
            }
            caption += photos.athletes[html_Athlete] + ' ' + photos.results[currentPhoto[3]][1];
            if (photos.results[currentPhoto[3]][4] === 1) {
                caption += " - Personal Record";
            }
            else if (photos.results[currentPhoto[3]][3] === 1) {
                caption += " - Season Record";
            }
        }
        $('#imgCaption').animate({ opacity: 'hide' }, 'fast', 'swing', function() { $('#imgCaption').html(caption).animate({ opacity: 'show' }, 'fast'); });
        $('#playPause').html(($('#scrItems img').index($('img.active')) + 1) + ' / ' + $('#scrItems img').length);
        
    }
    lastCaptionIndex = currentImageID;
}
function playShow(play) {
    if (play) {
        mainPhotos.play();
        $('#playPause').removeClass('paused');
    }
    else {
        mainPhotos.stop();
        $('#playPause').addClass('paused');
    }
}
function launchPlayer() {
    $.slimbox(allPhotoBuilder(), $('#scrItems img').index($('img.active')), slimboxOptions);
    playShow(false);
    mainPhotos.getConf().keyboard = false;
    mainPhotos.reload();
}
function filterPhotos(teamType) {
    if ($('#photoStore img.aP' + teamType).length > 0) {
        $('#cnt_profilePic').find('img').not('.aP' + teamType).animate({ opacity: 'hide' }, 'fast', 'swing', function() {
            $(this).appendTo('#photoStore');
            $('#photoStore').find('img.aP' + teamType).appendTo('#cnt_profilePic').animate({ opacity: 'show' }, 'fast');
        });
    }
    $('#scrollContainer').animate({ opacity: 'hide' }, 'fast', 'swing', function() {
        if (teamType != 'tT0') {
            $('#scrItems').find('img').not('.' + teamType).appendTo('#photoStore');
            $('#photoStore').find('img.' + teamType).appendTo('#scrItems');
        }
        else {
            $('#photoStore').find('img.tT3').prependTo('#scrItems');
            $('#photoStore').find('img.tT1').appendTo('#scrItems');
            $('#photoStore').find('img.tT2').appendTo('#scrItems');
            $('#photoStore').find('img.tT4').appendTo('#scrItems');

        }
        $('#scrItems img').eq(3).addClass('active');
        if (mainPhotos) { mainPhotos.reload().begin(); }
        if ($('#scrollContainer img').length > 0) {
            if ($('#scrollContainer').hasClass('hadPhotos')) {
                $('#scrollContainer').animate({ opacity: 'show' }, 'fast', 'swing');
            }
            else {
                $('#scrollContainer').animate({ height: 'show', opacity: 'show' }, 'fast', 'swing');
            }
            if (mainPhotos) { playShow(true); }
            $(this).addClass('hadPhotos');
        }
        else {
            $(this).animate({ height: 'hide', opacity: 'hide' }, 'fast').removeClass('hadPhotos');
        }
    });
}

$(function() {
    mainPhotos = $("div.scrollable").scrollable({
        size: 7,
        onBeforeSeek: function(event, index) {
            $('#scrItems img').removeClass('active');
            $('#scrItems img').eq(index + 3).addClass('active');
            changeCaption();
        },
        hoverClass: 'hover'
    }).navigator().mousewheel().autoscroll({
        autoplay: true,
        api: true
    });
    if ($('span.tab.selected').length > 0) {
        filterPhotos($('span.tab.selected').attr('id').split('_')[1]);
    }
    if (mainPhotos) {
        $('#scrItems img').click(function() {
            setTimeout('changeCaption();', 10);
            playShow(false);
        });
        $('a.browse, div.navi a').click(function() {
            playShow(false);
        });
        $('#click_pPlayer').click(function(e) {
            e.preventDefault();
            launchPlayer();
        });
        $('#scrItems img').live('dblclick', function() {
            launchPlayer();
        });
        $('#playPause').click(function() {
            if ($('#playPause').hasClass('paused')) playShow(true);
            else playShow(false);
        });
        setslimboxMax();
        $(window).resize(function() { setslimboxMax(); });
    }
    $('span.tab').click(function() {
        if ($(this).attr('id').split('_')[1] != currentTabID) {
            filterPhotos($(this).attr('id').split('_')[1]);
        }
        currentTabID = $(this).attr('id').split('_')[1];
    });
});