/* default values for 'Anreisedatum' and 'Abreisedatum' (+ X, bzw. Y days), 'Erwachsene'...	*/
var xDays = 28;
var yDays = 2;
var plusDays = 2; // after change of day in 'anreisedatum' should be 'abreisedatum' + plusDays more
var defErwachsene = 2;
var defKinder = 0;
var defKidAge = -1;

var oneMinute = 60 * 1000;  // milliseconds in a minute
var oneHour = oneMinute * 60;
var oneDay = oneHour * 24;
var oneWeek = oneDay * 7;

/* creates date from string, in case of error returns null */
function str2Date(dateStr) {

	var dt = new Date();
	
	try {
		spl = dateStr.split(".");
		dt.setFullYear(spl[2]);
		var month = spl[1];
		if (month.substr(0,1) == '0') month = month.substr(1,1);
		dt.setMonth(parseInt(month)-1);
		dt.setDate(spl[0]);
	}	catch (err) {
		dt = null;
	}

	return dt;
}


/* creates string from date, in case of error returns empty string */
function date2Str(dt) {
	
	var str = "";
	
	try {
		str = dt.getDate() + '.' + (dt.getMonth()+1) + '.' + dt.getFullYear();		
	} catch(err) {
		str = "";
	}
	
	return str;
}


/* returns difference in days between two dates */
function getDiffInDays(dateStart, dateEnd) {
	var diff = 0;
	try {
		diff = dateEnd - dateStart;
		diff = diff / oneDay;
	}	catch (err) {
	}
	
	return Math.round(diff);
}


/* counts date from src day and month component, adds 2 days and sets the dest day and month components */
function addPlusDays2DtComps(srcDayCompId, srcMonthCompId, destDayCompId, destMonthCompId) {
	// get the value from src-components
	var today = new Date();
	var selectedDay = document.getElementById(srcDayCompId).options[document.getElementById(srcDayCompId).selectedIndex].value;
	if (selectedDay.length == 1) selectedDay = '0'+selectedDay;
	var selectedMonth = document.getElementById(srcMonthCompId).options[document.getElementById(srcMonthCompId).selectedIndex].value;
	if (selectedMonth.length == 1) selectedMonth = '0'+selectedMonth;
	var selectedYear = ((selectedMonth >= (today.getMonth()+1)) ? today.getFullYear() : (today.getFullYear()+1));
	var srcDateStr = selectedDay + '.' + selectedMonth + '.' + selectedYear;
	var srcDate = str2Date(srcDateStr);
	
	// add x days
	var destDateInMS = srcDate.getTime() + oneDay * plusDays;
	var destDate = new Date(destDateInMS);
	
	// set the values for dest-components
	var destDateStr = date2Str(destDate);
	splDest = destDateStr.split(".");
	rebuildDays(document.getElementById(destMonthCompId), document.getElementById(destDayCompId), destDate);
	document.getElementById(destDayCompId).selectedIndex = parseInt(splDest[0])-1;
	if (srcDate.getMonth() == destDate.getMonth()) {
		document.getElementById(destMonthCompId).selectedIndex = document.getElementById(srcMonthCompId).selectedIndex;
	} else {
		document.getElementById(destMonthCompId).selectedIndex = document.getElementById(srcMonthCompId).selectedIndex+1;
	}
}


/* sets the index of month component compSelectMonthYear to idx */
function setMonth(compSelectMonthYear, idx) {
	compSelectMonthYear.selectedIndex = idx;
}


