// break out of iframes
if (window != top) {
    top.location.href = location.href;
}

// preload kids rollover image
var cacheImage = document.createElement('img');
cacheImage.src = '/img/nav-forkids.gif';

// set theme for recaptchas
var RecaptchaOptions = {
  theme : 'clean' // red, white, blackglass, clean
};

// popup window utility function
function popup(url,target,width,height) {
    var popwidth, popheight;
    if (target == null) { target = '_blank'; }

    popwidth = width;
    popheight = height;
    
    if (width == null) { popwidth = 800; }
    if (height == null) { popheight = 600; }
    
    if (url.indexOf("/events/") != -1) {
        // override for events popups
        if (width == null) { popwidth = 930; }
        if (height == null) { popheight = 650; }        
    }
    
    window.open(url, '', "width="+popwidth+",height="+popheight+",channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0");
    return false;
}

// tooltip function
function patara_tooltip(title, text) {
    Tip(text, TITLE, title);
}

// lightbox title function
function lightbox_caption(title, currentArray, currentIndex, currentOpts) {
    var caption = '<div class="lightbox_caption">';
    var $a = $(currentArray[currentIndex]);
    $a.children().filter(':not(img)').each(function() {
        caption += $(this).html();
    });
    if ($a.next('a').length > 0) {
        caption += $a.next('a').html()
    }
    caption += '</div>';
    var now = new Date();
    caption += '<div class="lightbox_footer"><a href="/pages/content-usage-copyright/">Copyright &copy; 2002-' + now.getFullYear() + ' St. Nicholas Center</a></div>';
    return caption;
}

// first of our two gallery cat/item resizer functions
function imgdiv_sizer(img) {
    var imgdiv = img.parent();
    var catdiv = imgdiv.parent().parent();
    if (img.height() && (imgdiv.height() < catdiv.height())) {
        imgdiv.height(catdiv.height());
    }
}

// gallery resizonator #2
function gallery_float_sizer(container, float_selector) {
    var floats = $(float_selector);

    // reset float widths
    var min_width = parseInt(floats.css('min-width'));
    floats.width(min_width);
    var base_width = floats.outerWidth(true);
    var numcols = parseInt($(container).width() / base_width);
    var new_width = min_width + parseInt(($(container).width() % base_width) / numcols);
    floats.width(new_width);

    // reset float heights
    floats.each(function(){
        $(this).height('auto');
        $(this).find('.gallery_category_imgdiv img').each(function() {
            imgdiv_sizer($(this));
        });
    });

    // tweak item image heights/margins
    var l_idx = 0;
    var idx = numcols;
    while (idx <= floats.length) {
        var row_imgdivs = floats.slice(l_idx, idx).find('.gallery_item_image_wrapper');
        var max_imgdiv_height = Math.max.apply(null, $.map(row_imgdivs,function(e){ return $(e).height() }));
        if (max_imgdiv_height) {
            row_imgdivs.height(max_imgdiv_height);
            row_imgdivs.find('img').each(function() {
                $(this).css('margin-top',(max_imgdiv_height-$(this).height())/2);
            });
        }
        l_idx = idx;
        idx += numcols;
    }

    // reset ends of rows
    $('.gallery_endrow', container).remove();
    idx = numcols;
    while (idx < floats.length) {
        $(floats[idx]).before('<div class="div_clear gallery_endrow"></div>');
        idx += numcols;
    }
}

$(document).ready(function() {
    // set up fancybox popup image viewer
    var $lightbox_links = $('a[href$=".jpg"],a[href$=".gif"],a[href$=".png"]').filter(':not([href^="http"])').filter(':not(.nolightbox)');
    $lightbox_links.each(function() {
        $this = $(this);
        if (!$this.attr('rel')) {
            $this.attr('rel','default_lightbox_gallery');
        }
    });
    $lightbox_links.fancybox({
        'opacity' : true,
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'cyclic' : true,
        'centerOnScroll' : false,
        'overlayColor' : '#000',
        'overlayOpacity' : 0.8,
        'titlePosition' : 'inside',
        'titleFormat' : lightbox_caption
    });
});

$(window).load(function(){
    // gallery resizer stuff
    if (!$.browser.msie || (parseInt($.browser.version)>6)) {
        var gallery_cats = $('#gallery_categories');
        var gallery_items = $('#gallery_items');
        if (gallery_cats.length) {
            gallery_cats.find('.gallery_category_imgdiv img').load(function() {
                imgdiv_sizer($(this));
            });
        }
        if (gallery_cats.length || gallery_items.length) {
            $(window).resize(function(){
                if (gallery_cats.length) {
                    gallery_float_sizer(gallery_cats,'.gallery_category');
                }
                if (gallery_items.length) {
                    gallery_float_sizer(gallery_items,'.gallery_item');
                }
            });
            $(window).resize();
        }
    }
    // ecard text center hack
    var $ecard = $('#ecard');
    var $ecard_text = $('#ecard_text');
    if ($ecard.length && $ecard_text.length) {
        if ($ecard_text.height() * 0.9 < $ecard.height()) {
            $ecard_text.css('margin-top', ($ecard.height()-$ecard_text.height())/2.5);
        }
    }
});
