// Image hover replacement, used in header logo; see http://www.selfcontained.us/2008/03/08/simple-jquery-image-rollover-script/
jQuery(document).ready(function($) {
    $('img[data-hover]').hover(function() {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('data-hover')).attr('data-hover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function() {
        $('<img />').attr('src', $(this).attr('data-hover'));
    });;
});

// Checkerboard animation, used on home page
jQuery(document).ready(function($) {
    if ($('div.ngg-checkerboard').length) {
        window.anim = {
            checkerboardOrigin: { top: false, left: false },
            wrapCutoff: $('div.inner').width() + $('div.inner').offset().left,
            isRightmost: function($obj) {
                return $obj.offset().left + ($obj.width() * 2) > anim.wrapCutoff;
            },
            drawOverlay: function($obj) {
                var $img = $obj.find('img');
                var $cover = $img.clone(true).addClass('clone').css({ position: 'absolute', zIndex: 9999 }).prependTo($obj);
                var $overlay = $('<div class="ngg-gallery-checkerboard-title-wrapper" style="top: 0; width: ' + $obj.width() + 'px; z-index: 20"><div class="ngg-gallery-checkerboard-title" style="width: ' + $obj.width() + 'px; height: ' + $obj.height() + 'px">' +$img.attr('alt') + '</div></div>');
                $obj.data('cover', $cover);
                $obj.data('overlay', $overlay);
                $obj.parent().width($obj.width() * 2);
                if (anim.isRightmost($obj)) {
                    $obj.prepend($overlay);
                    $overlay.animate({
                        left: '-=' + $obj.width()
                    }, 200);
                } else {
                    $obj.append($overlay);
                    $overlay.animate({
                        left: '+=' + $obj.width()
                    }, 200);
                }
            },
            removeOverlay: function($obj) {
                $obj.data('cover').remove();
                $obj.data('overlay').remove();
                $obj.width($(this).width() * 0.5);
            }
        };
        
        $('.ngg-checkerboard:eq(0)').data('keepHeight', $('.ngg-checkerboard:eq(0)').height());
        
        $('.ngg-gallery-checkerboard-box').each(function() {
            if (!window.anim.checkerboardOrigin.top || !window.anim.checkerboardOrigin.left) {
                window.anim.checkerboardOrigin.top = $(this).offset().top;
                window.anim.checkerboardOrigin.left = $(this).offset().left;
            }
            $(this).data('offset', $(this).offset());
        }).each(function() {
            $(this).css({ position: 'absolute',
                top: $(this).data('offset').top - window.anim.checkerboardOrigin.top,
                left: $(this).data('offset').left - window.anim.checkerboardOrigin.left });
        });
        
        $('.ngg-checkerboard:eq(0)').height($('.ngg-checkerboard:eq(0)').data('keepHeight'));
        
        $('.ngg-gallery-checkerboard-box').hover(function() {
            anim.drawOverlay($(this));
        }, function() {
            anim.removeOverlay($(this));
        });
    }
});

// Background color fix on News page only
if (location.href.split('/')[3] == 'news') {
    jQuery(document).ready(function($) {
        $('#page').css({ backgroundColor: '#FEFEFE' });
        $('article').css({ backgroundColor: 'inherit' });
    });
}

// Google Maps for Contact page only
if (jQuery('link[rel=canonical]').length && jQuery('link[rel=canonical]').attr('href').split('/')[4] == 'contact') {
    window.initializeMap = function() {
        jQuery('div.content:eq(0)').prepend('<div id="mapCanvas" style="float: right; width: 600px; height: 250px"></div>');
        var middlemarch = new google.maps.LatLng(40.749159, -73.993729);
        var mapStyle = [ { 
            featureType: "road.arterial",
            elementType: "geometry",
            stylers: [
                { hue: "#ffee00" }, { gamma: 0.68 }, { saturation: -90 }
            ]
        },{
            featureType: "road.arterial",
            elementType: "labels",
            stylers: [
                { invert_lightness: true }, { gamma: 2.39 }, { saturation: -96 }
            ]
        },{
            featureType: "road.local",
            elementType: "geometry",
            stylers: [
                { hue: "#ffff00" }, { invert_lightness: true }, { lightness: 96 }
            ]
        },{
            featureType: "transit.station.rail",
            elementType: "all",
            stylers: [
                { saturation: -90 }
            ]
        },{
            featureType: "road.highway",
            elementType: "all",
            stylers: [
                { saturation: -90 }
            ]
        },{
            featureType: "poi.business",
            elementType: "all",
            stylers: [
                { visibility: "off" }
            ]
        },{
            featureType: "poi.government",
            elementType: "all",
            stylers: [
                { visibility: "off" }
            ]
        },{
            featureType: "poi.sports_complex",
            elementType: "all",
            stylers: [
                { visibility: "off" }
            ]
        },{
            featureType: "poi.school",
            elementType: "all",
            stylers: [
                { visibility: "off" }
            ]
        }
        ];
        var styledMapType = new google.maps.StyledMapType(mapStyle, { name: 'Map' });
        var mapOptions = {
            zoom: 17,
            center: middlemarch,
            mapTypeId: 'mapTypeID',
            mapTypeControl: false,
            streetViewControl: false
        }
        var map = new google.maps.Map(jQuery('#mapCanvas')[0], mapOptions);
        map.mapTypes.set('mapTypeID', styledMapType);
        map.setMapTypeId('mapTypeID');
        var marker = new google.maps.Marker({
            map: map,
            animation: google.maps.Animation.DROP,
            position: middlemarch,
            icon: new google.maps.MarkerImage('http://www.google.com/intl/en_ALL/mapfiles/marker_grey.png')
        });
    };
    jQuery.getScript('http://maps.google.com/maps/api/js?sensor=false&callback=initializeMap');
}
