// 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).show('blind', 'fast');
                }, function() {
                    $(opts.targetObj).hide('blind', 'fast');
                });
            } else {
                $(opts.targetObj).show();
                $(this).toggle(function() {
                    $(opts.targetObj).hide('blind', 'fast');
                }, function() {
                    $(opts.targetObj).show('blind', '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') {
                    parentLi.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.oasis_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_address = function(options) {
        opts = $.extend({}, $.fn.map_address.defaults, options);
        return this.each(function() {
            var map = null;
            var geocoder = null;
            
            // Create the map.
            var options = {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                zoom: opts.zoom
            };
            map = new google.maps.Map(document.getElementById(this.id), options);
            
            // Create a geocoder instance and pass it the address.
            geocoder = new google.maps.Geocoder();
            geocoder.geocode({'address': opts.address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        map: map, 
                        position: results[0].geometry.location
                    });
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        });
    };
    $.fn.map_address.defaults = {
        address: '',
        zoom: 13
    };
})(jQuery);

// attach event handlers without overwriting existing ones
// see http://articles.sitepoint.com/article/structural-markup-javascript
// it's also in the admin's core.js
function addEvent(obj, evType, fn){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

