/**
 * 
 * This javascript file contains many of the bootstrapping methods required for
 * functionality such as video playing, colorbox display, and initialising the
 * tinyscrollbar.
 * 
 * Once the DOM has loaded, the script checks for the existence of specific
 * classes or IDs and initialises the components as required. If the page
 * doesn't contain a slideshow or a tinyscrollbar, etc, then the code isn't
 * executed.
 * 
 * @author Suffocate <jason@expansionware.com.au>
 */

var showBubbles = null;
var COOKIE_BUBBLES = 'enable_bubbles';
var SoapBubbleMachineNumber1 = null;
var SoapBubbleMachineNumber2 = null;
var SoapBubbleMachineNumber3 = null;
var googleMap;

$('document').ready(
		function() {

			// Build Bubble Machines with the Bubble Engine
			// ------------------------
			showBubbles = $.cookie(COOKIE_BUBBLES);
			if (showBubbles === null) {
				$.cookie(COOKIE_BUBBLES, 'true');
				showBubbles = 'true';
			} else {
				showBubbles = $.cookie(COOKIE_BUBBLES);
			}

			initBubbles();
			$(window).resize(
					function() {
						SoapBubbleMachineNumber1.setParticleSourceX($(
								"#bubbles").width() / 2);
						SoapBubbleMachineNumber1.setParticleSourceY($(
								"#bubbles").height());
						SoapBubbleMachineNumber2.setParticleSourceX($(
								"#bubbles").width() / 2 - 350);
						SoapBubbleMachineNumber2.setParticleSourceY($(
								"#bubbles").height());
						SoapBubbleMachineNumber3.setParticleSourceX($(
								"#bubbles").width() / 2 + 350);
						SoapBubbleMachineNumber3.setParticleSourceY($(
								"#bubbles").height());
					});

			if (showBubbles == 'true') {
				$('#bubbles_toggle').attr('src', '/images/bubblesoff.png');
				startBubbles();
			} else {
				$('#bubbles_toggle').attr('src', '/images/bubbleson.png');
			}

			if (typeof pageBootstrap == 'function') {
				pageBootstrap();
			}

			var myPlayer = null;

			var whereMap = $('#where_map');
			if (whereMap.length > 0) {
				initializeWhereMap();
				displayMarkers();
			}

			if ($('#gallery').length > 0) {
				$('#gallery').css("overflow", "hidden");
				$('#slides').cycle();
			}

			/*
			 * Still tossing around whether it's better as an inline div on the
			 * showSuccess template, or taken from the videoSuccess page.
			 * 
			 * This method uses the videoSuccess page.
			 */
			var videoBox = $('#video_button');
			if (videoBox.length > 0) {
				
				var width = parseInt($('#video_width').val());
				var height = parseInt($('#video_height').val());
				
				// Pad the dimension to allow for the colorbox border
				width += 8;
				height += 8;
				
				$("#video_button").colorbox({
					innerWidth : width,
					innerHeight : height,
					initialWidth : "150",
					initialHeight : "90",
					onComplete : function() {
						//myPlayer = VideoJS.setup("video_player");
						// myPlayer.play();
						//setTimeout(function() {
							// myPlayer.positionBox();
						//}, 500);
					},
					onClose : function() {
						//myPlayer.stop();
						//alert('stop!');
					}
				});
			}

			/*
			var videoPlayer = $('#blooper_video');
			if (videoPlayer.length > 0) {
				myPlayer = VideoJS.setup("video_player");
			}
			*/

			var scrollContainer = $('#scrollable_container');
			if (scrollContainer.length > 0) {
				scrollContainer.tinyscrollbar();
			}

			var blooperContainer = $('#scrollable_blooper');
			if (blooperContainer.length > 0) {
				blooperContainer.tinyscrollbar();
			}
		});

