var jtk = new JoookToolkit();
var mapservice;
var locale = null;


// Legacy functions

// Funktio laskee Amadeukselle sopivat lähtö- ja paluupäivät ja palauttaa muuttujat Amadeuksen GET-muuttujina
function calc_car_dates(){
	
	dept_date = new Date(); // Lähtöpäivä
	min_time = 0; // Auton varaamisesta auton noutamiseen tarvittava minimiaika vuorokausina

	
	// Lasketaan ensimmäinen mahdollinen menoaika
	dept_date.setDate(dept_date.getDate() + min_time);
	dept_date.setHours(dept_date.getHours() + 1);
	dept_time = dept_date.getHours();
	dept_day = dept_date.getDate();
	dept_month = dept_date.getMonth();
	dept_year = dept_date.getFullYear();

	
	// Lasketaan ensimmäinen mahdollinen paluuaika
	dept_date.setDate(dept_date.getDate() + 1);
	dept_date.setHours(dept_date.getHours());
	arr_time = dept_date.getHours();
	arr_day = dept_date.getDate();
	arr_month = dept_date.getMonth();
	arr_year = dept_date.getFullYear();
	
	
	// Muutetaan kuukaudet oikeaan muotoon
	if(dept_month + 1 < 10){
		dept_month = "0" + (dept_month + 1);
	}
	else{
		dept_month++;
	}
	
	if(arr_month + 1 < 10){
		arr_month = "0" + (arr_month + 1);
	}
	else{
		arr_month++;
	}
	
	
	
	if(dept_day < 10){
		dept_day = "0" + dept_day;
	}
	
	if(arr_day < 10){
		arr_day = "0" + arr_day;
	}
	
	b_date_1 = "&B_DATE=" + dept_year + "" + dept_month + "" + dept_day + "" + dept_time + "00";
	b_date_2 = "&E_DATE=" + arr_year + "" + arr_month + "" + arr_day + "" + arr_time + "00";
	
	return (b_date_1 + b_date_2);
}


function search_flights2(){
	url_base = "https://wftc2.e-travel.com/plnext/kalevatravel/CleanUpSessionPui.action?SITE=BBDFBBDF&LANGUAGE=FI";
	url = url_base;
	
	window.open(url);
}

function search_cars2(){
	url_base = "https://wftc2.e-travel.com/plnext/kalevatravel/CleanUpSessionPui.action?SITE=BBDFBBDF&LANGUAGE=FI&TYPE_FLOW=car";
	url = url_base + calc_car_dates();
	
	window.open(url);
}






// Revised functionalities

var activateSlideShow = false;

$(document).ready(function(){
	initU4();
	initBDates('amadeusBooker');
	if(activateSlideShow == true){
		generateSlideShow();
		jQuery('#slideDisplay ul').jcarousel({
        	scroll: 1,
			auto: 6,
			wrap: 'both'
	    });
	}
});

function generateSlideShow(){
	$('#slideImages img').appendTo('#slideDisplay');
	$('#slideDisplay img').wrap(document.createElement('li'));
	$('#slideDisplay li').wrapAll(document.createElement('ul'));
	$('#slideDisplay ul').addClass('jcarousel-skin-ie7');
	$('#slideImages').empty();
}




var layer = null;

function displayLayer(contentId){
	var optionsObj = {width: 660, height: 360, contentHolderId: contentId};
	var overlay = new Overlay(optionsObj);
	
	overlay.display();
}


function boundHide(switcherId, hidableIds, hideSwitcher, threshold){
	if(hideSwitcher != false){
		hideSwitcher = true;
	}
	
	if(threshold == null || threshold == 'undefined'){
		threshold = 3;
	}
	
	var switcher = jtk.select("#" + switcherId)[0];
	var displayMode = "none";
	
	if(switcher.innerHTML.length > threshold){
		displayMode = "block";
	}
	
	switcher.style.display = displayMode;
		
	for(var i = 0; i < hidableIds.length; i++){
		jtk.select("#" + hidableIds[i])[0].style.display = displayMode;
	}
}






function initMap(useDI){
	if(document.getElementById('geocode').innerHTML != ''){		
		var hotelCoords = eval("({" + document.getElementById('geocode').innerHTML + "})");
		var hotelPoint = new GLatLng(hotelCoords.lat, hotelCoords.lng);
		
		mapservice = new MapService('mapView');
		if(locale == null){
			locale = "fi_FI";
		}
		mapservice.setLocale(locale);
		mapservice.init();
		mapservice.showControls();
		
		if(useDI == true){
			var address = document.getElementById("street_address").innerHTML + ", " + document.getElementById("post_office").innerHTML;
			mapservice.getDirections(document.getElementById("diFrom").value, address, "directions");
		}
		else{
			mapservice.gotoPos(hotelPoint, true);
		}
	}
}


