
var head, body;

(function ($, window, document, undefined) {

    if (typeof $ == 'undefined') {
        //  jQuery isn't available - abort!
        return;
    }

    //	--	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 () {

        //	--	Define head and body elements
        head = $.root.find('head');
        body = $.root.find('body');

        //	--	Add "js" class to body for styling
        body.addClass('js');

        //	--	Invoke plugins, yo..
        body.find('.google_map').googleMap();
        body.find('.scroller').prepHomeGallery();
        body.find('.gallery_cycle').prepGalleryCycle();
        body.find('.gallery_switcher').gallerySwitcher();
        body.find('.video_embed').videoEmbed();

        body.find('.gallery_thumbs a').colorbox({
            opacity: 0.25,
            rel: 'colorbox'
        });

        body.find('.scroller li:first-child').addClass('first');
        body.find('.gallery_thumbs li:nth-child(3n+1)').addClass('clear_both');
        body.find('.gallery_categories li:nth-child(3n+1)').addClass('new_row')

    });



    //	BIG clickable targets on list items
    $.prototype.bigTarget = function () {
        return this.bind('click', function () {
            window.location = $(this).find('a').attr('href');
        }).css('cursor', 'pointer');
    };



    //	Gallery index tab switcher
    $.prototype.gallerySwitcher = function () {
        return this.each(function () {
            var container = $(this),
                lists     = container.find('ul'),
                tabs_html,
                tabs,
                tab_links;

            if (lists.length > 1) {
                lists.first().siblings().addClass('hide');

                tabs_html  = '<ul class="clearfix tabs">';

                lists.each(function () {
                    var list  = $(this),
                        query = list.data('cat-query'),
                        name  = list.data('cat-name');

                    tabs_html += '<li><a href="#cat_' + query + '" data-target="' + query + '">' + name + '</a></li>';
                });

                tabs_html += '</ul>';

                tab_links = $(tabs_html).insertBefore(container).find('li a');

                tab_links.bind('click', function () {
                    var tab  = $(this),
                        target = tab.data('target');

                    tab_links.removeClass('active');
                    tab.addClass('active');

                    lists.addClass('hide').filter('#cat_' + target).removeClass('hide');

                    return false;
                }).first().addClass('active');

            }
        });

    };



    //	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>');
            }
        });

    };



    //	Redirect to <select> value on change
    $.prototype.jumpDropDown = function () {

        return this.bind('change', function () {
            window.location = $(this).val();
        });

    };



    //	Gallery Index Cyclists
    $.prototype.prepGalleryCycle = function () {

        return this.each(function () {
            var cyclist, container, prev, next;

            cyclist = $(this);

            if (cyclist.children().length > 1) {
                container = cyclist.parent();

                $('<p class="cycle_control prev"><a href="#">Previous</a></p>').insertAfter(cyclist);
                $('<p class="cycle_control next"><a href="#">Next</a></p>').insertAfter(cyclist);

                prev = container.find('p.prev a');
                next = container.find('p.next a');

                cyclist.cycle({
                    next: next,
                    prev: prev,
                    speed: 500,
                    timeout: 5000,
                    pause: true
                });
            }
        });

    };



    //	Home page sliding gallery
    $.prototype.prepHomeGallery = function () {

        return this.each(function () {
            var scroller, container, tabs_html, tabs, slideshow, slides, slides_count,
                slide_width, slide_margin, slideshow_width, prev_button, next_button;

            scroller  = $(this);
            container = scroller.closest('div.home_gallery');

            slideshow = scroller.find('ul');
            slides    = slideshow.find('li');

            slides_count = slides.length;

            if (slides_count == 1) {
                return;
            }

            slide_width     = slides.first().outerWidth();
            slide_margin    = parseInt(slides.last().css('marginLeft'), 10);
            slideshow_width = (slide_width + slide_margin) * slides_count;

            slideshow.css('width', slideshow_width);

            $('<p class="scroller_control prev"><a href="#">Previous</a></p>').insertAfter(scroller);
            $('<p class="scroller_control next"><a href="#">Next</a></p>').insertAfter(scroller);

            prev_button = container.find('p.prev a');
            next_button = container.find('p.next a');

            scroller.find('.inner').serialScroll({
                cycle:    false,
                duration: 500,
                easing:   'swing',
                exclude:  3,
                force:    false,
                items:    'li',
                jump:     false,
                lock:     false,
                next:     next_button,
                prev:     prev_button,
                step:     4,
                stop:     true
            });
        });

    };



    //	General Video embedder thinger
    $.prototype.videoEmbed = function () {

        return this.each(function () {
            var video, type, width, height, config;

            video  = $(this);
            type   = video.data('type');
            width  = video.data('width');
            height = video.data('height');
            config = {
                params: {
                    allowfullscreen: true,
                    allowscriptaccess: 'always',
                    wmode: 'transparent'
                },
                height: height,
                width: width,
                wmode: 'transparent'
            };

            if (type == 'youtube') {
                config.swf = video.data('url');
                config.flashvars = {
                    fs: 1,
                    hl: 'en',
                    rel: 0
                };
            }
            else {
                config.swf = 'http://vimeo.com/moogaloop.swf';
                config.flashvars = {
                    clip_id: video.data('id'),
                    server: 'vimeo.com',
                    show_title: 1,
                    show_byline: 1,
                    show_portrait: 0,
                    fullscreen: 1
                };
            }

            video.empty().flash(config);
        });
    };
    //	--	Good.. Can you say it faster?

})(jQuery, this, this.document);
//	--	Custom Plugins DONE, yeah?

