/*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 one for each selector
    $.document = new $.prototype.init(document);

    //	--	Make Rocket Go Now
    $.document.ready(function () {

        //  Cache <html> <head> and <body> elements
        $.HTML = $.document.find('html');

        $.Head = $.HTML.find('head');
        $.Body = $.HTML.find('body');

        //  Invoke plugins, yo -> $.Body.find('#element').functionName({ ... });
		$.Body.find('.google_map').googleMap();
		$.Body.find('.home_slideshow').homeSlideshow();

		$.Body.find('ul.multi_thumbs a').colorbox({
			opacity: 0.4,
			title: true
		});

		if ($.HTML.hasClass('ie')) {
			$.Body.find('.sectors_summary li:last-child').addClass('last');
		}

	});



	//	Google Maps
	$.prototype.googleMap = function () {

		return this.each(function () {
			var map_wrap, latitude, longitude, map, point, marker;

			map_wrap = $(this);

			if (GBrowserIsCompatible()) {
				latitude  = map_wrap.data('latitude');
				longitude = map_wrap.data('longitude');

				map = new GMap(map_wrap[0]);

				map.addControl(new GLargeMapControl());
				map.centerAndZoom(new GPoint(longitude, latitude), 2);

				point = new GPoint(longitude, latitude);

				marker = new GMarker(point);

				map.addOverlay(marker);
			}
		});

	};



	//	Home Slideshow - isn't that obvious?
	$.prototype.homeSlideshow = function () {

		return this.each(function () {
			var container          = $(this),
			    slideshow          = container.find('ul'),
			    slides             = slideshow.find('li'),
			    counter            = 0,
			    nav_html           = '',
			    controls           = false,
			    at_last            = false,
                autoStepController = false,
                autoStepExecutor   = false;

			nav_html += '<ul class="controls">';

			for (counter; counter < slides.length; counter++) {
				nav_html += '<li>' + counter + '</li>';
				$(slides[counter]).css('z-index', 30 - counter);
			}

			nav_html += '</ul>';

			$(nav_html).appendTo(container);

			controls = container.find('.controls li');

			controls.bind('click', function (event, autoStepper) {
				var link          = $(this),
					index         = link.index(),
					current_slide = slideshow.data('current-slide');

				if (index != current_slide) {
					link.addClass('active').siblings().removeClass('active');

					$(slides[index]).fadeIn();
					$(slides[current_slide]).fadeOut();

                    if (!autoStepper) {
                        autoStepReset();
                    }

					slideshow.data('current-slide', index);
				}
			}).first().addClass('active');

            function autoStep() {
                var current_index = parseInt(slideshow.data('current-slide'), 10),
                    clicker_index = current_index + 1 < controls.length ? current_index + 1 : 0;

                controls.eq(clicker_index).trigger('click', [true]);
            }

            function autoStepReset() {
                if (autoStepController) {
                    window.clearTimeout(autoStepController);
                }

                autoStepController = window.setTimeout(function () {
                    autoStepExecutor = window.setInterval(autoStep, 10000);
                }, 10000);

                window.clearInterval(autoStepExecutor);
            }
            
            (function autoStepInit() {
                autoStepExecutor = window.setInterval(autoStep, 10000);
            }());
		});

	};

}(jQuery, this, this.document));
//	--	Good.. Can you say it faster?
