var currentScroll = 0;

$(document).ready(function() {
    // look for all links pointing to visitor maps, set to use shadowbox and set specific options
    $('a[rel][href]').filter(function(){
        return $(this).attr("rel").toLowerCase().indexOf('shadowbox') != -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/visitors/map') != -1;
    }).each(function(){
        $(this).addClass('ShadowMap');
        $(this).attr('rel','shadowbox;width=742;height=746;');
    });
    $('a[rel][href]').filter(function(){
        return $(this).attr("rel").toLowerCase().indexOf('shadowbox') != -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/images/flashmaps') != -1;
    }).each(function(){
        $(this).addClass('ShadowMap');
        $(this).attr('rel','shadowbox;width=742;height=746;');
    });

    // look for all links pointing to virtual tour, set to use shadowbox and set specific options
    $('a[rel][href]').filter(function(){
        return $(this).attr("rel").toLowerCase().indexOf('shadowbox') != -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/visitors/virtual') != -1;
    }).each(function(){
        $(this).addClass('ShadowVTour');
        $(this).attr('rel','shadowbox;width=742;height=746;');
    });

    // look for all links pointing to email forms, set to use shadowbox and set specific options
    $('a[rel][href]').filter(function(){
        return $(this).attr("rel").toLowerCase().indexOf('shadowbox') != -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/mail/') != -1;
    }).each(function(){
        $(this).addClass('ShadowMail');
        $(this).attr('rel','shadowbox;width=400;height=800;');
    });

    // look for all links pointing to workflow forms, set to use shadowbox and set specific options
    $('a[rel][href]').filter(function(){
        return $(this).attr("rel").toLowerCase().indexOf('shadowbox') != -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/builder/') != -1;
    }).each(function(){
        $(this).addClass('ShadowWork');
    });

    // add identifier class to other (non-map) shadowbox links to assist setup
    $('a[rel][href]').filter(function(){
        return $(this).attr("rel").toLowerCase().indexOf('shadowbox') != -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/visitors/map') == -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/images/flashmaps') == -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/visitors/virtual') == -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/builder/') == -1;
        }).filter(function(){return $(this).attr("href").toLowerCase().indexOf('/mail') == -1;
    }).each(function(){
        $(this).addClass('Shadow');
    });

    // set up links with "ShadowMap" class to work with custom Shadowbox options
    Shadowbox.setup("a.ShadowMap", {
        initialWidth: 742,
        initialHeight: 746,
        handleOversize: "none",
        viewportPadding:40,
        onOpen:addMapStyles,
        onFinish:changeMapHeight,
        onClose:resetMapStyles,
        modal: false
    });// set up links with "ShadowVTour" class to work with custom Shadowbox options
    Shadowbox.setup("a.ShadowVTour", {
        initialWidth: 740,
        initialHeight: 610,
        handleOversize: "none",
        onOpen:addVTourStyles,
        onFinish:changeVTourHeight,
        onClose:resetVTourStyles,
        modal: false
    });
    // set up links with "ShadowMail" class to work with custom Shadowbox options
    Shadowbox.setup("a.ShadowMail", {
        onOpen:addMailStyles,
        onClose:resetMailStyles,
        modal: true
    });
    // set up links with "ShadowWork" class to work with custom Shadowbox options
    Shadowbox.setup("a.ShadowWork", {
        initialWidth: 658,
        initialHeight: 600,
        onOpen:addMailStyles,
        onClose:resetMailStyles
    });
    // set up other shadowbox links with custom close box
    Shadowbox.setup("a.Shadow", {
        onOpen:addCloseBar,
        onClose:resetCloseBar
    });
});

function addVTourStyles() {
    currentScroll = $(window).scrollTop();
    $(window).scrollTop(0);
    $('#sb-container').addClass("ShadowVTourContainer");
}
function changeVTourHeight() {
    // fixes minimum height in google chrome
    $('#sb-wrapper-inner').css('height',642);
    // fixes margins
    reCenter();
}
function resetVTourStyles() {
    $(window).scrollTop(currentScroll);
    currentScroll = 0;
    $('#sb-container').removeClass("ShadowVTourContainer");
    // undo fix for chrome
    $('#sb-wrapper-inner').removeAttr('style');
}

function addMapStyles() {
    currentScroll = $(window).scrollTop();
    $(window).scrollTop(0);
    $('#sb-container').addClass("ShadowMapContainer");
}
function changeMapHeight() {
    // fixes height in google chrome
    $('#sb-wrapper-inner').css('height',746);
    // fixes margins
    reCenter();
}
function resetMapStyles() {
    $(window).scrollTop(currentScroll);
    currentScroll = 0;
    $('#sb-container').removeClass("ShadowMapContainer");
    // undo fix for chrome
    $('#sb-wrapper-inner').removeAttr('style');
}

function addMailStyles() {
    $('#sb-container').addClass("ShadowMailContainer");
}
function resetMailStyles() {
    $('#sb-container').removeClass("ShadowMailContainer");
}

function addCloseBar() {
    $('#sb-title').prepend('<div id="sb-ahClose" align="right"><a href="#" onclick="parent.window.Shadowbox.close();"><img id="closeBtn" src="/public/images/forms/btn_close.gif" style="padding:4px 4px 3px 3px;" border="0"/></a></div>');
}
function resetCloseBar() {
    $('#sb-ahClose').remove();
}

function reCenter() {
    var lft  = $('#sb-wrapper').width();
    lft  = Math.floor(($(window).width() - lft) / 2);
    lft  = lft + 'px';
    $('#sb-wrapper').css('left',lft);
}

// process embedded mailto-type links as if previously setup as shadowbox links
// <a href="javascript:sendByEmail('aacpmas@aacpm.org')">email us</a>
function sendByEmail(addr) {
    var url = window.location;
    var title = $('meta[name=content-name]').attr('content');
    var eForm = '/public/mail/disclaimer.htm';
    var q = '?title='+encodeURIComponent(title)+'&url='+encodeURIComponent(url)+'&to='+encodeURIComponent(addr);
    Shadowbox.open({
        content:    eForm+q,
        player:     "iframe",
        width:      400,
        height:     800,
        options: {
            onOpen:     addMailStyles,
            onClose:    resetMailStyles,
            modal:      true
        }
    });
}