/* sets default values for 'Anreisedatum' and 'Abreisedatum' (if desired) and inits date comboboxes */
function setDates() {
	// default values
	var today = new Date();
	var dateInMS = today.getTime() + oneDay * xDays;
	var anreisedatumDate = new Date(dateInMS);
	anreisedatum = date2Str(anreisedatumDate);
	
	dateInMS = today.getTime() + oneDay * (xDays + yDays);
	var abreisedatumDate = new Date(dateInMS);
	abreisedatum = date2Str(abreisedatumDate);
	
	// Suche ändern
	try {
		initDays(document.getElementById("hinreiseTag"), anreisedatumDate);
	} catch(err) {
	}
	
	try {
		initMonths(document.getElementById("hinreiseMonatJahr"), anreisedatumDate);
	} catch(err) {
	}
	
	try {
		initDays(document.getElementById("rueckreiseTag"), abreisedatumDate);
	} catch(err) {
	}

	try {
		initMonths(document.getElementById("rueckreiseMonatJahr"), abreisedatumDate);
	} catch(err) {
	}
	
}


/* sets default value for 'erwachsene' (if desired) and fills comboboxes */
function setAdults() {
	initErwSelect('PAX');
}


/* fills desired combobox for 'Erwachsene' */
function initErwSelect(id) {
	try {
		removeOptions(document.getElementById(id));
	} catch(err) {
	}
	for (var i=1; i<=4; i++) {
		var option = document.createElement("option");
		option.setAttribute("value", i);
		// selected value is set hier, because in IE6 doesn't work selectedIndex
		// compSelect.selectedIndex = selDate.getDate()-1;
		if (i == defErwachsene)
			option.setAttribute("selected", "selected");
		if (i == 1)
			option.appendChild(document.createTextNode(i + " Erwachsener"));	
		else
			option.appendChild(document.createTextNode(i + " Erwachsene"));
		document.getElementById(id).appendChild(option);		
	}
}


/* fills comboboxes for 'AnzahlKx_y' and 'Kindx' */
function setChildren() {
	for (var i=1; i<=3; i++)
		initChildSelect('Kind' + i, i);
}


/* fills desired combobox for children
	 pos - 1..3 - which from children
 */
function initChildSelect(id, pos) {
	try {
		removeOptions(document.getElementById(id));
	} catch(err) {
	}
	
	var option = document.createElement("option");
	option.setAttribute("value", defKidAge);
	option.setAttribute("selected", "selected");
	option.appendChild(document.createTextNode("---"));
	document.getElementById(id).appendChild(option);
	
	for (var i=0; i<=16; i++) {
		var option = document.createElement("option");
		option.setAttribute("value", i);
		option.appendChild(document.createTextNode(i+" Jahre"));
		document.getElementById(id).appendChild(option);
	}
}


/* initial visibility of elements */
function initVisibility() {
	setChildren();
	setDates(); // set the default values for an/abreisedatum and fill comboboxes
	setAdults(); // fill 'Erwachsene' comboboxes with default values
}


