//var console = window.console || {log:function() {}};
var currentPlayer = null;

/* Tour Header */
swfobject.embedSWF(js_base+'swf/header_tour_gesundheit.swf', 'header_flash', 960, 106, '6');

function showVideo(which, el, width, height, mp4) {
	width = width || 303;
	height = height || 250;
	//mp4 = mp4 || false;
	mp4 = true;
	
	var modes = [], file = js_base + 'flv/' + which;
	modes.push({type: 'flash', src: js_base + 'mediaplayer-5.5/player.swf', config: {
		'file': file + '.flv'
	}});
	
	if(mp4) {
		modes.push({type: 'html5', config: {
			'file': file + '.mp4'
		}});
	}
	
	var config = {
		flashplayer: js_base + 'mediaplayer-5.5/player.swf',
		file: file + '.flv',
		image: file + '.jpg',
		width: width,
		height: height,
		events: {
			onPlay: function(e) { 
				if(currentPlayer && currentPlayer != el) {
					jwplayer(currentPlayer).stop();
				}
				currentPlayer = el;
			},
			onComplete: function() {
				currentPlayer = null;
			},
			onPause: function() {
				currentPlayer = null;
			}
		},
		modes: modes
	};
	
	jwplayer(el).setup(config);
}

function playVideo(which) {
	//console.log('playVideo', which);
	var base = js_base + 'flv/', file = base + which;
	if(jwplayer('movie').renderingMode == 'flash') {
		file += '.flv';
	} else {
		file += '.mp4';
	}
	
	jwplayer('movie').load({
		file: file,
		image: base+which+'.jpg'
	}).play(true);
}

function makeMap(elementID) {
	
	var map = new GMap2(document.getElementById(elementID));
	var bounds = new GLatLngBounds(
		new GLatLng(47.27471900000000,5.86416600000000),
		new GLatLng(55.05666400000000,15.03888700000000)
	);
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	map.disableDragging(false);
	map.disableDoubleClickZoom();
	map.disableScrollWheelZoom();
	return map;
}

function loadMarker(bOnlyUpcoming, callback) {
	bOnlyUpcoming = bOnlyUpcoming || false;

	$.get(js_base + 'tpl/map.ajax.php', {
		bOnlyUpcoming: bOnlyUpcoming
	}, function(data) {
		for(var i=0, o; o=data[i]; ++i) {
			callback(o, i==0);
		}
	});
}

(function($) {
	$(function() {
		$('ul.sf-menu').supersubs({
			minWidth: 9,
			maxWidth:25,
			extraWidth: 1
		}).superfish({
			autoArrows: false,
			dropShadows: false,
			speed: 100,
			delay: 500,
			animation: { opacity:'show', height:'show' }
		})/*.find('ul').bgIframe({opacity:false})*/;

		if($('#checks').length) {
			$('map[name=checkmap], map[name=fusscheck]').click(function(e) {
				if(e.target.href.indexOf('?v=') > -1) {
					e.preventDefault();
					playVideo(e.target.href.split('?v=')[1]);
				}
			});
		}

		if($('#schuhcheck').length) {
			$('#kundennummer').focus(function() {
				this.value = this.value == 'Kundennummer' ? '' : this.value;
			}).blur(function() {
				this.value = this.value == '' ? 'Kundennummer' : this.value
			});
			$('#kennwort').focus(function() {
				this.value = this.value == 'Kennwort' ? '' : this.value;
			}).blur(function() {
				if(this.value == '') {
					this.value = 'Kennwort';
				}
			});
		}
		/*
		if($('#tvspots').length) {
			showVideo('tvspot_10', 'movie', 303, 250);
		}
		*/
		if($('#tourmap').length) {
			var m = google.maps;
			var map = makeMap('tourmap');
	
			GEvent.addListener(map, 'click', function() { window.location = js_base+'tour/'; })
			
			loadMarker(true, createMarker);
			function gotoTour(item) {
				item = item || false;
				if(item) {
					item = '?id='+item;
				} else {
					item = '';
				}
				window.location = js_base + 'tour/'+item;
			}
		}



		function createMarker(o, blueMarker) {
			var point = new GLatLng(o.lat, o.lng);
			var marker = new GMarker(point, {
				title: o.datum+': '+o.titel
			});
			map.addOverlay(marker);
			GEvent.addListener(marker, 'click', function() { gotoTour(o.id); });
		}

	});
	$(window).unload(GUnload);
})(jQuery);