function mapView(geoCode, emtId, showDIQuery){
	var position = findPos(document.getElementById("maplinks"));
	document.getElementById(emtId).style.left = (position[0] - 180) + "px";
	document.getElementById(emtId).style.top = (position[0] - 300) + "px";
	document.getElementById(emtId).style.display = "block";
	
	if(showDIQuery == true){
		document.getElementById(emtId).getElementsByTagName("form")[0].style.display = "block";
	}
	
	initMap(false);
}

function updateDI(){
	if(document.getElementById("diFrom").value != ''){
		document.getElementById("diTo").value = document.getElementById("street_address").innerHTML + ", " + document.getElementById("post_office").innerHTML;
		document.getElementById("diHotel").value = document.getElementById("content_head").innerHTML;
		return true;
	}
	else{
		return false;	
	}
}

function closeMap(emtId){
	document.getElementById(emtId).style.display = "none";
}

function printMap(emtId){
	var address = document.getElementById("street_address").innerHTML + ", " + document.getElementById("post_office").innerHTML;
	var newWin = window.open("/stc/cumulus/jsp/maps.jsp?from=" + urlEncode(document.getElementById("diFrom").value) + "&to=" + urlEncode(address) + "&hotel=" + urlEncode(document.getElementById("content_head").innerHTML) + "&locale=" + mapservice.locale);
}













/*
	Hotel mapper
	
	Dependencies:
		- Google Maps SDK
		- JoookToolkit as jtk
*/

var HotelMapper = {
	coordinates: new Array(),
	descriptions: new Array(),
	displayEmt: null,
	infoEmt: null,
	layerEmt: null,
	layerOffset: {x: -300, y: -150},
	layerPosition: null,
	zoomLevel: 11,
	baseIcon: new GIcon(),

	geocoder: null,	
	mapObject: null,
	mapControl: new GLargeMapControl(),
	
	addCoordinate: function(coord){
		this.coordinates.push(new GLatLng(coord.lat, coord.lng));
	},
	
	addCoordinateAsString: function(coordStr){
		var coord = eval("({" + coordStr + "})");
		this.addCoordinate(coord);
	},
	
	addDescription: function(descStr){
		this.descriptions.push(descStr.replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
	},
	
	addMarker: function(latLng, desc){
		var marker = new GMarker(latLng, new GIcon(this.baseIcon, "http://www.kalevatravel.fi/stc/destination2009/images/gmaps/marker.png", null, "http://www.kalevatravel.fi/stc/destination2009/images/gmaps/marker_shadow.png"));
		this.mapObject.addOverlay(marker);
		if(desc != 'undefined' && desc != null && desc != ''){
			GEvent.addListener(marker, "click", function(){
				marker.openInfoWindow(desc);
			});
		}
	},
	
	setDisplayEmt: function(domEmt){
		this.displayEmt = domEmt;
	},
	
	setInfoEmt: function(domEmt){
		this.infoEmt = domEmt;
	},
	
	setLayerEmt: function(domEmt){
		this.layerEmt = domEmt;
	},
	
	showMap: function(clickSrc){
		var srcPos = jtk.getPosition(clickSrc);
		var vpSize = jtk.getViewportSize();
		this.layerPosition = {x: (vpSize.width / 2) + this.layerOffset.x, y: srcPos.top + this.layerOffset.y};
		this.layerEmt.style.left = this.layerPosition.x + "px";
		this.layerEmt.style.top = this.layerPosition.y + "px";
		this.layerEmt.style.display = "block";
		this.mapObject.setCenter(this.coordinates[0], this.zoomLevel);
		this.mapObject.checkResize();
		this.mapObject.setCenter(this.coordinates[0], this.zoomLevel);
	},
	
	hideMap: function(){
		this.layerEmt.style.display = "none";
	},
	
	init: function(){
		this.baseIcon.iconSize=new GSize(32,32);
		this.baseIcon.shadowSize=new GSize(59,32);
		this.baseIcon.iconAnchor=new GPoint(16,32);
		this.baseIcon.infoWindowAnchor=new GPoint(16,0);
		this.mapObject = new GMap2(this.displayEmt);
		this.geocoder = new GClientGeocoder();
		
		this.mapObject.setUIToDefault();
		
		for(var i = 0; i < this.coordinates.length; i++){
			var infoEmt = this.infoEmt;
			var desc = this.descriptions[i];
			this.addMarker(this.coordinates[i], desc);
		}
		
		
	}
};
