/*globals $: false, jQuery: false, google: false*/
$(document).ready(function() {
    (function () {
        var map, shadow, latlng, marker, container;

        container = document.getElementById("map_canvas");

        if (container) {
            latlng = new google.maps.LatLng(-36.864881,174.755816);
            map = new google.maps.Map(container, {
                zoom: 15,
                scrollwheel: false,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            marker = new google.maps.Marker({
                position: latlng,
                icon: "/userfiles/gmaps-icon.png",
                map: map
            });
        }
    }());

    (function () {
        // Random Blockquote homepage
        var myRandom = Math.floor(Math.random() * $(".quote").length);
        $(".quote:eq(" + myRandom + ")").show();
    }());

    if ($.browser.msie && $.browser.version > 6 && $.browser.version < 8) {
        // ... this is only happening in IE7, so I'll force a redraw
        // every second... you think I feel good about this?
        var element = $("#Slideshow");
        window.setInterval(function () {
            element.forceRedraw();
        }, 1000);
    }

    (function () {
        // new show and hide for case studies
        // choose text for the show/hide link
        var showText="More about this project",
            hideText="Less about this project";

        // create the toggle link
        $("#hide_this").after("<a href='#' id='toggle_link'>"+showText+"</a>");

        // hide the content
        $('#hide_this').hide();

        // capture clicks on the newly created link
        $('a#toggle_link').click(function() {

            // change the link text
            if ($('a#toggle_link').text()==showText) {
                $('a#toggle_link').text(hideText);
            }
            else {
                $('a#toggle_link').text(showText);
            }

            // toggle the display
            $('#hide_this').fadeToggle("fast");

            // return false so any link destination is not followed
            return false;
        });
    }());
    
 // fancybox lightbox
$("a#fancyPic").fancybox({
	'overlayShow'	: true,
	'transitionIn'	: 'elastic',
	'transitionOut'	: 'elastic'
});

});

jQuery.fn.forceRedraw = function () {
    var element;
    if ($.browser.msie && $.browser.version < 8 && this.length > 0) {
        element = this[0];
        element.className = element.className;
    }

    return this;
};

jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

function initSlideshow() {
    $(document).ready(function() {
        if ($("#Slideshow").length === 0) { return; }

        $("#Slideshow")
            .slideshow({
                idle: 30000, // play after 30 seconds idle
                delay: 4000,
                wrap: false
            })
            .bind({
                mouseenter: function () {
                    $(".next, .previous", this).fadeIn();
                },

                mouseleave: function () {
                    $(".next, .previous", this).fadeOut();
                },

                'show:init': function (event, show) {
                    $(".next, .previous", this).hide();
                },

                'show:start': function (event, show){
                    // Highlight the corresponding navigation links when a slide starts showing
                    var link = $('a', show.currentElement).attr('href'),
                        activeLinks = $(this).data('current');

                    // this sets the style as well as the class because IE is awful AS
                    // and it wasn't updating the styles when the class updated and
                    // I got sick of wrestling with it..
                    if (typeof(activeLinks) !== 'undefined') {
                        activeLinks.removeClass('highlight');
                        activeLinks.removeAttr('style');
                    }

                    activeLinks = $('nav a[href='+link+']').addClass('highlight');
                    activeLinks.css({color: 'white'});
                    $(this).data('current', activeLinks);
                }
            });
    });
}