function toggleBubbles() {

	if (showBubbles == 'true') {
		$('#bubbles_toggle').attr('src', '/images/bubbleson.png');
		stopBubbles();
		showBubbles = 'false';
	} else {
		$('#bubbles_toggle').attr('src', '/images/bubblesoff.png');
		startBubbles();
		showBubbles = 'true';
	}

	$.cookie(COOKIE_BUBBLES, showBubbles);
}

function initializeWhereMap() {
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	var myOptions = {
		zoom : 2,
		center : latlng,
		mapTypeId : google.maps.MapTypeId.SATELLITE
	};
	googleMap = new google.maps.Map(document.getElementById('where_map'),
			myOptions);
	geocodeAddress("Australia");
}

function geocodeAddress(address) {
	geocoder = new google.maps.Geocoder();

	if (geocoder) {
		geocoder
				.geocode(
						{
							'address' : address
						},
						function(results, status) {
							if (status == google.maps.GeocoderStatus.OK) {
								googleMap.panTo(results[0].geometry.location);
							} else {
								alert("Unable to determine the location of the installer on a map. The reason given was: "
										+ status);
								isSuccessful = false;
							}
						});
	} else {
		alert("Could not contact the Google Maps server. Please try again later.");
	}

	return false;
}

function addMarkerAddress(address, markerTitle, href) {

	if (geocoder) {

		geocoder
				.geocode(
						{
							'address' : address
						},
						function(results, status) {
							if (status == google.maps.GeocoderStatus.OK) {
								addMarker(results[0].geometry.location,
										markerTitle, href);
							} else {
								alert("Unable to determine the location of the installer on a map. The reason given was: "
										+ status);
								isSuccessful = false;
							}
						});
	} else {
		alert("Could not contact the Google Maps server. Please try again later.");
	}

}

function addMarker(latLng, markerTitle, href) {

	var infowindow = new google.maps.InfoWindow({
		content : markerTitle
	});

	var markerOptions = {
		clickable : true,
		draggable : false,
		map : googleMap,
		position : latLng,
		title : markerTitle,
		visible : true
	};

	var newMarker = new google.maps.Marker(markerOptions);

	google.maps.event.addListener(newMarker, 'mouseover', function() {
		infowindow.open(googleMap, newMarker);
	});

	google.maps.event.addListener(newMarker, 'mouseout', function() {
		infowindow.close();
	});

	google.maps.event.addListener(newMarker, 'click', function() {
		window.location = href;
	});
}

function initBubbles() {

	SoapBubbleMachineNumber1 = createBubbleMachine($('#bubbles').width() / 2);
	SoapBubbleMachineNumber2 = createBubbleMachine($('#bubbles').width() / 2 - 350);
	SoapBubbleMachineNumber3 = createBubbleMachine($('#bubbles').width() / 2 + 350);

}

function startBubbles() {
	SoapBubbleMachineNumber1.addBubbles(5);
	SoapBubbleMachineNumber2.addBubbles(5);
	SoapBubbleMachineNumber3.addBubbles(5);
}

function stopBubbles() {
	SoapBubbleMachineNumber1.removeBubbles();
	SoapBubbleMachineNumber2.removeBubbles();
	SoapBubbleMachineNumber3.removeBubbles();
}

function createBubbleMachine(width) {
	var Machine = $('#bubbles').BubbleEngine({
		particleSizeMin : 10,
		particleSizeMax : 60,
		particleSourceX : width,
		particleSourceY : $('#bubbles').height(),
		particleAnimationDuration : 5000,
		particleDirection : 'center',
		particleAnimationDuration : 2000,
		particleAnimationVariance : 2000,
		particleScatteringX : 150,
		particleScatteringY : 200,
		gravity : -300
	});
	return Machine;
}

/**
 * Clears a Search input field and sets the text color to black
 * 
 * @param field
 *            the search field object
 */
function clearInput(field) {

	if (field.value == 'Search') {
		field.value = '';
		field.style.color = "#000000";
	}
}

