/*global jQuery */

//  This is where the magic happens, like on Cribs, except nowhere near as vile.
(function ($, window, document) {
    //  Is any of this actually going to work?
    if (typeof jQuery === 'undefined') {
        return; // Hot Christ! jQuery isn't available!
    }

    //  --  Instantiate just ONE jQuery object, use this to find other elements
    //      rather than creating a new jQuery object for each selector
    $.root = new $.prototype.init(document);

    //	--	Make Rocket Go Now
    $.root.ready(function () {

        //  Cache the godforsaken <html>, <head> and <body> elements
        $.HTML = $.root.find('html');
        $.Head = $.HTML.find('head');
        $.Body = $.HTML.find('body');

        //  --	Invoke plugins, yo -> body.find('#element').functionName({ ... });

		//	Home page slideshow
		if (document.getElementById('home_slideshow')) {
    		$.Body.find('#home_slideshow .slides').cycle({
    		    activePagerClass: 'active',
    		    timeout: 7000,
    		    pager:  '.pager',
                pagerAnchorBuilder: function(idx, slide) {
                    return '.pager li:eq(' + idx + ') a';
                }
    		});
    	}

		$.Body.find('#google_map').googleMap();

		$.Body.find('#gallery a').colorbox({
			opacity: 0.25,
			rel: 'gallery_images'
		});

    });



    //	--	Hey buddy I got your custom jQuery plugins right here...

	//	Google Maps
	$.prototype.googleMap = function () {

		this.each(function () {
			var map_el     = $(this),
				latlng     = new google.maps.LatLng(map_el.data('latitude'), map_el.data('longitude')),
				options    = {
					center:    latlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					zoom:      15
				},
				map        = new google.maps.Map(this, options),
				infowindow = new google.maps.InfoWindow({
				    content: map_el.data('address')
				}),
				marker     = new google.maps.Marker({
					animation: google.maps.Animation.DROP,
					map:       map,
					position:  latlng,
					title:     map_el.data('title')
				});

			google.maps.event.addListener(marker, 'click', function () {
				infowindow.open(map, marker);
			});
		});

		return this;

	};

}(jQuery, this, this.document));
//  --  FIX UP; LOOK SHARP!