/* redirects user to lastminute.de search hotel page due to input in "Suche anedern" */
function doRedirect() {
	// set variables for url
	// depDate
	var today = new Date();
	var selectedDay = document.getElementById("hinreiseTag").options[document.getElementById("hinreiseTag").selectedIndex].value;
	if (selectedDay.length == 1) selectedDay = '0'+selectedDay;
	var selectedMonth = document.getElementById("hinreiseMonatJahr").options[document.getElementById("hinreiseMonatJahr").selectedIndex].value;
	if (selectedMonth.length == 1) selectedMonth = '0'+selectedMonth;
	var selectedYear = ((selectedMonth >= (today.getMonth()+1)) ? today.getFullYear() : (today.getFullYear()+1));
	depDateDEFull = selectedDay + '.' + selectedMonth + '.' + selectedYear;
	depDateDEShort = selectedDay + '.' + selectedMonth + '.' + selectedYear.toString().substr(2,2);
	depDateEN = selectedYear + '-' + selectedMonth + '-' + selectedDay;
	
	// retDate
	selectedDay = document.getElementById("rueckreiseTag").options[document.getElementById("rueckreiseTag").selectedIndex].value;
	if (selectedDay.length == 1) selectedDay = '0'+selectedDay;
	selectedMonth = document.getElementById("rueckreiseMonatJahr").options[document.getElementById("rueckreiseMonatJahr").selectedIndex].value;
	if (selectedMonth.length == 1) selectedMonth = '0'+selectedMonth;
	selectedYear = ((selectedMonth >= (today.getMonth()+1)) ? today.getFullYear() : (today.getFullYear()+1));
	retDateDEFull = selectedDay + '.' + selectedMonth + '.' + selectedYear;
	retDateDEShort = selectedDay + '.' + selectedMonth + '.' + selectedYear.toString().substr(2,2);
	retDateEN = selectedYear + '-' + selectedMonth + '-' + selectedDay;
	
	var url = "http://www.lastminute.de/de_DE/lmn2/travel/hotel/search.do?agent_id=LAD&railSelected=off&destName="+document.getElementById("Reiseziel").value+
						"&="+depDateEN+
						"&="+depDateDEShort+
						"&adultAmount="+document.getElementById("PAX").options[document.getElementById("PAX").selectedIndex].value+
						"&="+retDateEN+
						"&="+retDateDEShort+
						"&kidAge="+document.getElementById("Kind1").options[document.getElementById("Kind1").selectedIndex].value+
						"&kidAge="+document.getElementById("Kind2").options[document.getElementById("Kind2").selectedIndex].value+
						"&kidAge="+document.getElementById("Kind3").options[document.getElementById("Kind3").selectedIndex].value+
						"&hotelSelected=on&hotelCategory="+document.getElementById("STA").options[document.getElementById("STA").selectedIndex].value+
						"&hotelName="+document.getElementById("Hotel").value+
						"&roomCount=1&catering=&adultsInRoom=2&adultsInRoom=1&adultsInRoom=1&adultsInRoom=1&adultsInRoom=1&adultsInRoom=1&adultsInRoom=1&adultsInRoom=1";
						
	if (document.getElementById("Kind1").options[document.getElementById("Kind1").selectedIndex].value > defKidAge) url = url + "&kid1room=1";
	if (document.getElementById("Kind2").options[document.getElementById("Kind2").selectedIndex].value > defKidAge) url = url + "&kid2room=1";
	if (document.getElementById("Kind3").options[document.getElementById("Kind3").selectedIndex].value > defKidAge) url = url + "&kid3room=1";
	
	url = url + //"&promotionCodes="+
						"&depDate="+depDateDEFull+
						"&retDate="+retDateDEFull;//+
						//"&flightType=return&scat=hotel&";
						
	window.location = url;
}


/* sets the src of default hotel picture */
function loadDefImage(comp) {
	if (comp.height > 50) {
		comp.setAttribute("src", "/oss/hotelseiten/img/Hotel-schnaeppchen_gross.gif");
	} else if (comp.height > 39) {
		comp.setAttribute("src", "/oss/hotelseiten/img/Hotel-schnaeppchen_medium.gif");
	} else {
		comp.setAttribute("src", "/oss/hotelseiten/img/Hotel-schnaeppchen_klein.gif");
		comp.style.border = "1px solid #000";
	}
}


/* redirects user to lastminute.de booking page */
function doBooking(roomPos) {
	var url = "https://www.lastminute.de/de_DE/lmn2/travel/hotel/selectAndVerify.do?"+
						"room_0="+roomPos+"&pos_id="+posId+"&offer_id="+offerId+"&service=Hotel&fpars=sd12&agent_id=LAD&sid="+sId+"&cmpId="+cmpId+"&sqc=0";
	//var url = "http://localhost/de_DE/lmn2/travel/hotel/selectAndVerify.do?"+
	//					"room_0="+roomPos+"&pos_id="+posId+"&offer_id="+offerId+"&service=Hotel&fpars=sd12&agent_id=LAD&sid="+sId+"&cmpId="+cmpId+"&sqc=0";
	
	window.location = url;
}


/* shifting country hotels in list one place to the top - begin */
window.setInterval ( "shiftCountryHotels()", 3000 );
var roll = 1;


