
//  This is where the magic happens, like on Cribs, except nowhere near as vile.
(function ($, window, document) {
    var H, // <head>
        B; // <body>

    //  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 jQuery.prototype.init(document);

    //	--  Make Rocket Go Now
    $.root.ready(function () {
        //  Cache <head> and <body> elements
        H = $.root.find('head');
        B = $.root.find('body');

        //  Invoke plugins, yo -> B.find('#element').functionName({ ... });
        B.find('.google_map').googleMap();
        B.find('.video_embed').videoEmbed();
		
		B.find('.multi_images').multiPics();
		
		
		B.find('.slideshow').slides({
        	preload: true,
        	preloadImage: '/img/design/loading.gif',
        	play: 5000,
        	pause: 2500,
        	hoverPause: false,
        	generatePagination: true,
        	generateNextPrev: false
        });
		
        //  Other bindings and that...

		$('form.default').submit( function() {
			$(this).find('button').html('Please Wait');
			return true;
		});

        //  Stick in any elements that need last-child (for IE)
		//$(".pagination li:last-child, ul.multi_thumbs li:last-child").addClass('last-child');

    });



    //	--	Hey buddy I got your custom jQuery plugins right here...

    //	Google Maps
    $.prototype.googleMap = function () {

        return this.each(function () {
            var map_wrap, latitude, longitude, title, address, map, point,
                map_html, marker;

            map_wrap = $(this);

            if (GBrowserIsCompatible()) {
                latitude  = map_wrap.data('latitude');
                longitude = map_wrap.data('longitude');
                title     = map_wrap.data('title');
                address   = map_wrap.data('address');

                map = new GMap(map_wrap[0]);

                map.addControl(new GLargeMapControl());
                map.centerAndZoom(new GPoint(longitude, latitude), 2);

                point = new GPoint(longitude, latitude);

                map_html = '<div class="map_html">';
                map_html += '<h2>' + title + '</h2>';
                map_html += '<p>' + address + '</p>';
                map_html += '</div>';

                marker = new GMarker(point);

                GEvent.addListener(marker, 'click', function () {
                    marker.openInfoWindowHtml(map_html);
                });

                map.addOverlay(marker);
                marker.openInfoWindowHtml(map_html);
            } else {
                map_wrap.html('<p>Your browser is not compatible with Google Maps</p>');
            }
        });

    };



    //  General video embedder thinger
    $.prototype.videoEmbed = function () {
        return this.each(function () {
            var video  = $(this),
                config = {};

            video.empty();

            config.params = {
                allowfullscreen:    true,
                allowscriptaccess: 'always',
                wmode:             'transparent'
            };

            config.swf       = video.data('swf');
            config.height    = video.data('height');
            config.width     = video.data('width');
            config.wmode     = 'transparent';
            config.flashvars = {};

            $.each(video.data('vars'), function (key, value) {
                config.flashvars[key] = value;
            });

            video.flash(config);
        });
    };

	// Multipics gallery
	$.prototype.multiPics = function () {
		
		return this.each(function () {
			
			var container	=	$(this);
			var items		=	container.find('ul.multi_thumbs li');
			var thumbs		=	items.find('a');
			var master		=	container.find('div.master_wrap img');
			var cache		= 	[];
			
			// Thumb Clicks
			thumbs.bind('click', function() {
				
				var thumb	=	$(this);
		        var src		=	thumb.attr('href');
		
				master.hide();
				
				// new <img> probably faster than doing cache[] lookup ??
				var img = document.createElement('img');
				
		        $(img).load(function() {

		            master.attr('src', src).fadeIn(250);
            
		            items.removeClass('active');
		
		            thumb.parent().addClass('active'); // apply class to <li>

		        }).attr('src', src); // odd, but fixes browser bug, can't remmeber which one
				
				return false;
					
			});
			
			
			// preload larger images
			thumbs.each( function() {
				
				var tmb = $(this);
				
				//tmb.css('opacity','0.2');
				
				var fullsize 		=	$(this).attr('href');
				var cache_fullsize	=	document.createElement('img');
			    //cache_fullsize.src	= 	fullsize;
			
				$(cache_fullsize).load(function() {
            		
					tmb.animate({opacity: 1}, 250);
					
					//tmb.css('opacity','1');

		        }).attr('src', fullsize);
			
			    //cache.push(cache_fullsize);
			});
			
			
		});
	};

}(jQuery, this, this.document));
//  --  FIX UP; LOOK SHARP!

