// Preload Images
(function($) {
    $.fn.preloadImages = function() {
        for(var i=0; i < arguments.length; i++) {
            $("<img>").attr("src", arguments[i]);
        }
    };
})(jQuery);

// Rollover
(function($) {
    $.fn.rollOver = function(options) {
        $.fn.preloadImages(options.hoverSrc);
        var src = $(this).attr('src');
        $(this).hover(function() {
            $(this).attr('src', options.hoverSrc);
        }, function() {
            $(this).attr('src', src);
        });
    };
})(jQuery);

// Search Box
(function($) {
    var opts;
    $.fn.searchBox = function(options) {
        opts = $.extend({}, $.fn.searchBox.defaults, options);
        return this.each(function() {
            // If the box is hidden by default
            if (opts.hidden == true) { 
                $(opts.targetObj).hide();
                $(this).toggle(function() {
                    $(opts.targetObj).slideDown('fast');
                }, function() {
                    $(opts.targetObj).slideUp('fast');
                });
            } else { 
                $(opts.targetObj).show();
                $(this).toggle(function() {
                    $(opts.targetObj).slideUp('fast');
                }, function() {
                    $(opts.targetObj).slideDown('fast');
                });
            };
        });
    };
    $.fn.searchBox.defaults = {
        targetObj: '.searchOptions',
        hidden: true // Should it be hidden by default?
    };
})(jQuery);

// Menu
(function($) {
    var opts;
    $.fn.menu = function(options) {
        opts = $.extend({}, $.fn.menu.defaults, options);
        return this.each(function() {
            var cont = this;
            $(this).children('li').children('ul').each(function() { // Find nested menus
                var parentLi = $(this).parent();
                var parentLink = parentLi.find('a');
                parentLi.addClass(opts.parentClass);
                if (opts.menuType == 'hover') {
                    parentLink.hover(function() {
                        parentLi.children('ul').show();
                    }, function() {
                        parentLi.children('ul').hide();
                    });
                };
                /*
                // Needs more work
                if (opts.menuType == 'click') {
                    parentLink.click(function() {
                        parentLi.parent().children().removeClass(opts.hoverClass); // Hide all open drop-downs
                        parentLi.addClass(opts.hoverClass);
                    });
                };
                */
            });
        });
    };
    $.fn.menu.defaults = {
        menuType: 'hover', // Options are 'click' and 'hover'
        parentClass: 'parent',
        hoverClass: 'active'
    };
})(jQuery);

// Tabs
(function($) {
    var opts;
    $.fn.tabs = function(options) {
        opts = $.extend({}, $.fn.tabs.defaults, options);
        return this.each(function() {
            var cont = this;
            $(this).find('ul > li > a').each(function(a) {
                $(this).addClass(opts.tab + a); // Add number class to tab
                $(cont).children('div').children('div').each(function(b) { // Add cell class and cell number class to cells
                    $(this).addClass(opts.cell + b);
                });
                $(this).click(function() {
                    $(cont).find('ul > li > a').removeClass(opts.active);
                    $(this).addClass(opts.active);
                    $(cont).children('div').children('div').each(function(b) {
                        $(this).removeClass(opts.active);
                    });
                    $(cont).find('.' + opts.cell + a).addClass(opts.active);
                    return false;
                });
            });
        });
    };
    $.fn.tabs.defaults = {
        tab: 'tab',
        cell: 'cell',
        active: 'active'
    };
})(jQuery);

// Print Page
(function($) {
    var opts;
    $.fn.printPage = function() {
        return this.each(function() {
            $(this).click(function() {
                window.print();
                return false;
            });    
        });
    };
})(jQuery);

/*
    Creates a simple popup that can be controlled by the optionString.
*/
(function($) {
    var opts;
    $.fn.popup = function(options) {
        opts = $.extend({}, $.fn.popup.defaults, options);
        return this.each(function() {
            $(this).click(function() {
                var linkURL = $(this).attr('href');
                window.open(linkURL, '', opts.optionString);
                return false;
            });    
        });
    };
    $.fn.popup.defaults = {
        optionString: ''
    };
})(jQuery);

/*
    Inserts the google map into the chosen element.
    
    There are two options, one is the address string (required) and 
    the zoom level which defaults to 13 (a sane default).
*/
(function($) {
    var opts;
    $.fn.map = function(options) {
        opts = $.extend({}, $.fn.map.defaults, options);
        return this.each(function() {
            var map = null;
            var geocoder = null;
            if (GBrowserIsCompatible()) {
                // Create the map.
                map = new GMap2(document.getElementById(this.id));
                map.addControl(new GSmallMapControl());
                // Create an icon for the map.
                if (opts.icon) {
                    var icon = new GIcon();
                    icon.image = opts.icon.image;
                    icon.shadow = opts.icon.shadow;
                    icon.iconSize = new GSize(opts.icon.image_width, opts.icon.image_height);
                    icon.shadowSize = new GSize(opts.icon.shadow_width, opts.icon.shadow_height);
                    icon.iconAnchor = new GPoint(6, 20);
                    icon.infoWindowAnchor = new GPoint(5, 1);
                };
                // Create a geocode instance and pass it the address.
                geocoder = new GClientGeocoder();
                if (geocoder) {
                    geocoder.getLatLng(opts.address, function(point) {
                        if (!point) {
                            // Map should not show if there are no points
                            // or it should display a notice.
                        } else {
                            map.setCenter(point, opts.zoom);
                            var marker = new GMarker(point);
                            map.addOverlay(marker);
                            //marker.openInfoWindowHtml(opts.address);
                        };
                    });
                };
            };
        });
    };
    $.fn.map.defaults = {
        address: '',
        zoom: 13,
        icon: {
            image: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
            image_width: 12,
            image_height: 20,
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png',
            shadow_width:  22,
            shadow_height: 20
        }
    };
})(jQuery);