function shiftCountryHotels () {
	try {
		for (var i=0; i<19; i++) {
			if (countryHotels[(i+roll)%countryHotels.length][2].substr(0,1) == '0') // no category
				document.getElementById("CountryHotel"+i).innerHTML = '<a href="'+countryHotels[(i+roll)%countryHotels.length][1]+'" title="'+countryHotels[(i+roll)%countryHotels.length][0]+'">'+countryHotels[(i+roll)%countryHotels.length][0]+'</a>, in '+countryHotels[(i+roll)%countryHotels.length][3];
			else
				document.getElementById("CountryHotel"+i).innerHTML = '<a href="'+countryHotels[(i+roll)%countryHotels.length][1]+'" title="'+countryHotels[(i+roll)%countryHotels.length][0]+'">'+countryHotels[(i+roll)%countryHotels.length][0]+'</a>, in '+countryHotels[(i+roll)%countryHotels.length][3]+' <img src="/oss/hotelseiten/img/'+countryHotels[(i+roll)%countryHotels.length][2].substr(0,1)+'sterne-hotel.gif" alt="'+countryHotels[(i+roll)%countryHotels.length][2]+' Sterne" />';
	 	}
	 	if (countryHotels[(0+roll)%countryHotels.length][2].substr(0,1) == '0') // no category
	 		document.getElementById("CountryHotel19").innerHTML = '<a href="'+countryHotels[roll%countryHotels.length][1]+'" title="'+countryHotels[roll%countryHotels.length][0]+'">'+countryHotels[roll%countryHotels.length][0]+'</a>, in '+countryHotels[roll%countryHotels.length][3];
	 	else
	 		document.getElementById("CountryHotel19").innerHTML = '<a href="'+countryHotels[roll%countryHotels.length][1]+'" title="'+countryHotels[roll%countryHotels.length][0]+'">'+countryHotels[roll%countryHotels.length][0]+'</a>, in '+countryHotels[roll%countryHotels.length][3]+' <img src="/oss/hotelseiten/img/'+countryHotels[roll%countryHotels.length][2].substr(0,1)+'sterne-hotel.gif" alt="'+countryHotels[roll%countryHotels.length][2]+' Sterne" />';
	 	
	 	if (roll > countryHotels.length)
	 		roll = 0;
	 	else
	 		roll++;
	} catch (err) {
	}
}
/* shifting country hotels in list one place to the top - end */


/* redirects user to another hotel page */
function redirectHotel(comp) {
	window.location = comp.options[comp.selectedIndex].value;
}


/* redirects user to first city page with selected sorting */
function redirectCity(comp) {
	window.location = comp.options[comp.selectedIndex].value;
}


/* toggles googlemap */
function toggleGM(compId) {
	// if the map still doesn't exist, create it
	/*
	try {
		if (maps[i] == null) {
			console.debug("pred vytvorenim");
			showMap(compId);
			console.debug("po vytvoreni");
		} else
			console.debug(maps[i]);
	} catch(err) {
		console.debug("error"+err);
	}
	*/
	compGM = document.getElementById("GM_"+compId);
	//console.debug(compId);
	//console.debug(compGM);
	if (compGM != null) {
		if (compGM.style.display == "none")
			compGM.style.display = "block";
		else
			compGM.style.display = "none";
	}
	compGMB = document.getElementById("Google-Map_"+compId);
	//console.debug(compGMB);
	if (compGMB != null) {
		//console.debug("in1");
		if (compGMB.style.display == "none") {
			//console.debug("in2");
			compGMB.style.display = "block";
			showMap(compId);
		} else {
			compGMB.style.display = "none";
			GUnload();
		}
	}
}

/* invis googlemap */
function invisGM(compId) {
	compGM = document.getElementById("GM_"+compId);
	if (compGM != null)
		compGM.style.display = "none";
	compGMB = document.getElementById("Google-Map_"+compId);
	if (compGMB != null)
		compGMB.style.display = "none";
}

function invisAllGM() {
	for (var i=0; i<20; i++) {
		invisGM(i);
	}
}
