
//  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('#home_slideshow').slides({
        	preload: true,
        	preloadImage: '/img/design/loading.gif',
        	play: 5000,
        	pause: 2500,
        	hoverPause: false,
        	generatePagination: true,
        	generateNextPrev: true
        });

		$('form.default').submit( function() {
            $(this).find('button').html('Please Wait');
            return true;
        });
		
		$('#full_examples').hide();
		
		$("body.bespoke a.colorbox").colorbox({
			opacity: .7,
			html: function(){
			    var example_num = $(this).attr('rel');
			    return $('#example' + example_num);
			}
		});
		
        //  Other bindings and that...
		//$("ul.product_list li:nth-child(5)").addClass('last');
		
		$(".pagination li:last-child, #footer 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('img.multi_single');
			
			thumbs.bind('click', function() {
				
				var thumb = $(this);
		        var src = thumb.attr('href');
		
				master.hide();
				
				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);
				
				return false;
					
			});
		});
	};
	

}(jQuery, this, this.document));
//  --  FIX UP; LOOK SHARP!
