
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 jQuery.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('#element').functionName();
		body.find('.google_map').googleMap();
		body.find('.home_slideshow').homeSlideshow();
		body.find('.launch_video').launchVideo();
		body.find('.product_photos').productPhotos();
		body.find('.product_tabs').productTabs();
		body.find('.video_embed').videoEmbed();
//		body.find('.where_form').whereToBuy();

		body.find('.product_shot a').colorbox({ opacity: 0.25 });
		body.find('.stockists_list li:nth-child(odd)').addClass('clear');

		$.root.bind('cbox_closed', function() {
			body.find('#video_el').remove();
		});

		//	I'll think of a more elegant way to do this some other time
		body.find('.list_product_families li:nth-child(odd)').addClass('new_row');
		body.find('.list_products li:nth-child(odd)').addClass('new_row');
		body.find('.docs_list li:first-child').addClass('new_row');
		body.find('.product_thumbs li:nth-child(4n+1)').addClass('new_row');

	});

	//	--	Custom Plugins

	//	Google Maps
	jQuery.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);
			}

		});
	};



	//	Home Slideshow - Fair obvious, that..
	jQuery.prototype.homeSlideshow = function () {
		return this.each(function () {

			var container,
			    slides,
			    slides_count,
			    controls_html,
			    pager_count,
			    pager,
			    prev,
			    next;

			container    = $(this);
			slides       = container.find('ul.slides');
			slides_count = slides.find('li').length;

			if (slides_count > 1) {
				controls_html = '<div class="controls">';
				controls_html += '<p class="prev"><a href="#">Previous Slide</a></p>';

				controls_html += '<ul class="pager">';

				for (pager_count = 1; pager_count <= slides_count;) {
					controls_html += '<li><a href="#">' + pager_count + '</a></li>';

					pager_count += 1;
				}

				controls_html += '</ul>';

				controls_html += '<p class="next"><a href="#">Next Slide</a></p>';
				controls_html += '</div>';

				$(controls_html).insertAfter(slides);

				pager = container.find('ul.pager');
				prev  = container.find('p.prev');
				next  = container.find('p.next');

				slides.cycle({
					speed:   'fast',
					timeout: 7000,
					next:    next.find('a'),
					pager:   pager,
					prev:    prev.find('a'),
					pagerAnchorBuilder: function (index, slide) {
						return pager.find('li:eq(' + index + ') a');
					}
				});
			}

		});
	};



	//	Lightbox Videos
	jQuery.prototype.launchVideo = function () {
		return this.bind('click', function () {

			var link     = $(this),
			    config   = {},
			    video_el = $('<div id="video_el" />');

			config.params = {
				allowfullscreen: true,
				allowscriptaccess: 'always',
				wmode: 'transparent'
			};

			config.height = link.data('height');
			config.width  = link.data('width');
			config.wmode  = 'transparent';

			switch (link.data('type')) {
				case 'youtube':
					config.swf = 'http://www.youtube.com/v/' + link.data('code');
					config.flashvars = {
						fs: 1,
						hl: 'en',
						rel: 0
					};
					break;

				case 'vimeo':
					config.swf = 'http://vimeo.com/moogaloop.swf';
					config.flashvars = {
						clip_id: link.data('clip_id'),
						server: 'vimeo.com',
						show_title: 1,
						show_byline: 1,
						show_portrait: 0,
						fullscreen: 1
					};
					break;
				
				case 'html5':
					
					config.swf = '/incs/flash/flowplayer-3.2.7.swf';
					
					config.flashvars = {
						config: '{"clip":"/assets/videos/' + link.data('code') + '.mp4"}'
					};
					
					break;
					
				default:
					config.swf = '/assets/swf/' + link.data('query') + '.swf';
					break;
			}
			
			
			video_el.flash(config);
			
			$.colorbox({
				height:  463,
				html:    video_el,
				opacity: 0.25,
				width:   682
			});
			
			
			return false;

		});
	};



	//	Multi photos
	jQuery.prototype.productPhotos = function () {
		return this.each(function () {

			var container    = $(this),
			    product_shot = container.find('.product_shot'),
			    zoom_link    = product_shot.find('a'),
			    current_img  = product_shot.find('img');

			container.find('.product_thumbs a').bind('click', function () {
				var link      = $(this),
					target_id = link.data('id'),
					ext       = this.href.split('.').pop();

				if (target_id !== zoom_link.data('id')) {
					link.parent().addClass('active')
						.siblings().removeClass('active');

					zoom_link
						.data('id', target_id)
						.attr('href', '/assets/fullsize/photo' + target_id + '.' + ext)
						.animate({opacity: 0}, 250, 'linear', function () {
							$('<img src="/assets/thumbs/photo' + target_id + '.' + ext + '" />').bind('load', function () {
								current_img.attr('src', '/assets/thumbs/photo' + target_id + '.' + ext);
								zoom_link.animate({opacity: 1}, 250, 'linear');
							});
						});
				}

				return false;
			});

		});
	};



	//	Product Description/Specification/Downloads tabs
	jQuery.prototype.productTabs = function () {
		return this.each(function () {

			var container = $(this),
			    tabs      = container.find('.tabs a'),
			    content   = container.find('.product_tab_content');

			tabs.bind('click', function () {
				var link = $(this),
				    id   = this.className;

				link.parent().addClass('active').siblings().removeClass('active');

				content.hide();
				container.find('#' + id).show();

				return false;
			});

		});
	};



	//	General Video embedder thinger
	jQuery.prototype.videoEmbed = function () {
		return this.each(function () {

			var video  = $(this),
			    config = {};

			config.params = {
				allowfullscreen: true,
				allowscriptaccess: 'always',
				wmode: 'transparent'
			};

			config.height = video.data('height');
			config.width = video.data('width');
			config.wmode = 'transparent';

			if (video.data('type') === 'youtube') {
				config.swf = video.data('url');
				config.flashvars = {
					fs: 1,
					hl: 'en',
					rel: 0
				};
			}
			if (video.data('type') === 'html5') {

				
				config.swf = '/incs/flash/flowplayer-3.2.7.swf';
				
				config.flashvars = {
					config: '{"clip":"/assets/videos/' + link.data('code') + '.mp4"}'
				};
				
			}
			
			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);

		});
	};



	//	Where to buy and that
	jQuery.prototype.whereToBuy = function () {
		return this.each(function () {

			var form = $(this)
			    s_region  = form.find('select#region'),
			    s_country = form.find('select#country');

			$('<input type="hidden" name="ajax" value="true" />').prependTo(form);

			s_region.bind('change', function () {
				$.ajax({
					type: 'POST',
					data: form.serializeArray(),
					dataType: 'html',
					url: '/incs/actions/dropdowns_countries.php',
					success: function (data) {
						s_country.empty().append(data);
					}
				});
			});

		});
	};
	//	--	Good.. Can you say it faster?

}(jQuery, this, this.document));
//	--	Custom Plugins DONE, yeah?

