/* ERROR */
function showError()
{
	var msg = 'Ihr Browser / Ihre Browsereinstellungen entsprechen nicht den Anforderungen für den Routenfinder: Ihr Browser ist nicht kompatibel, wir empfehlen Firefox;';

	alert(msg);
}
/* COOKIE */
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != null) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') { // Remove spaces between equal signs
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1'); // Remove spaces between equal signs
  }
};

/* SELECTOR */
var map;
var slideX = 0;
var startHarbours;
var selectedRegion;
var selectedStartHarbour;
var selectedDestinyHarbour;
var destinyHarbours;
var dates;

var Selector = Class.create();
Selector.prototype = {
	serviceUrl: null,
	regions: null,
	startHarbours: null,
	routeHarbours: null,

	initialize: function()
	{
		this.serviceUrl = 'interface.php';
		//this.serviceUrl = 'ajax.php';

		this.regions = new Array();
		//this.startHarbours = "moin";//new Array();
		this.routeHarbours = new Array();
		
	
		this.regions = new Array();
		this.regions.push({name:'Nordland/Arktis',lat:72.76406472320436,lng:-13.7109375,zoom:3});
		this.regions.push({name:'Nordsee & Britische Inseln',lat:55.825973254619015,lng:-0.5712890625,zoom:5});
		this.regions.push({name:'Ostsee',lat:58.40171166760797,lng:19.5556640625,zoom:5});
		this.regions.push({name:'Westeuropa',lat:49.439556958940855,lng:2.63671875,zoom:4});
		this.regions.push({name:'Atlantische Inseln',lat:33.87041555094183,lng:-21.3134765625,zoom:5});
		this.regions.push({name:'Mittelmeer',lat:38.41055825094609,lng:14.150390625,zoom:4});
		this.regions.push({name:'Schwarzes Meer',lat:43.644025847699496,lng:34.1015625,zoom:5});
		this.regions.push({name:'westl. Afrika',lat:-11.523087506868512,lng:-5.80078125,zoom:3});
		this.regions.push({name:'Südliches Afrika',lat:-22.593726063929296,lng:28.125,zoom:4});
		this.regions.push({name:'Indischer Ozean',lat:-11.6952727330294,lng:71.54296875,zoom:3});
		this.regions.push({name:'Südostasien',lat:2.28455066023697,lng:116.54296875,zoom:4});
		this.regions.push({name:'Ferner Osten',lat:34.161818161230386,lng:126.298828125,zoom:4});
		this.regions.push({name:'Australien/Neuseeland',lat:-31.503629305773018,lng:141.50390625,zoom:3});
		this.regions.push({name:'Südsee',lat:-0.8788717828324148,lng:174.375,zoom:3});
		this.regions.push({name:'Antarktis',lat:-65.87472467098547,lng:-52.03125,zoom:3});
		this.regions.push({name:'Südamerika/Ostküste',lat:-25.79989118208832,lng:-36.9140625,zoom:3});
		this.regions.push({name:'Südamerika/Westküste',lat:-25.165173368663943,lng:-84.19921875,zoom:3});
		this.regions.push({name:'Karibik & Mittelamerika',lat:20.05593126519445,lng:-82.001953125,zoom:4});
		this.regions.push({name:'Nordamerika/Westküste',lat:56.17002298293205,lng:-117.24609375,zoom:3});
		this.regions.push({name:'Nordamerika/Ostküste',lat:54.67383096593114,lng:-89.82421875,zoom:3});
	},
	
	setRegion: function(region_id)
	{		
		selectedRegion = region_id;
	
		$('regionselected').innerHTML = '<nobr>'+this.regions[selectedRegion].name+'</nobr>';
		this.hideRegions(true);
		$('step_1').innerHTML = '<img src="img/step_1.gif" />';

		$('map').innerHTML = '';
		showMap();
		placeMap(this.regions[selectedRegion].lat, this.regions[selectedRegion].lng, this.regions[selectedRegion].zoom);
	},
	
	checkState: function()
	{
		if(Cookie.get('selectedStartHarbour') != null && Cookie.get('selectedDestinyHarbour') != null && Cookie.get('selectedRegion') != null) {
			selectedRegion = Cookie.get('selectedRegion');
			$('regionselected').innerHTML = '<nobr>'+this.regions[selectedRegion].name+'</nobr>';
			$('step_1').innerHTML = '<img src="img/step_1.gif" />';
			$('map').innerHTML = '';
			showMap();
			placeMap(this.regions[selectedRegion].lat, this.regions[selectedRegion].lng, this.regions[selectedRegion].zoom);

			selectedStartHarbour =Cookie.get('selectedStartHarbour').evalJSON();
			$('startselected').innerHTML = '<nobr>'+selectedStartHarbour.harbour_name+'</nobr>';
			$('step_2').innerHTML = '<img src="img/step_2.gif" />';
			$('set_2').style.display = 'block';
			this.showMarker(selectedStartHarbour);
			
			selectedDestinyHarbour =Cookie.get('selectedDestinyHarbour').evalJSON();
			$('destinyselected').innerHTML = '<nobr>'+selectedDestinyHarbour.harbour_name+'</nobr>';
			$('step_3').innerHTML = '<img src="img/step_3.gif" />';
			$('set_3').style.display = 'block';
			this.showMarker(selectedDestinyHarbour);
			
			this.getRoutes();
		}else if(Cookie.get('selectedStartHarbour') != null && Cookie.get('selectedRegion') != null) {

			selectedRegion = Cookie.get('selectedRegion');
			$('regionselected').innerHTML = '<nobr>'+this.regions[selectedRegion].name+'</nobr>';
			$('step_1').innerHTML = '<img src="img/step_1.gif" />';
			$('map').innerHTML = '';
			showMap();
			placeMap(this.regions[selectedRegion].lat, this.regions[selectedRegion].lng, this.regions[selectedRegion].zoom);

			selectedStartHarbour =Cookie.get('selectedStartHarbour').evalJSON();
			$('startselected').innerHTML = '<nobr>'+selectedStartHarbour.harbour_name+'</nobr>';
			$('step_2').innerHTML = '<img src="img/step_2.gif" />';
			$('set_2').style.display = 'block';
			this.showMarker(selectedStartHarbour);
			this.getDestinyHarbours();
		}else if(Cookie.get('selectedRegion') != null) {
			selectedRegion = Cookie.get('selectedRegion');
		
			$('regionselected').innerHTML = '<nobr>'+this.regions[selectedRegion].name+'</nobr>';
			this.hideRegions(true);
			$('step_1').innerHTML = '<img src="img/step_1.gif" />';

			$('map').innerHTML = '';
			showMap();
			placeMap(this.regions[selectedRegion].lat, this.regions[selectedRegion].lng, this.regions[selectedRegion].zoom);
		}else {
			this.showRegions();
		}
	},

	// REGIONS
	getRegions: function()
	{
		this.showRegions();	
	},
	showRegions: function()
	{
		s.hideStartHarbours();
		s.hideDestinyHarbours();

		$('set_2').style.display = 'none';
		$('set_3').style.display = 'none';
		
		var list = "";
		for(var i=0; i<this.regions.length; i++) {
			list += '<li onclick="s.selectRegion('+ (i) +');">';
			list += this.regions[i].name;
			list += '</li>';
		}
		
		$('step_1').innerHTML = '<img src="img/step_1_active.gif" />';

		$('regionlist').innerHTML = list;
		new Effect.BlindDown('regionoptions');
		
		$('regiontool').innerHTML = '-';
		$('regiontool').onclick = function()
		{
			s.hideRegions();
		}
	},
	hideRegions: function(selected) 
	{
		if(selected)
			new Effect.BlindUp('regionoptions', {afterFinish:function() {s.getStartHarbours();}});
		else
			new Effect.BlindUp('regionoptions', {afterFinish:function() {s.emptyRegions();}});

		$('regiontool').innerHTML = '+';
		$('regiontool').onclick = function()
		{
			s.showRegions();
		}
	},
	emptyRegions: function()
	{
		$('regionlist').innerHTML = "";
	},
	selectRegion: function(id) 
	{
		selectedRegion = id;
		
		Cookie.set('selectedRegion', id);
		
		$('regionselected').innerHTML = '<nobr>'+this.regions[id].name+'</nobr>';
		this.hideRegions(true);

		$('step_1').innerHTML = '<img src="img/step_1.gif" />';

		$('map').innerHTML = '';
		showMap();
		placeMap(this.regions[id].lat, this.regions[id].lng, this.regions[id].zoom);
	},

	// START HARBOR
	getStartHarbours: function()
	{
		// window.console.log("getStartHarbours");
		
		placeMap(this.regions[selectedRegion].lat, this.regions[selectedRegion].lng, this.regions[selectedRegion].zoom);

		$('startselected').innerHTML = "Bitte Starthafen wählen";
		$('step_2').innerHTML = '<img src="img/loading_loop.gif" />';
		$('set_2').style.display = 'block';
		
		//,route_id:id
		var bounds = map.getBounds();
		var ne = bounds.getNorthEast();
		var sw = bounds.getSouthWest();
		/*
		this.output("ne lat:"+ne.lat());
		this.output("ne lng:"+ne.lng());
		this.output("sw lat:"+sw.lat());
		this.output("sw lng:"+sw.lng());
		*/
		/*
		action=getStartHarbours
		min_lat=<minimale Breite>
		max_lat=<maximale Breite>
		min_long=<minimale Länge>
		max_long=<maximale Länge>
		*/
		
		var min_lat = sw.lat();
		var max_lat = ne.lat();
		//var min_long= (sw.lng() < ne.lng()) ? sw.lng() : ne.lng();
		//var max_long= (sw.lng() < ne.lng()) ? ne.lng() : sw.lng();
		
		var min_long= sw.lng();
		var max_long= ne.lng();

		new Ajax.Request(this.serviceUrl, { method:'get', parameters: {action: 'getStartHarbours',min_lat:min_lat,max_lat:max_lat,min_long:min_long,max_long:max_long}, onSuccess:s.renderStartHarbours});
	},
	renderStartHarbours: function(transport, json)
	{
		// window.console.log("renderStartHarbours");

		startHarbours = transport.responseText.evalJSON();

		var list = "";
		for(var i=0; i<startHarbours.length; i++) {
			list += '<li onclick="s.selectStartHarbour('+ i +');" onmouseover="listMouseOver('+ i +');" onmouseout="listMouseOut();">';
			list += startHarbours[i].harbour_name;// +"/"+this.startHarbours[i].name;
			list += '</li>';
		}

		$('startlist').innerHTML = list;
		
		$('step_2').innerHTML = '<img src="img/step_2_active.gif" />';
		
		s.showStartMarkers();
		
		s.showStartHarbours();
	},
	showStartHarbours: function()
	{
		s.hideDestinyHarbours();
		$('set_3').style.display = 'none';
		
		//var listHeight = startHarbours.length * 16 +20;
		$('startoptions').style.height = '350px'; //(listHeight>290) ? '290px' : listHeight+'px';
		new Effect.BlindDown('startoptions');
		//, {afterFinish:function() {;}}
		
		$('starttool').innerHTML = '-';
		$('starttool').onclick = function()
		{
			s.hideStartHarbours();
		}
	},
	hideStartHarbours: function(selected) 
	{
		if(selected)
			new Effect.BlindUp('startoptions', {afterFinish:function() {s.getDestinyHarbours();}});
		else
			new Effect.BlindUp('startoptions', {afterFinish:function() {s.emptyStartHarbours();}});
	
		$('starttool').innerHTML = '+';
		$('starttool').onclick = function()
		{
			s.getStartHarbours();
		}
	},
	emptyStartHarbours: function()
	{
		$('startlist').innerHTML = "";
	},
	selectStartHarbour: function(index) 
	{
		var harbourJsonString = ";"
		Cookie.set('selectedStartHarbour', this.harbourToJson(startHarbours[index]));
		
		// if start == destiny
		if($('startdestiny') != null && $('startdestiny').checked) {
			selectedDestinyHarbour = startHarbours[index];
			selectedStartHarbour = startHarbours[index];
			$('destinyselected').innerHTML = '<nobr>'+selectedDestinyHarbour.harbour_name+'</nobr>';
			$('step_3').innerHTML = '<img src="img/step_3.gif" />';
			$('set_3').style.display = 'block';
			this.hideStartHarbours();
			this.showResults();
		}else {
			this.hideStartHarbours(true);
		}
			
		$('startselected').innerHTML = '<nobr>'+startHarbours[index].harbour_name+'</nobr>';
		selectedStartHarbour = startHarbours[index];
		$('step_2').innerHTML = '<img src="img/step_2.gif" />';
		this.showMarker(startHarbours[index]);

		this.closeInfoWindow();

		return false;
	},

	// DESTINY HARBOR
	getDestinyHarbours: function()
	{		
		$('destinyselected').innerHTML = "Bitte Zielhafen wählen";
		$('step_3').innerHTML = '<img src="img/loading_loop.gif" />';
		$('set_3').style.display = 'block';

		new Ajax.Request(this.serviceUrl, { method:'get', parameters: {action: 'getDestinies', harbour_id:selectedStartHarbour.harbour_id}, onSuccess:s.renderDestinyHarbours});
	},
	renderDestinyHarbours: function(transport, json)
	{
		destinyHarbours = transport.responseText.evalJSON();
		var list = "";
		var key = 0;
		var index=0;
		for(var i=0; i<destinyHarbours.length; i++) {
			if(destinyHarbours[i].harbour_id == selectedStartHarbour['harbour_id']) {
				index =  destinyHarbours.length-1;
			} else {
				index = key;
				key++;
			}	
	
			list += '<li onclick="s.selectDestinyHarbour('+ i +');" onmouseover="listMouseOver('+ index +');" onmouseout="listMouseOut();">';
			list += destinyHarbours[i].harbour_name;// +"/"+this.startHarbours[i].name;
			list += '</li>';
		}

		$('destinylist').innerHTML = list;
		
		$('set_3').style.display = 'block';
		$('step_3').innerHTML = '<img src="img/step_3_active.gif" />';

		s.showDestinyMarkers();
		
		s.showDestinyHarbours();
	},
	showDestinyHarbours: function()
	{	
		var listHeight = destinyHarbours.length * 16 +20;
		$('destinyoptions').style.height = '300px'; //(listHeight>290) ? '290px' : listHeight+'px';
		new Effect.BlindDown('destinyoptions');
		//, {afterFinish:function() {;}}
		
		$('destinytool').innerHTML = '-';
		$('destinytool').onclick = function()
		{
			s.hideDestinyHarbours();
		}
	},
	hideDestinyHarbours: function(selected)
	{
		if(selected)
			new Effect.BlindUp('destinyoptions', {afterFinish:function() {s.showResults();}});
		else
			new Effect.BlindUp('destinyoptions', {afterFinish:function() {s.emptyDestinyHarbours();}});
			
		$('step_3').innerHTML = '<img src="img/step_3.gif" />';

		$('destinytool').innerHTML = '+';
		$('destinytool').onclick = function()
		{
			s.getDestinyHarbours();
		}
	},
	emptyDestinyHarbours: function()
	{
		$('destinylist').innerHTML = "";
	},
	selectDestinyHarbour: function(index)
	{
		this.closeInfoWindow();
		
		Cookie.set('selectedDestinyHarbour', this.harbourToJson(destinyHarbours[index]));
		
		selectedDestinyHarbour = destinyHarbours[index];

		$('destinyselected').innerHTML = '<nobr>'+destinyHarbours[index].harbour_name+'</nobr>';
		$('step_3').innerHTML = '<img src="img/step_3.gif" />';
		
		s.hideDestinyHarbours(true);
		
		this.showMarker(selectedStartHarbour, true);
		this.showMarker(destinyHarbours[index], false);
		
		return false;
	},
	
	showDates: function()
	{
		var list = "";
		
		$('set_4').style.display = 'block';
		$('step_4').innerHTML = '<img src="img/step_4_active.gif" />';

		dates = new Array();
		dates.push('alle Zeiträume');
		dates.push('November 2007');
		dates.push('Dezember 2007');
		dates.push('Januar 2008');
		dates.push('Februar 2008');
		dates.push('März 2008');
		dates.push('April 2008');
		dates.push('Mai 2008');
		dates.push('Juni 2008');
		dates.push('Juli 2008');
		dates.push('August 2008');
		dates.push('September 2008');
		dates.push('Oktober 2008');
		dates.push('November 2008');

		for(var i=0; i<dates.length; i++) {
			list += '<li onclick="s.selectDate('+ (i) +');">'+ dates[i] +'</li>';
		}

		$('datelist').innerHTML = list;

		var listHeight = 14 * 16 +20;
		$('dateoptions').style.height = (listHeight>290) ? '290px' : listHeight+'px';
		new Effect.BlindDown('dateoptions');
	},
	hideDates: function(selected)
	{
		if(selected)
			new Effect.BlindUp('dateoptions', {afterFinish:function() {s.showResults();}});
		else
			new Effect.BlindUp('dateoptions', {afterFinish:function() {s.emptyDates();}});
			
		$('step_4').innerHTML = '<img src="img/step_4.gif" />';

		$('datetool').innerHTML = '+';
		$('datetool').onclick = function()
		{
			s.showDates();
		}
	},
	emptyDates: function()
	{
		$('datelist').innerHTML = "";
	},
	selectDate: function(index)
	{
		$('dateselected').innerHTML = dates[index];
		this.hideDates(true);
		$('step_4').innerHTML = '<img src="img/step_4.gif" />';
	},
	
	showResults: function()
	{
		this.getRoutes();
		
	},
	
	getRoutes: function()
	{
		$('resultlink').innerHTML = '<img src="img/rf_loading_routes.gif" alt="loading" id="loading" style="float:left;margin-left:2px;margin-right:4px;"/><b>EINEN MOMENT BITTE<br />REISEN WERDEN GELADEN<b/>';
		
		new Ajax.Request(this.serviceUrl, { method:'get', parameters: {action: 'getRoutes', harbour_id:selectedStartHarbour.harbour_id, destiny_id:selectedDestinyHarbour.harbour_id}, onSuccess:s.renderRoutes});
	},
	renderRoutes: function(transport, json)
	{
		$('resultlink').innerHTML = '';
		
		var routes = transport.responseText.evalJSON();

		if(routes.length == 0) {
			var table = '<table><tr><th class="left">Keine Routen zu Ihrer Auswahl gefunden.</th></tr></table>';
		}else {
			var table = '<table><tr><th class="left">Schiff</th><th class="date">Termin</th><th class="duration">Dauer</th><th class="route">Routenverlauf</th><th class="right">Preis</th></tr>';
			var i=0;
			for(routeId in routes) {
				table += '<tr>';
				table += '<td><b>'+ routes[routeId][0].ship_name +'</b></td>';
				table += '<td>'+ routes[routeId][0].departure +'-'+ routes[routeId][0].arrival +'</td>';
				table += '<td>'+ routes[routeId][0].duration +' Tage</td>';
				table += '<td><b>'+ routes[routeId][0].start +' - '+ routes[routeId][0].destiny +'</b></td>';
				table += '<td class="right"><div class="price">ab '+ routes[routeId][0].price +' €</div>'+ '<div class="link"><a href="'+ routes[routeId][0].link +'" class="booking">Infos & Buchen</a></div></td>';
				table += '</tr>';
				i++;
			}
			table += '</table>';
		}

		$('resultlist').innerHTML = table;
		
		// new Element.scrollTo('resultlist');
		parent.window.scrollBy(0, 250-parent.window.pageYOffset);
	},
	
	// map
	showStartMarkers: function()
	{
		//map.setCenter(new GLatLng(startHarbours[0].latitude, startHarbours[0].longitude), 3);
		
		clearOverlay();
		
		for(var i=0; i<startHarbours.length; i++) {
			var point_0 = new GLatLng(startHarbours[i].latitude, startHarbours[i].longitude);
			var infoWindowContent = '';
			infoWindowContent += '<h1>'+startHarbours[i].harbour_name +'</h1>'+'markieren als Starthafen?';			
			infoWindowContent += '<br /><br /><input type="checkbox" id="startdestiny" />auch Zielhafen';
			infoWindowContent += '<br /><br /><a href="#" class="infoWindow" onclick="return s.selectStartHarbour('+ i +');">&raquo;&nbsp;Ja</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="infoWindow" onclick="return s.closeInfoWindow();">&raquo;&nbsp;Nein</a>';

			map.addOverlay(createMarker(point_0, 0, infoWindowContent, startHarbours[i].harbour_name));
		}
		
		// map.panTo(new GLatLng(harbour.latitude, harbour.longitude))
	},
	
	closeInfoWindow: function()
	{
		map.closeInfoWindow();

		return false;
	},
	
	showDestinyMarkers: function()
	{
		//map.setCenter(new GLatLng(startHarbours[0].latitude, startHarbours[0].longitude), 3);
		
		clearOverlay();
		
		
		for(var i=0; i<destinyHarbours.length; i++) {
			if(selectedStartHarbour.harbour_id != destinyHarbours[i].harbour_id) {
				var point_0 = new GLatLng(destinyHarbours[i].latitude, destinyHarbours[i].longitude);
				var infoContent = '<h1>'+ destinyHarbours[i].harbour_name +'</h1>markieren als Zielthafen?';
				infoContent += '<br /><br /><a href="#" class="infoWindow" onclick="return s.selectDestinyHarbour('+ i +');">&raquo;&nbsp;Ja</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="infoWindow" onclick="return s.closeInfoWindow();">&raquo;&nbsp;Nein</a>';
				
				map.addOverlay(createMarker(point_0, 0, infoContent, destinyHarbours[i].harbour_name));
			}
		}
		
		this.showMarker(selectedStartHarbour, false);
		
		// map.panTo(new GLatLng(harbour.latitude, harbour.longitude))
	},
	showMarker: function(harbour, clearoverlay)
	{
		//map.setCenter(new GLatLng(40.74205475883487, -74.014892578125), 5);
		if(clearoverlay != null && clearoverlay== true)
			clearOverlay();
		
		map.panTo(new GLatLng(harbour.latitude, harbour.longitude))
		var point_0 = new GLatLng(harbour.latitude, harbour.longitude);
		var infoContent = '<h1>'+harbour.harbour_name+'</h1>';
		map.addOverlay(createMarker(point_0, 0, infoContent, harbour.harbour_name, 'rf_anker_hi.png'));
	},
	placeMap: function(lat,lng,zoom)
	{
		placeMap(lat,lng,zoom);
	},
	
	harbourToJson: function(harbour)
	{
		return $H({harbour_id:harbour.harbour_id, harbour_name: harbour.harbour_name, latitude:harbour.latitude, longitude:harbour.longitude}).toJSON();
	},
	
	// debug
	output: function(s)
	{
		alert(s);
	}
	
};

