$(document).ready(function() {
	
	// Show elements that require javascript
	$('.script').css({display: 'block'});
	$('.noscript').css({display: 'none'});
});

/**
 * Listpod specific jQuery functions */
(function($) 
{
	// Turn elements into flowplayers
	$.fn.flowplayer = function(url, fullscreen) {
		return this.each(function() {
			var oldId = this.id;
			this.id = Math.random(); // temporarily reassign id
			flashembed(this.id, '/js/flowplayer/flowplayer-3.1.5.swf', {config: {
			    clip: {
			    	url: escape(url),
			    	autoPlay: false,
			    	scaling: 'fit'
				},
			    plugins: {
			        controls: {
			        	height: 22,
			        	backgroundColor: '#555555',
		        		backgroundGradient: [0, 0],
				    	fullscreen: fullscreen ? true : false
			        }
			    }
			}});
			this.id = oldId;
		});
	};
	
	// Turn an element into a map.
	$.fn.mapFormField = function(name, value, createMarker) {
		
		return this.each(function() {
		
			// Create elements
			var $elem = $(this);
			$elem.html('<div style="width: 100%; height: '+$elem.height()+'px"></div><input type="hidden" name="'+name+'"/>');
			var div = $elem.children('div')[0];
			var input = $elem.children('input')[0];
		
			var point = new google.maps.LatLng(value.latitude, value.longitude);
			var map = new google.maps.Map(div, {
				zoom: value.zoom,
				center: point,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
		
			// set the value of the hidden input field.
			function setValue()
			{	input.value = '{"lat": ' + map.getCenter().lat() +
					', "lng": ' + map.getCenter().lng() + ', "zoom": ' + map.getZoom() + '}';
			}
		
			var marker;
			if (createMarker)
			{	marker = new google.maps.Marker({
					position: point,
					map: map,
					title: 'Click the map to move me!'
				});
				setValue();
			}
			
			google.maps.event.addListener(map, 'zoom_changed', setValue);
			
			var timeout;
			google.maps.event.addListener(map, 'click', function(e) {
				clearTimeout(timeout);
				timeout = setTimeout(function() {
					if (marker) // remove old marker
						marker.setMap(null);
		
					marker = new google.maps.Marker({
						position: e.latLng,
						map: map,
						title: 'Click the map to move me!'
					});
					
					map.panTo(e.latLng);
					setValue();
				}, 350); // wait for double click.
			});				
		});
	};
	
})(jQuery);
